Skip to content
Snippets Groups Projects
cron.py 3.01 KiB
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 filter_disallowed_persons

class CleanGroups(CronJobBase):
    RUN_EVERY_MINS = 5

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'ticdesk_account.clean_groups'

    def do(self):
        groups = TeckidsGroup.objects.all()

        for group in groups:
            group.clean()

            try:
                group.save()
            except INVALID_SYNTAX:
                # Uncritical because we probably hit a no-op
                pass

class CleanPersons(CronJobBase):
    RUN_EVERY_MINS = 5

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'ticdesk_account.clean_persons'

    def do(self):
        persons = TeckidsPerson.objects.all()

        for person in persons:
            person.clean()

            try:
                person.save()
            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()

        # 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:

            # Get persons who have their birthday in that period
            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 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))
                    )
                message.send()