Пример #1
0
    def test_as_list_single_dict(self):
        PersonRedirect.objects.create(old_person_id=33, new_person_id=2009)
        PersonRedirect.objects.create(old_person_id=44, new_person_id=2009)
        person_extra = PersonExtra.objects \
            .joins_for_csv_output().get(pk=self.gb_person_extra.id)
        # After the select_related and prefetch_related calls
        # PersonExtra there should only be one more query - that to
        # find the complex fields mapping:
        redirects = PersonRedirect.all_redirects_dict()
        with self.assertNumQueries(1):
            person_dict_list = person_extra.as_list_of_dicts(
                self.election, redirects=redirects)
        self.assertEqual(len(person_dict_list), 1)
        person_dict = person_dict_list[0]
        self.assertEqual(len(person_dict), 37)
        self.assertEqual(person_dict['id'], 2009)

        # Test the extra CSV fields:
        self.assertEqual(person_dict['gss_code'], 'E14000615')
        self.assertEqual(person_dict['parlparse_id'],
                         'uk.org.publicwhip/person/10326')
        self.assertEqual(person_dict['theyworkforyou_url'],
                         'http://www.theyworkforyou.com/mp/10326')
        self.assertEqual(person_dict['party_ec_id'], 'PP53')
        self.assertEqual(person_dict['old_person_ids'], '33;44')
Пример #2
0
    def test_as_list_single_dict(self):
        PersonRedirect.objects.create(old_person_id=33, new_person_id=2009)
        PersonRedirect.objects.create(old_person_id=44, new_person_id=2009)
        person = Person.objects.joins_for_csv_output().get(pk=self.gb_person.id)
        # After the select_related and prefetch_related calls
        # PersonExtra there should only be one more query - that to
        # find the complex fields mapping:
        redirects = PersonRedirect.all_redirects_dict()
        with self.assertNumQueries(0):
            person_dict_list = person.as_list_of_dicts(
                self.election, redirects=redirects
            )
        self.assertEqual(len(person_dict_list), 1)
        person_dict = person_dict_list[0]
        self.assertEqual(len(person_dict), 38)
        self.assertEqual(person_dict["id"], 2009)

        # Test the extra CSV fields:
        self.assertEqual(person_dict["gss_code"], "")
        self.assertEqual(
            person_dict["parlparse_id"], "uk.org.publicwhip/person/10326"
        )
        self.assertEqual(
            person_dict["theyworkforyou_url"],
            "http://www.theyworkforyou.com/mp/10326",
        )
        self.assertEqual(person_dict["party_ec_id"], "PP53")
        self.assertEqual(person_dict["old_person_ids"], "33;44")
def memberships_dicts_for_csv(election_slug=None, post_slug=None):
    redirects = PersonRedirect.all_redirects_dict()
    memberships = Membership.objects.for_csv()
    if election_slug:
        memberships = memberships.filter(
            post_election__election__slug=election_slug)
    if post_slug:
        memberships = memberships.filter(post_election__post__slug=post_slug)

    memberships_by_election = defaultdict(list)
    elected_by_election = defaultdict(list)

    for membership in memberships:
        election_slug = membership.post_election.election.slug
        line = membership.dict_for_csv(redirects=redirects)
        memberships_by_election[election_slug].append(line)
        if membership.elected:
            elected_by_election[election_slug].append(line)

    for election_slug, membership_list in memberships_by_election.items():
        sort_memberships(membership_list)
    for election_slug, membership_list in elected_by_election.items():
        sort_memberships(membership_list)

    return (memberships_by_election, elected_by_election)
Пример #4
0
 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\r\n"
         +
         "2009,Tessa Jowell,Ms,DBE,female,,2015,party:53,Labour Party,65913,Camberwell and Peckham,,,[email protected],,,,,,,,{image_url},,example-license,john,A photo of Tessa Jowell,,{election_date},True,False,,,,,,,,\r\n"
         .format(image_url=tessa_image_url, **d) +
         "2009,Tessa Jowell,Ms,DBE,female,,2010,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,,,,,,,,\r\n"
         .format(image_url=tessa_image_url, **d) +
         "1953,Daith\xed McKay,,,male,,2015,party:39,Sinn F\xe9in,66135,North Antrim,,,,,,,,,,,,,,,,,{election_date},True,False,,12;56,,,,,,\r\n"
         .format(**d) +
         "1953,Daith\xed McKay,,,male,,2010,party:39,Sinn F\xe9in,66135,North Antrim,,,,,,,,,,,,,,,,,{earlier_election_date},False,False,,12;56,,,,,,\r\n"
         .format(**d))
     gb_person = get_person_with_joins(self.gb_person.id)
     ni_person = get_person_with_joins(self.ni_person.id)
     # After the select_related and prefetch_related calls on
     # PersonExtra, there should only be one query per PersonExtra:
     redirects = PersonRedirect.all_redirects_dict()
     with self.assertNumQueries(0):
         list_of_dicts = gb_person.as_list_of_dicts(None,
                                                    redirects=redirects)
         list_of_dicts += ni_person.as_list_of_dicts(None,
                                                     redirects=redirects)
     self.assertEqual(list_to_csv(list_of_dicts), example_output)
Пример #5
0
    def handle(self, **options):
        if options["election"]:
            try:
                all_elections = [
                    Election.objects.get(slug=options["election"])
                ]
            except Election.DoesNotExist:
                message = "Couldn't find an election with slug {election_slug}"
                raise CommandError(
                    message.format(election_slug=options["election"]))
        else:
            all_elections = list(Election.objects.all()) + [None]

        self.options = options
        self.complex_popolo_fields = get_complex_popolo_fields()
        self.redirects = PersonRedirect.all_redirects_dict()
        self.output_prefix = "candidates"

        for election in all_elections:
            if election is None:
                # Get information for every candidate in every
                # election.
                qs = PersonExtra.objects.all()
                all_people, elected_people = self.get_people(election, qs)
                output_filenames = {
                    "all": self.output_prefix + "-all.csv",
                    "elected": self.output_prefix + "-elected-all.csv",
                }
            else:
                # Only get the candidates standing in that particular
                # election
                role = election.candidate_membership_role
                qs = PersonExtra.objects.filter(
                    base__memberships__post_election__election=election,
                    base__memberships__role=role,
                )
                all_people, elected_people = self.get_people(election, qs)
                output_filenames = {
                    "all":
                    self.output_prefix + "-" + election.slug + ".csv",
                    "elected":
                    self.output_prefix + "-elected-" + election.slug + ".csv",
                }
            group_by_post = election is not None
            safely_write(output_filenames["all"], all_people, group_by_post)
            safely_write(output_filenames["elected"], elected_people,
                         group_by_post)