def test_edit_ballot(self): proj = Project(name='test',order=0, doc_type='STD', description='test edit ballot', task_group='TGx', par_date=datetime.datetime.now()) proj.save() bal = Ballot(number=123,project=proj, ballot_type=Ballot.WGInitial.code, draft='1.0', opened=datetime.datetime.now(), pool=100) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() url = reverse('ballot.views.edit_ballot',args=[bal.number]) response = self._check_page(url) data = response.context['form'].initial for key in data.keys(): if data[key] is None: data[key] = '' data['curpk'] = bal.pk data['draft'] = '2.0' data['submit']='Save' form = BallotForm(data, instance=bal) valid = form.is_valid() self.failUnless(valid) response = self.client.post(url,data) self.failIf(response.status_code!=302 and response.status_code!=303) self.failUnlessEqual(Ballot.objects.count(),1) bal = Ballot.objects.get(pk=123) self.failUnlessEqual(float(bal.draft),2.0) # test renumbering the ballot, which should cause the old ballot object to be deleted data['number'] = 125 form = BallotForm(data, instance=bal) valid = form.is_valid() self.failUnless(valid) response = self.client.post(url,data) self.failIf(response.status_code!=302 and response.status_code!=303) self.failUnlessEqual(Ballot.objects.count(),1) bal = Ballot.objects.get(pk=125) self.failUnlessRaises(Ballot.DoesNotExist,Ballot.objects.get,pk=123)
def finish(request): if request.method != 'POST': return HttpResponseBadRequest() errors = [] token = request.POST.get('token', '') if not token: return HttpResponseBadRequest() pin = request.POST.get('pin', '') if not pin: return HttpResponseBadRequest() voter = Voter.objects.get(token=token, pin=pin, used=False) selectedIds = request.POST.getlist('candidate',[]) if len(selectedIds) > 3: return HttpResponseBadRequest() selected = Candidate.objects.all().filter(id__in=selectedIds) voter.used = True voter.save() b = Ballot() b.save() for candidate in selected: v = Vote(ballot=b, candidate=candidate) v.save() return render_to_response('ballot/thanks.html', {'voter': voter}, context_instance=RequestContext(request))
def test_delete_ballot(self): proj = Project(name='test',order=0, doc_type='STD', \ description='test_delete_ballot', task_group='TGx', \ par_date=datetime.datetime.now()) proj.save() bal = Ballot(number=123,project=proj, draft='1.0', \ opened=datetime.datetime.now(), pool=100, \ ballot_type=Ballot.WGInitial.code) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() self.failUnlessEqual(bal.pk, 123) self.failUnlessEqual(Ballot.objects.count(), 1) url = reverse('ballot.views.main_page', args=[]) response = self._check_page(url) self.failUnlessEqual(Ballot.objects.count(), 1) self.wait_for_backlog_completion(DenormalizedBallot, 10) run_test_task_queue(self.client) self.failUnlessEqual(Ballot.objects.count(), 1) dn = DenormalizedBallot.objects.get(pk=bal.pk) self.failIfEqual(dn, None) self.failUnlessEqual(Ballot.objects.count(), 1) Ballot.objects.filter(pk=bal.number).delete() run_test_task_queue(self.client) self.failUnlessRaises(DenormalizedBallot.DoesNotExist, DenormalizedBallot.objects.get, pk=123) bal = Ballot(number=124,project=proj, draft='2.0', opened=datetime.datetime.now(), \ pool=100, ballot_type=Ballot.WGInitial.code) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() run_test_task_queue(self.client) url = reverse('ballot.views.edit_ballot', args=[bal.number]) response = self._check_page(url) data = response.context['form'].initial for key in data.keys(): if data[key] is None: data[key] = '' data['curpk'] = bal.pk data['delete'] = 'Delete' form = BallotForm(data, instance=bal) valid = form.is_valid() self.failUnless(valid) response = self.client.post(url, data) self.failIf(response.status_code != 302 and response.status_code != 303) url = reverse('del_ballot', args=[bal.number]) response.get('Location').index(url) response = self._check_page(url) match = re.search(r'input\s+type="submit"\s+name="confirm"', str(response), re.IGNORECASE) self.assertTrue(match) data = {"confirm": "Yes, I'm sure"} response = self.client.post(url, data) self.failIf(response.status_code != 302 and response.status_code != 303) self.failUnlessRaises(Ballot.DoesNotExist, Ballot.objects.get, pk=124) run_test_task_queue(self.client) self.failUnlessRaises(DenormalizedBallot.DoesNotExist, DenormalizedBallot.objects.get, pk=124)
def test_renumber_ballot(self): proj = Project(name='test', order=0, doc_type='STD', description='test_renumber_ballot', task_group='TGx', par_date=datetime.datetime.now()) proj.save() self.failUnlessEqual(Project.objects.count(), 1) self.failUnlessEqual(Ballot.objects.count(), 0) bal = Ballot(number=123, project=proj, draft='1.0', opened=datetime.datetime.now(), pool=100) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() self.failUnlessEqual(Ballot.objects.count(), 1) url = reverse('ballot.views.main_page', args=[]) self._check_page(url) #response = self.client.get(url) self.wait_for_backlog_completion(DenormalizedBallot, 10) dn = DenormalizedBallot.objects.get(pk=bal.pk) bal2 = Ballot.objects.get(number=bal.number) bal2.number = 321 bal2.save() Ballot.objects.get(number=123).delete() self.wait_for_backlog_completion(DenormalizedBallot, 10) self.failUnlessRaises(DenormalizedBallot.DoesNotExist, DenormalizedBallot.objects.get, number=123) dn = DenormalizedBallot.objects.get(number=bal2.number)
def import_json(progress): """ Called by import worker to process a JSON file that as been uploaded by import_view """ text = [] for linenum in range(1, progress.linecount + 1): impline = ImportLine.objects.get(line=linenum) if impline.text is None: continue text.append(''.join(impline.text)) #impline = ImportLine.objects.get(line=1) text = '\n'.join(text) #if impline is not None and impline.text is not None: if text: try: #js = json.loads(''.join(impline.text)) js = json.loads(text) except ValueError, e: js = {} logging.error(e) progress.add_error(1, e, impline) try: for proj in js['projects']: proj = Project(**proj) proj.save() progress.add_project(proj) except KeyError: pass try: for bal in js['ballots']: try: proj = Project.objects.get(task_group=bal['task_group']) bal['project_id'] = proj.pk del bal['task_group'] except KeyError: pass bal = Ballot(**bal) bal.save() progress.add_ballot(bal) except KeyError: pass try: for rep in js['reports']: rep = MeetingReport(**rep) rep.save() progress.add_report(rep) except KeyError: pass
def test_edit_ballot(self): proj = Project(name='test', order=0, doc_type='STD', description='test edit ballot', task_group='TGx', par_date=datetime.datetime.now()) proj.save() bal = Ballot(number=123, project=proj, ballot_type=Ballot.WGInitial.code, draft='1.0', opened=datetime.datetime.now(), pool=100) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() url = reverse('ballot.views.edit_ballot', args=[bal.number]) response = self._check_page(url) data = response.context['form'].initial for key in data.keys(): if data[key] is None: data[key] = '' data['curpk'] = bal.pk data['draft'] = '2.0' data['submit'] = 'Save' form = BallotForm(data, instance=bal) valid = form.is_valid() self.failUnless(valid) response = self.client.post(url, data) self.failIf(response.status_code != 302 and response.status_code != 303) self.failUnlessEqual(Ballot.objects.count(), 1) bal = Ballot.objects.get(pk=123) self.failUnlessEqual(float(bal.draft), 2.0) # test renumbering the ballot, which should cause the old ballot object to be deleted data['number'] = 125 form = BallotForm(data, instance=bal) valid = form.is_valid() self.failUnless(valid) response = self.client.post(url, data) self.failIf(response.status_code != 302 and response.status_code != 303) self.failUnlessEqual(Ballot.objects.count(), 1) bal = Ballot.objects.get(pk=125) self.failUnlessRaises(Ballot.DoesNotExist, Ballot.objects.get, pk=123)
def test_delete_ballot(self): proj = Project(name='test',order=0, doc_type='STD', \ description='test_delete_ballot', task_group='TGx', \ par_date=datetime.datetime.now()) proj.save() bal = Ballot(number=123,project=proj, draft='1.0', \ opened=datetime.datetime.now(), pool=100, \ ballot_type=Ballot.WGInitial.code) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() self.failUnlessEqual(bal.pk,123) self.failUnlessEqual(Ballot.objects.count(),1) url = reverse('ballot.views.main_page',args=[]) response = self._check_page(url) self.failUnlessEqual(Ballot.objects.count(),1) self.wait_for_backlog_completion(DenormalizedBallot, 10) run_test_task_queue(self.client) self.failUnlessEqual(Ballot.objects.count(),1) dn = DenormalizedBallot.objects.get(pk=bal.pk) self.failIfEqual(dn,None) self.failUnlessEqual(Ballot.objects.count(),1) Ballot.objects.filter(pk=bal.number).delete() run_test_task_queue(self.client) self.failUnlessRaises(DenormalizedBallot.DoesNotExist, DenormalizedBallot.objects.get, pk=123) bal = Ballot(number=124,project=proj, draft='2.0', opened=datetime.datetime.now(), \ pool=100, ballot_type=Ballot.WGInitial.code) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() run_test_task_queue(self.client) url = reverse('ballot.views.edit_ballot',args=[bal.number]) response = self._check_page(url) data = response.context['form'].initial for key in data.keys(): if data[key] is None: data[key] = '' data['curpk'] = bal.pk data['delete']='Delete' form = BallotForm(data, instance=bal) valid = form.is_valid() self.failUnless(valid) response = self.client.post(url,data) self.failIf(response.status_code!=302 and response.status_code!=303) url = reverse('del_ballot',args=[bal.number]) response.get('Location').index(url) response = self._check_page(url) match = re.search(r'input\s+type="submit"\s+name="confirm"', str(response), re.IGNORECASE) self.assertTrue(match) data = {"confirm":"Yes, I'm sure"} response = self.client.post(url,data) self.failIf(response.status_code!=302 and response.status_code!=303) self.failUnlessRaises(Ballot.DoesNotExist, Ballot.objects.get, pk=124) run_test_task_queue(self.client) self.failUnlessRaises(DenormalizedBallot.DoesNotExist, DenormalizedBallot.objects.get, pk=124)
def test_renumber_ballot(self): proj = Project(name='test',order=0, doc_type='STD', description='test_renumber_ballot', task_group='TGx', par_date=datetime.datetime.now()) proj.save() self.failUnlessEqual(Project.objects.count(),1) self.failUnlessEqual(Ballot.objects.count(),0) bal = Ballot(number=123,project=proj, draft='1.0', opened=datetime.datetime.now(), pool=100) bal.closed = bal.opened + datetime.timedelta(days=15) bal.save() self.failUnlessEqual(Ballot.objects.count(),1) url = reverse('ballot.views.main_page',args=[]) self._check_page(url) #response = self.client.get(url) self.wait_for_backlog_completion(DenormalizedBallot, 10) dn = DenormalizedBallot.objects.get(pk=bal.pk) bal2 = Ballot.objects.get(number=bal.number) bal2.number = 321 bal2.save() Ballot.objects.get(number=123).delete() self.wait_for_backlog_completion(DenormalizedBallot, 10) self.failUnlessRaises(DenormalizedBallot.DoesNotExist, DenormalizedBallot.objects.get, number=123) dn = DenormalizedBallot.objects.get(number=bal2.number)
def edit_ballot(request, bal): context = {} context.update(csrf(request)) next_page = request.GET.get('next', '/') if bal is None: lbnum = Ballot.FIRST_SPONSOR_LBNUM for b in Ballot.objects.all(): lbnum = max(lbnum, b.number + 1) ballot = Ballot(number=lbnum) else: ballot = get_object_or_404(Ballot, pk=bal) if request.method == 'POST': if request.POST.has_key('cancel'): return http.HttpResponseRedirect(next_page) if request.POST.has_key('delete'): return http.HttpResponseRedirect(reverse('del_ballot', args=[bal])) form = BallotForm(request.POST, request.FILES, instance=ballot) if form.is_valid(): data = form.cleaned_data ballot = form.save() if data['curpk'] and data['curpk'] != ballot.pk: try: Ballot.objects.get(pk=data['curpk']).delete() except Ballot.DoesNotExist: pass cc = CacheControl() if data['curstat'] != ballot.project.status.id: if InProgress.id == data['curstat']: cc.in_progress_ver = cc.in_progress_ver + 1 if Published.id == data['curstat']: cc.published_ver = cc.published_ver + 1 if Withdrawn.id == data['curstat']: cc.withdrawn_ver = cc.withdrawn_ver + 1 return http.HttpResponseRedirect(next_page) else: initial = { 'curstat': ballot.project.status.id, 'curpk': ballot.pk } if bal is not None else { 'curstat': 0 } form = BallotForm(instance=ballot, initial=initial) context['form'] = form context['object'] = ballot context['no_delete'] = bal is None return render_to_response('edit-object.html', context, context_instance=RequestContext(request))
def index(request): sunetid = request.user.webauth_username ballot, c = Ballot.get_or_create_by_sunetid(sunetid) if ballot.needs_ballot_choice(): return HttpResponseRedirect('/ballot/choose') ballotform = ballot_form_factory(ballot)(instance=ballot) record = VoteRecord() record.sunetid = sunetid record.ip = request.META['REMOTE_ADDR'] record.datetime = datetime.now() record.type = "start" record.save() return render_to_response('ballot/ballot.html', {'ballotform': ballotform, 'ballot': ballot, 'issues_json': make_issues_json(), 'cp_slates': get_cp_slates(ballot), 'csac_members': get_csac_members(), 'exec_slates': get_exec_slates(),'sunetid': sunetid}, context_instance=RequestContext(request))
def parse_ballot_info(row, locality, verbosity=1): ballot = Ballot.from_date(date=date_parse(row['tran_Date']), locality=locality) # Figure out beneficiary from past entries. past_money = models.IndependentMoney.objects.filter( beneficiary__name=clean_name(row['filerName']), beneficiary__ballot_item_selection__ballot_item__ballot=ballot) if past_money.count() > 0: # Figure it out from past contributions. ballot_item_selection = past_money[0] \ .beneficiary.ballot_item_selection else: # Figure out beneficiary from item text. try: ballot_item_selection = parse_candidate_info( row, ballot=ballot, verbosity=verbosity) except ValueError: ballot_item_selection = parse_referendum_info( row, ballot=ballot, verbosity=verbosity) return ballot_item_selection, True
def import_ballot(item, last_ballot, cache, fields=None): if fields is None: fields = ballot_fields entry = item.as_dict(fields) #if entry['number'] is None: # return None try: number = entry['number'].as_int() except (ValueError, TypeError): return None if number == 0: number = cache.get_next_lb_number() cache.set_next_lb_number(number + 1) b = None try: b = Ballot.objects.get(number=number) except Ballot.DoesNotExist: b = Ballot(number=number) for field in b._meta.fields: if not field.primary_key: try: value = to_python(field, entry[field.attname].value) if entry[ field.attname] is not None else None setattr(b, field.attname, value) except KeyError: pass try: b.project = cache.get_project( task_group=entry['project.task_group'].value) except KeyError: b.project = Project.objects.get( task_group=entry['project.task_group'].value) cache.put_project(b.project) if b.opened is None: set_date(b, 'closed', entry['date'].value, format='%Y-%m-%d') b.opened = b.closed + datetime.timedelta(days=-15) if b.pool is None: try: b.pool = int(b.vote_for) + int(b.vote_against) + int( b.vote_abstain) except TypeError: pass #b.save() return b
def parse_ballot_info(row, locality, verbosity=1): ballot = Ballot.from_date(date=date_parse(row['tran_Date']), locality=locality) # Figure out beneficiary from past entries. past_money = models.IndependentMoney.objects.filter( beneficiary__name=clean_name(row['filerName']), beneficiary__ballot_item_selection__ballot_item__ballot=ballot) if past_money.count() > 0: # Figure it out from past contributions. ballot_item_selection = past_money[0] \ .beneficiary.ballot_item_selection else: # Figure out beneficiary from item text. try: ballot_item_selection = parse_candidate_info(row, ballot=ballot, verbosity=verbosity) except ValueError: ballot_item_selection = parse_referendum_info(row, ballot=ballot, verbosity=verbosity) return ballot_item_selection, True
def import_html_sponsor_ballot(item, last_ballot, cache): class Container(object): pass entry = item.as_dict(html_sponsor_ballot_fields) tg = entry['Group'].strip() if not tg: if entry['Disapprove'] and last_ballot is not None: try: last_ballot.vote_invalid = as_int(entry['Disapprove']) except ValueError: pass return None if entry['Draft'][0] == 'D': entry['Draft'] = entry['Draft'][1:] try: float(entry['Draft']) except (ValueError, TypeError): entry['Draft'] = 0 if tg.lower().startswith('std. '): std = tg[5:] try: project = cache.get_project(name=std) except KeyError: try: project = Project.objects.get(name=std) except Project.DoesNotExist: return None else: if tg.lower().startswith('p802.11-rev'): tg = 'TG' + tg[11:] elif tg.lower().startswith('p802.11'): tg = 'TG' + tg[7:] try: project = cache.get_project(task_group=tg) except KeyError: try: project = Project.objects.get(task_group=tg) except Project.DoesNotExist: return None obj = Container() if '/' in entry['Closed']: set_date(obj, 'opened', entry['Opened'], format='%d/%m/%Y') set_date(obj, 'closed', entry['Closed'], format='%d/%m/%Y') else: set_date(obj, 'opened', entry['Opened'], format='%Y-%m-%d') set_date(obj, 'closed', entry['Closed'], format='%Y-%m-%d') try: b = Ballot.objects.get(project=project, draft=entry['Draft'], closed=obj.closed) except Ballot.DoesNotExist: lbnum = cache.get_next_lb_number() b = Ballot(project=project, draft=entry['Draft'], number=lbnum, closed=obj.closed, opened=obj.opened) cache.set_next_lb_number(lbnum + 1) if 'recirc' in entry['Ballot Type'].lower(): b.ballot_type = 'SR' else: b.ballot_type = 'SI' #b.result = as_int(entry['Approve_pct']) b.vote_for = as_int(entry['Approve']) b.vote_against = as_int(entry['Disapprove']) b.vote_abstain = as_int(entry['Abstain']) if entry['Cmnt']: b.comments = as_int(entry['Cmnt']) b.pool = as_int(entry['Pool']) #b.save() return b
def get_ballot(sunetid): b, created = Ballot.get_or_create_by_sunetid(sunetid) return b
def test_create_empty(self): ballot = Ballot() # noqa
def import_html_letter_ballot(item, last_ballot, cache): entry = item.as_dict(html_ballot_fields) if entry['number'] and entry['task_group']: lbnum = as_int(entry['number']) try: b = Ballot.objects.get(number=lbnum) except Ballot.DoesNotExist: b = Ballot(number=lbnum) if entry['task_group'] == 'TGm': # TODO: Find a better solution entry['task_group'] = 'TGma' try: wg = cache.get_project(task_group=entry['task_group']) except KeyError: try: wg = Project.objects.get(task_group=entry['task_group']) except Project.DoesNotExist: entry['task_group'] = entry['task_group'][:-1] + entry[ 'task_group'][-1].lower() try: wg = Project.objects.get(task_group=entry['task_group']) except Project.DoesNotExist: return None b.project = wg try: index = entry['comments'].index('Draft ') b.draft = entry['comments'][index + 6:] except ValueError: try: index = entry['documents'].index('_D') index2 = entry['documents'].index('.pdf') b.draft = entry['documents'][index + 2:index2] except ValueError: b.draft = 0 try: b.instructions_url = entry['instructions'].anchor except AttributeError: b.instructions_url = entry['instructions'] try: b.draft_url = entry['documents'].anchor except AttributeError: b.draft_url = make_url( entry['documents'], '/11/private/Draft_Standards/11%s/' % (wg.task_group[2:])) b.pool = as_int_or_none(entry['result']) set_date(b, 'opened', entry['opened']) set_date(b, 'closed', entry['closed']) if b.opened is None or b.closed is None: raise Exception("Failed to find date in " + str(entry)) comments = entry['comments'].lower() if comments.find('recirculation') >= 0: b.ballot_type = Ballot.WGRecirc.code elif comments.find('procedural') >= 0: b.ballot_type = Ballot.Procedural.code else: b.ballot_type = Ballot.WGInitial.code return b if last_ballot is None: return None if entry['draft']: draft = entry['draft'].lower() if 'redline' in draft: try: last_ballot.redline_url = entry['documents'].anchor except AttributeError: last_ballot.redline_url = None #make_url(entry['documents'],'/11/private/Draft_Standards/11%s/'%(last_ballot.project.task_group[2:])) elif 'resolution' in draft: try: last_ballot.resolution_url = entry['documents'].anchor except AttributeError: last_ballot.resolution_url = make_url( entry['documents'], '/11/private/Draft_Standards/11%s/' % (last_ballot.project.task_group[2:])) elif 'comment' in draft: try: last_ballot.template_url = entry['documents'].anchor except AttributeError: last_ballot.template_url = '/11/LetterBallots/LB%ds/LB%d_comment_form.xls' % ( last_ballot.number, last_ballot.number) elif 'pool' in draft: try: last_ballot.pool_url = entry['documents'].anchor except AttributeError: last_ballot.pool_url = '/11/LetterBallots/LB%ds/LB%d_voters_list.xls' % ( last_ballot.number, last_ballot.number) result_type = entry['result_type'].lower( ) if entry['result_type'] is not None else None #if result_type =='approve%': # last_ballot.result = as_int_or_none(item[-1]) if result_type == 'approve': last_ballot.vote_for = as_int_or_none(entry['result']) elif result_type == 'disapprove': last_ballot.vote_against = as_int_or_none(entry['result']) elif result_type == 'abstain': last_ballot.vote_abstain = as_int_or_none(entry['result']) return None