Skip to content

Commit 1ea8b10

Browse files
committed
REST: Null out previous, current relation info
These fields don't work like we expect them to. Because we're linking to a non-idempotent entity, an instance of 'relation', what we're storing in either of these fields is subject to change as patches are added and removed. This makes the information pretty much useless after the fact. It's best to just state the patch and request that people query the information themselves if necessary. We don't want to remove the field entirely from the API - that would be a truly breaking change - so instead we null it out like we do for patch tags. In a v2 API (i.e. a major version bump) we can remove this entirely. A small bug with the schema generation is corrected. Signed-off-by: Stephen Finucane <[email protected]> Related: #379 (cherry picked from commit 646a2f2)
1 parent a8492f0 commit 1ea8b10

File tree

6 files changed

+28
-38
lines changed

6 files changed

+28
-38
lines changed

docs/api/schemas/patchwork.j2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ components:
18211821
current_state:
18221822
title: Current state
18231823
type: string
1824-
{% if version >= (1, 1) %}
1824+
{% if version >= (1, 2) %}
18251825
EventPatchRelationChanged:
18261826
allOf:
18271827
- $ref: '#/components/schemas/EventBase'
@@ -1837,9 +1837,11 @@ components:
18371837
previous_relation:
18381838
title: Previous relation
18391839
type: string
1840+
nullable: true
18401841
current_relation:
18411842
title: Current relation
18421843
type: string
1844+
nullable: true
18431845
{% endif %}
18441846
EventPatchDelegated:
18451847
allOf:

docs/api/schemas/v1.1/patchwork.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,24 +1551,6 @@ components:
15511551
current_state:
15521552
title: Current state
15531553
type: string
1554-
EventPatchRelationChanged:
1555-
allOf:
1556-
- $ref: '#/components/schemas/EventBase'
1557-
- type: object
1558-
properties:
1559-
category:
1560-
enum:
1561-
- patch-relation-changed
1562-
payload:
1563-
properties:
1564-
patch:
1565-
$ref: '#/components/schemas/PatchEmbedded'
1566-
previous_relation:
1567-
title: Previous relation
1568-
type: string
1569-
current_relation:
1570-
title: Current relation
1571-
type: string
15721554
EventPatchDelegated:
15731555
allOf:
15741556
- $ref: '#/components/schemas/EventBase'

docs/api/schemas/v1.2/patchwork.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,9 +1768,11 @@ components:
17681768
previous_relation:
17691769
title: Previous relation
17701770
type: string
1771+
nullable: true
17711772
current_relation:
17721773
title: Current relation
17731774
type: string
1775+
nullable: true
17741776
EventPatchDelegated:
17751777
allOf:
17761778
- $ref: '#/components/schemas/EventBase'

patchwork/api/event.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ class EventSerializer(ModelSerializer):
3333
current_delegate = UserSerializer()
3434
created_check = SerializerMethodField()
3535
created_check = CheckSerializer()
36-
previous_relation = PatchSerializer(
37-
source='previous_relation.patches', many=True, default=None)
38-
current_relation = PatchSerializer(
39-
source='current_relation.patches', many=True, default=None)
36+
previous_relation = SerializerMethodField()
37+
current_relation = SerializerMethodField()
4038

4139
_category_map = {
4240
Event.CATEGORY_COVER_CREATED: ['cover'],
@@ -53,6 +51,12 @@ class EventSerializer(ModelSerializer):
5351
Event.CATEGORY_SERIES_COMPLETED: ['series'],
5452
}
5553

54+
def get_previous_relation(self, instance):
55+
return None
56+
57+
def get_current_relation(self, instance):
58+
return None
59+
5660
def to_representation(self, instance):
5761
data = super(EventSerializer, self).to_representation(instance)
5862
payload = OrderedDict()
@@ -72,10 +76,12 @@ def to_representation(self, instance):
7276

7377
class Meta:
7478
model = Event
75-
fields = ('id', 'category', 'project', 'date', 'actor', 'patch',
76-
'series', 'cover', 'previous_state', 'current_state',
77-
'previous_delegate', 'current_delegate', 'created_check',
78-
'previous_relation', 'current_relation',)
79+
fields = (
80+
'id', 'category', 'project', 'date', 'actor', 'patch',
81+
'series', 'cover', 'previous_state', 'current_state',
82+
'previous_delegate', 'current_delegate', 'created_check',
83+
'previous_relation', 'current_relation',
84+
)
7985
read_only_fields = fields
8086
versioned_fields = {
8187
'1.2': ('actor', ),

patchwork/signals.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,12 @@ def create_event(patch, before, after):
137137
@receiver(pre_save, sender=Patch)
138138
def create_patch_relation_changed_event(sender, instance, raw, **kwargs):
139139

140-
def create_event(patch, before, after):
140+
def create_event(patch):
141141
return Event.objects.create(
142142
category=Event.CATEGORY_PATCH_RELATION_CHANGED,
143143
project=patch.project,
144144
actor=getattr(patch, '_edited_by', None),
145-
patch=patch,
146-
previous_relation=before,
147-
current_relation=after)
145+
patch=patch)
148146

149147
# don't trigger for items loaded from fixtures or new items
150148
if raw or not instance.pk:
@@ -155,7 +153,7 @@ def create_event(patch, before, after):
155153
if orig_patch.related == instance.related:
156154
return
157155

158-
create_event(instance, orig_patch.related, instance.related)
156+
create_event(instance)
159157

160158

161159
@receiver(pre_save, sender=Patch)

patchwork/tests/test_events.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ def test_patch_relations_changed(self):
190190
self.assertEqual(
191191
events[1].category, Event.CATEGORY_PATCH_RELATION_CHANGED)
192192
self.assertEqual(events[1].project, patches[1].project)
193-
self.assertEqual(events[1].previous_relation, None)
194-
self.assertEqual(events[1].current_relation, relation)
193+
self.assertIsNone(events[1].previous_relation)
194+
self.assertIsNone(events[1].current_relation)
195195

196196
# add the third patch
197197

@@ -203,8 +203,8 @@ def test_patch_relations_changed(self):
203203
self.assertEqual(
204204
events[1].category, Event.CATEGORY_PATCH_RELATION_CHANGED)
205205
self.assertEqual(events[1].project, patches[1].project)
206-
self.assertEqual(events[1].previous_relation, None)
207-
self.assertEqual(events[1].current_relation, relation)
206+
self.assertIsNone(events[1].previous_relation)
207+
self.assertIsNone(events[1].current_relation)
208208

209209
# drop the third patch
210210

@@ -216,8 +216,8 @@ def test_patch_relations_changed(self):
216216
self.assertEqual(
217217
events[2].category, Event.CATEGORY_PATCH_RELATION_CHANGED)
218218
self.assertEqual(events[2].project, patches[1].project)
219-
self.assertEqual(events[2].previous_relation, relation)
220-
self.assertEqual(events[2].current_relation, None)
219+
self.assertIsNone(events[2].previous_relation)
220+
self.assertIsNone(events[2].current_relation)
221221

222222

223223
class CheckCreatedTest(_BaseTestCase):

0 commit comments

Comments
 (0)