Refactor code to send reminders in a unique function

Refactor reminder command.
Fixes #1040
 - Legacy-Id: 5736
This commit is contained in:
Emilio Jiménez 2013-05-15 09:08:49 +00:00
parent 3d25fa261e
commit a6fdc52397
3 changed files with 66 additions and 66 deletions

View file

@ -3,21 +3,15 @@ import syslog
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from ietf.utils.mail import send_mail
from ietf.dbtemplate.models import DBTemplate
from ietf.nomcom.models import Nominee, NomCom
from ietf.nomcom.utils import NOMINEE_REMINDER_TEMPLATE
from nomcom.utils import send_reminder_to_nominee
class Command(BaseCommand):
help = (u"Send reminders to nominees")
option_list = BaseCommand.option_list + (
make_option('--nomcom-year', dest='year', help='NomCom year'),
)
make_option('--nomcom-year', dest='year', help='NomCom year'),)
def handle(self, *args, **options):
year = options.get('year', None)
@ -33,33 +27,22 @@ class Command(BaseCommand):
raise CommandError("NomCom %s does not exist or it isn't active" % year)
today = datetime.date.today()
nomcom_template_path = '/nomcom/%s/' % nomcom.group.acronym
mail_path = nomcom_template_path + NOMINEE_REMINDER_TEMPLATE
mail_template = DBTemplate.objects.filter(group=nomcom.group, path=mail_path)
mail_template = mail_template and mail_template[0] or None
subject = 'IETF Nomination Information'
from_email = settings.NOMCOM_FROM_EMAIL
if nomcom.reminder_interval:
nominees = Nominee.objects.get_by_nomcom(nomcom).not_duplicated().filter(nomineeposition__state='pending').distinct()
for nominee in nominees:
positions = []
for np in nominee.nomineeposition_set.all():
nomination_date = np.time.date()
for nominee_position in nominee.nomineeposition_set.all():
nomination_date = nominee_position.time.date()
if not (today - nomination_date).days <= 0:
if (today - nomination_date).days % nomcom.reminder_interval == 0:
positions.append(np.position)
if positions:
to_email = nominee.email.address
context = {'positions': ', '.join([p.name for p in positions])}
send_mail(None, to_email, from_email, subject, mail_path, context)
syslog.syslog(u"Sent reminder to %s" % to_email)
send_reminder_to_nominee(nominee_position)
syslog.syslog(u"Sent reminder to %s" % nominee_position.nominee.email.address)
print u"Sent reminder to %s" % nominee_position.nominee.email.address
else:
if nomcom.reminderdates_set.filter(date=today):
nominees = Nominee.objects.get_by_nomcom(nomcom).not_duplicated().filter(nomineeposition__state='pending').distinct()
for nominee in nominees:
to_email = nominee.email.address
positions = ', '.join([nominee_position.position.name for nominee_position in nominee.nomineeposition_set.pending()])
context = {'positions': positions}
send_mail(None, to_email, from_email, subject, mail_path, context)
syslog.syslog(u"Sent reminder to %s" % to_email)
for nominee_position in nominee.nomineeposition_set.pending():
send_reminder_to_nominee(nominee_position)
syslog.syslog(u"Sent reminder to %s" % nominee_position.nominee.email.address)
print u"Sent reminder (by dates) to %s" % nominee_position.nominee.email.address

View file

