Skip to content
Snippets Groups Projects
Verified Commit 73ca3816 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Implement birthday mails, advances #17.

parent 6daf0812
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup(
name='ticdesk',
version='0.11',
version='0.12',
packages=find_packages(),
include_package_data=True,
author='Teckids e.V.',
......
......@@ -60,6 +60,7 @@ MIDDLEWARE = [
CRON_CLASSES = [
'ticdesk_account.cron.CleanGroups',
'ticdesk_account.cron.CleanPersons',
'ticdesk_account.cron.BirthdayMails',
]
ROOT_URLCONF = 'ticdesk.urls'
......
from datetime import date, timedelta
from django.core.mail import EmailMessage
from django.utils.translation import ugettext_lazy as _
from django_cron import CronJobBase, Schedule
from ldap import INVALID_SYNTAX
from .models import TeckidsGroup, TeckidsPerson
from ticdesk_org.util import may_see_person
class CleanGroups(CronJobBase):
RUN_EVERY_MINS = 5
......@@ -38,3 +43,40 @@ class CleanPersons(CronJobBase):
except INVALID_SYNTAX:
# Uncritical because we probably hit a no-op
pass
class BirthdayMails(CronJobBase):
RUN_EVERY_MINS = 1440
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'ticdesk_account.birthday_mails'
def do(self):
# Get persons who subscribed to birthday mails
persons = TeckidsPerson.objects.filter(setting_birthday_mail_days__gte=1).all()
for person in persons:
today = date.today()
last = today + timedelta(days=person.setting_birthday_mail_days)
# Get persons who have their birthday in that period
birthday_persons = []
for delta in range(0, person.setting_birthday_mail_days + 1):
day = today + timedelta(days=delta)
birthday_persons += TeckidsPerson.objects.filter(
date_of_birth_str__endswith=day.strftime('%m-%d')
).order_by('date_of_birth_str').all()
# Iterate over persons and check read access
matching_persons = [birthday_person for birthday_person in birthday_persons if may_see_person(birthday_person, person)]
if matching_persons:
# Generate mail
message = EmailMessage()
message.to = [person.mail]
message.subject = _('Geburtstage von %s bis %s') % (str(today), str(last))
message.body = _('Hallo %s,\n\ndie folgenden Personen haben in nächster Zeit Geburtstag:\n\n') % person.given_name
for birthday_person in matching_persons:
message.body += _('%s\t%s (%s Jahre)\n') % (str(birthday_person.date_of_birth),
birthday_person.cn, str(birthday_person.age_at(last))
)
message.send()
......@@ -176,6 +176,7 @@ class TeckidsPerson(ldapdb.models.Model, TeckidsLdapMixin):
home_phone = CharField(db_column='homePhone', max_length=20, verbose_name=_('Telefon (Festnetz privat)'))
mobile = CharField(db_column='mobile', max_length=20, verbose_name=_('Handy'))
date_of_birth = DateField(db_column='dateOfBirth', verbose_name=_('Geburtsdatum'))
date_of_birth_str = CharField(db_column='dateOfBirth', max_length=10)
jpeg_photo = ImageField(db_column='jpegPhoto')
# Location
......@@ -216,6 +217,11 @@ class TeckidsPerson(ldapdb.models.Model, TeckidsLdapMixin):
# Group membership
member_of = ListField(db_column='memberOf')
# Settings
setting_birthday_mail_days = IntegerField(db_column='teckidsSettingsBirthdayMailDays',
default=0, verbose_name=_('Geburtstags-E-Mail-Tage')
)
def clean(self):
# Set default cn if not given
if not self.cn:
......
......@@ -45,12 +45,13 @@ def may_see_group(group, user):
return False
def may_see_person(person, user):
# Not logged-in users cannot see anything
if not user.is_authenticated:
return False
if not isinstance(user, TeckidsPerson):
# Not logged-in users cannot see anything
if not user.is_authenticated:
return False
# Replace user variable with the user's TeckidsPerson
user = TeckidsPerson.objects.get(uid=user.username)
# Replace user variable with the user's TeckidsPerson
user = TeckidsPerson.objects.get(uid=user.username)
# Everyone can see themselves
if user == person:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment