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

Use working query for birthdays on date column.

parent 0a220627
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ from django_cron import CronJobBase, Schedule
from ldap import INVALID_SYNTAX
from .models import TeckidsGroup, TeckidsPerson
from ticdesk_org.util import may_see_person
from ticdesk_org.util import filter_disallowed_persons
class CleanGroups(CronJobBase):
RUN_EVERY_MINS = 5
......@@ -54,28 +54,34 @@ class BirthdayMails(CronJobBase):
# Get persons who subscribed to birthday mails
persons = TeckidsPerson.objects.filter(setting_birthday_mail_days__gte=1).all()
# Get minimum year of birth
today = date.today()
min_year = TeckidsPerson.objects.filter(date_of_birth__lte=today).order_by('date_of_birth').first().date_of_birth.year
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__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:
dates = []
for year in range(min_year, today.year + 1):
for delta in range(0, person.setting_birthday_mail_days + 1):
day = today + timedelta(days=delta)
dates.append(date(year=year, month=day.month, day=day.day))
birthday_persons = filter_disallowed_persons(
TeckidsPerson.objects.filter(
date_of_birth__in=dates
).order_by('date_of_birth').all(),
person
)
if birthday_persons:
# Generate mail
last = today + timedelta(days=person.setting_birthday_mail_days)
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:
for birthday_person in birthday_persons:
message.body += _('%s\t%s (%s Jahre)\n') % (str(birthday_person.date_of_birth),
birthday_person.cn, str(birthday_person.age_at(last))
)
......
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