]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/views/xmlrpc.py
Fix non-ascii character encodings on xmlrpc interface
[patchwork] / apps / patchwork / views / xmlrpc.py
index f7a8dac8381ca1c74dd01da09709ae3ca8367898..89c6bd6bb446dec5357a198944410c182fbf65e1 100644 (file)
@@ -39,13 +39,20 @@ class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher):
         if sys.version_info[:3] >= (2,5,):
             SimpleXMLRPCDispatcher.__init__(self, allow_none=False,
                     encoding=None)
+            def _dumps(obj, *args, **kwargs):
+                kwargs['allow_none'] = self.allow_none
+                kwargs['encoding'] = self.encoding
+                return xmlrpclib.dumps(obj, *args, **kwargs)
         else:
+            def _dumps(obj, *args, **kwargs):
+                return xmlrpclib.dumps(obj, *args, **kwargs)
             SimpleXMLRPCDispatcher.__init__(self)
 
+        self.dumps = _dumps
+
         # map of name => (auth, func)
         self.func_map = {}
 
-
     def register_function(self, fn, auth_required):
         self.func_map[fn.__name__] = (auth_required, fn)
 
@@ -99,16 +106,13 @@ class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher):
             response = self._dispatch(request, method, params)
             # wrap response in a singleton tuple
             response = (response,)
-            response = xmlrpclib.dumps(response, methodresponse=1,
-                           allow_none=self.allow_none, encoding=self.encoding)
+            response = self.dumps(response, methodresponse=1)
         except xmlrpclib.Fault, fault:
-            response = xmlrpclib.dumps(fault, allow_none=self.allow_none,
-                                       encoding=self.encoding)
+            response = self.dumps(fault)
         except:
             # report exception back to server
-            response = xmlrpclib.dumps(
+            response = self.dumps(
                 xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value)),
-                encoding=self.encoding, allow_none=self.allow_none,
                 )
 
         return response
@@ -169,7 +173,7 @@ def person_to_dict(obj):
          'id'           : obj.id,
          'email'        : obj.email,
          'name'         : obj.name,
-         'user'         : str(obj.user),
+         'user'         : unicode(obj.user).encode("utf-8"),
         }
 
 def patch_to_dict(obj):
@@ -178,17 +182,17 @@ def patch_to_dict(obj):
     return \
         {
          'id'           : obj.id,
-         'date'         : str(obj.date),
+         'date'         : unicode(obj.date).encode("utf-8"),
          'filename'     : obj.filename(),
          'msgid'        : obj.msgid,
          'name'         : obj.name,
-         'project'      : str(obj.project),
+         'project'      : unicode(obj.project).encode("utf-8"),
          'project_id'   : obj.project_id,
-         'state'        : str(obj.state),
+         'state'        : unicode(obj.state).encode("utf-8"),
          'state_id'     : obj.state_id,
-         'submitter'    : str(obj.submitter),
+         'submitter'    : unicode(obj.submitter).encode("utf-8"),
          'submitter_id' : obj.submitter_id,
-         'delegate'     : str(obj.delegate),
+         'delegate'     : unicode(obj.delegate).encode("utf-8"),
          'delegate_id'  : max(obj.delegate_id, 0),
          'commit_ref'   : max(obj.commit_ref, ''),
         }
@@ -342,6 +346,15 @@ def patch_get(patch_id):
     except:
         return {}
 
+@xmlrpc_method(False)
+def patch_get_by_hash(hash):
+    """Return structure for the given patch hash."""
+    try:
+        patch = Patch.objects.filter(hash = hash)[0]
+        return patch_to_dict(patch)
+    except:
+        return {}
+
 @xmlrpc_method(False)
 def patch_get_mbox(patch_id):
     """Return mbox string for the given patch ID."""