Skip to content

Commit a6fdc52

Browse files
committed
Refactor code to send reminders in a unique function
Refactor reminder command. Fixes ietf-tools#1040 - Legacy-Id: 5736
1 parent 3d25fa2 commit a6fdc52

3 files changed

Lines changed: 66 additions & 66 deletions

File tree

ietf/nomcom/management/commands/send_reminders.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
from optparse import make_option
44

55
from django.core.management.base import BaseCommand, CommandError
6-
from django.conf import settings
7-
8-
from ietf.utils.mail import send_mail
9-
10-
from ietf.dbtemplate.models import DBTemplate
116

127
from ietf.nomcom.models import Nominee, NomCom
13-
from ietf.nomcom.utils import NOMINEE_REMINDER_TEMPLATE
8+
from nomcom.utils import send_reminder_to_nominee
149

1510

1611
class Command(BaseCommand):
1712
help = (u"Send reminders to nominees")
1813
option_list = BaseCommand.option_list + (
19-
make_option('--nomcom-year', dest='year', help='NomCom year'),
20-
)
14+
make_option('--nomcom-year', dest='year', help='NomCom year'),)
2115

2216
def handle(self, *args, **options):
2317
year = options.get('year', None)
@@ -33,33 +27,22 @@ def handle(self, *args, **options):
3327
raise CommandError("NomCom %s does not exist or it isn't active" % year)
3428

3529
today = datetime.date.today()
36-
nomcom_template_path = '/nomcom/%s/' % nomcom.group.acronym
37-
mail_path = nomcom_template_path + NOMINEE_REMINDER_TEMPLATE
38-
mail_template = DBTemplate.objects.filter(group=nomcom.group, path=mail_path)
39-
mail_template = mail_template and mail_template[0] or None
40-
subject = 'IETF Nomination Information'
41-
from_email = settings.NOMCOM_FROM_EMAIL
4230

4331
if nomcom.reminder_interval:
4432
nominees = Nominee.objects.get_by_nomcom(nomcom).not_duplicated().filter(nomineeposition__state='pending').distinct()
4533
for nominee in nominees:
46-
positions = []
47-
for np in nominee.nomineeposition_set.all():
48-
nomination_date = np.time.date()
34+
for nominee_position in nominee.nomineeposition_set.all():
35+
nomination_date = nominee_position.time.date()
4936
if not (today - nomination_date).days <= 0:
5037
if (today - nomination_date).days % nomcom.reminder_interval == 0:
51-
positions.append(np.position)
52-
if positions:
53-
to_email = nominee.email.address
54-
context = {'positions': ', '.join([p.name for p in positions])}
55-
send_mail(None, to_email, from_email, subject, mail_path, context)
56-
syslog.syslog(u"Sent reminder to %s" % to_email)
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
5741
else:
5842
if nomcom.reminderdates_set.filter(date=today):
5943
nominees = Nominee.objects.get_by_nomcom(nomcom).not_duplicated().filter(nomineeposition__state='pending').distinct()
6044
for nominee in nominees:
61-
to_email = nominee.email.address
62-
positions = ', '.join([nominee_position.position.name for nominee_position in nominee.nomineeposition_set.pending()])
63-
context = {'positions': positions}
64-
send_mail(None, to_email, from_email, subject, mail_path, context)
65-
syslog.syslog(u"Sent reminder to %s" % to_email)
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

ietf/nomcom/utils.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import datetime
12
import email
23
import hashlib
34
import os
45
import re
56
import tempfile
67

78
from django.conf import settings
9+
from django.contrib.sites.models import Site
810
from django.core.exceptions import PermissionDenied
11+
from django.core.urlresolvers import reverse
12+
from django.template.loader import render_to_string
913
from django.shortcuts import get_object_or_404
1014

1115
from ietf.dbtemplate.models import DBTemplate
1216
from ietf.person.models import Email
1317
from ietf.utils.pipe import pipe
18+
from ietf.utils.mail import send_mail_text
1419

1520
MAIN_NOMCOM_TEMPLATE_PATH = '/nomcom/defaults/'
1621
QUESTIONNAIRE_TEMPLATE = 'position/questionnaire.txt'
@@ -185,3 +190,48 @@ def validate_public_key(public_key):
185190

