changeset 5:7745feb7ca9c

Added BugzillaApi.post_attachment()
author Atul Varma <avarma@mozilla.com>
date Tue, 13 Apr 2010 11:03:54 -0700
parents 4e31f7aeb1b8
children 548a4fe96851
files bugzilla
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/bugzilla	Tue Apr 13 10:16:23 2010 -0700
+++ b/bugzilla	Tue Apr 13 11:03:54 2010 -0700
@@ -2,8 +2,10 @@
 
 import os
 import sys
+import base64
 import httplib
 import urllib
+import mimetypes
 from urlparse import urlparse
 from getpass import getpass
 
@@ -130,6 +132,32 @@
         self.config = config
         self.__jsonreq = jsonreq
 
+    def post_attachment(self, bug_id, contents, filename, description,
+                        content_type=None, is_patch=False, is_private=False,
+                        is_obsolete=False,
+                        guess_mime_type=mimetypes.guess_type):
+        if content_type is None:
+            content_type = guess_mime_type(filename)[0]
+            if not content_type:
+                raise ValueError('could not guess content type for "%s"' %
+                                 filename)
+
+        attachment = {
+            'data': base64.b64encode(contents),
+            'description': description,
+            'encoding': 'base64',
+            'file_name': filename,
+            'flags': [],
+            'is_obsolete': is_obsolete,
+            'is_patch': is_patch,
+            'is_private': is_private,
+            'size': len(contents),
+            'content_type': content_type
+            }
+
+        return self.request('POST', '/bug/%d/attachment' % bug_id,
+                            body=attachment)
+
     def request(self, method, path, query_args=None, body=None):
         if query_args is None:
             query_args = {}
@@ -159,3 +187,7 @@
     bzapi = BugzillaApi()
     print bzapi.request('GET', '/attachment/436897',
                         query_args={'attachmentdata': 1})
+    #bzapi.post_attachment(bug_id=536619,
+    #                      contents="testing!",
+    #                      filename="contents.txt",
+    #                      description="test upload")