def test_election_just_general_elections(self): self.sp_c_election.delete() self.sp_r_election.delete() with self.assertNumQueries(1): self.assertEqual(Election.group_and_order_elections(), [ { 'current': True, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ { 'election': self.election }, ] }, ] }, { 'current': False, 'roles': [{ 'role': 'Member of Parliament', 'elections': [ { 'election': self.earlier_election }, ] }] }, ])
def get_counts(for_json=True): election_id_to_candidates = {} qs = Membership.objects.filter( role=F('post_election__election__candidate_membership_role')).values( 'post_election__election').annotate( count=Count('post_election__election')) for d in qs: election_id_to_candidates[d['post_election__election']] = d['count'] grouped_elections = Election.group_and_order_elections(for_json=for_json) for era_data in grouped_elections: for date, elections in era_data['dates'].items(): for role_data in elections: for election_data in role_data['elections']: e = election_data['election'] total = election_id_to_candidates.get(e.id, 0) election_counts = { 'id': e.slug, 'html_id': e.slug.replace('.', '-'), 'name': e.name, 'total': total, } election_data.update(election_counts) del election_data['election'] return grouped_elections
def test_election_just_general_elections(self): self.sp_c_election.delete() self.sp_r_election.delete() with self.assertNumQueries(1): self.assertEqual( Election.group_and_order_elections(), [ { 'current': True, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ {'election': self.election}, ] }, ] }, { 'current': False, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ {'election': self.earlier_election}, ] } ] }, ] )
def test_election_just_general_elections(self): self.sp_c_election.delete() self.sp_r_election.delete() with self.assertNumQueries(1): self.assertEqual( Election.group_and_order_elections(), [ { 'current': True, 'dates': OrderedDict([ (self.local_election.election_date, [{ 'role': 'Local Councillor', 'elections': [ {'election': self.local_election} ] }]), (self.election.election_date, [{ 'role': 'Member of Parliament', 'elections': [ {'election': self.election} ] }]) ]) }, { 'current': False, 'dates': OrderedDict([ (self.earlier_election.election_date, [{ 'role': 'Member of Parliament', 'elections': [ {'election': self.earlier_election} ] }]) ]) } ] )
def get_counts(for_json=True): election_id_to_candidates = {} qs = (Membership.objects.filter( role=F("post_election__election__candidate_membership_role")).values( "post_election__election").annotate( count=Count("post_election__election"))) for d in qs: election_id_to_candidates[d["post_election__election"]] = d["count"] grouped_elections = Election.group_and_order_elections(for_json=for_json) for era_data in grouped_elections: for date, elections in era_data["dates"].items(): for role_data in elections: for election_data in role_data["elections"]: e = election_data["election"] total = election_id_to_candidates.get(e.id, 0) election_counts = { "id": e.slug, "html_id": e.slug.replace(".", "-"), "name": e.name, "total": total, } election_data.update(election_counts) del election_data["election"] return grouped_elections
def test_election_grouping_with_posts(self): with self.assertNumQueries(2): self.assertEqual( Election.group_and_order_elections(include_posts=True), [ { 'current': True, 'roles': [{ 'role': 'Member of Parliament', 'elections': [ { 'election': self.election, 'posts': [ self.camberwell_post_extra, self.dulwich_post_extra, self.edinburgh_east_post_extra, self.edinburgh_north_post_extra, ] }, ] }, { 'role': 'Member of the Scottish Parliament', 'elections': [ { 'election': self.sp_c_election, 'posts': [] }, { 'election': self.sp_r_election, 'posts': [] }, ] }] }, { 'current': False, 'roles': [{ 'role': 'Member of Parliament', 'elections': [ { 'election': self.earlier_election, 'posts': [ self.camberwell_post_extra, self.dulwich_post_extra, self.edinburgh_east_post_extra, self.edinburgh_north_post_extra, ] }, ] }] }, ])
def get_context_data(self, **kwargs): context = super(HelpApiView, self).get_context_data(**kwargs) context['grouped_elections'] = Election.group_and_order_elections() context['help_results_url'] = reverse('help-results') context['base_api_url'] = self.request.build_absolute_uri( reverse('api-root', kwargs={'version': 'v0.9'})) return context
def get_context_data(self, **kwargs): context = super(HelpApiView, self).get_context_data(**kwargs) context['grouped_elections'] = Election.group_and_order_elections() context['base_api_url'] = self.request.build_absolute_uri( reverse('api-root', kwargs={'version': 'v0.9'}) ) return context
def test_election_grouping_with_posts(self): with self.assertNumQueries(2): self.assertEqual( Election.group_and_order_elections(include_posts=True), [ { 'current': True, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ { 'election': self.election, 'posts': [ self.camberwell_post_extra, self.dulwich_post_extra, self.edinburgh_east_post_extra, self.edinburgh_north_post_extra, ] }, ] }, { 'role': 'Member of the Scottish Parliament', 'elections': [ {'election': self.sp_c_election, 'posts': []}, {'election': self.sp_r_election, 'posts': []}, ] } ] }, { 'current': False, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ { 'election': self.earlier_election, 'posts': [ self.camberwell_post_extra, self.dulwich_post_extra, self.edinburgh_east_post_extra, self.edinburgh_north_post_extra, ] }, ] } ] }, ] )
def test_election_grouping_with_posts(self): with self.assertNumQueries(2): self.assertEqual( Election.group_and_order_elections(include_posts=True), [{ 'current': True, 'dates': OrderedDict([(datetime.date(2016, 5, 5), [{ 'role': 'Member of the Scottish Parliament', 'elections': [{ 'posts': [], 'election': self.sp_c_election }, { 'posts': [], 'election': self.sp_r_election }] }]), (self.election.election_date, [{ 'role': 'Member of Parliament', 'elections': [{ 'posts': [ self.camberwell_post_extra, self.dulwich_post_extra, self.edinburgh_east_post_extra, self.edinburgh_north_post_extra, ], 'election': self.election }] }])]) }, { 'current': False, 'dates': OrderedDict([(self.earlier_election.election_date, [{ 'role': 'Member of Parliament', 'elections': [{ 'posts': [ self.camberwell_post_extra, self.dulwich_post_extra, self.edinburgh_east_post_extra, self.edinburgh_north_post_extra, ], 'election': self.earlier_election }] }])]) }])
def test_election_just_general_elections(self): self.sp_c_election.delete() self.sp_r_election.delete() with self.assertNumQueries(1): self.assertEqual( Election.group_and_order_elections(), [ { "current_or_future": True, "dates": OrderedDict([ ( self.local_election.election_date, [{ "role": "Local Councillor", "elections": [{ "election": self.local_election }], }], ), ( self.election.election_date, [{ "role": "Member of Parliament", "elections": [{ "election": self.election }], }], ), ]), }, { "current_or_future": False, "dates": OrderedDict([( self.earlier_election.election_date, [{ "role": "Member of Parliament", "elections": [{ "election": self.earlier_election }], }], )]), }, ], )
def get_context_data(self, **kwargs): context = super(HelpResultsView, self).get_context_data(**kwargs) context['all_results_exists'] = self.results_file_exists(None) context['grouped_elections'] = Election.group_and_order_elections() for era_data in context['grouped_elections']: for role_data in era_data['roles']: for election_dict in role_data['elections']: election = election_dict['election'] election_dict['results_file_exists'] = \ self.results_file_exists(election.slug) return context
def get_context_data(self, **kwargs): context = super(HelpResultsView, self).get_context_data(**kwargs) context['all_results_exists'] = self.results_file_exists(None) context['grouped_elections'] = Election.group_and_order_elections() for era_data in context['grouped_elections']: for role_data in era_data['roles']: for election_dict in role_data['elections']: election = election_dict['election'] election_dict['results_file_exists'] = \ self.results_file_exists(election.slug) return context
def test_election_grouping(self): with self.assertNumQueries(1): self.assertEqual( Election.group_and_order_elections(), [ { 'current': True, 'dates': OrderedDict([ (datetime.date(2016, 5, 5), [ { 'role': 'Local Councillor', 'elections': [ {'election': self.local_election}, ] }, { 'role': 'Member of the Scottish Parliament', 'elections': [ {'election': self.sp_c_election}, {'election': self.sp_r_election}, ] }, ]), (self.election.election_date, [ { 'role': 'Member of Parliament', 'elections': [ {'election': self.election} ] } ]) ]) }, { 'current': False, 'dates': OrderedDict([ (self.earlier_election.election_date, [ { 'role': 'Member of Parliament', 'elections': [ {'election':self.earlier_election} ] } ]) ]) } ] )
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["all_results_exists"] = self.results_file_exists(None) context["grouped_elections"] = Election.group_and_order_elections() for era_data in context["grouped_elections"]: for date, elections in era_data["dates"].items(): for role_data in elections: for election_dict in role_data["elections"]: election = election_dict["election"] election_dict[ "results_file_exists" ] = self.results_file_exists(election.slug) return context
def test_election_grouping(self): with self.assertNumQueries(1): self.assertEqual(Election.group_and_order_elections(), [ { 'current': True, 'roles': [{ 'role': 'Member of Parliament', 'elections': [ { 'election': self.election }, ] }, { 'role': 'Member of the Scottish Parliament', 'elections': [ { 'election': self.sp_c_election }, { 'election': self.sp_r_election }, ] }] }, { 'current': False, 'roles': [{ 'role': 'Member of Parliament', 'elections': [ { 'election': self.earlier_election }, ] }] }, ])
def get_counts(): election_id_to_candidates = { d['extra__election']: d['count'] for d in Membership.objects \ .filter(role=F('extra__election__candidate_membership_role')) \ .values('extra__election') \ .annotate(count=Count('extra__election')) } grouped_elections = Election.group_and_order_elections() past_elections = [ election_data['election'] for era_data in grouped_elections for role_data in era_data['roles'] for election_data in role_data['elections'] if not era_data['current'] ] for era_data in grouped_elections: for role_data in era_data['roles']: for election_data in role_data['elections']: e = election_data['election'] total = election_id_to_candidates.get(e.id, 0) election_counts = { 'id': e.slug, 'html_id': e.slug.replace('.', '-'), 'name': e.name, 'total': total, } if era_data['current']: election_counts['prior_elections'] = [ get_prior_election_data( total, e, election_id_to_candidates.get(pe.id, 0), pe ) for pe in past_elections if pe.for_post_role == e.for_post_role ] election_data.update(election_counts) del election_data['election'] return grouped_elections
def get_counts(): election_id_to_candidates = { d['extra__election']: d['count'] for d in Membership.objects \ .filter(role=F('extra__election__candidate_membership_role')) \ .values('extra__election') \ .annotate(count=Count('extra__election')) } grouped_elections = Election.group_and_order_elections() past_elections = [ election_data['election'] for era_data in grouped_elections for role_data in era_data['roles'] for election_data in role_data['elections'] if not era_data['current'] ] for era_data in grouped_elections: for role_data in era_data['roles']: for election_data in role_data['elections']: e = election_data['election'] total = election_id_to_candidates.get(e.id, 0) election_counts = { 'id': e.slug, 'html_id': e.slug.replace('.', '-'), 'name': e.name, 'total': total, } if era_data['current']: election_counts['prior_elections'] = [ get_prior_election_data( total, e, election_id_to_candidates.get(pe.id, 0), pe ) for pe in past_elections if pe.for_post_role == e.for_post_role ] election_data.update(election_counts) del election_data['election'] return grouped_elections
def test_election_grouping(self): with self.assertNumQueries(1): self.assertEqual( Election.group_and_order_elections(), [ { 'current': True, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ {'election': self.election}, ] }, { 'role': 'Member of the Scottish Parliament', 'elections': [ {'election': self.sp_c_election}, {'election': self.sp_r_election}, ] } ] }, { 'current': False, 'roles': [ { 'role': 'Member of Parliament', 'elections': [ {'election': self.earlier_election}, ] } ] }, ] )
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["elections_and_posts"] = Election.group_and_order_elections( include_postextraelections=True, include_noncurrent=False) return context
def test_election_grouping(self): with self.assertNumQueries(1): self.assertEqual( Election.group_and_order_elections(), [ { "current_or_future": True, "dates": OrderedDict([ ( datetime.date(2016, 5, 5), [ { "role": "Local Councillor", "elections": [{ "election": self.local_election }], }, { "role": "Member of the Scottish Parliament", "elections": [ { "election": self.sp_c_election }, { "election": self.sp_r_election }, ], }, ], ), ( self.election.election_date, [{ "role": "Member of Parliament", "elections": [{ "election": self.election }], }], ), ]), }, { "current_or_future": False, "dates": OrderedDict([( self.earlier_election.election_date, [{ "role": "Member of Parliament", "elections": [{ "election": self.earlier_election }], }], )]), }, ], )
def test_election_grouping_with_posts(self): camberwell_ballot = get_election_extra(self.camberwell_post, self.election) dulwich_ballot = get_election_extra(self.dulwich_post, self.election) edinburgh_east_ballot = get_election_extra(self.edinburgh_east_post, self.election) edinburgh_north_ballot = get_election_extra(self.edinburgh_north_post, self.election) camberwell_earlier = get_election_extra(self.camberwell_post, self.earlier_election) dulwich_earlier = get_election_extra(self.dulwich_post, self.earlier_election) edinburgh_east_earlier = get_election_extra(self.edinburgh_east_post, self.earlier_election) edinburgh_north_earlier = get_election_extra(self.edinburgh_north_post, self.earlier_election) local_council_ballot = get_election_extra(self.local_post, self.local_election) with self.assertNumQueries(3): self.assertEqual( Election.group_and_order_elections(include_ballots=True), [ { "current_or_future": True, "dates": OrderedDict([ ( datetime.date(2016, 5, 5), [ { "role": "Local Councillor", "elections": [{ "ballots": [local_council_ballot], "election": self.local_election, }], }, { "role": "Member of the Scottish Parliament", "elections": [ { "ballots": [], "election": self.sp_c_election, }, { "ballots": [], "election": self.sp_r_election, }, ], }, ], ), ( self.election.election_date, [{ "role": "Member of Parliament", "elections": [{ "ballots": [ camberwell_ballot, dulwich_ballot, edinburgh_east_ballot, edinburgh_north_ballot, ], "election": self.election, }], }], ), ]), }, { "current_or_future": False, "dates": OrderedDict([( self.earlier_election.election_date, [{ "role": "Member of Parliament", "elections": [{ "ballots": [ camberwell_earlier, dulwich_earlier, edinburgh_east_earlier, edinburgh_north_earlier, ], "election": self.earlier_election, }], }], )]), }, ], )
def get_context_data(self, **kwargs): context = super(PostListView, self).get_context_data(**kwargs) context['elections_and_posts'] = \ Election.group_and_order_elections(include_posts=True) return context
def test_election_grouping_with_posts(self): camberwell_postextraelection = get_election_extra( self.camberwell_post_extra, self.election ) dulwich_postextraelection = get_election_extra( self.dulwich_post_extra, self.election ) edinburgh_east_postextraelection = get_election_extra( self.edinburgh_east_post_extra, self.election ) edinburgh_north_postextraelection = get_election_extra( self.edinburgh_north_post_extra, self.election ) camberwell_earlier = get_election_extra( self.camberwell_post_extra, self.earlier_election ) dulwich_earlier = get_election_extra( self.dulwich_post_extra, self.earlier_election ) edinburgh_east_earlier = get_election_extra( self.edinburgh_east_post_extra, self.earlier_election ) edinburgh_north_earlier = get_election_extra( self.edinburgh_north_post_extra, self.earlier_election ) local_council_pee = get_election_extra( self.local_post, self.local_election ) with self.assertNumQueries(4): self.assertEqual( Election.group_and_order_elections(include_postextraelections=True), [ { 'current': True, 'dates': OrderedDict([ (datetime.date(2016, 5, 5), [{ 'role': 'Local Councillor', 'elections': [ { 'postextraelections': [ local_council_pee ], 'election': self.local_election, } ] }, { 'role': 'Member of the Scottish Parliament', 'elections': [ { 'postextraelections': [], 'election': self.sp_c_election }, { 'postextraelections': [], 'election': self.sp_r_election } ] }]), (self.election.election_date, [{ 'role': 'Member of Parliament', 'elections': [ { 'postextraelections': [ camberwell_postextraelection, dulwich_postextraelection, edinburgh_east_postextraelection, edinburgh_north_postextraelection, ], 'election': self.election } ] }]) ]) }, { 'current': False, 'dates': OrderedDict([ (self.earlier_election.election_date, [{ 'role': 'Member of Parliament', 'elections': [ { 'postextraelections': [ camberwell_earlier, dulwich_earlier, edinburgh_east_earlier, edinburgh_north_earlier, ], 'election': self.earlier_election } ] }]) ]) } ] )
def get_context_data(self, **kwargs): context = super(PostListView, self).get_context_data(**kwargs) context['elections_and_posts'] = \ Election.group_and_order_elections( include_posts=True, include_noncurrent=False) return context