Restore "content_type" kwarg to Comment.new_reply().
authorW. Trevor King <wking@drexel.edu>
Tue, 2 Feb 2010 17:32:53 +0000 (12:32 -0500)
committerW. Trevor King <wking@drexel.edu>
Tue, 2 Feb 2010 17:32:53 +0000 (12:32 -0500)
It had been removed in
    revno: 473.1.43
    committer: W. Trevor King <wking@drexel.edu>
    branch nick: be.restructure
    timestamp: Mon 2009-12-14 07:37:51 -0500
    message:
      Transitioned comment to Command format
when we pushed unicode encoding/decoding back to the Storage backend.

However, with the addition of libbe.util.id.short_to_long_text(),
we need it again.

Also add a Doctest showing a non-text/* comment, so utilities dealing
with them can see what they'll be working with.

libbe/command/comment.py
libbe/comment.py

index b26984025654ad254679d07dda2761a907191781..5bf6acf97c90f75d1ac23bc0bd1d4245d40fa80a 100644 (file)
@@ -150,8 +150,8 @@ class Comment (libbe.command.Command):
         if params['author'] == None:
             params['author'] = self._get_user_id()
 
-        new = parent.new_reply(body=body)
-        for key in ['alt-id', 'author', 'content-type']:
+        new = parent.new_reply(body=body, content_type=params['content-type'])
+        for key in ['alt-id', 'author']:
             if params[key] != None:
                 setattr(new, new._setting_name_to_attr_name(key), params[key])
         print >> self.stdout, 'Created comment with ID %s' % new.id.user()
index f850042ec6d8ae936801af6d86e54d2de448a073..a6a4ebdea305dd9008aa2ef568adce939c9ccb89 100644 (file)
@@ -162,7 +162,8 @@ class Comment (Tree, settings_object.SavedSettingsObject):
                 decode=self.content_type.startswith("text/"))
     def _set_comment_body(self, old=None, new=None, force=False):
         assert self.uuid != INVALID_UUID, self
-        if self.bug != None and self.bug.bugdir != None:
+        if self.content_type.startswith('text/') \
+                and self.bug != None and self.bug.bugdir != None:
             new = libbe.util.id.short_to_long_text([self.bug.bugdir], new)
         if (self.storage != None and self.storage.writeable == True) \
                 or force==True:
@@ -191,16 +192,19 @@ class Comment (Tree, settings_object.SavedSettingsObject):
     def extra_strings(): return {}
 
     def __init__(self, bug=None, uuid=None, from_storage=False,
-                 in_reply_to=None, body=None):
+                 in_reply_to=None, body=None, content_type=None):
         """
         Set from_storage=True to load an old comment.
         Set from_storage=False to create a new comment.
 
         The uuid option is required when from_storage==True.
 
-        The in_reply_to and body options are only used if
-        from_storage==False (the default).  When from_storage==True,
-        they are loaded from the bug database.
+        The in_reply_to, body, and content_type options are only used
+        if from_storage==False (the default).  When
+        from_storage==True, they are loaded from the bug database.
+        content_type decides if the body should be run through
+        libbe.util.id.short_to_long_text() before saving.  See
+        ._set_comment_body() for details.
 
         in_reply_to should be the uuid string of the parent comment.
         """
@@ -215,6 +219,8 @@ class Comment (Tree, settings_object.SavedSettingsObject):
                 self.uuid = libbe.util.id.uuid_gen()
             self.time = int(time.time()) # only save to second precision
             self.in_reply_to = in_reply_to
+            if content_type != None:
+                self.content_type = content_type
             self.body = body
         if self.bug != None:
             self.storage = self.bug.storage
@@ -291,6 +297,17 @@ class Comment (Tree, settings_object.SavedSettingsObject):
         insightful
         remarks</body>
           </comment>
+        >>> comm.content_type = 'image/png'
+        >>> print comm.xml()
+        <comment>
+          <uuid>0123</uuid>
+          <short-name>//012</short-name>
+          <author></author>
+          <date>Thu, 01 Jan 1970 00:00:00 +0000</date>
+          <content-type>image/png</content-type>
+          <body>U29tZQppbnNpZ2h0ZnVsCnJlbWFya3MK
+        </body>
+        </comment>
         """
         if self.content_type.startswith('text/'):
             body = (self.body or '').rstrip('\n')
@@ -644,7 +661,7 @@ class Comment (Tree, settings_object.SavedSettingsObject):
             reply.in_reply_to = self.uuid
         self.append(reply)
 
-    def new_reply(self, body=None):
+    def new_reply(self, body=None, content_type=None):
         """
         >>> comm = Comment(bug=None, body="Some insightful remarks")
         >>> repA = comm.new_reply("Critique original comment")
@@ -652,7 +669,7 @@ class Comment (Tree, settings_object.SavedSettingsObject):
         >>> repB.in_reply_to == repA.uuid
         True
         """
-        reply = Comment(self.bug, body=body)
+        reply = Comment(self.bug, body=body, content_type=content_type)
         self.add_reply(reply)
         return reply