]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/views/xmlrpc.py
views: Move mbox handling from models to views
[patchwork] / apps / patchwork / views / xmlrpc.py
index 0d3321fc259d5e431cfc643d7fbd48d850ecf890..eebc2aae48600179471782fe8ee55e52a845d83a 100644 (file)
 # Patchwork XMLRPC interface
 #
 
-from django.core.exceptions import ImproperlyConfigured
 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
 from django.http import HttpResponse, HttpResponseRedirect, \
      HttpResponseServerError
-from django.conf import settings
 from django.core import urlresolvers
-from django.shortcuts import render_to_response
 from django.contrib.auth import authenticate
-from patchwork.models import Patch, Project, Person, Bundle, State
+from patchwork.models import Patch, Project, Person, State
+from patchwork.views import patch_to_mbox
 from django.views.decorators.csrf import csrf_exempt
 
 import sys
@@ -59,14 +57,14 @@ class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher):
 
 
     def _user_for_request(self, request):
-       auth_header = None
+        auth_header = None
 
-        if request.META.has_key('HTTP_AUTHORIZATION'):
-           auth_header = request.META.get('HTTP_AUTHORIZATION')
-        elif request.META.has_key('Authorization'):
-           auth_header = request.META.get('Authorization')
+        if 'HTTP_AUTHORIZATION' in request.META:
+            auth_header = request.META.get('HTTP_AUTHORIZATION')
+        elif 'Authorization' in request.META:
+            auth_header = request.META.get('Authorization')
 
-       if auth_header is None or auth_header == '':
+        if auth_header is None or auth_header == '':
             raise Exception("No authentication credentials given")
 
         str = auth_header.strip()
@@ -132,7 +130,7 @@ def xmlrpc(request):
     try:
         ret = dispatcher._marshaled_dispatch(request)
         response.write(ret)
-    except Exception, e:
+    except Exception:
         return HttpResponseServerError()
 
     return response
@@ -170,11 +168,19 @@ def project_to_dict(obj):
 def person_to_dict(obj):
     """Return a trimmed down dictionary representation of a Person
     object which is OK to send to the client."""
+
+    # Make sure we don't return None even if the user submitted a patch
+    # with no real name.  XMLRPC can't marshall None.
+    if obj.name is not None:
+        name = obj.name
+    else:
+        name = obj.email
+
     return \
         {
          'id'           : obj.id,
          'email'        : obj.email,
-         'name'         : obj.name,
+         'name'         : name,
          'user'         : unicode(obj.user).encode("utf-8"),
         }
 
@@ -331,7 +337,7 @@ def patch_list(filter={}):
         patches = Patch.objects.filter(**dfilter)
 
         if max_count > 0:
-            return map(patch_to_dict, patches)[:max_count]
+            return map(patch_to_dict, patches[:max_count])
         else:
             return map(patch_to_dict, patches)
 
@@ -371,7 +377,7 @@ def patch_get_mbox(patch_id):
     """Return mbox string for the given patch ID."""
     try:
         patch = Patch.objects.filter(id = patch_id)[0]
-        return patch.mbox().as_string()
+        return patch_to_mbox(patch).as_string()
     except:
         return ""