<commit_before>import datetime
import os

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponseRedirect, HttpResponse, HttpRequest
from django.core.urlresolvers import reverse
from django.views import generic

from .models import *

from django.utils import timezone
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login,logout
from django.contrib.auth.decorators import login_required
from django.core import mail
from .prefpy.mechanism import *
from groups.models import *
from django.conf import settings
import random
import string

def setupEmail(question):
    title = question.question_text
    creator = question.question_owner.username
    emailInvite = Email(question=question, type=1,
        subject="You have been invited to vote on " + title,
        message='Hello [user_name],\n\n' + creator
                + ' has invited you to vote on a poll. Please login at [url] to check it out.\n\nSincerely,\nOPRAH Staff')
    emailRemove = Email(question=question, type=2,
        subject="You have been removed from " + title,
        message='Hello [user_name],\n\n' + creator
                + ' has deleted you from a poll.\n\nSincerely,\nOPRAH Staff')
    emailStart = Email(question=question, type=3,
        subject=title + ' has started!',
        message='Hello [user_name],\n\n' + creator
                + ' has started a poll. It is now available to vote on at [url] \n\nSincerely,\nOPRAH Staff')
    emailStop = Email(question=question, type=4,
        subject=title + ' has stopped',
        message='Hello [user_name],\n\n' + creator
                + ' has ended a poll. Please visit [url] to view the decision.\n\nSincerely,\nOPRAH Staff')
    emailInvite.save()
    emailRemove.save()
    emailStart.save()
    emailStop.save()

def switchHTML(x, name, uname, creator, request, question_id, options):
    return {
        'invite': '<h1>Hello ' + name + ',</h1><p>' + creator
                + ' has invited you to vote on a poll. Please login <a href=\''
                + request.build_absolute_uri(reverse('appauth:login')+'?name='+uname)
                + '\'>here</a> to check it out.</p><p>Sincerely,</p><p>OPRAH Staff</p>',
        'remove': 'Hello ' + name + ',\n\n' + creator
                + ' has deleted you from a poll.\n\nSincerely,\nOPRAH Staff',
        'start': '<h1>Hello ' + name + ',</h1><p>' + creator
                + ' has started a poll. It is now available to vote on <a href=\''
                + '\'>here</a>.</p>' + options + '<p>Sincerely,</p><p>OPRAH Staff</p>',
        'stop': 'Hello ' + name + ',\n\n' + creator
                + ' has ended a poll. Please visit '
                + request.build_absolute_uri(reverse('polls:index'))
                + ' to view the decision.\n\nSincerely,\nOPRAH Staff',
    }.get(x, x)

def getOptions(items):
    arr = []
    for item in items:
        arr.append(item.item_text)
    return arr

def switchModel(type, question):
    if type == 'invite':
        email = Email.objects.filter(question=question, type=1)
    if type == 'remove':
        email = Email.objects.filter(question=question, type=2)
    if type == 'start':
        email = Email.objects.filter(question=question, type=3)
    if type == 'stop':
        email = Email.objects.filter(question=question, type=4)
    if len(email) != 1:
        setupEmail(question)
    return email[0]

def translateEmail(text, uname, url):
    text = text.replace("[user_name]", uname)
    text = text.replace("[url]", url)
    return text

def translateHTML(text, uname, url):
    text = translateEmail(text, uname, url)
    text = "<p>" + text + "</p>"
    text = text.replace("\n\n", "</p><br /><p>")
    text = text.replace("\n", "</p><p>")
    return text

#function to send email
def sendEmail(request, question_id, type):
    question = get_object_or_404(Question, pk=question_id)
    email = switchModel(type, question)
    options = ''
    title = question.question_text
    creator_obj = User.objects.get(id=question.question_owner_id)
    creator = creator_obj.username
    if creator_obj.first_name != "":
        creator = creator_obj.first_name + " " + creator_obj.last_name

    if type == 'invite' or type == 'remove':
        voters = request.POST.getlist('voters')
    else:
        voters = question.question_voters.all()
    for voter in voters:
        if type == 'invite' or type == 'remove':
     	    voter = get_object_or_404(User, username=voter)
        name = voter.username
        uname = voter.username
        if question.poll_algorithm == 1 and type == 'start':
            items = Item.objects.all().filter(question=question)
            item_array = getOptions(items)
            options = ''
            for i in items:
                rand = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(20))
                response = EmailResponse(item=i, user=voter, identity=rand)
                response.save()
                options += '<p><a href=\'' + request.build_absolute_uri(reverse('polls:index') + str(response.pk) + "/" + rand + "/voteEmail/") + '\'>' + i.item_text + '</a></p>'
        if voter.first_name != "":
            name = voter.first_name + " " + voter.last_name
        url = request.build_absolute_uri(reverse('appauth:login')+'?name='+uname)
        mail.send_mail(translateEmail(email.subject, name, url),
            translateEmail(email.message, name, url),
            '<EMAIL>',[voter.email],
            html_message=translateHTML(email.message, name, url))

def emailSettings(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    emailInvite = Email.objects.filter(question=question, type=1)[0]
    emailInvite.subject = request.POST.get('inviteSubject')
    emailInvite.message = request.POST.get('inviteMessage')
    emailInvite.save()<commit_msg>Fix voting through email<commit_after>import datetime
import os

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponseRedirect, HttpResponse, HttpRequest
from django.core.urlresolvers import reverse
from django.views import generic

from .models import *

from django.utils import timezone
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login,logout
from django.contrib.auth.decorators import login_required
from django.core import mail
from .prefpy.mechanism import *
from groups.models import *
from django.conf import settings
import random
import string

def setupEmail(question):
    title = question.question_text
    creator = question.question_owner.username
    emailInvite = Email(question=question, type=1,
        subject="You have been invited to vote on " + title,
        message='Hello [user_name],\n\n' + creator
                + ' has invited you to vote on a poll. Please login at [url] to check it out.\n\nSincerely,\nOPRAH Staff')
    emailRemove = Email(question=question, type=2,
        subject="You have been removed from " + title,
        message='Hello [user_name],\n\n' + creator
                + ' has deleted you from a poll.\n\nSincerely,\nOPRAH Staff')
    emailStart = Email(question=question, type=3,
        subject=title + ' has started!',
        message='Hello [user_name],\n\n' + creator
                + ' has started a poll. It is now available to vote on at [url] \n\nSincerely,\nOPRAH Staff')
    emailStop = Email(question=question, type=4,
        subject=title + ' has stopped',
        message='Hello [user_name],\n\n' + creator
                + ' has ended a poll. Please visit [url] to view the decision.\n\nSincerely,\nOPRAH Staff')
    emailInvite.save()
    emailRemove.save()
    emailStart.save()
    emailStop.save()

def getOptions(items):
    arr = []
    for item in items:
        arr.append(item.item_text)
    return arr

def switchModel(type, question):
    if type == 'invite':
        email = Email.objects.filter(question=question, type=1)
    if type == 'remove':
        email = Email.objects.filter(question=question, type=2)
    if type == 'start':
        email = Email.objects.filter(question=question, type=3)
    if type == 'stop':
        email = Email.objects.filter(question=question, type=4)
    if len(email) != 1:
        setupEmail(question)
    return email[0]

def translateEmail(text, uname, url):
    text = text.replace("[user_name]", uname)
    text = text.replace("[url]", url)
    return text

def translateHTML(text, uname, url, options):
    text = translateEmail(text, uname, url)
    text = "<p>" + text + "</p>"
    text = text.replace("\n\n", "</p><br /><p>")
    text = text.replace("\n", "</p><p>")
    return text

#function to send email
def sendEmail(request, question_id, type):
    question = get_object_or_404(Question, pk=question_id)
    email = switchModel(type, question)
    options = ''
    title = question.question_text
    creator_obj = User.objects.get(id=question.question_owner_id)
    creator = creator_obj.username
    if creator_obj.first_name != "":
        creator = creator_obj.first_name + " " + creator_obj.last_name

    if type == 'invite' or type == 'remove':
        voters = request.POST.getlist('voters')
    else:
        voters = question.question_voters.all()
    for voter in voters:
        if type == 'invite' or type == 'remove':
     	    voter = get_object_or_404(User, username=voter)
        name = voter.username
        uname = voter.username
        if question.poll_algorithm == 1 and type == 'start':
            items = Item.objects.all().filter(question=question)
            item_array = getOptions(items)
            options = ''
            for i in items:
                rand = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(20))
                response = EmailResponse(item=i, user=voter, identity=rand)
                response.save()
                options += '<p><a href=\'' + request.build_absolute_uri(reverse('polls:index') + str(response.pk) + "/" + rand + "/voteEmail/") + '\'>' + i.item_text + '</a></p>'
        if voter.first_name != "":
            name = voter.first_name + " " + voter.last_name
        url = request.build_absolute_uri(reverse('appauth:login')+'?name='+uname)
        mail.send_mail(translateEmail(email.subject, name, url),
            translateEmail(email.message, name, url),
            '<EMAIL>',[voter.email],
            html_message=translateHTML(email.message, name, url, options))

def emailSettings(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    emailInvite = Email.objects.filter(question=question, type=1)[0]
    emailInvite.subject = request.POST.get('inviteSubject')
    emailInvite.message = request.POST.get('inviteMessage')
    emailInvite.save()
{'fraction_non_alphanumeric': 0.12962455467251302, 'fraction_numerical': 0.0052982552297433086, 'mean_word_length': 2.9883424408014574, 'pattern_counts': {'":': 4, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6406936', 'n_tokens_mistral': 3342, 'n_tokens_neox': 3268, 'n_words': 824}