]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: Use SerializerMethod field
authorStephen Finucane <stephen@that.guru>
Wed, 23 Nov 2016 19:35:40 +0000 (19:35 +0000)
committerStephen Finucane <stephen@that.guru>
Fri, 23 Dec 2016 23:37:23 +0000 (23:37 +0000)
Use 'SerializerMethodField' to override how we generate fields, rather
than hacking the 'to_representation' method. This is the more idiomatic
way to do this.

The Meta class for the Project and Patch serializers are moved to the
bottom of the serializers and some variables renamed, both for
consistency's sake.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/api/patch.py
patchwork/api/project.py

index 2dc77d16e423a135c6ee6adf743b2a231af661ea..3f464b26c0d066676e75b6f35eefea640a9e6802 100644 (file)
@@ -40,6 +40,34 @@ class PatchListSerializer(ListSerializer):
 class PatchSerializer(HyperlinkedModelSerializer):
     mbox = SerializerMethodField()
     state = SerializerMethodField()
+    tags = SerializerMethodField()
+    headers = SerializerMethodField()
+    check = SerializerMethodField()
+
+    def get_state(self, instance):
+        return instance.state.name
+
+    def get_mbox(self, instance):
+        request = self.context.get('request')
+        return request.build_absolute_uri(instance.get_mbox_url())
+
+    def get_tags(self, instance):
+        # TODO(stephenfin): I don't think this is correct - too many queries
+        return [{'name': x.tag.name, 'count': x.count}
+                for x in instance.patchtag_set.all()]
+
+    def get_headers(self, instance):
+        if instance.headers:
+            return
+        email.parser.Parser().parsestr(instance.headers, True)
+
+    def get_check(self, instance):
+        return instance.combined_check_state
+
+    def to_representation(self, instance):
+        data = super(PatchSerializer, self).to_representation(instance)
+        data['checks'] = data['url'] + 'checks/'
+        return data
 
     class Meta:
         model = Patch
@@ -50,24 +78,6 @@ class PatchSerializer(HyperlinkedModelSerializer):
         # render this field
         exclude = ('tags',)
 
-    def get_state(self, obj):
-        return obj.state.name
-
-    def get_mbox(self, patch):
-        request = self.context.get('request', None)
-        return request.build_absolute_uri(patch.get_mbox_url())
-
-    def to_representation(self, instance):
-        data = super(PatchSerializer, self).to_representation(instance)
-        data['checks'] = data['url'] + 'checks/'
-        data['check'] = instance.combined_check_state
-        headers = data.get('headers')
-        if headers is not None:
-            data['headers'] = email.parser.Parser().parsestr(headers, True)
-        data['tags'] = [{'name': x.tag.name, 'count': x.count}
-                        for x in instance.patchtag_set.all()]
-        return data
-
 
 class PatchViewSet(PatchworkViewSet):
     permission_classes = (PatchworkPermission,)
index 7def2ed9a4473b23afdc49ada4971d3ef48ceaae..b4debb61c10e35b946f354779189951e15ede0ca 100644 (file)
@@ -25,10 +25,6 @@ from patchwork.models import Project
 
 
 class ProjectSerializer(HyperlinkedModelSerializer):
-    class Meta:
-        model = Project
-        exclude = ('send_notifications', 'use_tags')
-
     def to_representation(self, instance):
         data = super(ProjectSerializer, self).to_representation(instance)
         data['link_name'] = data.pop('linkname')
@@ -36,6 +32,10 @@ class ProjectSerializer(HyperlinkedModelSerializer):
         data['list_id'] = data.pop('listid')
         return data
 
+    class Meta:
+        model = Project
+        exclude = ('send_notifications', 'use_tags')
+
 
 class ProjectViewSet(PatchworkViewSet):
     permission_classes = (PatchworkPermission,)