186191
os.unlink(key_file.name)
187192
return (not error, error)
193+
194+
195+
def send_reminder_to_nominee(nominee_position):
196+
today = datetime.date.today().strftime('%Y%m%d')
197+
subject = 'IETF Nomination Information'
198+
from_email = settings.NOMCOM_FROM_EMAIL
199+
domain = Site.objects.get_current().domain
200+
position = nominee_position.position
201+
nomcom = position.nomcom
202+
nomcom_template_path = '/nomcom/%s/' % nomcom.group.acronym
203+
mail_path = nomcom_template_path + NOMINEE_REMINDER_TEMPLATE
204+
nominee = nominee_position.nominee
205+
to_email = nominee.email.address
206+
207+
hash = get_hash_nominee_position(today, nominee_position.id)
208+
accept_url = reverse('nomcom_process_nomination_status',
209+
None,
210+
args=(get_year_by_nomcom(nomcom),
211+
nominee_position.id,
212+
'accepted',
213+
today,
214+
hash))
215+
decline_url = reverse('nomcom_process_nomination_status',
216+
None,
217+
args=(get_year_by_nomcom(nomcom),
218+
nominee_position.id,
219+
'declined',
220+
today,
221+
hash))
222+
223+
context = {'nominee': nominee,
224+
'position': position,
225+
'domain': domain,
226+
'accept_url': accept_url,
227+
'decline_url': decline_url}
228+
body = render_to_string(mail_path, context)
229+
path = '%s%d/%s' % (nomcom_template_path, position.id, QUESTIONNAIRE_TEMPLATE)
230+
body += '\n\n%s' % render_to_string(path, context)
231+
send_mail_text(None, to_email, from_email, subject, body)
232+
233+
234+
def send_reminder_to_nominees(nominees):
235+
for nominee in nominees:
236+
for nominee_position in nominee.nomineeposition_set.pending():
237+
send_reminder_to_nominee(nominee_position)

ietf/nomcom/views.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
23
import datetime
34

45
from django.views.generic.create_update import delete_object
@@ -12,9 +13,7 @@
1213
from django.utils import simplejson
1314
from django.db.models import Count, Q
1415
from django.forms.models import modelformset_factory, inlineformset_factory
15-
from django.contrib.sites.models import Site
1616

17-
from ietf.utils.mail import send_mail_text
1817

1918
from ietf.dbtemplate.models import DBTemplate
2019
from ietf.dbtemplate.views import template_edit
@@ -26,9 +25,9 @@
2625
PrivateKeyForm, EditNomcomForm, PendingFeedbackForm,
2726
ReminderDatesForm)
2827
from ietf.nomcom.models import Position, NomineePosition, Nominee, Feedback, NomCom, ReminderDates
29-
from ietf.nomcom.utils import (get_nomcom_by_year, get_year_by_nomcom, HOME_TEMPLATE,
30-
store_nomcom_private_key, get_hash_nominee_position,
31-
NOMINEE_REMINDER_TEMPLATE, QUESTIONNAIRE_TEMPLATE)
28+
from ietf.nomcom.utils import (get_nomcom_by_year, store_nomcom_private_key,
29+
get_hash_nominee_position, send_reminder_to_nominees,
30+
HOME_TEMPLATE, NOMINEE_REMINDER_TEMPLATE)
3231

3332

3433
def index(request, year):
@@ -148,39 +147,7 @@ def send_reminder_mail(request, year):
148147
selected_nominees = request.POST.getlist('selected')
149148
selected_nominees = nominees.filter(id__in=selected_nominees)
150149
if selected_nominees:
151-
subject = 'IETF Nomination Information'
152-
from_email = settings.NOMCOM_FROM_EMAIL
153-
domain = Site.objects.get_current().domain
154-
today = datetime.date.today().strftime('%Y%m%d')
155-
for nominee in nominees:
156-
to_email = nominee.email.address
157-
for nominee_position in nominee.nomineeposition_set.pending():
158-
hash = get_hash_nominee_position(today, nominee_position.id)
159-
accept_url = reverse('nomcom_process_nomination_status',
160-
None,
161-
args=(get_year_by_nomcom(nomcom),
162-
nominee_position.id,
163-
'accepted',
164-
today,
165-
hash))
166-
decline_url = reverse('nomcom_process_nomination_status',
167-
None,
168-
args=(get_year_by_nomcom(nomcom),
169-
nominee_position.id,
170-
'declined',
171-
today,
172-
hash))
173-
174-
context = {'nominee': nominee,
175-
'position': nominee_position.position,
176-
'domain': domain,
177-
'accept_url': accept_url,
178-
'decline_url': decline_url}
179-
body = render_to_string(mail_path, context)
180-
path = '%s%d/%s' % (nomcom_template_path, nominee_position.position.id, QUESTIONNAIRE_TEMPLATE)
181-
body += '\n\n%s' % render_to_string(path, context)
182-
send_mail_text(None, to_email, from_email, subject, body)
183-
150+
send_reminder_to_nominees(selected_nominees)
184151
message = ('success', 'An query has been sent to each person, asking them to accept (or decline) the nominations')
185152
else:
186153
message = ('warning', "Please, select some nominee")

0 commit comments

Comments
 (0)