|
1 | 1 | import datetime |
2 | 2 | import syslog |
3 | 3 | from optparse import make_option |
| 4 | +from django.core.management.base import BaseCommand |
| 5 | +from ietf.nomcom.models import NomCom, NomineePosition |
| 6 | +from nomcom.utils import send_accept_reminder_to_nominee,send_questionnaire_reminder_to_nominee |
4 | 7 |
|
5 | | -from django.core.management.base import BaseCommand, CommandError |
6 | | - |
7 | | -from ietf.nomcom.models import Nominee, NomCom |
8 | | -from nomcom.utils import send_reminder_to_nominee |
| 8 | +def log(message): |
| 9 | + syslog.syslog(message) |
9 | 10 |
|
| 11 | +def is_time_to_send(nomcom,send_date,nomination_date): |
| 12 | + if nomcom.reminder_interval: |
| 13 | + days_passed = (send_date - nomination_date).days |
| 14 | + return days_passed > 0 and days_passed % nomcom.reminder_interval == 0 |
| 15 | + else: |
| 16 | + return bool(nomcom.reminderdates_set.filter(date=send_date)) |
10 | 17 |
|
11 | 18 | class Command(BaseCommand): |
12 | | - help = (u"Send reminders to nominees") |
13 | | - option_list = BaseCommand.option_list + ( |
14 | | - make_option('--nomcom-year', dest='year', help='NomCom year'),) |
| 19 | + help = (u"Send acceptance and questionnaire reminders to nominees") |
15 | 20 |
|
16 | 21 | def handle(self, *args, **options): |
17 | | - year = options.get('year', None) |
18 | | - help_message = 'Usage: send_reminders --nomcom-year <nomcom-year>' |
19 | | - |
20 | | - if not year: |
21 | | - raise CommandError(help_message) |
22 | | - |
23 | | - try: |
24 | | - nomcom = NomCom.objects.get(group__acronym__icontains=year, |
25 | | - group__state__slug='active') |
26 | | - except NomCom.DoesNotExist: |
27 | | - raise CommandError("NomCom %s does not exist or it isn't active" % year) |
28 | | - |
29 | | - today = datetime.date.today() |
30 | | - |
31 | | - if nomcom.reminder_interval: |
32 | | - nominees = Nominee.objects.get_by_nomcom(nomcom).not_duplicated().filter(nomineeposition__state='pending').distinct() |
33 | | - for nominee in nominees: |
34 | | - for nominee_position in nominee.nomineeposition_set.all(): |
35 | | - nomination_date = nominee_position.time.date() |
36 | | - if not (today - nomination_date).days <= 0: |
37 | | - if (today - nomination_date).days % nomcom.reminder_interval == 0: |
38 | | - send_reminder_to_nominee(nominee_position) |
39 | | - syslog.syslog(u"Sent reminder to %s" % nominee_position.nominee.email.address) |
40 | | - print u"Sent reminder to %s" % nominee_position.nominee.email.address |
41 | | - else: |
42 | | - if nomcom.reminderdates_set.filter(date=today): |
43 | | - nominees = Nominee.objects.get_by_nomcom(nomcom).not_duplicated().filter(nomineeposition__state='pending').distinct() |
44 | | - for nominee in nominees: |
45 | | - for nominee_position in nominee.nomineeposition_set.pending(): |
46 | | - send_reminder_to_nominee(nominee_position) |
47 | | - syslog.syslog(u"Sent reminder to %s" % nominee_position.nominee.email.address) |
48 | | - print u"Sent reminder (by dates) to %s" % nominee_position.nominee.email.address |
| 22 | + for nomcom in NomCom.objects.filter(group__state__slug='active'): |
| 23 | + for state in ('pending','accepted'): |
| 24 | + for nominee_position in NomineePosition.objects.filter(nominee__nomcom=nomcom, |
| 25 | + state=state, |
| 26 | + nominee__duplicated__isnull=True): |
| 27 | + if is_time_to_send(nomcom, datetime.date.today(), nominee_position.time.date()): |
| 28 | + if state=='pending': |
| 29 | + send_accept_reminder_to_nominee(nominee_position) |
| 30 | + log('Sent accept reminder to %s' % nominee_position.nominee.email.address) |
| 31 | + elif state=='accepted': |
| 32 | + send_questionnaire_reminder_to_nominee(nominee_position) |
| 33 | + log('Sent questionnaire reminder to %s' % nominee_position.nominee.email.address) |
| 34 | + else: |
| 35 | + pass |
0 commit comments