def poll_complete(request): question = request.POST['question'] p = Poll(question=question, pub_date=timezone.now()) p.author = request.user.username raw_choicedict = request.POST.copy() if 'restricted_domain' in request.POST: p.restrict_to_domain = request.POST['restricted_domain'] del raw_choicedict['restricted_domain'] if 'email_list' in request.POST: p.restrict_to_emails = request.POST['email_list'] p.restrict_to_emails = raw_choicedict['email_list'].split(',') p.restrict_to_domain = u'Invalid' del raw_choicedict['email_list'] if 'close_time' in request.POST: time_string = raw_choicedict['close_time'] time_pattern = r'(\d+)-(\d+)-(\d+)\s(\d+):(\d+)' match = re.search(time_pattern, time_string) given_date = datetime.datetime(int(match.group(1)), int(match.group(2)),int(match.group(3)), int(match.group(4)), int(match.group(5))) p.close_date = given_date del raw_choicedict['close_time'] p.save() del raw_choicedict['csrfmiddlewaretoken'] del raw_choicedict['question'] choice_list = raw_choicedict.values() for option in choice_list: p.choice_set.create(choice=option, votes=0) p.save() poll_message = get_template('polls/poll_mail.txt') d = Context({ 'poll_id': p.id, 'SITE_URL': os.environ['SITE_URL'], }) text_content = poll_message.render(d) msg = EmailMessage('New Poll Created', text_content, '*****@*****.**', [request.user.username]) msg.send() return HttpResponseRedirect(reverse('polls.views.detail', args=(p.id,)))