コード例 #1
0
ファイル: views.py プロジェクト: BingyZhang/finer
def pubdef(request):
    if request.method == 'POST':
        # check captcha first
        form = DefForm(request.POST)
        if not form.is_valid():
            return HttpResponse('The captcha is invalid!')
        name = request.POST['name']
        if name == "":
            name = "Anonymous"
        email = request.POST['email']
        if email == "":
            email = "*****@*****.**"
        q = request.POST['question']
        start = request.POST['elect_start']
        end = request.POST['elect_end']
        Paffiliation = request.POST['Paffiliation'].lower()
        title = request.POST['title'].lower()
        Porg = request.POST['Porg'].lower()
        total = request.POST['total']
        keyemails = request.POST['keyemails'].rstrip()
        pdf = request.POST.get('pdf', '')
        if pdf != '':
            intpdf = int(pdf)
        else:
            intpdf = 0
        opts = []

        # maximum 50 options cheat
        for pi in range(len(party_list) / 2):
            temp = party_list[2 * pi] + ";" + party_list[2 * pi + 1]
            opts.append(temp.decode('utf-8'))

        if Paffiliation == '':
            Paffiliation = '*'
        if title == '':
            title = '*'
        if Porg == '':
            Porg = '*'
        if total == '':
            total = "1"
        if start == '':
            start = timezone.now().strftime("%m/%d/%Y %H:%M")
        if end == '':
            end = timezone.now().strftime("%m/%d/%Y %H:%M")
        start_time = time.strptime(start, "%m/%d/%Y %H:%M")
        end_time = time.strptime(end, "%m/%d/%Y %H:%M")
        #EID should be hash of question start and end time short eid
        #eid = hashlib.sha1(q + start + end).hexdigest()
        counter = Election.objects.count()
        eid = base36encode(counter + 1)
        #first post to BB
        files = {
            'question': q,
            'start': start,
            'end': end,
            'eid': eid,
            'total': total
        }
        for i in range(len(opts)):
            files["opt" + str(i)] = opts[i]
        r = requests.post(BB_URL + 'def/', data=files, verify=False)
        #if r != "success":
        #    return HttpResponse(r)#('Error!')
        #create election
        new_e = Election(creator=name,
                         c_email=email,
                         Paffiliation=Paffiliation,
                         title=title,
                         Porg=Porg,
                         start=datetime.fromtimestamp(time.mktime(start_time)),
                         end=datetime.fromtimestamp(time.mktime(end_time)),
                         question=q,
                         EID=eid,
                         total=total)
        new_e.save()
        # store choices
        for x in opts:
            new_c = Choice(election=new_e, text=x)
            new_c.save()
        #go through the email files
        voter_emails = []
        emailfile = request.FILES.get('emailfile', '')
        if emailfile != '':
            reader = emailfile.read().splitlines()
            for line in reader:
                temp = line.rstrip().lower()
                if temp != '':
                    voter_emails.append(temp)
        #confirm EA
        data = []
        data.append("Question: " + q)
        for i in range(len(opts)):
            data.append("Option " + str(i + 1) + ": " + opts[i])
        data.append("Start time: " + start)
        data.append("End time: " + end)
        #data.append("End time: "+end)
        data.append("Key holders' emails: " + keyemails)
        data.append("Maximum number of voters: " + total)
        data.append("eduPersonPrimaryAffiliation: " + Paffiliation)
        data.append("Tile: " + title)
        data.append("eduPersonPrimaryOrgUnitDN: " + Porg)
        VBB_url = BB_URL + "vbb/" + eid + "/"
        ABB_url = BB_URL + "abb/" + eid + "/"
        #send email
        emailbody = "Hello " + name + ",\n\nThe following election is created.\n"
        emailbody += "\n".join(data)
        emailbody += "\nVBB_url: " + VBB_url + "\n"
        emailbody += "ABB_url: " + ABB_url + "\n"
        emailbody += "\nFINER  Election Authority\n"

        #send email
        p = subprocess.Popen([
            "sudo", "/var/www/finer/bingmail.sh",
            "Election Definition: " + q.encode('utf-8'),
            emailbody.encode('utf-8'), email
        ],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             env=env)
        output, err = p.communicate()
        #celery prepare ballots
        prepare_ballot.delay(new_e, int(total), len(opts), voter_emails,
                             keyemails, intpdf)
        return render_to_response(
            'confirm.html', {
                'name': name,
                'data': data,
                'email': email,
                'VBB': VBB_url,
                'ABB': ABB_url
            })
    else:
        return render_to_response('pubdef.html', {'form': DefForm},
                                  context_instance=RequestContext(request))
コード例 #2
0
ファイル: tasks.py プロジェクト: BingyZhang/finer
def prepare_ballot(x, y):
    print "test...creating ballot.."
    #create election
    new_e = Election(start = timezone.now(), end = timezone.now(), question = "test", EID = 1234)
    new_e.save()
    return x + y