BROKEN! MIGRATE! BIG UPD: not yet complete support for publish[ed|ing]_[web|facebook]
This commit is contained in:
@@ -4,9 +4,10 @@ from django.apps import apps
|
||||
from django.utils import timezone
|
||||
|
||||
from . import emails
|
||||
from .utils import get_users_by_role
|
||||
from .utils import get_users_by_role, has_role
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
today = datetime.date.today()
|
||||
|
||||
|
||||
class BasicWorkflow(object):
|
||||
@@ -18,16 +19,26 @@ class BasicWorkflow(object):
|
||||
valid = True
|
||||
return_code = 'OK'
|
||||
message = u'OK'
|
||||
if code == 'accepted':
|
||||
if code.startswith('accept'):
|
||||
if not event.is_flagged('submitted'):
|
||||
valid = False
|
||||
return_code = 'not-submitted'
|
||||
message = u'Event is not submitted.'
|
||||
elif code == 'publishing' or code == 'published':
|
||||
elif code.startswith('publishing'):
|
||||
if not event.is_flagged('accepted'):
|
||||
valid = False
|
||||
return_code = 'not-accepted'
|
||||
message = u'Event is not accepted.'
|
||||
elif code.startswith('published'):
|
||||
if event.planned_publication_date and event.planned_publication_date > today:
|
||||
valid = False
|
||||
return_code = 'not-due'
|
||||
message = u'Event is not due to publication.'
|
||||
elif not event.is_flagged('accepted'):
|
||||
valid = False
|
||||
return_code = 'not-accepted'
|
||||
message = u'Event is not accepted.'
|
||||
|
||||
if callback is not None:
|
||||
callback(valid, return_code, message, *args, **kwargs)
|
||||
return valid, return_code, message
|
||||
@@ -37,7 +48,6 @@ class BasicWorkflow(object):
|
||||
if not event.id:
|
||||
return
|
||||
|
||||
today = datetime.date.today()
|
||||
midnight = datetime.time(00, 00, 00)
|
||||
|
||||
if code in (None, 'draft'):
|
||||
@@ -53,18 +63,86 @@ class BasicWorkflow(object):
|
||||
event.set_next_number()
|
||||
logger.info('Setting number on Event %s', event)
|
||||
|
||||
if code in (None, 'published', 'publishing'):
|
||||
# Check if event with publishing flag should now have a published flag also.
|
||||
if (event.flags.filter(status__code='publishing').exists() and
|
||||
not event.flags.filter(status__code='published').exists()):
|
||||
if not event.planned_publication_date:
|
||||
logger.info('Detected published state of Event %s', event)
|
||||
flag = event.flags.filter(status__code='publishing').last()
|
||||
event.set_flag(status='published', timestamp=flag.timestamp)
|
||||
elif event.planned_publication_date <= today:
|
||||
logger.info('Detected published state of Event %s', event)
|
||||
timestamp = timezone.make_aware(datetime.datetime.combine(event.planned_publication_date, midnight))
|
||||
event.set_flag(status='published', timestamp=timestamp)
|
||||
if code in (None,
|
||||
'publishing_facebook',
|
||||
'publishing_web',
|
||||
'publishing',
|
||||
'published_facebook',
|
||||
'published_web',
|
||||
'published'):
|
||||
|
||||
if not event.flags.filter(status__code='published').exists():
|
||||
# Event is not fully published yet.
|
||||
|
||||
# We use some querysets, just to make code more readable.
|
||||
pub_flags = event.flags.filter(status__code__in=('publishing_facebook',
|
||||
'publishing_web',
|
||||
'publishing',
|
||||
'published_facebook',
|
||||
'published_web',
|
||||
'published')).order_by('timestamp')
|
||||
|
||||
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')
|
||||
published_facebook = pub_flags.filter(status__code='published_facebook')
|
||||
|
||||
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.
|
||||
if event.planned_publication_date:
|
||||
timestamp = timezone.make_aware(datetime.datetime.combine(
|
||||
event.planned_publication_date,
|
||||
midnight)
|
||||
)
|
||||
else:
|
||||
timestamp = None
|
||||
|
||||
if event.flags.filter(status__code='publishing').exists():
|
||||
# The publishers have confirmed the publications date,
|
||||
# so we can flag the complete publication.
|
||||
if not timestamp:
|
||||
timestamp = event.flags.filter(status__code='publishing').last().timestamp
|
||||
logger.info('Detected published state of Event %s', event)
|
||||
event.set_flag(status='published', timestamp=timestamp)
|
||||
elif ((publishing_web.exists() or published_web.exists()) and
|
||||
(publishing_facebook.exists() or published_facebook.exists())):
|
||||
# All publishers have confirmed the publication date or have published already
|
||||
# so we can flag the complete published state.
|
||||
if not timestamp:
|
||||
timestamp = pub_flags.last().timestamp
|
||||
logger.info('Detected general published state of Event %s', event)
|
||||
event.set_flag(status='published', timestamp=timestamp)
|
||||
else:
|
||||
if publishing_web.exists() and not published_web.exists():
|
||||
# One publisher has confirmed the publication date,
|
||||
# so we can flag, that he/she has published.
|
||||
if not timestamp:
|
||||
timestamp = event.flags.filter(status__code='publishing_web').last().timestamp
|
||||
logger.info('Detected published_web state of Event %s', event)
|
||||
event.set_flag(status='published_web', timestamp=timestamp)
|
||||
if publishing_facebook.exists() and not published_facebook.exists():
|
||||
# One publisher has confirmed the publication date,
|
||||
# so we can flag, that he/she has published.
|
||||
if not timestamp:
|
||||
timestamp = event.flags.filter(status__code='publishing_facebook').last().timestamp
|
||||
logger.info('Detected published_facebook state of Event %s', event)
|
||||
event.set_flag(status='published_facebook', timestamp=timestamp)
|
||||
if published_web.exists() and published_facebook.exists():
|
||||
# All publishers have published,
|
||||
# so we can flag the complete published state.
|
||||
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():
|
||||
# Event is not due to be published yet,
|
||||
# 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'))
|
||||
timestamp = flags.order_by('timestamp').last().timestamp
|
||||
event.set_flag(status='publishing', timestamp=timestamp)
|
||||
|
||||
if code in (None, 'expired'):
|
||||
# Check if event is expired now and need a expired flag.
|
||||
@@ -88,6 +166,26 @@ class BasicWorkflow(object):
|
||||
timestamp = timezone.make_aware(datetime.datetime.combine(expired_at, midnight))
|
||||
event.set_flag(status='expired', timestamp=timestamp)
|
||||
|
||||
@classmethod
|
||||
def get_status_flags(cls, event):
|
||||
cls.status_code_update(event)
|
||||
last_flag = event.flags.last()
|
||||
if not last_flag:
|
||||
#last_flag = event.set_flag('void')
|
||||
return []
|
||||
flags = [last_flag]
|
||||
|
||||
last_status = last_flag.status
|
||||
if last_status.code.startswith('publishing_'):
|
||||
flags += event.flags.filter(status__code='accepted')
|
||||
elif last_status.code.startswith('published_'):
|
||||
if event.is_flagged('publishing'):
|
||||
flags += event.flags.filter(status__code='publishing')
|
||||
else:
|
||||
flags += event.flags.filter(status__code='accepted')
|
||||
|
||||
return flags
|
||||
|
||||
@classmethod
|
||||
def get_number(cls, event):
|
||||
if event.number and event.flags.filter(status__code='accepted').exists():
|
||||
@@ -99,25 +197,50 @@ class BasicWorkflow(object):
|
||||
# Permissions
|
||||
#
|
||||
@classmethod
|
||||
def has_permission(cls, event, user, permission):
|
||||
raise NotImplementedError('not ready yet')
|
||||
def has_global_permission(cls, user, permission):
|
||||
if user.is_superuser:
|
||||
return True
|
||||
|
||||
if permission == 'export':
|
||||
return has_role(user, 'publisher')
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def has_object_permission(cls, user, permission, obj):
|
||||
if user.is_superuser:
|
||||
return True
|
||||
|
||||
if permission == 'view':
|
||||
if user == event.owner:
|
||||
if user == obj.owner:
|
||||
return True
|
||||
if has_role(user, 'manager_super'):
|
||||
return True
|
||||
if has_role(user, 'manager_{}'.format(obj.sport.lower())):
|
||||
return True
|
||||
if has_role(user, 'publisher') and obj.is_flagged('accepted'):
|
||||
return True
|
||||
raise Exception('must check roles')
|
||||
elif permission == 'submit':
|
||||
if user == event.owner:
|
||||
if user == obj.owner:
|
||||
return True
|
||||
elif permission == 'accept':
|
||||
raise Exception('must check roles')
|
||||
if has_role(user, 'manager_super'):
|
||||
return True
|
||||
if has_role(user, 'manager_{}'.format(obj.sport.lower())):
|
||||
return True
|
||||
elif permission == 'publish':
|
||||
raise Exception('must check roles')
|
||||
if has_role(user, 'publisher'):
|
||||
return True
|
||||
elif permission == 'update':
|
||||
raise Exception('must check roles')
|
||||
if not obj.is_flagged('submitted'):
|
||||
if user == obj.owner:
|
||||
return True
|
||||
elif not obj.is_flagged('accepted'):
|
||||
if has_role(user, 'manager_super'):
|
||||
return True
|
||||
if has_role(user, 'manager_{}'.format(obj.sport.lower())):
|
||||
return True
|
||||
elif has_role(user, 'publisher'):
|
||||
return True
|
||||
return False
|
||||
|
||||
#
|
||||
@@ -142,11 +265,12 @@ class BasicWorkflow(object):
|
||||
recipients = [event.owner]
|
||||
if event.is_flagged('submitted'):
|
||||
# If the event is already submitted, add managers to the recipients.
|
||||
recipients += get_users_by_role('manage_all')
|
||||
recipients += get_users_by_role('manage_{}'.format(event.sport.lower()))
|
||||
recipients += get_users_by_role('manager_super')
|
||||
recipients += get_users_by_role('manager_{}'.format(event.sport.lower()))
|
||||
if event.is_flagged('accepted'):
|
||||
# If the event is already published, add publishers to the recipients.
|
||||
recipients += get_users_by_role('publish_incremental')
|
||||
recipients += get_users_by_role('publisher_web')
|
||||
recipients += get_users_by_role('publisher_facebook')
|
||||
|
||||
for recipient in recipients:
|
||||
if recipient.email and recipient.email != updater.email:
|
||||
@@ -172,13 +296,13 @@ 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('manage_all')
|
||||
recipients += get_users_by_role('manage_{}'.format(event.sport.lower()))
|
||||
recipients = get_users_by_role('manager_super')
|
||||
recipients += get_users_by_role('manager_{}'.format(event.sport.lower()))
|
||||
OneClickAction = app_config.get_model('OneClickAction')
|
||||
for recipient in recipients:
|
||||
if recipient.email:
|
||||
action = OneClickAction(command='EA')
|
||||
action.parameters = '{event},{user}'.format(event=event.id, user=recipient.id)
|
||||
action = OneClickAction(command='EVENT_STATUS_UPDATE')
|
||||
action.parameters = '{event},accepted,{user}'.format(event=event.id, user=recipient.id)
|
||||
action.save()
|
||||
email = emails.EventToAcceptMail(recipient=recipient, event=event, accept_action=action)
|
||||
email.send()
|
||||
@@ -192,15 +316,35 @@ class BasicWorkflow(object):
|
||||
# Inform publishers that they have to publish the event.
|
||||
# Also create OneClickActions for all of them and add the link to the mail,
|
||||
# so they can confirm the publication with a click into the mail.
|
||||
recipients = get_users_by_role('publish_incremental')
|
||||
|
||||
# Website
|
||||
recipients = get_users_by_role('publisher_web')
|
||||
status_code = 'publishing_web'
|
||||
OneClickAction = app_config.get_model('OneClickAction')
|
||||
for recipient in recipients:
|
||||
if recipient.email:
|
||||
action = OneClickAction(command='EP')
|
||||
action.parameters = '{event},{user}'.format(event=event.id, user=recipient.id)
|
||||
action = OneClickAction(command='EVENT_STATUS_UPDATE')
|
||||
action.parameters = '{event},{status_code},{user}'.format(event=event.id,
|
||||
status_code=status_code,
|
||||
user=recipient.id)
|
||||
action.save()
|
||||
email = emails.EventToPublishMail(recipient=recipient, event=event, editor=updater,
|
||||
confirm_publication_action=action)
|
||||
email = emails.EventToPublishWebMail(recipient=recipient, event=event, editor=updater,
|
||||
confirm_publication_action=action)
|
||||
email.send()
|
||||
|
||||
# Facebook
|
||||
recipients = get_users_by_role('publisher_facebook')
|
||||
status_code = 'publishing_facebook'
|
||||
OneClickAction = app_config.get_model('OneClickAction')
|
||||
for recipient in recipients:
|
||||
if recipient.email:
|
||||
action = OneClickAction(command='EVENT_STATUS_UPDATE')
|
||||
action.parameters = '{event},{status_code},{user}'.format(event=event.id,
|
||||
status_code=status_code,
|
||||
user=recipient.id)
|
||||
action.save()
|
||||
email = emails.EventToPublishFacebookMail(recipient=recipient, event=event, editor=updater,
|
||||
confirm_publication_action=action)
|
||||
email.send()
|
||||
|
||||
#
|
||||
@@ -215,8 +359,6 @@ class BasicWorkflow(object):
|
||||
else:
|
||||
publication_deadline = first_day - datetime.timedelta(app_config.settings.publish_before_begin_days)
|
||||
|
||||
today = datetime.date.today()
|
||||
|
||||
for year in (today.year, today.year + 1):
|
||||
for issue in app_config.settings.publish_issues:
|
||||
if not ('issue' in issue and 'release' in issue and 'deadline' in issue):
|
||||
|
||||
Reference in New Issue
Block a user