-
Notifications
You must be signed in to change notification settings - Fork 50
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 #232 from christophmeissner/hotfix/mailing_groupin…
…g_and_weekdays Hotfix/mailing grouping and weekdays
- Loading branch information
Showing
4 changed files
with
69 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
# coding=utf-8 | ||
import logging | ||
from django.core.mail import EmailMessage | ||
from datetime import datetime, timedelta | ||
|
||
from django.db.models.signals import pre_delete, post_save, pre_save | ||
from django.template.defaultfilters import time as date_filter | ||
from django.core.mail import EmailMessage | ||
from django.db.models.signals import pre_delete, pre_save | ||
from django.dispatch import receiver | ||
from django.template.loader import render_to_string | ||
|
||
from scheduler.models import Shift | ||
|
||
logger = logging.getLogger(__name__) | ||
grace = 5*60 # 5 minutes in seconds | ||
|
||
|
||
@receiver(pre_delete, sender=Shift) | ||
|
@@ -21,43 +22,68 @@ def send_email_notifications(sender, instance, **kwargs): | |
add some error handling, some sane max recipient handling, tests, etc. | ||
""" | ||
shift = instance | ||
subject = u'Schicht am {} wurde abgesagt'.format(shift.starting_time.strftime('%d.%m.%y')) | ||
if shift.ending_time >= datetime.now(): | ||
subject = u'Schicht am {} wurde abgesagt'.format( | ||
shift.starting_time.strftime('%d.%m.%y')) | ||
|
||
message = render_to_string('shift_cancellation_notification.html', | ||
dict(shift=shift)) | ||
|
||
from_email = "Volunteer-Planner.org <[email protected]>" | ||
|
||
message = render_to_string('shift_cancellation_notification.html', dict(shift=shift)) | ||
addresses = shift.helpers.values_list('user__email', flat=True) | ||
|
||
from_email = "Volunteer-Planner.org <[email protected]>" | ||
if addresses: | ||
mail = EmailMessage(subject=subject, body=message, | ||
to=['[email protected]'], | ||
from_email=from_email, | ||
bcc=addresses) | ||
mail.send() | ||
|
||
addresses = shift.helpers.values_list('user__email', flat=True) | ||
|
||
mail = EmailMessage(subject=subject, body=message, | ||
to=['[email protected]'], | ||
from_email=from_email, | ||
bcc=addresses) | ||
mail.send() | ||
def times_changed(shift, old_shift, grace=timedelta(minutes=5)): | ||
starting_time = min(shift.starting_time, shift.ending_time) | ||
ending_time = max(shift.starting_time, shift.ending_time) | ||
|
||
old_starting_time = min(old_shift.starting_time, old_shift.ending_time) | ||
old_ending_time = max(old_shift.starting_time, old_shift.ending_time) | ||
|
||
starting_diff = max(old_starting_time, starting_time) - min( | ||
old_starting_time, starting_time) | ||
ending_diff = max(old_ending_time, ending_time) - min( | ||
old_ending_time, ending_time) | ||
|
||
return ending_diff > grace or starting_diff > grace | ||
|
||
|
||
@receiver(pre_save, sender=Shift) | ||
def notify_users_shift_change(sender, instance, **kwargs): | ||
shift = instance | ||
old = Shift.objects.filter(pk=shift.pk) | ||
# Test whether this is modification or creation, to avoid DoesNotExist exception | ||
if shift.pk and old.exists(): | ||
old_shift = old.get() | ||
diff_start = (old_shift.starting_time - shift.starting_time).seconds | ||
diff_end = (old_shift.ending_time - shift.ending_time).seconds | ||
if grace < diff_start or grace < diff_end: | ||
subject = u'Schicht am {} wurde verändert'.format(shift.starting_time.strftime('%d.%m.%y')) | ||
if shift.pk: | ||
old_shift = Shift.objects.get(pk=shift.pk) | ||
|
||
if old_shift.starting_time >= datetime.now() and times_changed(shift, | ||
old_shift): | ||
subject = u'Schicht wurde verändert: {task} am {date}'.format( | ||
task=old_shift.task.name, | ||
date=date_filter(old_shift.starting_time)) | ||
|
||
message = render_to_string('shift_modification_notification.html', dict(old=old_shift, shift=shift)) | ||
message = render_to_string('shift_modification_notification.html', | ||
dict(old=old_shift, shift=shift)) | ||
|
||
from_email = "Volunteer-Planner.org <[email protected]>" | ||
|
||
addresses = shift.helpers.values_list('user__email', flat=True) | ||
if 0 < len(addresses): | ||
mail = EmailMessage(subject=subject, body=message, to=['[email protected]'], from_email=from_email, | ||
if addresses: | ||
mail = EmailMessage(subject=subject, | ||
body=message, | ||
to=['[email protected]'], | ||
from_email=from_email, | ||
bcc=addresses) | ||
logger.info(u'Shift %s at %s changed: (%s-%s -> %s->%s). Sending email notification to %d affected user(s).', | ||
shift.task.name, shift.facility.name, | ||
old_shift.starting_time, old_shift.ending_time, shift.starting_time, shift.ending_time, | ||
len(addresses)) | ||
logger.info( | ||
u'Shift %s at %s changed: (%s-%s -> %s->%s). Sending email notification to %d affected user(s).', | ||
shift.task.name, shift.facility.name, | ||
old_shift.starting_time, old_shift.ending_time, | ||
shift.starting_time, shift.ending_time, | ||
len(addresses)) | ||
mail.send() |
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