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
# 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,)
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')
data['list_id'] = data.pop('listid')
return data
+ class Meta:
+ model = Project
+ exclude = ('send_notifications', 'use_tags')
+
class ProjectViewSet(PatchworkViewSet):
permission_classes = (PatchworkPermission,)