class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) comments = SerializerMethodField() def get_web_url(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_absolute_url()) def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) def get_comments(self, cover): return self.context.get('request').build_absolute_uri( reverse('api-cover-comment-list', kwargs={'pk': cover.id})) class Meta: model = CoverLetter fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name', 'submitter', 'mbox', 'series', 'comments') read_only_fields = fields versioned_fields = { '1.1': ('web_url', 'mbox', 'comments'), } extra_kwargs = { 'url': {'view_name': 'api-cover-detail'}, }
class SeriesSerializer(BaseHyperlinkedModelSerializer): web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() cover_letter = CoverSerializer(read_only=True) patches = PatchSerializer(read_only=True, many=True) def get_web_url(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_absolute_url()) def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) class Meta: model = Series fields = ('id', 'url', 'web_url', 'project', 'name', 'date', 'submitter', 'version', 'total', 'received_total', 'received_all', 'mbox', 'cover_letter', 'patches') read_only_fields = ('date', 'submitter', 'total', 'received_total', 'received_all', 'mbox', 'cover_letter', 'patches') versioned_fields = { '1.1': ('web_url', ), } extra_kwargs = { 'url': { 'view_name': 'api-series-detail' }, }
class PatchListSerializer(BaseHyperlinkedModelSerializer): web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) state = StateField() submitter = PersonSerializer(read_only=True) delegate = UserSerializer() mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) comments = SerializerMethodField() check = SerializerMethodField() checks = SerializerMethodField() tags = SerializerMethodField() def get_web_url(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_absolute_url()) def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) def get_comments(self, patch): return self.context.get('request').build_absolute_uri( reverse('api-patch-comment-list', kwargs={'pk': patch.id})) def get_check(self, instance): return instance.combined_check_state def get_checks(self, instance): return self.context.get('request').build_absolute_uri( reverse('api-check-list', kwargs={'patch_id': instance.id})) def get_tags(self, instance): # TODO(stephenfin): Make tags performant, possibly by reworking the # model return {} class Meta: model = Patch fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name', 'commit_ref', 'pull_url', 'state', 'archived', 'hash', 'submitter', 'delegate', 'mbox', 'series', 'comments', 'check', 'checks', 'tags') read_only_fields = ('web_url', 'project', 'msgid', 'date', 'name', 'hash', 'submitter', 'mbox', 'series', 'comments', 'check', 'checks', 'tags') versioned_fields = { '1.1': ('comments', 'web_url'), } extra_kwargs = { 'url': { 'view_name': 'api-patch-detail' }, }
class CoverListSerializer(BaseHyperlinkedModelSerializer): web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() series = SeriesSerializer(read_only=True) comments = SerializerMethodField() def get_web_url(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_absolute_url()) def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) def get_comments(self, cover): return self.context.get('request').build_absolute_uri( reverse('api-cover-comment-list', kwargs={'cover_id': cover.id}) ) def to_representation(self, instance): # NOTE(stephenfin): This is here to ensure our API looks the same even # after we changed the series-patch relationship from M:N to 1:N. It # will be removed in API v2 data = super(CoverListSerializer, self).to_representation(instance) data['series'] = [data['series']] if data['series'] else [] return data class Meta: model = Cover fields = ( 'id', 'url', 'web_url', 'project', 'msgid', 'list_archive_url', 'date', 'name', 'submitter', 'mbox', 'series', 'comments', ) read_only_fields = fields versioned_fields = { '1.1': ('web_url', 'mbox', 'comments'), '1.2': ('list_archive_url',), } extra_kwargs = { 'url': {'view_name': 'api-cover-detail'}, }
class CoverLetterListSerializer(HyperlinkedModelSerializer): project = ProjectSerializer(read_only=True) submitter = PersonSerializer(read_only=True) mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) class Meta: model = CoverLetter fields = ('id', 'url', 'project', 'msgid', 'date', 'name', 'submitter', 'mbox', 'series') read_only_fields = fields extra_kwargs = { 'url': {'view_name': 'api-cover-detail'}, }
class BaseCommentListSerializer(BaseHyperlinkedModelSerializer): web_url = SerializerMethodField() subject = SerializerMethodField() headers = SerializerMethodField() submitter = PersonSerializer(read_only=True) def get_web_url(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_absolute_url()) def get_subject(self, comment): return email.parser.Parser().parsestr(comment.headers, True).get('Subject', '') def get_headers(self, comment): headers = {} if comment.headers: parsed = email.parser.Parser().parsestr(comment.headers, True) for key in parsed.keys(): headers[key] = parsed.get_all(key) # Let's return a single string instead of a list if only one # header with this key is present if len(headers[key]) == 1: headers[key] = headers[key][0] return headers class Meta: fields = ('id', 'web_url', 'msgid', 'list_archive_url', 'date', 'subject', 'submitter', 'content', 'headers', 'addressed') read_only_fields = ('id', 'web_url', 'msgid', 'list_archive_url', 'date', 'subject', 'submitter', 'content', 'headers') versioned_fields = { '1.1': ('web_url', ), '1.2': ('list_archive_url',), '1.3': ('addressed',), }
class PatchListSerializer(BaseHyperlinkedModelSerializer): web_url = SerializerMethodField() project = ProjectSerializer(read_only=True) state = StateField() submitter = PersonSerializer(read_only=True) delegate = UserSerializer(allow_null=True) mbox = SerializerMethodField() series = SeriesSerializer(read_only=True) comments = SerializerMethodField() check = SerializerMethodField() checks = SerializerMethodField() tags = SerializerMethodField() related = PatchSerializer( source='related.patches', many=True, default=[], style={'base_template': 'input.html'}) def get_web_url(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_absolute_url()) def get_mbox(self, instance): request = self.context.get('request') return request.build_absolute_uri(instance.get_mbox_url()) def get_comments(self, patch): return self.context.get('request').build_absolute_uri( reverse('api-patch-comment-list', kwargs={'pk': patch.id})) def get_check(self, instance): return instance.combined_check_state def get_checks(self, instance): return self.context.get('request').build_absolute_uri( reverse('api-check-list', kwargs={'patch_id': instance.id})) def get_tags(self, instance): # TODO(stephenfin): Make tags performant, possibly by reworking the # model return {} def validate_delegate(self, value): """Check that the delgate is a maintainer of the patch's project.""" if not value: return value if not value.profile.maintainer_projects.only('id').filter( id=self.instance.project.id).exists(): raise ValidationError("User '%s' is not a maintainer for project " "'%s'" % (value, self.instance.project)) return value def to_representation(self, instance): # NOTE(stephenfin): This is here to ensure our API looks the same even # after we changed the series-patch relationship from M:N to 1:N. It # will be removed in API v2 data = super(PatchListSerializer, self).to_representation(instance) data['series'] = [data['series']] if data['series'] else [] # Remove this patch from 'related' if 'related' in data and data['related']: data['related'] = [ x for x in data['related'] if x['id'] != data['id'] ] return data class Meta: model = Patch fields = ('id', 'url', 'web_url', 'project', 'msgid', 'list_archive_url', 'date', 'name', 'commit_ref', 'pull_url', 'state', 'archived', 'hash', 'submitter', 'delegate', 'mbox', 'series', 'comments', 'check', 'checks', 'tags', 'related',) read_only_fields = ('web_url', 'project', 'msgid', 'list_archive_url', 'date', 'name', 'hash', 'submitter', 'mbox', 'series', 'comments', 'check', 'checks', 'tags') versioned_fields = { '1.1': ('comments', 'web_url'), '1.2': ('list_archive_url', 'related',), } extra_kwargs = { 'url': {'view_name': 'api-patch-detail'}, }