Skip to content

Commit

Permalink
Merge pull request #232 from christophmeissner/hotfix/mailing_groupin…
Browse files Browse the repository at this point in the history
…g_and_weekdays

Hotfix/mailing grouping and weekdays
  • Loading branch information
pitpalme committed Oct 29, 2015
2 parents 1b49075 + 101f6d9 commit c1283aa
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
80 changes: 53 additions & 27 deletions scheduler/signals.py
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)
Expand All @@ -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()
4 changes: 2 additions & 2 deletions scheduler/templates/shift_cancellation_notification.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% load i18n %}{% blocktrans with shift_title=shift.task.name location=shift.location.name from_date=shift.starting_time.date from_time=shift.starting_time.time to_time=shift.ending_time.time %}
{% load i18n %}{% blocktrans with shift_title=shift.task.name.strip location=shift.facility.name.strip from_date=shift.starting_time.date|date from_time=shift.starting_time.time to_time=shift.ending_time.time %}
Hallo,

leider musste auf Wunsch der Zuständigen die folgende Schicht abgesagt werden:

{{ shift_title }}, {{ location }}
{{ from_date }} von {{ from_time }} bis {{ to_time }}
{{ from_date }} von {{ from_time }} bis {{ to_time }} Uhr

Dies hier ist eine automatisch generierte Email. Bei Fragen, wenden Sie sich bitte an die Emailadresse, die bei den Unterkünften in der Beschreibung angegeben ist.

Expand Down
6 changes: 3 additions & 3 deletions scheduler/templates/shift_modification_notification.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{% load i18n %}{% blocktrans with shift_title=shift.task.name location=shift.location.name from_date=shift.starting_time.date from_time=shift.starting_time.time to_time=shift.ending_time.time old_from_date=shift.starting_time.date old_from_time=shift.starting_time.time old_to_time=shift.ending_time.time %}
{% load i18n %}{% blocktrans with shift_title=shift.task.name.strip location=shift.facility.name.strip from_date=shift.starting_time.date|date from_time=shift.starting_time.time to_time=shift.ending_time.time old_from_date=old.starting_time.date|date old_from_time=old.starting_time.time old_to_time=old.ending_time.time %}
Hallo,

wir mussten die folgende Schicht zeitlich anpassen:

{{ shift_title }}, {{ location }}
{{ old_from_date }} von {{ old_from_time }} bis {{ old_to_time }}
{{ old_from_date }} von {{ old_from_time }} bis {{ old_to_time }} Uhr

Die neuen Schichtzeiten sind:

{{ from_date }} von {{ from_time }} bis {{ to_time }}
{{ from_date }} von {{ from_time }} bis {{ to_time }} Uhr

Wenn Sie zur neuen Uhrzeit unglücklicher Weise nicht mehr helfen kännen, bitten
wir Sie, sich im Volunteer-Planner aus dieser Schicht auszutragen, damit jemand
Expand Down
21 changes: 11 additions & 10 deletions scheduler/views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# coding: utf-8

from datetime import date, datetime
from datetime import date
import logging
import json
import itertools
from time import mktime
from django.core.serializers.json import DjangoJSONEncoder

from django.core.serializers.json import DjangoJSONEncoder
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.db.models import Count
from django.templatetags.l10n import localize
from django.utils.safestring import mark_safe
from django.views.generic import TemplateView, FormView, DetailView
from django.shortcuts import get_object_or_404

from django.template.defaultfilters import date as date_filter

from django.utils.translation import ugettext_lazy as _

from accounts.models import UserAccount
Expand Down Expand Up @@ -55,9 +55,9 @@ def getNewsFacility(facility):

for item in news_query:
news.append({
'title':item.title,
'date':item.creation_date,
'text':item.text
'title': item.title,
'date': item.creation_date,
'text': item.text
})
return news

Expand Down Expand Up @@ -92,7 +92,7 @@ def get_context_data(self, **kwargs):
'description': mark_safe(facility.description),
'area_slug': facility.place.area.slug,
'shifts': [{
'date_string': localize(shift_date),
'date_string': date_filter(shift_date),
'link': reverse('planner_by_facility', kwargs={
'pk': facility.pk,
'year': shift_date.year,
Expand All @@ -105,7 +105,8 @@ def get_context_data(self, **kwargs):
context['areas_json'] = json.dumps(
[{'slug': area.slug, 'name': area.name} for area in
sorted(used_places, key=lambda p: p.name)])
context['facility_json'] = json.dumps(facility_list, cls=DjangoJSONEncoder)
context['facility_json'] = json.dumps(facility_list,
cls=DjangoJSONEncoder)
context['shifts'] = open_shifts
return context

Expand Down Expand Up @@ -160,7 +161,7 @@ def get_context_data(self, **kwargs):
shifts = Shift.objects.filter(facility=facility)
shifts = shifts.on_shiftdate(schedule_date)
shifts = shifts.annotate(volunteer_count=Count('helpers'))
shifts = shifts.order_by('task', 'ending_time')
shifts = shifts.order_by('task', 'workplace', 'ending_time')
shifts = shifts.select_related('task', 'workplace', 'facility')
shifts = shifts.prefetch_related('helpers', 'helpers__user')

Expand Down

0 comments on commit c1283aa

Please sign in to comment.