@ -1,3 +1,4 @@
import datetime
import email
import hashlib
import os
@ -5,12 +6,16 @@ import re
import tempfile
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.shortcuts import get_object_or_404
from ietf.dbtemplate.models import DBTemplate
from ietf.person.models import Email
from ietf.utils.pipe import pipe
from ietf.utils.mail import send_mail_text
MAIN_NOMCOM_TEMPLATE_PATH = '/nomcom/defaults/'
QUESTIONNAIRE_TEMPLATE = 'position/questionnaire.txt'
@ -185,3 +190,48 @@ def validate_public_key(public_key):
os.unlink(key_file.name)
return (not error, error)
def send_reminder_to_nominee(nominee_position):
today = datetime.date.today().strftime('%Y%m%d')
subject = 'IETF Nomination Information'
from_email = settings.NOMCOM_FROM_EMAIL
domain = Site.objects.get_current().domain
position = nominee_position.position
nomcom = position.nomcom
nomcom_template_path = '/nomcom/%s/' % nomcom.group.acronym
mail_path = nomcom_template_path + NOMINEE_REMINDER_TEMPLATE
nominee = nominee_position.nominee
to_email = nominee.email.address
hash = get_hash_nominee_position(today, nominee_position.id)
accept_url = reverse('nomcom_process_nomination_status',
None,
args=(get_year_by_nomcom(nomcom),
nominee_position.id,
'accepted',
today,
hash))
decline_url = reverse('nomcom_process_nomination_status',
None,
args=(get_year_by_nomcom(nomcom),
nominee_position.id,
'declined',
today,
hash))
context = {'nominee': nominee,
'position': position,
'domain': domain,
'accept_url': accept_url,
'decline_url': decline_url}
body = render_to_string(mail_path, context)
path = '%s%d/%s' % (nomcom_template_path, position.id, QUESTIONNAIRE_TEMPLATE)
body += '\n\n%s' % render_to_string(path, context)
send_mail_text(None, to_email, from_email, subject, body)
def send_reminder_to_nominees(nominees):
for nominee in nominees:
for nominee_position in nominee.nomineeposition_set.pending():
send_reminder_to_nominee(nominee_position)

View file

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import datetime
from django.views.generic.create_update import delete_object
@ -12,9 +13,7 @@ from django.template.loader import render_to_string
from django.utils import simplejson
from django.db.models import Count, Q
from django.forms.models import modelformset_factory, inlineformset_factory
from django.contrib.sites.models import Site
from ietf.utils.mail import send_mail_text
from ietf.dbtemplate.models import DBTemplate
from ietf.dbtemplate.views import template_edit
@ -26,9 +25,9 @@ from ietf.nomcom.forms import (NominateForm, FeedbackForm, QuestionnaireForm,
PrivateKeyForm, EditNomcomForm, PendingFeedbackForm,
ReminderDatesForm)
from ietf.nomcom.models import Position, NomineePosition, Nominee, Feedback, NomCom, ReminderDates
from ietf.nomcom.utils import (get_nomcom_by_year, get_year_by_nomcom, HOME_TEMPLATE,
store_nomcom_private_key, get_hash_nominee_position,
NOMINEE_REMINDER_TEMPLATE, QUESTIONNAIRE_TEMPLATE)
from ietf.nomcom.utils import (get_nomcom_by_year, store_nomcom_private_key,
get_hash_nominee_position, send_reminder_to_nominees,
HOME_TEMPLATE, NOMINEE_REMINDER_TEMPLATE)
def index(request, year):
@ -148,39 +147,7 @@ def send_reminder_mail(request, year):
selected_nominees = request.POST.getlist('selected')
selected_nominees = nominees.filter(id__in=selected_nominees)
if selected_nominees:
subject = 'IETF Nomination Information'
from_email = settings.NOMCOM_FROM_EMAIL
domain = Site.objects.get_current().domain
today = datetime.date.today().strftime('%Y%m%d')
for nominee in nominees:
to_email = nominee.email.address
for nominee_position in nominee.nomineeposition_set.pending():
hash = get_hash_nominee_position(today, nominee_position.id)
accept_url = reverse('nomcom_process_nomination_status',
None,
args=(get_year_by_nomcom(nomcom),
nominee_position.id,
'accepted',
today,
hash))
decline_url = reverse('nomcom_process_nomination_status',
None,
args=(get_year_by_nomcom(nomcom),
nominee_position.id,
'declined',
today,
hash))
context = {'nominee': nominee,
'position': nominee_position.position,
'domain': domain,
'accept_url': accept_url,
'decline_url': decline_url}
body = render_to_string(mail_path, context)
path = '%s%d/%s' % (nomcom_template_path, nominee_position.position.id, QUESTIONNAIRE_TEMPLATE)
body += '\n\n%s' % render_to_string(path, context)
send_mail_text(None, to_email, from_email, subject, body)
send_reminder_to_nominees(selected_nominees)
message = ('success', 'An query has been sent to each person, asking them to accept (or decline) the nominations')
else:
message = ('warning', "Please, select some nominee")