Skip to content

Commit 646a2f2

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 <stephen@that.guru> Related: #379
1 parent fe07f30 commit 646a2f2

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
@@ -1765,7 +1765,7 @@ components:
17651765
current_state:
17661766
title: Current state
17671767
type: string
1768-
{% if version >= (1, 1) %}
1768+
{% if version >= (1, 2) %}
17691769
EventPatchRelationChanged:
17701770
allOf:
17711771
- $ref: '#/components/schemas/EventBase'
@@ -1781,9 +1781,11 @@ components:
17811781
previous_relation:
17821782
title: Previous relation
17831783
type: string
1784+
nullable: true
17841785
current_relation:
17851786
title: Current relation
17861787
type: string
1788+
nullable: true
17871789
{% endif %}
17881790
EventPatchDelegated:
17891791
allOf:

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,24 +1510,6 @@ components:
15101510
current_state:
15111511
title: Current state
15121512
type: string
1513-
EventPatchRelationChanged:
1514-
allOf:
1515-
- $ref: '#/components/schemas/EventBase'
1516-
- type: object
1517-
properties:
1518-
category:
1519-
enum:
1520-
- patch-relation-changed
1521-
payload:
1522-
properties:
1523-
patch:
1524-
$ref: '#/components/schemas/PatchEmbedded'
1525-
previous_relation:
1526-
title: Previous relation
1527-
type: string
1528-
current_relation:
1529-
title: Current relation
1530-
type: string
15311513
EventPatchDelegated:
15321514
allOf:
15331515
- $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
@@ -1712,9 +1712,11 @@ components:
17121712
previous_relation:
17131713
title: Previous relation
17141714
type: string
1715+
nullable: true
17151716
current_relation:
17161717
title: Current relation
17171718
type: string
1719+
nullable: true
17181720
EventPatchDelegated:
17191721
allOf:
17201722
- $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)