-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4442 from magfest/guidebook-update-updates
Add more improvements to Guidebook syncing
- Loading branch information
Showing
6 changed files
with
102 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
import json | ||
|
||
from collections import defaultdict | ||
from datetime import timedelta | ||
from dateutil import parser as dateparser | ||
from sqlalchemy import or_ | ||
|
||
from uber.config import c | ||
from uber.decorators import render | ||
from uber.models import Session | ||
from uber.models import Email, Session, Tracking | ||
from uber.tasks import celery | ||
from uber.tasks.email import send_email | ||
from uber.utils import GuidebookUtils, localized_now | ||
|
||
|
||
__all__ = ['panels_waitlist_unaccepted_panels', 'sync_guidebook_models', 'check_stale_guidebook_models'] | ||
__all__ = ['panels_waitlist_unaccepted_panels', 'sync_guidebook_models', | ||
'check_deleted_guidebook_models', 'check_stale_guidebook_models'] | ||
|
||
|
||
def _get_deleted_models(session, deleted_since=None): | ||
deleted_synced = session.query(Tracking).filter(Tracking.action == c.DELETED, | ||
Tracking.snapshot.contains('"last_synced": {"data": {"guidebook"')) | ||
if deleted_since: | ||
deleted_synced = deleted_synced.filter(Tracking.when > deleted_since) | ||
|
||
deleted_models = defaultdict(list) | ||
model_names = {} | ||
|
||
for key, label in c.GUIDEBOOK_MODELS: | ||
model_names[key] = label | ||
|
||
for tracking_entry in deleted_synced: | ||
snapshot = json.loads(tracking_entry.snapshot) | ||
|
||
model = snapshot['_model'] | ||
if model == 'GuestGroup': | ||
model += '_band' if snapshot['group_type'] == c.BAND else '_guest' | ||
elif model == 'Group': | ||
model += '_dealer' | ||
|
||
model_name = 'Schedule Item' if model == 'Event' else model_names[model] | ||
|
||
deleted_models[model_name].append(snapshot['last_synced']['data']['guidebook']['name']) | ||
return deleted_models | ||
|
||
|
||
@celery.task | ||
|
@@ -31,21 +62,51 @@ def sync_guidebook_models(selected_model, sync_time, id_list): | |
session.commit() | ||
|
||
|
||
@celery.schedule(timedelta(hours=12)) | ||
def check_deleted_guidebook_models(): | ||
if not c.PRE_CON or not c.GUIDEBOOK_UPDATES_EMAIL: | ||
return | ||
|
||
with Session() as session: | ||
subject = f"Deleted Guidebook Items: {localized_now().strftime("%A %-I:%M %p")}" | ||
last_email = session.query(Email).filter(Email.subject.contains("Deleted Guidebook Items") | ||
).order_by(Email.when.desc()).first() | ||
|
||
deleted_models = _get_deleted_models(session, deleted_since=last_email.when) if last_email else _get_deleted_models(session) | ||
|
||
if deleted_models: | ||
body = render('emails/guidebook_deletes.txt', { | ||
'deleted_models': deleted_models, | ||
}, encoding=None) | ||
send_email.delay(c.REPORTS_EMAIL, c.GUIDEBOOK_UPDATES_EMAIL, | ||
subject, body, ident="guidebook_deletes" | ||
) | ||
|
||
|
||
@celery.schedule(timedelta(minutes=15)) | ||
def check_stale_guidebook_models(): | ||
if not c.AT_THE_CON: | ||
if not c.AT_THE_CON or not c.GUIDEBOOK_UPDATES_EMAIL: | ||
return | ||
|
||
with Session() as session: | ||
cl_updates, schedule_updates = GuidebookUtils.get_changed_models(session) | ||
stale_models = [key for key in cl_updates if cl_updates[key]] | ||
if schedule_updates: | ||
stale_models.append('Schedule') | ||
if stale_models: | ||
|
||
last_email = session.query(Email).filter(or_( | ||
Email.subject.contains("Guidebook Updates"), | ||
Email.subject.contains("Deleted Guidebook Items")) | ||
).order_by(Email.when.desc()).first() | ||
|
||
deleted_models = _get_deleted_models(session, deleted_since=last_email.when) if last_email else _get_deleted_models(session) | ||
|
||
if stale_models or deleted_models: | ||
body = render('emails/guidebook_updates.txt', { | ||
'stale_models': stale_models, | ||
'deleted_models': deleted_models, | ||
}, encoding=None) | ||
send_email.delay(c.REPORTS_EMAIL, "[email protected]", | ||
send_email.delay(c.REPORTS_EMAIL, c.GUIDEBOOK_UPDATES_EMAIL, | ||
f"Guidebook Updates: {localized_now().strftime("%A %-I:%M %p")}", | ||
body, ident="guidebook_updates" | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Attention Guidebook admins! The items below have been deleted from the system. | ||
|
||
{% for category in deleted_models %}{{ category }}(s): | ||
{% for item in deleted_models[category] %} - {{ item }} | ||
{% endfor %}{% endfor %} | ||
|
||
This email will be the only record of these item deletions. Please keep it until you delete all the above items from Guidebook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
Attention Guidebook admins! | ||
The following categories have stale items: | ||
|
||
{% if stale_models %}The following categories have been updated in the system: | ||
{% for label in stale_models %}- {{ label }} | ||
{% endfor %} | ||
|
||
You can review all pending items here: {{ c.URL_BASE }}/schedule_reports/index | ||
{% endif %}{% if deleted_models %}The following items have been deleted: | ||
{% for category in deleted_models %}{{ category }}(s): | ||
{% for item in deleted_models[category] %} - {{ item }} | ||
{% endfor %} | ||
{% endfor %} | ||
|
||
{% endif %}You can review all pending items here: {{ c.URL_BASE }}/schedule_reports/index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters