-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathevent.py
More file actions
173 lines (155 loc) · 5.67 KB
/
Copy pathevent.py
File metadata and controls
173 lines (155 loc) · 5.67 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
171
172
173
# Patchwork - automated patch tracking system
# Copyright (C) 2017 Stephen Finucane <stephen@that.guru>
#
# SPDX-License-Identifier: GPL-2.0-or-later
from collections import OrderedDict
from rest_framework.generics import ListAPIView
from rest_framework.serializers import ModelSerializer
from rest_framework.serializers import SerializerMethodField
from rest_framework.serializers import SlugRelatedField
from patchwork.api.embedded import CheckSerializer
from patchwork.api.embedded import CoverSerializer
from patchwork.api.embedded import CoverCommentSerializer
from patchwork.api.embedded import PatchSerializer
from patchwork.api.embedded import PatchCommentSerializer
from patchwork.api.embedded import ProjectSerializer
from patchwork.api.embedded import SeriesSerializer
from patchwork.api.embedded import UserSerializer
from patchwork.api.filters import EventFilterSet
from patchwork.api import utils
from patchwork.models import Event
class EventSerializer(ModelSerializer):
project = ProjectSerializer(read_only=True)
actor = UserSerializer()
patch = PatchSerializer(read_only=True)
series = SeriesSerializer(read_only=True)
cover = CoverSerializer(read_only=True)
previous_state = SlugRelatedField(slug_field='slug', read_only=True)
current_state = SlugRelatedField(slug_field='slug', read_only=True)
previous_delegate = UserSerializer()
current_delegate = UserSerializer()
created_check = SerializerMethodField()
created_check = CheckSerializer()
previous_relation = SerializerMethodField()
current_relation = SerializerMethodField()
cover_comment = CoverCommentSerializer()
patch_comment = PatchCommentSerializer()
# Mapping of event type to fields to include in the response
_category_map = {
Event.CATEGORY_COVER_CREATED: ['cover'],
Event.CATEGORY_PATCH_CREATED: ['patch'],
Event.CATEGORY_PATCH_COMPLETED: ['patch', 'series'],
Event.CATEGORY_PATCH_STATE_CHANGED: [
'patch',
'previous_state',
'current_state',
],
Event.CATEGORY_PATCH_DELEGATED: [
'patch',
'previous_delegate',
'current_delegate',
],
Event.CATEGORY_PATCH_RELATION_CHANGED: [
'patch',
'previous_relation',
'current_relation',
],
Event.CATEGORY_CHECK_CREATED: ['patch', 'created_check'],
Event.CATEGORY_SERIES_CREATED: ['series'],
Event.CATEGORY_SERIES_COMPLETED: ['series'],
Event.CATEGORY_COVER_COMMENT_CREATED: ['cover', 'cover_comment'],
Event.CATEGORY_PATCH_COMMENT_CREATED: ['patch', 'patch_comment'],
}
# Mapping of database column names to REST API representations
_field_name_map = {
'created_check': 'check',
'cover_comment': 'comment',
'patch_comment': 'comment',
}
def get_previous_relation(self, instance):
return None
def get_current_relation(self, instance):
return None
def to_representation(self, instance):
data = super(EventSerializer, self).to_representation(instance)
payload = OrderedDict()
kept_fields = self._category_map[instance.category] + [
'id',
'category',
'project',
'date',
'actor',
]
for field in [x for x in data]:
if field not in kept_fields:
del data[field]
elif field in self._category_map[instance.category]:
# remap fields if necessary
field_name = self._field_name_map.get(field, field)
payload[field_name] = data.pop(field)
data['payload'] = payload
return data
class Meta:
model = Event
fields = (
'id',
'category',
'project',
'date',
'actor',
'patch',
'series',
'cover',
'previous_state',
'current_state',
'previous_delegate',
'current_delegate',
'created_check',
'previous_relation',
'current_relation',
'cover_comment',
'patch_comment',
)
read_only_fields = fields
versioned_fields = {
'1.2': ('actor',),
}
class EventList(ListAPIView):
"""List events."""
serializer_class = EventSerializer
filter_class = filterset_class = EventFilterSet
page_size_query_param = None # fixed page size
ordering_fields = ('date',)
ordering = '-date'
def get_queryset(self):
events = Event.objects.all().prefetch_related(
'project',
'patch__project',
'series__project',
'cover',
'previous_state',
'current_state',
'previous_delegate',
'current_delegate',
'created_check',
)
# NOTE(stephenfin): We need to exclude comment-related events because
# until API v1.3, we didn't have an comment detail API to point to.
# This goes against our pledge to version events in the docs but must
# be done.
# TODO(stephenfin): Make this more generic.
if utils.has_version(self.request, '1.3'):
events = events.prefetch_related(
'cover_comment',
'cover_comment__cover__project',
'patch_comment',
'patch_comment__patch__project',
)
else:
events = events.exclude(
category__in=[
Event.CATEGORY_COVER_COMMENT_CREATED,
Event.CATEGORY_PATCH_COMMENT_CREATED,
]
)
return events