def get(self): response = {'constituencies': []} for const in Constituency.all(): c = db.to_dict(const) c['details'] = '/constituencies/%s' % const.slug response['constituencies'].append(c) self.returnJSON(200, response)
def get(self, slug): response = {} try: response['constituency'] = db.to_dict(Constituency.all().filter('slug =', slug)[0]) except: response['error'] = 'Cannot find constituency' self.returnJSON(404, response) self.returnJSON(200, response)
def get(self, slug): response = {} try: response['constituency'] = db.to_dict(Constituency.all().filter( 'slug =', slug)[0]) except: response['error'] = 'Cannot find constituency' self.returnJSON(404, response) self.returnJSON(200, response)
def get(self): response = { 'constituencies': [] } for const in Constituency.all(): c = db.to_dict(const) c['details'] = '/constituencies/%s' % const.slug response['constituencies'].append(c) self.returnJSON(200, response)
def test_election(self): """Test the public ``add_candidate'' function provided by the Election() class. """ el = Election() con1 = Constituency("test") con2 = Constituency("test2") party1 = Party("party1") party2 = Party("party2") candidate1 = Candidate("candidate1", party1, con1) candidate2 = Candidate("candidate2", party2, con1) candidate3 = Candidate("candidate3", party2, con2) el.add_candidate(candidate1) self.assertTrue(candidate1 in el.candidates) self.assertTrue(party1 in el.parties) self.assertTrue(con1 in el.constituencies) with self.assertRaises(AssertionError): el.add_candidate(candidate1) el.add_candidate(candidate2) self.assertTrue(candidate2 in el.candidates) self.assertTrue(party1 in el.parties) self.assertTrue(con1 in el.constituencies) self.assertEqual(len(el.candidates), 2) self.assertEqual(len(el.parties), 2) self.assertEqual(len(el.constituencies), 1) el.add_candidate(candidate3) self.assertTrue(candidate3 in el.candidates) self.assertTrue(party2 in el.parties) self.assertTrue(con2 in el.constituencies) self.assertEqual(len(el.candidates), 3) self.assertEqual(len(el.parties), 2) self.assertEqual(len(el.constituencies), 2)
def test_add_winner(self): """Test the public ``add_winner'' function provided by the Outcome() class. """ con = Constituency("test") con2 = Constituency("test2") party1 = Party("party1") party2 = Party("party2") candidate1 = Candidate("candidate1", party1, con) candidate2 = Candidate("candidate2", party2, con2) outcome = Outcome() outcome.add_winner(candidate1) self.assertEqual(len(outcome.winners), 1) self.assertTrue(candidate1 in outcome.winners) # attempt to add the same candidate with self.assertRaises(AssertionError): outcome.add_winner(candidate1)
def add_constituency(request): my_constituencies = request.user.current_constituencies.all() if len(my_constituencies) > 0: neighbours = Constituency.neighbours(my_constituencies[0]) neighbours = neighbours.exclude(pk__in=my_constituencies) else: neighbours = [] context = { 'my_constituencies': my_constituencies, 'constituencies': list(neighbours) } # searching for a constituency by postcode or placename if request.method == "GET": if request.GET.has_key("q"): place = request.GET["q"] context['constituencies'] = Constituency.objects.filter( name__icontains=place) const = geo.constituency(place) if const != None: context['constituencies'] = Constituency.objects.filter( name=const) | context['constituencies'] if context['constituencies'].count() == 0: context['search_fail'] = "Alas, we can't find '%s'" % place # adding another constituency if request.method == "POST": if request.POST.has_key('add') and request.POST.has_key('add_c'): add_c = request.POST.getlist('add_c') if type(add_c) != types.ListType: add_c = [add_c] constituencies = Constituency.objects.all().filter(slug__in=add_c) constituencies = constituencies.exclude(pk__in=my_constituencies) request.user.constituencies.add(*constituencies.all()) request.user.save() return HttpResponseRedirect("/add_constituency/") return render_with_context(request, 'add_constituency.html', context)
def add_constituency(request): my_constituencies = request.user.current_constituencies.all() if len(my_constituencies) > 0: neighbours = Constituency.neighbours(my_constituencies[0]) neighbours = neighbours.exclude(pk__in=my_constituencies) else: neighbours = [] context = {'my_constituencies': my_constituencies, 'constituencies': list(neighbours)} # searching for a constituency by postcode or placename if request.method == "GET": if request.GET.has_key("q"): place = request.GET["q"] context['constituencies'] = Constituency.objects.filter(name__icontains=place) const = geo.constituency(place) if const != None: context['constituencies'] = Constituency.objects.filter(name=const) | context['constituencies'] if context['constituencies'].count() == 0: context['search_fail'] = "Alas, we can't find '%s'" % place # adding another constituency if request.method == "POST": if request.POST.has_key('add') and request.POST.has_key('add_c'): add_c = request.POST.getlist('add_c') if type(add_c) != types.ListType: add_c = [add_c] constituencies = Constituency.objects.all().filter(slug__in=add_c) constituencies = constituencies.exclude(pk__in=my_constituencies) request.user.constituencies.add(*constituencies.all()) request.user.save() return HttpResponseRedirect("/add_constituency/") return render_with_context(request, 'add_constituency.html', context)
def test_add_candidate(self): """Test the public ``add_candidate'' function provided by the Constituency() class. """ con = Constituency("test") con2 = Constituency("test2") party1 = Party("party1") party2 = Party("party2") party3 = Party("party3") candidate1 = Candidate("candidate1", party1, con) candidate2 = Candidate("candidate2", party2, con) candidate3 = Candidate("candidate3", party3, con2) candidate4 = Candidate("candidate4", party2, con) con.add_candidate(candidate1) self.assertTrue(candidate1 in con.candidates) self.assertEqual(len(con.candidates), 1) # attempt to add a candidate twice with self.assertRaises(AssertionError): con.add_candidate(candidate1) self.assertEqual(len(con.candidates), 1) self.assertEqual(len(con.parties), 1) con.add_candidate(candidate2) self.assertTrue(candidate1 in con.candidates) self.assertTrue(candidate2 in con.candidates) self.assertEqual(len(con.candidates), 2) self.assertEqual(len(con.parties), 2) # attempt to add a candidate with the wrong constituency with self.assertRaises(AssertionError): con.add_candidate(candidate3) self.assertEqual(len(con.candidates), 2) # attempt to add a candidate with the same party with self.assertRaises(AssertionError): con.add_candidate(candidate4) self.assertEqual(len(con.candidates), 2)
def election_from_csv(filename): """ Create an Election object from a csv file of election data. """ election = None with open(filename, 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='"') # create an Election object election = Election() constituencies = {} parties = {} candidates = {} for row in reader: # extract the fields from the CSV data constituencyName = row[0] candidateName = row[1] partyName = row[2] voteCount = int(row[3]) # reconcile the constituency if constituencyName not in constituencies: con = Constituency(constituencyName) constituencies[constituencyName] = con else: con = constituencies[constituencyName] # reconcile the party if partyName not in parties: par = Party(partyName) parties[partyName] = par else: par = parties[partyName] # reconcile the candidate if (candidateName, partyName, constituencyName) \ not in candidates: can = Candidate(candidateName, par, con, voteCount) candidates[(candidateName, partyName, constituencyName)] = can else: raise ValueError( """A candidate with the same details already exists""") # add the candidate to the constituency con.add_candidate(can) # add the candidate to the party par.add_candidate(can) # add the candidate to the election election.add_candidate(can) return election
def import_mp_votes(subset=False): if MPVote.all().count() > 0: print "Import already complete" return subset_const = [ "Brighton, Kemptown", "Brighton, Pavillion", "Hove", "Hackney South and Shoreditch", "Edinburgh North, and Leith" ] subset_mp = [ "Caroline Lucas", "Simon Kirby", "Mike Weatherley", "Meg Hillier", "Mark Lazarowicz" ] question_list = {} csvfile = open('fixtures/mp_votes/vote_questions.csv', 'rU') for row in csv.reader(csvfile): d = Question() d.question = row[0] d.title = row[1] d.date = datetime.datetime.now() d.publicwhip_url = row[3] d.put() question_list[row[4]] = d mps_created = [] consts_created = [] for question in question_list: print question csvfile = open('fixtures/mp_votes/%s.csv' % question, 'rU') for row in csv.reader(csvfile): if subset and row[1] not in subset_const and row[0] not in subset_mp: continue try: v = MPVote(parent=question_list[question]) v.question = str(question_list[question].key()) v.mp_name = row[0] v.mp_slug = slugify(row[0]) v.mp_constituency = row[1] v.mp_party = normalise_party(row[2]).lower() v.selection = normalise_selection(row[3]) v.mp_whilst = get_whilst(row[2]) v.put() if v.mp_slug not in mps_created: mp = MP() mp.slug = v.mp_slug mp.name = v.mp_name mp.constituency = v.mp_constituency mp.party = v.mp_party mp.put() mps_created.append(v.mp_slug) if v.mp_constituency not in consts_created: const = Constituency() const.name = v.mp_constituency const.slug = slugify(v.mp_constituency) const.mp_name = v.mp_name const.mp_party = v.mp_party const.put() consts_created.append(v.mp_constituency) except: print "Failed insert"