Commit 3613819f authored by Jose Lamego's avatar Jose Lamego Committed by Damien Lespiau
Browse files

models: Log patch-state-change event


Signed-off-by: default avatarJose Lamego <jose.a.lamego@linux.intel.com>
Signed-off-by: default avatarDamien Lespiau <damien.lespiau@intel.com>
parent 100977fd
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('patchwork', '0018_test_email_on_error'),
]
operations = [
migrations.AddField(
model_name='eventlog',
name='patch',
field=models.ForeignKey(to='patchwork.Patch', null=True),
),
]
......@@ -31,6 +31,7 @@ from django.utils.encoding import python_2_unicode_compatible
from django.utils.functional import cached_property
from django.utils.six import add_metaclass
import jsonfield
import threadlocalrequest
from patchwork.parser import hash_patch, extract_tags
......@@ -682,6 +683,7 @@ class EventLog(models.Model):
series = models.ForeignKey(Series)
user = models.ForeignKey(User, null=True)
parameters = jsonfield.JSONField(null=True)
patch = models.ForeignKey(Patch, null=True)
class Meta:
ordering = ['-event_time']
......@@ -809,12 +811,20 @@ class PatchChangeNotification(models.Model):
orig_state = models.ForeignKey(State)
def find_series_for_patch(patch):
try:
revision = SeriesRevisionPatch.objects.filter(patch=patch)[0].revision
return revision.series
except:
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 or not instance.project.send_notifications:
if instance.project is None:
return
try:
......@@ -823,10 +833,33 @@ def _patch_change_callback(sender, instance, **kwargs):
return
# If there's no interesting changes, abort without creating the
# notification
# notification or log
if orig_patch.state == instance.state:
return
# 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)
changed_patch = Patch.objects.get(pk=instance.pk)
# 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)
if series:
log = EventLog(event=event_state_change,
user=curr_user,
series_id=series.id,
patch=changed_patch,
parameters={'previous_state': previous_state,
'new_state': new_state,
})
log.save()
if not instance.project.send_notifications:
return
notification = None
try:
notification = PatchChangeNotification.objects.get(patch=instance)
......
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