1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Stephen Finucane <stephen@that.guru>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import email.parser
from rest_framework.generics import ListAPIView
from rest_framework.generics import RetrieveAPIView
from rest_framework.reverse import reverse
from rest_framework.serializers import SerializerMethodField
from patchwork.api.base import BaseHyperlinkedModelSerializer
from patchwork.api.filters import CoverLetterFilterSet
from patchwork.api.embedded import PersonSerializer
from patchwork.api.embedded import ProjectSerializer
from patchwork.api.embedded import SeriesSerializer
from patchwork.models import CoverLetter
class CoverLetterListSerializer(BaseHyperlinkedModelSerializer):
project = ProjectSerializer(read_only=True)
submitter = PersonSerializer(read_only=True)
mbox = SerializerMethodField()
series = SeriesSerializer(many=True, read_only=True)
comments = SerializerMethodField()
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', 'project', 'msgid', 'date', 'name', 'submitter',
'mbox', 'series', 'comments')
read_only_fields = fields
versioned_fields = {
'1.1': ('mbox', 'comments'),
}
extra_kwargs = {
'url': {'view_name': 'api-cover-detail'},
}
class CoverLetterDetailSerializer(CoverLetterListSerializer):
headers = SerializerMethodField()
def get_headers(self, instance):
headers = {}
if instance.headers:
parsed = email.parser.Parser().parsestr(instance.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:
model = CoverLetter
fields = CoverLetterListSerializer.Meta.fields + (
'headers', 'content')
read_only_fields = fields
extra_kwargs = CoverLetterListSerializer.Meta.extra_kwargs
versioned_fields = CoverLetterListSerializer.Meta.versioned_fields
class CoverLetterList(ListAPIView):
"""List cover letters."""
serializer_class = CoverLetterListSerializer
filter_class = CoverLetterFilterSet
search_fields = ('name',)
ordering_fields = ('id', 'name', 'date', 'submitter')
ordering = 'id'
def get_queryset(self):
return CoverLetter.objects.all().prefetch_related('series')\
.select_related('project', 'submitter')\
.defer('content', 'headers')
class CoverLetterDetail(RetrieveAPIView):
"""Show a cover letter."""
serializer_class = CoverLetterDetailSerializer
def get_queryset(self):
return CoverLetter.objects.all().prefetch_related('series')\
.select_related('project', 'submitter')
|