CONT: fixed the previous changes.

This commit is contained in:
2019-01-30 16:35:37 +01:00
parent 7667277862
commit f0e225c5fd
11 changed files with 79 additions and 59 deletions

View File

@@ -18,11 +18,11 @@ DEFAULT_EVENT_STATI = {
'draft': (10, _(u'Entwurf'), 'info'),
'submitted': (30, _(u'Eingereicht'), 'danger'),
'accepted': (50, _(u'Freigegeben'), 'warning'),
'publishing_facebook': (68, _(u'Veröffentlichung Facebook am {planned_publication_date}'), 'warning'),
'publishing_web': (69, _(u'Veröffentlichung Web am {planned_publication_date}'), 'warning'),
'publishing': (70, _(u'Veröffentlichung am {planned_publication_date}'), 'warning'),
'published_facebook': (78, _(u'Veröffentlicht Facebook'), 'success'),
'published_web': (79, _(u'Veröffentlicht Web'), 'success'),
'publishing_facebook': (68, _(u'Veröffentlichung (Facebook)'), 'warning'),
'publishing_web': (69, _(u'Veröffentlichung (Web)'), 'warning'),
'publishing': (70, _(u'Veröffentlichung'), 'warning'),
'published_facebook': (78, _(u'Veröffentlicht (Facebook)'), 'success'),
'published_web': (79, _(u'Veröffentlicht (Web)'), 'success'),
'published': (80, _(u'Veröffentlicht'), 'success'),
'expired': (100, _(u'Ausgelaufen'), None),
}
@@ -49,10 +49,10 @@ class BasicWorkflow(object):
year = event.first_day.year
year_begin = datetime.date(year, 1, 1)
year_end = datetime.date(year, 12, 31)
qs = event.objects.filter(number__isnull=False,
sport=event.sport,
first_day__gte=year_begin,
first_day__lte=year_end).order_by('number')
qs = event.__class__.objects.filter(number__isnull=False,
sport=event.sport,
first_day__gte=year_begin,
first_day__lte=year_end).order_by('number')
last = qs.last()
if last:
match = re.match(r'^(?P<sport>[A-Z])(?P<count>[0-9][0-9]*)/(?P<year>[0-9][0-9]*)', last.number)
@@ -65,6 +65,8 @@ class BasicWorkflow(object):
event.save(implicit_update=True)
return event.number
# TODO: the name/intention of this method is unclear.
# Could we make it obsolete? At least it should be private
def check_status(self, code=None):
event = self._event
if not event.id:
@@ -104,6 +106,7 @@ class BasicWorkflow(object):
'published_web',
'published')).order_by('timestamp')
publishing = pub_flags.filter(status__code='publishing')
publishing_web = pub_flags.filter(status__code='publishing_web')
publishing_facebook = pub_flags.filter(status__code='publishing_facebook')
published_web = pub_flags.filter(status__code='published_web')
@@ -112,7 +115,7 @@ class BasicWorkflow(object):
if not event.planned_publication_date or event.planned_publication_date <= today:
# Event is due to be published.
# Timestamp of the detected action flag. No very good.
# Timestamp of the detected action flag. No very good. TODO
if event.planned_publication_date:
timestamp = timezone.make_aware(datetime.datetime.combine(
event.planned_publication_date,
@@ -157,12 +160,13 @@ class BasicWorkflow(object):
timestamp = pub_flags.last().timestamp
logger.info('Detected general published state of Event %s', event)
event.set_flag(status='published', timestamp=timestamp)
elif publishing_web.exists() and publishing_facebook.exists():
elif not publishing.exists() and publishing_web.exists() and publishing_facebook.exists():
# Event is not due to be published yet,
# does not have a general publishing flag,
# but all publishers have confirmed the publication date,
# so we set a general publishing flag.
logger.info('Detected publishing state of Event %s', event)
flags = event.flags.filter(status_code__in=('publishing_web', 'publishing_facebook'))
flags = event.flags.filter(status__code__in=('publishing_web', 'publishing_facebook'))
timestamp = flags.order_by('timestamp').last().timestamp
event.set_flag(status='publishing', timestamp=timestamp)
@@ -190,9 +194,9 @@ class BasicWorkflow(object):
def has_reached_status(self, code):
self.check_status(code)
if code == 'publishing':
if code == 'publishing*':
codes = ['publishing', 'publishing_web', 'publishing_facebook']
elif code == 'published':
elif code == 'published*':
codes = ['published', 'published_web', 'published_facebook']
else:
codes = [code]
@@ -235,7 +239,7 @@ class BasicWorkflow(object):
if flag:
return flag
valid, return_code, message = self.validate_status_update(code, user)
valid, return_code, message = self.validate_status_update(code)
if not valid:
logger.warning(u'Invalid status update to \'%s\': %s Event: %s', code, message, event)
@@ -251,10 +255,10 @@ class BasicWorkflow(object):
status_list = []
event.workflow.check_status()
last_flag = event.flags.last()
if last_flag:
flags = [last_flag]
last_status = last_flag.status
heaviest_flag = event.flags.order_by('status').last()
if heaviest_flag:
flags = []
last_status = heaviest_flag.status
if last_status.code.startswith('publishing_'):
flags += event.flags.filter(status__code='accepted')
elif last_status.code.startswith('published_'):
@@ -262,12 +266,20 @@ class BasicWorkflow(object):
flags += event.flags.filter(status__code='publishing')
else:
flags += event.flags.filter(status__code='accepted')
flags.append(heaviest_flag)
deferred_publishing = event.planned_publication_date and event.planned_publication_date > today
add_publishing_date = False
for flag in flags:
format_kwargs = event.get_template_context()
label = flag.status.label.format(**format_kwargs)
flag.status.label = label
status_list.append(flag.status)
if deferred_publishing and flag.status.code.startswith('publishing'):
add_publishing_date = True
if add_publishing_date:
date_str = event.planned_publication_date.strftime('%d.%m.%Y')
label = _(u'Veröffentlichung am {date}').format(date=date_str)
status_list.append({'label': label})
return status_list
@@ -313,6 +325,7 @@ class BasicWorkflow(object):
return True
return False
# TODO: is a class method a good idea?
@classmethod
def has_global_permission(cls, user, permission):
if user.is_superuser:
@@ -325,6 +338,8 @@ class BasicWorkflow(object):
#
# Signal handlers
#
# TODO: the signal handlers should not be part of the workflow class,
# but call a method from the workflow of the particular event.
@classmethod
def send_emails_on_event_update(cls, sender, **kwargs):
event = kwargs.get('event')
@@ -344,7 +359,6 @@ class BasicWorkflow(object):
recipients = [event.owner]
if event.workflow.has_reached_status('submitted'):
# If the event is already submitted, add managers to the recipients.
recipients += get_users_by_role('manager_super')
recipients += get_users_by_role('manager_{}'.format(event.sport.lower()))
if event.workflow.has_reached_status('accepted'):
# If the event is already published, add publishers to the recipients.
@@ -375,8 +389,7 @@ class BasicWorkflow(object):
# Inform managers that they have to accept the event.
# Also create OneClickActions for all of them and add the link to the mail,
# so they can accept the event with a click into the mail.
recipients = get_users_by_role('manager_super')
recipients += get_users_by_role('manager_{}'.format(event.sport.lower()))
recipients = get_users_by_role('manager_{}'.format(event.sport.lower()))
OneClickAction = app_config.get_model('OneClickAction')
for recipient in recipients:
if recipient.email:
@@ -429,6 +442,7 @@ class BasicWorkflow(object):
#
# Misc logic
#
# TODO: is a class method a good idea?
@classmethod
def plan_publication(cls, first_day, deadline=None):
app_config = apps.get_containing_app_config(__package__)