Commit 604cb264 authored by Damien Lespiau's avatar Damien Lespiau
Browse files

patch: Split the patch change callback into smaller functions



We start to have a number of different things to do when a patch is
saved. Split each step into its own function.
Signed-off-by: default avatarDamien Lespiau <damien.lespiau@intel.com>
parent b37d1b7a
......@@ -819,57 +819,42 @@ def find_series_for_patch(patch):
return None
def _patch_change_callback(sender, instance, **kwargs):
# we only want notification of modified patches
if instance.pk is None:
return
if instance.project is None:
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 or log
if orig_patch.state == instance.state:
return
def _patch_change_log_event(old_patch, new_patch):
# If state changed, log the event
event_state_change = Event.objects.get(name='patch-state-change')
curr_user = threadlocalrequest.get_current_user()
previous_state = str(orig_patch.state)
new_state = str(instance.state)
previous_state = str(old_patch.state)
new_state = str(new_patch.state)
# Do not log patch-state-change events for Patches that are not part of a
# Series (ie patches older than the introduction of Series)
series = find_series_for_patch(orig_patch)
series = find_series_for_patch(old_patch)
if series:
log = EventLog(event=event_state_change,
user=curr_user,
series_id=series.id,
patch=orig_patch,
patch=old_patch,
parameters={'previous_state': previous_state,
'new_state': new_state,
})
log.save()
if not instance.project.send_notifications:
def _patch_change_send_notification(old_patch, new_patch):
if not new_patch.project.send_notifications:
return
notification = None
try:
notification = PatchChangeNotification.objects.get(patch=instance)
notification = PatchChangeNotification.objects.get(patch=new_patch)
except PatchChangeNotification.DoesNotExist:
pass
if notification is None:
notification = PatchChangeNotification(patch=instance,
orig_state=orig_patch.state)
notification = PatchChangeNotification(patch=new_patch,
orig_state=old_patch.state)
elif notification.orig_state == instance.state:
elif notification.orig_state == new_patch.state:
# If we're back at the original state, there is no need to notify
notification.delete()
return
......@@ -877,6 +862,28 @@ def _patch_change_callback(sender, instance, **kwargs):
notification.last_modified = datetime.datetime.now()
notification.save()
def _patch_change_callback(sender, instance, **kwargs):
# we only want notification of modified patches
if instance.pk is None:
return
if instance.project is None:
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 or log
if orig_patch.state == instance.state:
return
_patch_change_log_event(orig_patch, instance)
_patch_change_send_notification(orig_patch, instance)
models.signals.pre_save.connect(_patch_change_callback, sender=Patch)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment