def test_csv_output(self): tessa_image_url = self.gb_person.primary_image.url d = { "election_date": date_in_near_future, "earlier_election_date": date_in_near_future - timedelta(days=FOUR_YEARS_IN_DAYS), } PersonRedirect.objects.create(old_person_id=12, new_person_id=1953) PersonRedirect.objects.create(old_person_id=56, new_person_id=1953) self.maxDiff = None example_output = ( "id,name,honorific_prefix,honorific_suffix,gender,birth_date,election,party_id,party_name,post_id,post_label,mapit_url,elected,email,twitter_username,facebook_page_url,party_ppc_page_url,facebook_personal_url,homepage_url,wikipedia_url,linkedin_url,image_url,proxy_image_url_template,image_copyright,image_uploading_user,image_uploading_user_notes,twitter_user_id,election_date,election_current,party_lists_in_use,party_list_position,old_person_ids,gss_code,parlparse_id,theyworkforyou_url,party_ec_id,favourite_biscuits,cancelled_poll,wikidata_url\r\n" + "1953,Daith\xed McKay,,,male,,parl.2010-05-06,party:39,Sinn F\xe9in,66135,North Antrim,,,,,,,,,,,,,,,,,{earlier_election_date},False,False,,12;56,,,,PP39,,False,\r\n".format( **d ) + "2009,Tessa Jowell,Ms,DBE,female,,parl.2010-05-06,party:53,Labour Party,65808,Dulwich and West Norwood,,,[email protected],,,,,,,,{image_url},,example-license,john,A photo of Tessa Jowell,,{earlier_election_date},False,False,,,,uk.org.publicwhip/person/10326,http://www.theyworkforyou.com/mp/10326,PP53,,False,\r\n".format( image_url=tessa_image_url, **d ) + "1953,Daith\xed McKay,,,male,,parl.2015-05-07,party:39,Sinn F\xe9in,66135,North Antrim,,,,,,,,,,,,,,,,,{election_date},True,False,,12;56,,,,PP39,,False,\r\n".format( **d ) + "2009,Tessa Jowell,Ms,DBE,female,,parl.2015-05-07,party:53,Labour Party,65913,Camberwell and Peckham,,,[email protected],,,,,,,,{image_url},,example-license,john,A photo of Tessa Jowell,,{election_date},True,False,,,,uk.org.publicwhip/person/10326,http://www.theyworkforyou.com/mp/10326,PP53,,False,\r\n".format( image_url=tessa_image_url, **d ) ) with self.assertNumQueries(5): memberships_dicts, elected = memberships_dicts_for_csv() all_members = [] for slug, members in memberships_dicts.items(): all_members += members self.assertEqual(list_to_csv(all_members), example_output)
def test_as_dict_2010(self): with self.assertNumQueries(5): memberships_dicts, elected = memberships_dicts_for_csv( self.earlier_election.slug) self.assertEqual(len(memberships_dicts[self.earlier_election.slug]), 2) membership_dict = memberships_dicts["parl.2010-05-06"][1] self.assertEqual(len(membership_dict.keys()), 38) self.assertEqual(membership_dict["id"], 2009)
def handle(self, **options): if options["election"]: try: election = Election.objects.get(slug=options["election"]) election_slug = election.slug except Election.DoesNotExist: message = "Couldn't find an election with slug {election_slug}" raise CommandError( message.format(election_slug=options["election"]) ) else: election_slug = None self.options = options self.output_prefix = "candidates" membership_by_election, elected_by_election = memberships_dicts_for_csv( election_slug ) # Write a file per election, optionally adding candidates # We still want a file to exist if there are no candidates yet, # as the files linked to as soon as the election is created election_qs = Election.objects.all() if election_slug: election_qs = election_qs.filter(slug=election_slug) for election in election_qs: safely_write( self.slug_to_file_name(election.slug), membership_by_election.get(election.slug, []), ) # Make a CSV file per election date slugs_by_date = defaultdict(list) for slug in membership_by_election.keys(): slugs_by_date[slug.split(".")[-1]].append(slug) for date, slugs in slugs_by_date.items(): memberships_for_date = [] for slug in slugs: memberships_for_date += membership_by_election[slug] safely_write(self.slug_to_file_name(date), memberships_for_date) # If we're not outputting a single election, output all elections if not election_slug: sorted_elections = sorted( membership_by_election.keys(), key=lambda key: key.split(".")[-1], ) all_memberships = [] all_elected = [] for slug in sorted_elections: all_memberships += membership_by_election[slug] all_elected += elected_by_election[slug] safely_write(self.slug_to_file_name("all"), all_memberships) safely_write(self.slug_to_file_name("elected-all"), all_elected)
def get(self, request, *args, **kwargs): pee = self.get_object() memberships_dict, elected = memberships_dicts_for_csv( election_slug=pee.election.slug, post_slug=pee.post.slug) filename = "{ballot_paper_id}.csv".format( ballot_paper_id=pee.ballot_paper_id) response = HttpResponse(content_type="text/csv") response[ "Content-Disposition"] = 'attachment; filename="%s"' % filename response.write(list_to_csv(memberships_dict[pee.election.slug])) return response