forked from getpatchwork/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.py
More file actions
170 lines (137 loc) · 6.13 KB
/
Copy pathpatch.py
File metadata and controls
170 lines (137 loc) · 6.13 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Linaro Corporation
#
# 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 django.utils.translation import ugettext_lazy as _
from rest_framework.generics import ListAPIView
from rest_framework.generics import RetrieveUpdateAPIView
from rest_framework.relations import RelatedField
from rest_framework.reverse import reverse
from rest_framework.serializers import HyperlinkedModelSerializer
from rest_framework.serializers import SerializerMethodField
from patchwork.api.base import PatchworkPermission
from patchwork.api.filters import PatchFilter
from patchwork.api.embedded import PersonSerializer
from patchwork.api.embedded import ProjectSerializer
from patchwork.api.embedded import SeriesSerializer
from patchwork.api.embedded import UserSerializer
from patchwork.models import Patch
from patchwork.models import State
from patchwork.parser import clean_subject
def format_state_name(state):
return ' '.join(state.split('-'))
class StateField(RelatedField):
"""Avoid the need for a state endpoint.
NOTE(stephenfin): This field will only function for State names consisting
of alphanumeric characters, underscores and single spaces. In Patchwork
2.0+, we should consider adding a slug field to the State object and make
use of the SlugRelatedField in DRF.
"""
default_error_messages = {
'required': _('This field is required.'),
'invalid_choice': _('Invalid state {name}. Expected one of: '
'{choices}.'),
'incorrect_type': _('Incorrect type. Expected string value, received '
'{data_type}.'),
}
queryset = '' # django 1.6, rest_framework 3.2 require this
def to_internal_value(self, data):
try:
data = format_state_name(data)
return self.get_queryset().get(name__iexact=data)
except State.DoesNotExist:
self.fail('invalid_choice', name=data, choices=', '.join([
format_state_name(x.name) for x in self.get_queryset()]))
except (TypeError, ValueError):
self.fail('incorrect_type', data_type=type(data).__name__)
def to_representation(self, obj):
return obj.slug
def get_queryset(self):
return State.objects.all()
class PatchListSerializer(HyperlinkedModelSerializer):
project = ProjectSerializer(read_only=True)
state = StateField()
submitter = PersonSerializer(read_only=True)
delegate = UserSerializer()
mbox = SerializerMethodField()
series = SeriesSerializer(many=True, read_only=True)
check = SerializerMethodField()
checks = SerializerMethodField()
tags = SerializerMethodField()
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): Make tags performant, possibly by reworking the
# model
return {}
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}))
class Meta:
model = Patch
fields = ('id', 'url', 'project', 'msgid', 'date', 'name',
'commit_ref', 'pull_url', 'state', 'archived', 'hash',
'submitter', 'delegate', 'mbox', 'series', 'check', 'checks',
'tags')
read_only_fields = ('project', 'msgid', 'date', 'name', 'hash',
'submitter', 'mbox', 'mbox', 'series', 'check',
'checks', 'tags')
extra_kwargs = {
'url': {'view_name': 'api-patch-detail'},
}
class PatchDetailSerializer(PatchListSerializer):
headers = SerializerMethodField()
prefixes = SerializerMethodField()
def get_headers(self, patch):
if patch.headers:
return email.parser.Parser().parsestr(patch.headers, True)
def get_prefixes(self, instance):
return clean_subject(instance.name)[1]
class Meta:
model = Patch
fields = PatchListSerializer.Meta.fields + (
'headers', 'content', 'diff', 'prefixes')
read_only_fields = PatchListSerializer.Meta.read_only_fields + (
'headers', 'content', 'diff', 'prefixes')
extra_kwargs = PatchListSerializer.Meta.extra_kwargs
class PatchList(ListAPIView):
"""List patches."""
permission_classes = (PatchworkPermission,)
serializer_class = PatchListSerializer
filter_class = PatchFilter
search_fields = ('name',)
ordering_fields = ('id', 'name', 'project', 'date', 'state', 'archived',
'submitter', 'check')
def get_queryset(self):
# TODO(stephenfin): Does the defer here cause issues with Django 1.6
# (like /cover)?
return Patch.objects.all()\
.prefetch_related('series', 'check_set')\
.select_related('project', 'state', 'submitter', 'delegate')\
.defer('content', 'diff', 'headers')
class PatchDetail(RetrieveUpdateAPIView):
"""Show a patch."""
permission_classes = (PatchworkPermission,)
serializer_class = PatchDetailSerializer
def get_queryset(self):
return Patch.objects.all()\
.prefetch_related('series', 'check_set')\
.select_related('project', 'state', 'submitter', 'delegate')