Ejemplo n.º 1
0
def update_picture_for_candidate(candidate_data, cache_directory, **options):
    picture_intro = 'Picture from the IEBC API for candidate'
    candidate_code = candidate_data['code']
    filename = os.path.join(cache_directory,
                            "candidate-%s.jpg" % (candidate_code, ))
    if not os.path.exists(filename):
        image_url = candidate_data['picture']
        r = requests.get(image_url)
        if r.status_code == 200:
            with open(filename, 'w') as fp:
                fp.write(r.content)
    # If that image now exists, use it:
    if os.path.exists(filename):
        # Find the position from the candidate code, so we can get the right person:
        positions = Position.objects.filter(
            external_id=candidate_code).currently_active()
        if not positions:
            print "#### Missing position for:", candidate_code
        elif len(positions) > 1:
            print "#### Multiple positions for:", candidate_code
        else:
            person = positions[0].person
            if options['commit']:
                # Remove old IEBC images for that person:
                person.images.filter(source__startswith=picture_intro).delete()
                # And now create the new one:
                new_image = Image(content_object=person,
                                  source="%s %s" %
                                  (picture_intro, candidate_code))
                with open(filename) as fp:
                    new_image.image.save(
                        name="%s-%s.jpg" %
                        (candidate_code, file_mtime_iso8601(filename)),
                        content=ContentFile(fp.read()))
Ejemplo n.º 2
0
        continue

    try:
        person = models.Person.objects.get(original_id=member_id)
    except models.Person.DoesNotExist:
        print "Could not find %s - ignoring" % person
        continue

    url = 'http://mzalendo.com/Images/%s' % image_link


    source_string = "Original Mzalendo.com website (%s)" % image_link

    # check to see if this photo has already been used
    if Image.objects.filter(source=source_string).count():
        print "Skipping %s - image already used" % person
        continue

    print "Fetching image for '%s': '%s'" % ( person, url )
    person_image = Image(
        content_object = person,
        source = source_string,
    )
    person_image.image.save(
        name    = image_link,
        content = ContentFile( urllib.urlopen( url ).read() ),
    )

    # break
    time.sleep(2)
Ejemplo n.º 3
0
    def setUp(self):
        self.maxDiff = None

        self.person = models.Person.objects.create(
            legal_name='Test Person',
            slug='test-person',
            date_of_birth='1970-01-01',
        )
        self.contact_kind = models.ContactKind.objects.create(
            name="Email Address",
            slug="email",
        )
        self.email_contact = models.Contact.objects.create(
            kind=self.contact_kind,
            value="*****@*****.**",
            content_type=ContentType.objects.get_for_model(models.Person),
            object_id=self.person.id,
            note='Found on the parliament website',
        )
        self.missing_email_contact = models.Contact.objects.create(
            kind=self.contact_kind,
            value='',
            content_type=ContentType.objects.get_for_model(models.Person),
            object_id=self.person.id)
        self.person.add_alternative_name('Test H Person',
                                         name_to_use=True,
                                         note='Used in a newspapers')
        self.person_id = models.Identifier.objects.create(
            identifier="/person/someone",
            scheme="some.schema",
            object_id=self.person.id,
            content_type=ContentType.objects.get_for_model(models.Person))
        self.person_image = Image(
            content_object=self.person,
            source='Not a real image, so no source...',
        )
        self.person_image.image.save(
            name="some-image",
            content=ContentFile(''),
        )

        self.organisation_kind = models.OrganisationKind.objects.create(
            name='Example Org Kind',
            slug='example-org-kind',
        )
        self.organisation = models.Organisation.objects.create(
            name='Test Organisation',
            slug='test-organisation',
            kind=self.organisation_kind,
            started=ApproximateDate(2009),
            ended=ApproximateDate(2011, 3, 20),
        )
        self.position_title = models.PositionTitle.objects.create(
            name="Knight of the Realm",
            slug="knight",
        )
        self.position = models.Position.objects.create(
            person=self.person,
            organisation=self.organisation,
            end_date=ApproximateDate(2013, 06),
            title=self.position_title,
        )

        self.expected_memberships = [{
            "end_date": "2013-06",
            "identifiers": [],
            "organization_id": "core_organisation:{organization_id}",
            "role": u"Knight of the Realm",
            "person_id": "core_person:{person_id}",
            "id": "core_position:{position_id}"
        }]

        self.expected_persons = [{
            "contact_details": [{
                "note": u"Found on the parliament website",
                "type": u"email",
                "value": u"*****@*****.**"
            }],
            "name":
            u"Test Person",
            "identifiers": [{
                "scheme": u"some.schema",
                "identifier": u"/person/someone"
            }],
            "other_names": [{
                "note": u"Used in a newspapers",
                "name": u"Test H Person"
            }],
            "sort_name":
            u"Person",
            "images": [{
                "url":
                "http://pombola.example.org/media_root/{image_name}"
            }],
            "birth_date":
            "1970-01-01",
            "id":
            "core_person:{person_id}"
        }]

        self.expected_organizations = [{
            "category": u"other",
            "dissolution_date": "2011-03-20",
            "founding_date": "2009",
            "contact_details": [],
            "name": u"Test Organisation",
            "classification": u"Example Org Kind",
            "identifiers": [],
            "id": "core_organisation:{organization_id}",
            "slug": u"test-organisation"
        }]