def get_existing_popit_person(vi_person_id):
    from candidates.models import PopItPerson
    from candidates.popit import get_search_url
    # See if this person already exists by searching for the
    # ID they were imported with:
    query_format = \
        'identifiers.identifier:"{id}" AND ' + \
        'identifiers.scheme:"{scheme}"'
    search_url = get_search_url(
        'persons',
        query_format.format(
            id=vi_person_id, scheme='import-id'
        ),
        embed='membership.organization'
    )
    results = requests.get(search_url).json()

    total = results['total']
    if total > 1:
        message = "Multiple matches for CI ID {0}"
        raise Exception(message.format(vi_person_id))
    if total == 0:
        return None
    # Otherwise there was exactly one result:
    return PopItPerson.create_from_dict(results['result'][0])
def get_party_data(party_name):
    from candidates.popit import get_search_url
    # See if this person already exists by searching for the
    # ID they were imported with:
    party_name = party_name.replace('/', '')
    party_name = party_name.decode('utf-8')
    query_format = \
        'name:"{name}"'
    search_url = get_search_url(
        'organizations',
        query_format.format(
            name=party_name
        )
    )
    print(party_name)
    results = requests.get(search_url).json()
    print(results)
    total = results['total']
    if total > 1:
        message = "Multiple matches for party {0}"
        raise Exception(message.format(party_name))
    if total == 0:
        return None
    # Otherwise there was exactly one result:
    return results['result'][0]
Exemplo n.º 3
0
 def _objects_from_popit_search(self):
     self.page = int(self.request.GET.get(self.page_kwarg) or 1)
     url = get_search_url(
         'persons',
         "_missing_:%s AND _exists_:standing_in.2015.post_id" %
             self.get_field(),
         page=self.page,
         per_page=20
     )
     page_result = requests.get(url).json()
     return page_result
Exemplo n.º 4
0
def get_existing_popit_person(vi_person_id):
    # See if this person already exists by searching for the
    # ID they were imported with:
    query_format = \
        'identifiers.identifier:"{id}" AND ' + \
        'identifiers.scheme:"{scheme}"'
    search_url = get_search_url('persons',
                                query_format.format(id=vi_person_id,
                                                    scheme='import-id'),
                                embed='membership.organization')
    results = requests.get(search_url).json()
    total = results['total']
    if total > 1:
        message = "Multiple matches for CI ID {0}"
        raise Exception(message.format(vi_person_id))
    if total == 0:
        return None
    # Otherwise there was exactly one result:
    return PopItPerson.create_from_dict(results['result'][0])
Exemplo n.º 5
0
def get_party_data(party_name):
    from candidates.popit import get_search_url
    # See if this person already exists by searching for the
    # ID they were imported with:
    party_name = party_name.replace('/', '')
    party_name = party_name.decode('utf-8')
    query_format = \
        'name:"{name}"'
    search_url = get_search_url('organizations',
                                query_format.format(name=party_name))
    print(party_name)
    results = requests.get(search_url).json()
    print(results)
    total = results['total']
    if total > 1:
        message = "Multiple matches for party {0}"
        raise Exception(message.format(party_name))
    if total == 0:
        return None
    # Otherwise there was exactly one result:
    return results['result'][0]
    def handle_person(self, ppc_data, image_filename):
        print u"PPC ({party_slug}): {name}".format(**ppc_data).encode('utf-8')
        # Search PopIt for anyone with the same name. (FIXME: we
        # should make this a bit fuzzier when the PopIt API
        # supports that.)
        person_search_url = get_search_url(
            'persons', '"' + ppc_data['name'] + '"'
        )
        r = requests.get(
            person_search_url
        )
        add_new_person = True
        for result in r.json()['result']:
            # FIXME: With the current code, updating Elasticsearch
            # fails because it rejects the empty string as a null date
            # value. To just get this working, GET the person directly
            # so the information's defintely up to date.
            result = self.api.persons(result['id']).get()['result']
            # We don't need past versions of the person or their
            # memberships, and it's unnecessary verbose to record them
            # with decisions.
            del result['versions']
            del result['memberships']
            popit_person_id = result['id']

            message = "  Considering PopIt person search result {0}"
            print message.format(popit_person_id)

            added_for_2015 = self.already_added_for_2015(ppc_data, result)
            if added_for_2015 is None:
                add_new_person = False
                print "    already_added_for_2015 returned None, so ignoring"
                print "    !!! CHECK THIS MANUALLY !!!"
                break
            elif added_for_2015:
                # Then just update that person with the possibly
                # updated scraped data:
                add_new_person = False
                print "    matched a 2015 candidate, so updating that"
                self.update_popit_person(popit_person_id, ppc_data, image_filename)
                break
            else:
                # Do we already have a decision about whether this PPC is
                # the same as another from 2010?
                decision = self.already_matched_to_a_person(ppc_data, popit_person_id)
                if decision is None:
                    print "    no previous decision found, so asking"
                    # We have to ask the user for a decision:
                    if self.decision_from_user(ppc_data, result):
                        print "      updating, and recording the decision that they're a match"
                        add_new_person = False
                        self.update_popit_person(popit_person_id, ppc_data, image_filename)
                        record_human_decision(ppc_data, popit_person_id, True)
                        break
                    else:
                        print "      recording the decision that they're not a match"
                        record_human_decision(ppc_data, popit_person_id, False)
                else:
                    # Otherwise we'd previously decided that they're
                    # the same person, so just update that person.
                    print "    there was a previous decision found"
                    if decision:
                        print "      the previous decision was that they're a match, so updating"
                        add_new_person = False
                        self.update_popit_person(popit_person_id, ppc_data, image_filename)
                        break
        if add_new_person:
            new_person_id = self.add_popit_person(ppc_data, image_filename)
            print "  Added them as a new person ({0})".format(new_person_id)