def import_line(self, line):
        if not self.line_has_values(line):
            return
        party = self.get_party_from_line(line)
        ballot = self.get_ballot_from_line(line)
        person = self.get_person_from_line(line, ballot, party)
        if not person:
            return
        try:
            Membership.objects.update_or_create(person=person,
                                                ballot=ballot,
                                                defaults={"party": party})

        except (Membership.DoesNotExist, NotStandingValidationError) as e:
            print("Error creating membership for line {} ({})".format(line, e))
            return
        except Exception as e:
            print(e)
            import ipdb

            ipdb.set_trace()

        bot = CandidateBot(person.pk)
        bot.save(self.get_source_from_line(line),
                 action_type="candidacy-create")

        self.add_contact_details(bot, person, line)
Exemple #2
0
    def test_edit_invalid_value(self):
        # First, make sure we can't do this
        bot = CandidateBot(self.person.pk)
        with self.assertRaises(ValueError):
            bot.edit_field("email", "INVALID")

        # Now, ignore errors and verify nothing changes, but no errors
        # are raised
        bot = CandidateBot(self.person.pk)
        bot.IGNORE_ERRORS = True
        bot.edit_field("email", "INVALID")
        person = bot.save("a source")
        self.assertEqual(person.get_email, None)
Exemple #3
0
    def test_edit_fields(self):
        bot = CandidateBot(self.person.pk)

        edit = {"email": "*****@*****.**"}

        self.assertFalse(LoggedAction.objects.all().exists())
        self.assertEqual(self.person.versions, "[]")
        person = bot.edit_fields(edit, "a source", save=True)
        expected = {
            "biography": "",
            "birth_date": "",
            "blog_url": "",
            "death_date": "",
            "email": "*****@*****.**",
            "extra_fields": {
                "favourite_biscuits": ""
            },
            "facebook_page_url": "",
            "facebook_personal_url": "",
            "gender": "",
            "homepage_url": "",
            "honorific_prefix": "",
            "honorific_suffix": "",
            "id": "2009",
            "instagram_url": "",
            "linkedin_url": "",
            "name": "Tessa Jowell",
            "other_names": [],
            "party_ppc_page_url": "",
            "candidacies": {},
            "twitter_username": "",
            "wikidata_id": "",
            "wikipedia_url": "",
            "youtube_profile": "",
        }
        got = json.loads(person.versions)
        self.assertEqual(got[0]["data"], expected)
        self.assertEqual(got[0]["information_source"], "a source")
        self.assertEqual(got[0]["username"], settings.CANDIDATE_BOT_USERNAME)
        la = LoggedAction.objects.first()
        self.assertEqual(la.user.username, settings.CANDIDATE_BOT_USERNAME)
    def test_edit_fields(self):
        bot = CandidateBot(self.person_extra.base.pk)

        edit = {
            'email': '*****@*****.**'
        }

        self.assertFalse(LoggedAction.objects.all().exists())
        self.assertEqual(self.person_extra.versions, '[]')
        person = bot.edit_fields(edit, 'a source', save=True)
        expected = {
            "twitter_username": "",
            "standing_in": {},
            "name": "Tessa Jowell",
            "honorific_suffix": "",
            "facebook_page_url": "",
            "gender": "",
            "image": None,
            "facebook_personal_url": "",
            "id": "2009",
            "honorific_prefix": "",
            "linkedin_url": "",
            "homepage_url": "",
            "extra_fields": {"favourite_biscuits": ""},
            "wikipedia_url": "",
            "party_memberships": {},
            "birth_date": "",
            "party_ppc_page_url": "",
            "other_names": [],
            "email": "*****@*****.**",
            "biography": ""
        }
        got = json.loads(person.extra.versions)
        self.assertEqual(got[0]['data'], expected)
        self.assertEqual(got[0]['information_source'], "a source")
        self.assertEqual(got[0]['username'], settings.CANDIDATE_BOT_USERNAME)
        la = LoggedAction.objects.first()
        self.assertEqual(la.user.username, settings.CANDIDATE_BOT_USERNAME)
    def get_person_from_line(self, line, ballot, party):
        url = line.get("Existing Candidate Profile URL")
        if url:
            # Someone in the sheet has asserted that this is the same person
            # who are we to doubt them?
            person_id = url.split("/")[4]
            try:
                return Person.objects.get_by_id_with_redirects(person_id)
            except Person.DoesNotExist:
                return None
        else:
            # All we have is a name. We might create a duplicate person,
            # but we absolutely don't want to make a duplicate person on this
            # ballot. We also have to consider that names might change, or be
            # duplicated on a ballot, so we can't just rely on name alone. Use
            # party too, and accept that there is an error margin. We'll mop
            # this up manually, or at least when the SOPNs are out
            name = line["Candidate Name"]
            # First, find all the people standing for this party and ballot:
            memberships_qs = Membership.objects.filter(ballot=ballot,
                                                       party=party)

            exact_name_match = memberships_qs.filter(person__name=name)
            if exact_name_match.exists():
                return exact_name_match.get().person

            other_name_match = memberships_qs.filter(
                person__other_names__name=name)
            if other_name_match.exists():
                return other_name_match.get().person

            # If we get here, it looks like we need to create a new person
            # This can always be undone with a merge later
            new_person = Person.objects.create(name=name)
            bot = CandidateBot(new_person.pk)
            bot.save(self.get_source_from_line(line))
            return new_person
    def handle(self, **options):
        with open(options['filename'], 'r') as fh:
            reader = csv.DictReader(fh)
            for row in reader:
                source = row.get('source', options.get('source'))
                if not row['democlub_id']:
                    continue
                if not source:
                    raise ValueError("A source is required")

                try:
                    bot = CandidateBot(row['democlub_id'])
                    try:
                        bot.add_email(row['email'])
                        bot.save(source)
                    except ValueError:
                        #Email exists, move on
                        pass
                except Person.DoesNotExist:
                    print("Person ID {} not found".format(row['democlub_id']))
 def test_cant_edit_linkedin(self):
     bot = CandidateBot(self.person_extra.base.pk)
     with self.assertRaises(ValueError):
         bot._edit_field("linkedin", "https://linkedin.com/CandidateBot")
 def test_add_email(self):
     bot = CandidateBot(self.person_extra.base.pk)
     bot.add_email("*****@*****.**")
     person = bot.save('a source')
     self.assertEqual(person.email, "*****@*****.**")
 def test_user_smoke_test(self):
     bot = CandidateBot(self.person_extra.base.pk)
     self.assertEqual(bot.user.username, settings.CANDIDATE_BOT_USERNAME)
 def test_add_email(self):
     bot = CandidateBot(self.person.pk)
     bot.add_email("*****@*****.**")
     person = bot.save("a source")
     del person.get_all_idenfitiers
     self.assertEqual(person.get_email, "*****@*****.**")
Exemple #11
0
    def test_update_field(self):
        self.person.tmp_person_identifiers.create(value_type="email",
                                                  value="*****@*****.**")
        self.assertEqual(self.person.get_email, "*****@*****.**")
        bot = CandidateBot(self.person.pk)
        bot.edit_field("email", "*****@*****.**", update=True)
        person = bot.save("a source")
        self.assertEqual(person.get_email, "*****@*****.**")

        # Now test this doesn't work if update==False
        bot = CandidateBot(self.person.pk)
        with self.assertRaises(IntegrityError):
            bot.edit_field("email", "*****@*****.**", update=False)

        # Now test we can ignore the above error if we want to
        bot = CandidateBot(self.person.pk)
        bot.IGNORE_ERRORS = True
        bot.edit_field("email", "*****@*****.**", update=False)
        person = bot.save("a source")
        # The edit failed, but didn't raise an error
        self.assertEqual(person.get_email, "*****@*****.**")