summaryrefslogtreecommitdiff
path: root/patchwork/signals.py
blob: 4db9909f6063096bc9d41b0fe707c9afea23a0d9 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Patchwork - automated patch tracking system
# Copyright (C) 2016 Stephen Finucane <stephen@that.guru>
#
# SPDX-License-Identifier: GPL-2.0-or-later

from datetime import datetime as dt

from django.db.models.signals import post_save
from django.db.models.signals import pre_save
from django.dispatch import receiver

from patchwork.models import Check
from patchwork.models import Cover
from patchwork.models import Event
from patchwork.models import Patch
from patchwork.models import PatchChangeNotification
from patchwork.models import Series


@receiver(pre_save, sender=Patch)
def patch_change_callback(sender, instance, raw, **kwargs):
    # we only want notification of modified patches
    if raw or instance.pk is None:
        return

    if instance.project is None or not instance.project.send_notifications:
        return

    try:
        orig_patch = Patch.objects.get(pk=instance.pk)
    except Patch.DoesNotExist:
        return

    # If there's no interesting changes, abort without creating the
    # notification
    if orig_patch.state == instance.state:
        return

    notification = None
    try:
        notification = PatchChangeNotification.objects.get(patch=instance)
    except PatchChangeNotification.DoesNotExist:
        pass

    if notification is None:
        notification = PatchChangeNotification(patch=instance,
                                               orig_state=orig_patch.state)
    elif notification.orig_state == instance.state:
        # If we're back at the original state, there is no need to notify
        notification.delete()
        return

    notification.last_modified = dt.utcnow()
    notification.save()


@receiver(post_save, sender=Cover)
def create_cover_created_event(sender, instance, created, raw, **kwargs):

    def create_event(cover):
        return Event.objects.create(
            category=Event.CATEGORY_COVER_CREATED,
            project=cover.project,
            cover=cover)

    # don't trigger for items loaded from fixtures or new items
    if raw or not created:
        return

    create_event(instance)


@receiver(post_save, sender=Patch)
def create_patch_created_event(sender, instance, created, raw, **kwargs):

    def create_event(patch):
        return Event.objects.create(
            category=Event.CATEGORY_PATCH_CREATED,
            project=patch.project,
            patch=patch)

    # don't trigger for items loaded from fixtures or new items
    if raw or not created:
        return

    create_event(instance)


@receiver(pre_save, sender=Patch)
def create_patch_state_changed_event(sender, instance, raw, **kwargs):

    def create_event(patch, before, after):
        return Event.objects.create(
            category=Event.CATEGORY_PATCH_STATE_CHANGED,
            project=patch.project,
            actor=getattr(patch, '_edited_by', None),
            patch=patch,
            previous_state=before,
            current_state=after)

    # don't trigger for items loaded from fixtures or new items
    if raw or not instance.pk:
        return

    orig_patch = Patch.objects.get(pk=instance.pk)

    if orig_patch.state == instance.state:
        return

    create_event(instance, orig_patch.state, instance.state)


@receiver(pre_save, sender=Patch)
def create_patch_delegated_event(sender, instance, raw, **kwargs):

    def create_event(patch, before, after):
        return Event.objects.create(
            category=Event.CATEGORY_PATCH_DELEGATED,
            project=patch.project,
            actor=getattr(patch, '_edited_by', None),
            patch=patch,
            previous_delegate=before,
            current_delegate=after)

    # don't trigger for items loaded from fixtures or new items
    if raw or not instance.pk:
        return

    orig_patch = Patch.objects.get(pk=instance.pk)

    if orig_patch.delegate == instance.delegate:
        return

    create_event(instance, orig_patch.delegate, instance.delegate)


@receiver(pre_save, sender=Patch)
def create_patch_relation_changed_event(sender, instance, raw, **kwargs):

    def create_event(patch, before, after):
        return Event.objects.create(
            category=Event.CATEGORY_PATCH_RELATION_CHANGED,
            project=patch.project,
            actor=getattr(patch, '_edited_by', None),
            patch=patch,
            previous_relation=before,
            current_relation=after)

    # don't trigger for items loaded from fixtures or new items
    if raw or not instance.pk:
        return

    orig_patch = Patch.objects.get(pk=instance.pk)

    if orig_patch.related == instance.related:
        return

    create_event(instance, orig_patch.related, instance.related)


@receiver(pre_save, sender=Patch)
def create_patch_completed_event(sender, instance, raw, **kwargs):

    def create_event(patch):
        return Event.objects.create(
            category=Event.CATEGORY_PATCH_COMPLETED,
            project=patch.project,
            patch=patch,
            series=patch.series)

    # don't trigger for items loaded from fixtures, new items or items that
    # (still) don't have a series
    if raw or not instance.pk or not instance.series:
        return

    orig_patch = Patch.objects.get(pk=instance.pk)

    # we don't currently allow users to change a series, though this might
    # change in the future. However, we handle that here nonetheless
    if orig_patch.series == instance.series:
        return

    # if dependencies not met, don't raise event. There's also no point raising
    # events for successors since they'll have the same issue
    predecessors = Patch.objects.filter(
        series=instance.series, number__lt=instance.number)
    if predecessors.count() != instance.number - 1:
        return

    create_event(instance)

    # if this satisfies dependencies for successor patch, raise events for
    # those
    count = instance.number + 1
    for successor in Patch.objects.order_by('number').filter(
            series=instance.series, number__gt=instance.number):
        if successor.number != count:
            break

        create_event(successor)
        count += 1


@receiver(post_save, sender=Check)
def create_check_created_event(sender, instance, created, raw, **kwargs):

    def create_event(check):
        # TODO(stephenfin): It might make sense to add a 'project' field to
        # 'check' to prevent lookups here and in the REST API
        return Event.objects.create(
            category=Event.CATEGORY_CHECK_CREATED,
            project=check.patch.project,
            actor=check.user,
            patch=check.patch,
            created_check=check)

    # don't trigger for items loaded from fixtures or existing items
    if raw or not created:
        return

    create_event(instance)


@receiver(post_save, sender=Series)
def create_series_created_event(sender, instance, created, raw, **kwargs):

    def create_event(series):
        return Event.objects.create(
            category=Event.CATEGORY_SERIES_CREATED,
            project=series.project,
            series=series)

    # don't trigger for items loaded from fixtures or existing items
    if raw or not created:
        return

    create_event(instance)


@receiver(pre_save, sender=Patch)
def create_series_completed_event(sender, instance, raw, **kwargs):

    # NOTE(stephenfin): It's actually possible for this event to be fired
    # multiple times for a given series. To trigger this case, you would need
    # to send an additional patch to already exisiting series. This pattern
    # exists in the wild ('PATCH 5/n'), so we probably want to retest a series
    # in that case.

    def create_event(series):
        return Event.objects.create(
            category=Event.CATEGORY_SERIES_COMPLETED,
            project=series.project,
            series=series)

    # don't trigger for items loaded from fixtures, new items or items that
    # (still) don't have a series
    if raw or not instance.pk or not instance.series:
        return

    orig_patch = Patch.objects.get(pk=instance.pk)

    # we don't currently allow users to change a series (though this might
    # change in the future) meaning if the patch already had a series, there's
    # nothing to notify about
    if orig_patch.series:
        return

    # we can't use "series.received_all" here since we haven't actually saved
    # the instance yet so we duplicate that logic here but with an offset
    if (instance.series.received_total + 1) >= instance.series.total:
        create_event(instance.series)