From b9a5fbadf507951a696e21e90f3030b2ee5ef35b Mon Sep 17 00:00:00 2001 From: Dominik George <dominik.george@teckids.org> Date: Fri, 5 Jan 2018 01:14:31 +0100 Subject: [PATCH] Use working query for birthdays on date column. --- ticdesk_account/cron.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/ticdesk_account/cron.py b/ticdesk_account/cron.py index 7c4bc0d..11920b6 100644 --- a/ticdesk_account/cron.py +++ b/ticdesk_account/cron.py @@ -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)) ) -- GitLab