ADD: added support for Event Updated Mail.

This commit is contained in:
2018-11-21 13:33:45 +01:00
parent 6cae9d5e33
commit 30b5e920ee
7 changed files with 217 additions and 115 deletions

View File

@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import datetime
import difflib
import logging
import os
import re
@@ -252,6 +253,16 @@ class Event(models.Model):
internal_note = models.TextField(blank=True,
verbose_name=_(u'Bearbeitungshinweis'))
@property
def editor(self):
if not hasattr(self, '_editor'):
self._editor = None
return self._editor
@editor.setter
def editor(self, editor):
self._editor = editor
class Meta:
verbose_name = _(u'Veranstaltung')
verbose_name_plural = _(u'Veranstaltungen')
@@ -268,6 +279,7 @@ class Event(models.Model):
def save(self, **kwargs):
creating = False
original_text = ''
if not self.id:
user_model = get_user_model()
@@ -295,65 +307,96 @@ class Event(models.Model):
owner.save()
logger.info('Owner created: %s', owner.username)
if self.editor and self.editor.is_authenticated and self.editor != owner:
logger.warning('Event is not created by its owner (Current user: %s, Owner: %s)!', self.editor, owner)
self.owner = owner
creating = True
else:
original = Event.objects.get(id=self.id)
original_text = original.render_as_text()
if not self.editor or not self.editor.is_authenticated:
self.editor = self.owner
super(Event, self).save(**kwargs)
if creating:
self.confirm_status('draft', self.owner)
logger.info('Event created: %s', self)
self.confirm_status('draft', self.editor)
else:
modified_text = self.render_as_text()
o_lines = original_text.split('\n')
m_lines = modified_text.split('\n')
diff_lines = list(difflib.unified_diff(o_lines, m_lines, n=len(m_lines), lineterm=''))
diff_text = '\n'.join(diff_lines[3:])
signals.event_updated.send(sender=self.__class__, event=self, diff=diff_text, user=self.editor)
logger.info('Event updated: %s', self)
def update_flags(self):
def _internal_update(self):
if not self.id:
logger.critical('Event._internal_update() was called before Event was saved properly.')
raise Exception('Code is on fire!')
super(Event, self).save()
def update_flags(self, for_status=None):
if not self.id:
return
if isinstance(for_status, EventStatus):
code = for_status.code
else:
code = for_status
today = datetime.date.today()
midnight = datetime.time(00, 00, 00)
if not self.flags.filter(status__code='draft').exists():
new_flag = EventFlag(event=self, status=get_event_status('draft'), timestamp=self.created_at)
new_flag.save()
logger.info('Detected draft state of Event %s', self)
if (self.flags.filter(status__code='publishing').exists() and
not self.flags.filter(status__code='published').exists()):
if not self.planned_publication_date:
flag = self.flags.filter(status__code='publishing').last()
new_status = get_event_status('published')
new_flag = EventFlag(event=self, status=new_status, timestamp=flag.timestamp)
if code in (None, 'draft'):
if not self.flags.filter(status__code='draft').exists():
new_flag = EventFlag(event=self, status=get_event_status('draft'), timestamp=self.created_at)
new_flag.save()
logger.info('Detected published state of Event %s', self)
elif self.planned_publication_date <= today:
new_status = get_event_status('published')
new_timestamp = timezone.make_aware(datetime.datetime.combine(self.planned_publication_date, midnight))
new_flag = EventFlag(event=self, status=new_status, timestamp=new_timestamp)
new_flag.save()
logger.info('Detected published state of Event %s', self)
logger.info('Detected draft state of Event %s', self)
if not self.flags.filter(status__code='expired').exists():
expired_at = None
if code in (None, 'published', 'publishing'):
if (self.flags.filter(status__code='publishing').exists() and
not self.flags.filter(status__code='published').exists()):
if not self.planned_publication_date:
flag = self.flags.filter(status__code='publishing').last()
new_status = get_event_status('published')
new_flag = EventFlag(event=self, status=new_status, timestamp=flag.timestamp)
new_flag.save()
logger.info('Detected published state of Event %s', self)
elif self.planned_publication_date <= today:
new_status = get_event_status('published')
new_timestamp = timezone.make_aware(datetime.datetime.combine(self.planned_publication_date,
midnight))
new_flag = EventFlag(event=self, status=new_status, timestamp=new_timestamp)
new_flag.save()
logger.info('Detected published state of Event %s', self)
if self.alt_last_day:
if self.alt_last_day < today:
expired_at = self.alt_last_day
elif self.last_day:
if self.last_day < today:
expired_at = self.last_day
elif self.alt_first_day:
if self.alt_first_day < today:
expired_at = self.alt_first_day
elif self.first_day and self.first_day < today:
expired_at = self.first_day
if code in (None, 'expired'):
if not self.flags.filter(status__code='expired').exists():
expired_at = None
if expired_at:
new_timestamp = timezone.make_aware(datetime.datetime.combine(expired_at, midnight))
new_flag = EventFlag(event=self, status=get_event_status('expired'), timestamp=new_timestamp)
new_flag.save()
logger.info('Detected expired state of Event %s', self)
if self.alt_last_day:
if self.alt_last_day < today:
expired_at = self.alt_last_day
elif self.last_day:
if self.last_day < today:
expired_at = self.last_day
elif self.alt_first_day:
if self.alt_first_day < today:
expired_at = self.alt_first_day
elif self.first_day and self.first_day < today:
expired_at = self.first_day
if expired_at:
new_timestamp = timezone.make_aware(datetime.datetime.combine(expired_at, midnight))
new_flag = EventFlag(event=self, status=get_event_status('expired'), timestamp=new_timestamp)
new_flag.save()
logger.info('Detected expired state of Event %s', self)
def is_flagged(self, status):
self.update_flags()
self.update_flags(status)
if isinstance(status, EventStatus):
code = status.code
else:
@@ -388,7 +431,7 @@ class Event(models.Model):
logger.warning('Event.confirm_status(): yet not submitted event got accepted! (Event: %s)', self)
if not self.number:
self.number = self.get_next_number()
self.save()
self._internal_update()
elif code == 'publishing' or code == 'published':
if not self.is_flagged('accepted'):
logger.warning('Event.confirm_status(): yet not accepted event got published! (Event: %s)', self)
@@ -397,17 +440,7 @@ class Event(models.Model):
flag = EventFlag(event=self, status=status_obj, user=user)
flag.save()
logger.info('Flagging status \'%s\' for %s', code, self)
if code == 'submitted':
signals.event_submitted.send(sender=self.__class__, event=self)
elif code == 'accepted':
signals.event_accepted.send(sender=self.__class__, event=self)
elif code == 'publishing':
signals.event_publishing.send(sender=self.__class__, event=self)
elif code == 'published':
signals.event_published.send(sender=self.__class__, event=self)
elif code == 'expired':
signals.event_expired.send(sender=self.__class__, event=self)
signals.event_status_updated.send(sender=self.__class__, event=self, flag=flag)
return flag