-
Notifications
You must be signed in to change notification settings - Fork 6
WIP:Add Reminder Email for events #209
base: master
Are you sure you want to change the base?
Changes from all commits
32017b2
d9e1ddd
4c884b4
16f93c3
25828e5
f9d50d2
49a5097
fa9b4aa
3cd00b3
04e99b8
686e17f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,11 @@ def wrapped(): | |
|
||
def schedule_task(time, func, *args): | ||
""" Schedule a task at some point in the future. """ | ||
func_s = func_str(func) | ||
if func_str(func) is "remindermail": | ||
item = pickle.load(args) | ||
func_s = "remindermail"+str(item['_id']) | ||
else: | ||
func_s = func_str(func) | ||
|
||
if func_s not in schedulable_functions: | ||
raise NotSchedulable("%s is not schedulable. Did you forget the " | ||
|
@@ -114,7 +118,11 @@ def schedule_task(time, func, *args): | |
|
||
def update_scheduled_task(time, func, *args): | ||
""" Update a scheduled task that was previously registered. """ | ||
func_s = func_str(func) | ||
if func_str(func) is "remindermail": | ||
item = pickle.load(args) | ||
func_s = "remindermail_"+str(item['_id']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will break the update. the |
||
else: | ||
func_s = func_str(func) | ||
|
||
if func_s not in schedulable_functions: | ||
raise NotSchedulable("%s is not schedulable. Did you forget the " | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,14 @@ | |
Needed when external users want to sign up for public events. | ||
""" | ||
from bson import ObjectId | ||
from datetime import datetime, timedelta | ||
from eve.methods.delete import deleteitem_internal | ||
from eve.methods.patch import patch_internal | ||
from flask import Blueprint, current_app, redirect, url_for | ||
from itsdangerous import BadSignature, Signer | ||
|
||
from amivapi.events.queue import update_waiting_list | ||
from amivapi.cron import schedulable, schedule_task, update_scheduled_task | ||
from amivapi.utils import mail | ||
|
||
email_blueprint = Blueprint('emails', __name__) | ||
|
@@ -59,6 +61,91 @@ def send_confirmmail_to_unregistered_users_bulk(items): | |
send_confirmmail_to_unregistered_users(item) | ||
|
||
|
||
@schedulable | ||
def remindermail(item): | ||
"""Send a reminder email to all participants registered to an event | ||
|
||
Args: | ||
item: The item, which was just inserted into the database | ||
""" | ||
event = item | ||
|
||
# Fetch all the infos needed for the content | ||
if event['title_en'] is not "": | ||
title = event['title_en'] | ||
else: | ||
title = event['title_de'] | ||
|
||
if event['location'] is not "": | ||
location = event['location'] | ||
else: | ||
location = "" | ||
|
||
if event['time_start'] is not None: | ||
date_time_event = event['time_start'].strftime(' %d %B %Y at %-H:%M ') | ||
else: | ||
date_time_event = " NaN " | ||
|
||
fields = { | ||
'location': location, | ||
'datetime': date_time_event, | ||
'title': title | ||
} | ||
|
||
# Populate content text with fetched infos | ||
email_content = current_app.config['REMINDER_EMAIL_TEXT'] % fields | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As fair as I can see, |
||
|
||
# Fetch the related eventsignups to get the emails. | ||
eventsignups = current_app.data.find( | ||
'eventsignups', | ||
**{current_app.config['event']: item['event']}) | ||
|
||
participants_list = list() | ||
for signup in eventsignups: | ||
participants_list.append(signup['email']) | ||
|
||
mail(current_app.config['API_MAIL'], | ||
participants_list, | ||
'Reminder for AMIV event %s' % title, | ||
email_content) | ||
|
||
|
||
def add_scheduled_remindermail(item): | ||
""" Schedule the sending of a remindermail by [REMINDER_EMAIL_DAYS2EVENT] | ||
days before the event time_start. If no time_start is defined, | ||
then no remindermail will be sent. | ||
""" | ||
if 'time_start' in item: | ||
time_start_event = datetime.strptime(item['time_start'], | ||
'%Y-%m-%dT%H:%M:%SZ') | ||
datetime_reminder = time_start_event - \ | ||
timedelta( | ||
days=int(current_app.config['REMINDER_EMAIL_DAYS2EVENT'])) | ||
schedule_task(datetime_reminder, | ||
remindermail, | ||
item) | ||
|
||
|
||
def add_scheduled_remindermail_bulk(items): | ||
for item in items: | ||
add_scheduled_remindermail(item) | ||
|
||
|
||
def update_scheduled_remindermail(item): | ||
""" Update the schedule of a remindermal to fit updated time_start | ||
or other infos of the event | ||
""" | ||
if 'time_start' in item: | ||
time_start_event = datetime.strptime(item['time_start'], | ||
'%Y-%m-%dT%H:%M:%SZ') | ||
datetime_reminder = time_start_event - \ | ||
timedelta( | ||
days=int(current_app.config['REMINDER_EMAIL_DAYS2EVENT'])) | ||
update_scheduled_task(datetime_reminder, | ||
remindermail, | ||
item) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have already discussed this in the chat shortly -- might it not be easier to add a periodic task sending reminder mails for all upcoming events? |
||
def add_confirmed_before_insert(item): | ||
"""Add the confirmed field to a event signup before it is inserted to the | ||
database. We accept all registered users instantly, others need to click the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems super hacky to me. Is this special case for reminder mails really necessary?