django.utils.six.moves.urllib_parse.urlsplit

Here are the examples of the python api django.utils.six.moves.urllib_parse.urlsplit taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

58 Examples 7

Example 1

Project: yournextrepresentative
Source File: tests.py
View license
    @patch.object(pygeocoder.Geocoder, 'geocode', side_effect=fake_geocode)
    @patch('elections.st_paul_municipal_2015.views.frontpage.requests')
    def test_front_page_good_address_lookup(self, patched_requests, patched_geocode):
        patched_requests.get.side_effect = fake_represent_boundaries
        response = self.app.get('/')
        form = response.forms['form-address']
        form['address'] = '631 Snelling Ave S, Saint Paul, MN'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/ocd-division,country:us,state:mn,place:st_paul;ocd-division,country:us,state:mn,place:st_paul,ward:3',
        )

Example 2

View license
    def test_fields_are_saved_when_creating(self):
        response = self.app.get(
            '/election/2015/person/create/',
            user=self.user
        )
        form = response.forms['new-candidate-form']
        form['name'] = 'Naomi Newperson'
        form['additional_link'] = 'http://example.com/morelink'
        form['standing_2015'] = 'not-standing'
        form['source'] = 'Test creating someone with simple fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        m = re.search(r'^/person/(.*)', split_location.path)
        self.assertTrue(m)

        person = Person.objects.get(id=m.group(1))
        self.assertEqual(person.extra.additional_link, 'http://example.com/morelink')

Example 3

View license
    def test_fields_are_saved_when_editing(self):
        response = self.app.get(
            '/person/{person_id}/update'.format(person_id=self.person.id),
            user=self.user,
        )
        form = response.forms['person-details']
        form['additional_link'] = 'http://example.com/anotherlink'
        form['source'] = 'Testing setting complex fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/{person_id}'.format(person_id=self.person.id),
            split_location.path
        )

        person = Person.objects.get(id=self.person.id)
        self.assertEqual(person.extra.additional_link, 'http://example.com/anotherlink')

Example 4

View license
    def test_update_dd_mm_yyyy_birth_date(self):
        response = self.app.get(
            '/person/2009/update',
            user=self.user_who_can_lock,
        )
        form = response.forms['person-details']
        form['birth_date'] = '1/4/1875'
        form['source'] = "An update for testing purposes"
        response = form.submit()

        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/person/2009', split_location.path)

        person = Person.objects.get(id='2009')
        self.assertEqual(person.birth_date, '1875-04-01')

Example 5

View license
    def test_fields_are_saved_when_editing(self):
        response = self.app.get(
            '/person/{person_id}/update'.format(person_id=self.person.id),
            user=self.user,
        )
        form = response.forms['person-details']
        form['additional_link'] = 'http://example.com/anotherlink'
        form['source'] = 'Testing setting complex fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/{person_id}'.format(person_id=self.person.id),
            split_location.path
        )

        person = Person.objects.get(id=self.person.id)
        self.assertEqual(person.extra.additional_link, 'http://example.com/anotherlink')

Example 6

View license
    def test_new_person_submission_refused_copyright(self):
        response = self.app.get(
            '/constituency/65808/dulwich-and-west-norwood',
            user=self.user_refused,
        )
        split_location = urlsplit(response.location)
        self.assertEqual(
            '/copyright-question',
            split_location.path
        )
        self.assertEqual(
            'next=/constituency/65808/dulwich-and-west-norwood',
            split_location.query
        )

Example 7

View license
    def test_photo_review_queue_view_not_logged_in(self):
        queue_url = reverse('photo-review-list')
        response = self.app.get(queue_url)
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/accounts/login/', split_location.path)
        self.assertEqual('next=/moderation/photo/review', split_location.query)

Example 8

Project: yournextrepresentative
Source File: tests.py
View license
    @patch.object(pygeocoder.Geocoder, 'geocode', side_effect=fake_geocode)
    @patch('elections.st_paul_municipal_2015.views.frontpage.requests')
    def test_front_page_good_address_outside(self, patched_requests, patched_geocode):
        patched_requests.get.side_effect = fake_represent_boundaries
        response = self.app.get('/')
        form = response.forms['form-address']
        form['address'] = 'Empire State Building, New York'
        response = form.submit()
        # FIXME: I'm not sure this is what's really intended, since it
        # would be better to stay on the front page with a validation
        # error saying the area is outside the twin cities, but this
        # is what the current lookup code from Datamade does:
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/ocd-division,country:us,state:ny,place:new_york,council_district:4',
        )

Example 9

View license
    def test_valid_postcode_redirects_to_constituency(self, mock_requests):
        mock_requests.get.side_effect = fake_requests_for_mapit
        response = self.app.get('/')
        form = response.forms['form-postcode']
        form['postcode'] = 'SE24 0AG'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/WMC-gss:E14000673',
        )

Example 10

View license
    def test_photo_review_queue_view_not_logged_in(self):
        queue_url = reverse('photo-review-list')
        response = self.app.get(queue_url)
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/accounts/login/', split_location.path)
        self.assertEqual('next=/moderation/photo/review', split_location.query)

Example 11

View license
    def test_update_person_submission_copyright_refused(self):
        response = self.app.get('/person/2009/update', user=self.user)
        form = response.forms['person-details']
        form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell'
        form['party_gb_2015'] = self.labour_party_extra.base_id
        form['source'] = "Some source of this information"
        submission_response = form.submit(user=self.user_refused)
        split_location = urlsplit(submission_response.location)
        self.assertEqual('/copyright-question', split_location.path)
        self.assertEqual('next=/person/2009/update', split_location.query)

Example 12

View license
    def test_fields_are_saved_when_editing(self):
        response = self.app.get(
            '/person/{person_id}/update'.format(person_id=self.person.id),
            user=self.user,
        )
        form = response.forms['person-details']
        form['additional_name'] = 'An extra name'
        form['source'] = 'Testing setting simple fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/{person_id}'.format(person_id=self.person.id),
            split_location.path
        )

        person = Person.objects.get(id=self.person.id)
        self.assertEqual(person.additional_name, 'An extra name')

Example 13

View license
    def test_update_person_view_get_refused_copyright(self):
        response = self.app.get('/person/2009/update', user=self.user_refused)
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/copyright-question', split_location.path)
        self.assertEqual('next=/person/2009/update', split_location.query)

Example 14

View license
    def test_update_person_submission_copyright_refused(self):
        response = self.app.get('/person/2009/update', user=self.user)
        form = response.forms['person-details']
        form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell'
        form['party_gb_2015'] = self.labour_party_extra.base_id
        form['source'] = "Some source of this information"
        submission_response = form.submit(user=self.user_refused)
        split_location = urlsplit(submission_response.location)
        self.assertEqual('/copyright-question', split_location.path)
        self.assertEqual('next=/person/2009/update', split_location.query)

Example 15

View license
    def test_update_person_view_get_without_login(self):
        response = self.app.get('/person/2009/update')
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/accounts/login/', split_location.path)
        self.assertEqual('next=/person/2009/update', split_location.query)

Example 16

View license
    def test_update_dd_mm_yyyy_birth_date(self):
        response = self.app.get(
            '/person/2009/update',
            user=self.user_who_can_lock,
        )
        form = response.forms['person-details']
        form['birth_date'] = '1/4/1875'
        form['source'] = "An update for testing purposes"
        response = form.submit()

        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/person/2009', split_location.path)

        person = Person.objects.get(id='2009')
        self.assertEqual(person.birth_date, '1875-04-01')

Example 17

View license
    def test_valid_postcode_redirects_to_constituency(self, mock_requests):
        mock_requests.get.side_effect = fake_requests_for_mapit
        response = self.app.get('/')
        form = response.forms['form-postcode']
        form['postcode'] = 'SE24 0AG'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/WMC-gss:E14000673',
        )

Example 18

View license
    def handle(self, **options):
        with transaction.atomic():
            split_url = urlsplit(options['SITE-URL'])
            if (split_url.path not in ('', '/') \
                or split_url.query
                or split_url.fragment):
                raise CommandError('You must only supply the base URL of the site')
            # Then form the base API URL:
            new_url_parts = list(split_url)
            new_url_parts[2] = ''
            self.base_url = urlunsplit(new_url_parts)
            new_url_parts[2] = '/api/v0.9/'
            self.base_api_url = urlunsplit(new_url_parts)
            self.check_database_is_empty()
            self.remove_field_objects()
            self.mirror_from_api()

Example 19

View license
    def test_update_person_view_get_refused_copyright(self):
        response = self.app.get('/person/2009/update', user=self.user_refused)
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/copyright-question', split_location.path)
        self.assertEqual('next=/person/2009/update', split_location.query)

Example 20

Project: yournextrepresentative
Source File: tests.py
View license
    @patch.object(pygeocoder.Geocoder, 'geocode', side_effect=fake_geocode)
    @patch('elections.st_paul_municipal_2015.views.frontpage.requests')
    def test_front_page_good_address_outside(self, patched_requests, patched_geocode):
        patched_requests.get.side_effect = fake_represent_boundaries
        response = self.app.get('/')
        form = response.forms['form-address']
        form['address'] = 'Empire State Building, New York'
        response = form.submit()
        # FIXME: I'm not sure this is what's really intended, since it
        # would be better to stay on the front page with a validation
        # error saying the area is outside the twin cities, but this
        # is what the current lookup code from Datamade does:
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/ocd-division,country:us,state:ny,place:new_york,council_district:4',
        )

Example 21

View license
    def test_new_person_submission_refused_copyright(self):
        response = self.app.get(
            '/constituency/65808/dulwich-and-west-norwood',
            user=self.user_refused,
        )
        split_location = urlsplit(response.location)
        self.assertEqual(
            '/copyright-question',
            split_location.path
        )
        self.assertEqual(
            'next=/constituency/65808/dulwich-and-west-norwood',
            split_location.query
        )

Example 22

View license
    def test_new_person_submission_refused_copyright(self):
        # Just a smoke test for the moment:
        response = self.app.get(
            '/constituency/65808/dulwich-and-west-norwood',
            user=self.user_refused
        )
        split_location = urlsplit(response.location)
        self.assertEqual(
            '/copyright-question',
            split_location.path
        )
        self.assertEqual(
            'next=/constituency/65808/dulwich-and-west-norwood',
            split_location.query
        )

Example 23

Project: yournextrepresentative
Source File: tests.py
View license
    @patch.object(pygeocoder.Geocoder, 'geocode', side_effect=fake_geocode)
    @patch('elections.st_paul_municipal_2015.views.frontpage.requests')
    def test_front_page_good_address_lookup(self, patched_requests, patched_geocode):
        patched_requests.get.side_effect = fake_represent_boundaries
        response = self.app.get('/')
        form = response.forms['form-address']
        form['address'] = '631 Snelling Ave S, Saint Paul, MN'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/ocd-division,country:us,state:mn,place:st_paul;ocd-division,country:us,state:mn,place:st_paul,ward:3',
        )

Example 24

View license
    def test_new_person_submission_refused_copyright(self):
        # Just a smoke test for the moment:
        response = self.app.get(
            '/constituency/65808/dulwich-and-west-norwood',
            user=self.user_refused
        )
        split_location = urlsplit(response.location)
        self.assertEqual(
            '/copyright-question',
            split_location.path
        )
        self.assertEqual(
            'next=/constituency/65808/dulwich-and-west-norwood',
            split_location.query
        )

Example 25

View license
    def test_fields_are_saved_when_editing(self):
        response = self.app.get(
            '/person/{person_id}/update'.format(person_id=self.person.id),
            user=self.user,
        )
        form = response.forms['person-details']
        form['additional_name'] = 'An extra name'
        form['source'] = 'Testing setting simple fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/{person_id}'.format(person_id=self.person.id),
            split_location.path
        )

        person = Person.objects.get(id=self.person.id)
        self.assertEqual(person.additional_name, 'An extra name')

Example 26

View license
    def handle(self, **options):
        with transaction.atomic():
            split_url = urlsplit(options['SITE-URL'])
            if (split_url.path not in ('', '/') \
                or split_url.query
                or split_url.fragment):
                raise CommandError('You must only supply the base URL of the site')
            # Then form the base API URL:
            new_url_parts = list(split_url)
            new_url_parts[2] = ''
            self.base_url = urlunsplit(new_url_parts)
            new_url_parts[2] = '/api/v0.9/'
            self.base_api_url = urlunsplit(new_url_parts)
            self.check_database_is_empty()
            self.remove_field_objects()
            self.mirror_from_api()

Example 27

View license
    def test_fields_are_saved_when_creating(self):
        response = self.app.get(
            '/election/2015/person/create/',
            user=self.user
        )
        form = response.forms['new-candidate-form']
        form['name'] = 'Naomi Newperson'
        form['additional_name'] = 'Naomi Newcomer'
        form['standing_2015'] = 'not-standing'
        form['source'] = 'Test creating someone with simple fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        m = re.search(r'^/person/(.*)', split_location.path)
        self.assertTrue(m)

        person = Person.objects.get(id=m.group(1))
        self.assertEqual(person.additional_name, 'Naomi Newcomer')

Example 28

View license
    def test_fields_are_saved_when_creating(self):
        response = self.app.get(
            '/election/2015/person/create/',
            user=self.user
        )
        form = response.forms['new-candidate-form']
        form['name'] = 'Naomi Newperson'
        form['additional_link'] = 'http://example.com/morelink'
        form['standing_2015'] = 'not-standing'
        form['source'] = 'Test creating someone with simple fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        m = re.search(r'^/person/(.*)', split_location.path)
        self.assertTrue(m)

        person = Person.objects.get(id=m.group(1))
        self.assertEqual(person.extra.additional_link, 'http://example.com/morelink')

Example 29

View license
    def test_fields_are_saved_when_creating(self):
        response = self.app.get(
            '/election/2015/person/create/',
            user=self.user
        )
        form = response.forms['new-candidate-form']
        form['name'] = 'Naomi Newperson'
        form['additional_name'] = 'Naomi Newcomer'
        form['standing_2015'] = 'not-standing'
        form['source'] = 'Test creating someone with simple fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        m = re.search(r'^/person/(.*)', split_location.path)
        self.assertTrue(m)

        person = Person.objects.get(id=m.group(1))
        self.assertEqual(person.additional_name, 'Naomi Newcomer')

Example 30

View license
    def test_update_person_view_get_without_login(self):
        response = self.app.get('/person/2009/update')
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/accounts/login/', split_location.path)
        self.assertEqual('next=/person/2009/update', split_location.query)

Example 31

View license
    def test_update_person_submission(self):
        response = self.app.get(
            '/person/2009/update',
            user=self.user_who_can_lock,
        )
        form = response.forms['person-details']
        form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell'
        form['party_gb_2015'] = self.labour_party_extra.base_id
        form['source'] = "Some source of this information"
        submission_response = form.submit()

        person = Person.objects.get(id='2009')
        party = person.memberships.filter(role='Candidate')

        self.assertEqual(party.count(), 1)
        self.assertEqual(party[0].on_behalf_of.extra.slug, 'party:53')

        links = person.links.all()
        self.assertEqual(links.count(), 1)
        self.assertEqual(links[0].url, 'http://en.wikipedia.org/wiki/Tessa_Jowell')

        # It should redirect back to the same person's page:
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/2009',
            split_location.path
        )

Example 32

View license
    def test_update_person_extra_fields(self):
        ExtraField.objects.create(
            type='url',
            key='cv',
            label='CV or Resumé',
        )
        ExtraField.objects.create(
            type='longer-text',
            key='notes',
            label='Notes',
        )
        response = self.app.get(
            '/person/2009/update',
            user=self.user_who_can_lock,
        )
        form = response.forms['person-details']
        form['birth_date'] = '1/4/1875'
        form['source'] = "An update for testing purposes"
        form['cv'] = 'http://example.org/cv.pdf'
        response = form.submit()

        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/person/2009', split_location.path)

        person = Person.objects.get(id='2009')
        self.assertEqual(person.birth_date, '1875-04-01')
        versions_data = json.loads(person.extra.versions)
        self.assertEqual(
            versions_data[0]['data']['extra_fields'],
            {
                'cv': 'http://example.org/cv.pdf',
                'notes': '',
            }
        )

Example 33

View license
    def handle_inner(self, *args, **options):
        post_label_format = options['post_label']

        mapit_url = options['MAPIT-URL']
        split_mapit_url = urlsplit(mapit_url)
        if any([split_mapit_url.path not in ('', '/'),
                split_mapit_url.query,
                split_mapit_url.fragment]):
            raise CommandError("MAPIT-URL must only be the base URL, "
                "with no path, query or fragment")
        area_type = options['AREA-TYPE']
        post_id_format = options['POST-ID-FORMAT']
        manual_area_ids = []
        if options['area_ids']:
            try:
                manual_area_ids = [
                    int(n, 10) for n in options['area_ids'].split(',')
                ]
            except ValueError:
                raise Command("The --area-ids option must be comma separated numeric IDs")
        enclosing_area_id = options['enclosing_area_id']
        if enclosing_area_id:
            # Just check that area actually exists:
            enclosing_area_url = urljoin(
                mapit_url,
                '/area/{0}'.format(enclosing_area_id))
            r = requests.get(enclosing_area_url)
            if r.status_code == 404:
                msg = "The enclosing area {0} doesn't exist"
                raise CommandError(msg.format(enclosing_area_url))

        party_set, created = PartySet.objects.get_or_create(
            slug=slugify(options['party_set']),
            defaults={'name': options['party_set']}
        )

        elections = Election.objects.all()

        if elections.count() == 0:
            raise CommandError("There must be at least one election")

        for election in elections:
            if manual_area_ids:
                all_areas_url = urljoin(mapit_url, '/areas/' + ','.join(
                    text_type(a_id) for a_id in manual_area_ids
                ))
            else:
                query = {}
                if enclosing_area_id:
                    all_areas_url = enclosing_area_url + '/covers?type=' + area_type
                    query['type'] = area_type
                else:
                    all_areas_url = urljoin(mapit_url, '/areas/{0}'.format(area_type))
                if election.area_generation:
                    query['generation'] = election.area_generation
                query_string = '&'.join('{0}={1}'.format(k, v) for k, v in query.items())
                if query_string:
                    all_areas_url += '?' + query_string

            mapit_result = requests.get(all_areas_url)
            mapit_json = mapit_result.json()

            if 'error' in mapit_json:
                raise Command("Fetching the areas failed: {0}".format(
                    mapit_json['error']))

            for_post_role = election.for_post_role
            org = election.organization

            if org is None:
                raise CommandError("Election {0} requires an organization".format(election.slug))

            for item in mapit_json.items():
                area_json = item[1]

                area_url = urljoin(mapit_url, '/area/' + text_type(area_json['id']))

                area, area_created = Area.objects.get_or_create(
                    name=area_json['name'],
                    identifier=area_url,
                    classification=area_json['type_name']
                )

                area_type, created = AreaType.objects.get_or_create(
                    name=area_json['type'],
                    source='MapIt'
                )

                if area_created:
                    area_extra, area_extra_created = AreaExtra.objects.get_or_create(
                        base=area
                    )

                if area_created and area_extra_created:
                    area_extra.type = area_type
                    area_extra.save()

                post_id = post_id_format.format(area_id=area_json['id'])
                post_name = post_label_format.format(
                    area_name=area_json['name'],
                    post_role=for_post_role
                )

                post, created = Post.objects.get_or_create(
                    label=post_name,
                    area=area,
                    organization=org
                )

                post_extra, created = PostExtra.objects.get_or_create(
                    base=post,
                    slug=post_id,
                    defaults={'party_set': party_set},
                )

                PostExtraElection.objects.get_or_create(
                    postextra=post_extra,
                    election=election,
                )

Example 34

View license
    def test_copyright_assigned(self):
        response = self.app.get(
            '/constituency/65808/dulwich-and-west-norwood',
            user=self.user_refused,
            auto_follow=True
        )

        form = response.forms['copyright_assignment']
        form['assigned_to_dc'] = True
        form_response = form.submit()

        split_location = urlsplit(form_response.location)
        self.assertEqual(
            '/constituency/65808/dulwich-and-west-norwood',
            split_location.path
        )

        agreement = self.user_refused.terms_agreement
        agreement.refresh_from_db()
        self.assertTrue(agreement.assigned_to_dc)

Example 35

View license
    def test_update_person_submission(self):
        response = self.app.get(
            '/person/2009/update',
            user=self.user_who_can_lock,
        )
        form = response.forms['person-details']
        form['wikipedia_url'] = 'http://en.wikipedia.org/wiki/Tessa_Jowell'
        form['party_gb_2015'] = self.labour_party_extra.base_id
        form['source'] = "Some source of this information"
        submission_response = form.submit()

        person = Person.objects.get(id='2009')
        party = person.memberships.filter(role='Candidate')

        self.assertEqual(party.count(), 1)
        self.assertEqual(party[0].on_behalf_of.extra.slug, 'party:53')

        links = person.links.all()
        self.assertEqual(links.count(), 1)
        self.assertEqual(links[0].url, 'http://en.wikipedia.org/wiki/Tessa_Jowell')

        # It should redirect back to the same person's page:
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/2009',
            split_location.path
        )

Example 36

View license
    def test_valid_postcode_redirects_to_multiple_areas(self, mock_requests):
        mock_requests.get.side_effect = fake_requests_for_mapit
        # Create some extra posts and areas:
        london_assembly = ParliamentaryChamberExtraFactory.create(
            slug='london-assembly', base__name='London Assembly'
        )
        lac_area_type = AreaTypeFactory.create(name='LAC')
        gla_area_type = AreaTypeFactory.create(name='GLA')
        area_extra_lac = AreaExtraFactory.create(
            base__identifier='gss:E32000010',
            base__name="Dulwich and West Norwood",
            type=lac_area_type,
        )
        area_extra_gla = AreaExtraFactory.create(
            base__identifier='unit_id:41441',
            base__name='Greater London Authority',
            type=gla_area_type,
        )
        election_lac = ElectionFactory.create(
            slug='gb-gla-2016-05-05-c',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Constituencies)',
            area_types=(lac_area_type,),
        )
        election_gla = ElectionFactory.create(
            slug='gb-gla-2016-05-05-a',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Additional)',
            area_types=(gla_area_type,),
        )
        PostExtraFactory.create(
            elections=(election_lac,),
            base__area=area_extra_lac.base,
            base__organization=london_assembly.base,
            slug='11822',
            base__label='Assembly Member for Lambeth and Southwark',
        )
        PostExtraFactory.create(
            elections=(election_gla,),
            base__area=area_extra_gla.base,
            base__organization=london_assembly.base,
            slug='2247',
            base__label='2016 London Assembly Election (Additional)',
        )
        # ----------------------------
        response = self.app.get('/')
        form = response.forms['form-postcode']
        form['postcode'] = 'SE24 0AG'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/GLA-unit_id:41441,LAC-gss:E32000010,WMC-gss:E14000673',
        )

Example 37

View license
    def test_valid_postcode_redirects_to_only_real_areas(self, mock_requests):
        mock_requests.get.side_effect = fake_requests_for_mapit
        # Create some extra posts and areas:
        london_assembly = ParliamentaryChamberExtraFactory.create(
            slug='london-assembly', base__name='London Assembly'
        )
        lac_area_type = AreaTypeFactory.create(name='LAC')
        gla_area_type = AreaTypeFactory.create(name='GLA')
        area_extra_gla = AreaExtraFactory.create(
            base__identifier='unit_id:41441',
            base__name='Greater London Authority',
            type=gla_area_type,
        )
        ElectionFactory.create(
            slug='gb-gla-2016-05-05-c',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Constituencies)',
            area_types=(lac_area_type,),
        )
        election_gla = ElectionFactory.create(
            slug='gb-gla-2016-05-05-a',
            organization=london_assembly.base,
            name='2016 London Assembly Election (Additional)',
            area_types=(gla_area_type,),
        )
        PostExtraFactory.create(
            elections=(election_gla,),
            base__area=area_extra_gla.base,
            base__organization=london_assembly.base,
            slug='2247',
            base__label='2016 London Assembly Election (Additional)',
        )
        # ----------------------------
        response = self.app.get('/')
        form = response.forms['form-postcode']
        form['postcode'] = 'SE24 0AG'
        response = form.submit()
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual(
            split_location.path,
            '/areas/GLA-unit_id:41441,WMC-gss:E14000673',
        )

Example 38

View license
    def test_fields_are_saved_when_editing(self):
        response = self.app.get(
            '/person/{person_id}/update'.format(person_id=self.person.id),
            user=self.user,
        )
        form = response.forms['person-details']
        form['cv'] = 'http://homepage.example.org/john-the-described'
        form['profession'] = 'Soda Jerk'
        form['reelection'] = 'no'
        form['source'] = 'Testing setting additional fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/{person_id}'.format(person_id=self.person.id),
            split_location.path
        )

        person = Person.objects.get(id=self.person.id)
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='cv'
            ).value,
            'http://homepage.example.org/john-the-described'
        )
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='profession'
            ).value,
            'Soda Jerk'
        )
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='reelection'
            ).value,
            'no'
        )

Example 39

View license
    def test_update_person_extra_fields(self):
        ExtraField.objects.create(
            type='url',
            key='cv',
            label='CV or Resumé',
        )
        ExtraField.objects.create(
            type='longer-text',
            key='notes',
            label='Notes',
        )
        response = self.app.get(
            '/person/2009/update',
            user=self.user_who_can_lock,
        )
        form = response.forms['person-details']
        form['birth_date'] = '1/4/1875'
        form['source'] = "An update for testing purposes"
        form['cv'] = 'http://example.org/cv.pdf'
        response = form.submit()

        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/person/2009', split_location.path)

        person = Person.objects.get(id='2009')
        self.assertEqual(person.birth_date, '1875-04-01')
        versions_data = json.loads(person.extra.versions)
        self.assertEqual(
            versions_data[0]['data']['extra_fields'],
            {
                'cv': 'http://example.org/cv.pdf',
                'notes': '',
            }
        )

Example 40

View license
    def test_photo_upload(self):
        queued_images = QueuedImage.objects.all()
        initial_count = queued_images.count()
        upload_form_url = reverse(
            'photo-upload',
            kwargs={'person_id': '2009'}
        )
        form_page_response = self.app.get(
            upload_form_url,
            user=self.test_upload_user
        )
        form = form_page_response.forms['person-upload-photo']
        with open(self.example_image_filename, 'rb') as f:
            form['image'] = Upload('pilot.jpg', f.read())
        form['why_allowed'] = 'copyright-assigned'
        form['justification_for_use'] = 'I took this photo'
        upload_response = form.submit()
        self.assertEqual(upload_response.status_code, 302)
        split_location = urlsplit(upload_response.location)
        self.assertEqual('/moderation/photo/upload/2009/success', split_location.path)
        queued_images = QueuedImage.objects.all()
        self.assertEqual(initial_count + 1, queued_images.count())
        queued_image = queued_images.last()
        self.assertEqual(queued_image.decision, 'undecided')
        self.assertEqual(queued_image.why_allowed, 'copyright-assigned')
        self.assertEqual(
            queued_image.justification_for_use,
            'I took this photo'
        )
        self.assertEqual(queued_image.person.id, 2009)
        self.assertEqual(queued_image.user, self.test_upload_user)

Example 41

View license
    @patch('moderation_queue.views.send_mail')
    def test_photo_review_upload_approved_privileged(
            self,
            mock_send_mail
    ):
        settings = get_current_usersettings()
        settings.DEFAULT_FROM_EMAIL = '[email protected]'
        settings.site_id = self.site.id
        settings.save()
        with self.settings(SITE_ID=self.site.id):
            review_url = reverse(
                'photo-review',
                kwargs={'queued_image_id': self.q1.id}
            )
            review_page_response = self.app.get(
                review_url,
                user=self.test_reviewer
            )
            form = review_page_response.forms['photo-review-form']
            form['decision'] = 'approved'
            form['moderator_why_allowed'] = 'profile-photo'
            response = form.submit(user=self.test_reviewer)
            # FIXME: check that mocked_person_put got the right calls
            self.assertEqual(response.status_code, 302)
            split_location = urlsplit(response.location)
            self.assertEqual('/moderation/photo/review', split_location.path)

            mock_send_mail.assert_called_once_with(
                'YNR image upload approved',
                "Thank-you for submitting a photo to YNR; that's been uploaded\nnow for the candidate page here:\n\n  http://localhost:80/person/2009/tessa-jowell\n\nMany thanks from the YNR volunteers\n",
                '[email protected]',
                ['[email protected]'],
                fail_silently=False
            )

            person = Person.objects.get(id=2009)
            image = person.extra.images.last()

            self.assertTrue(image.is_primary)
            self.assertEqual(
                'Uploaded by john: Approved from photo moderation queue',
                image.source
            )
            self.assertEqual(427, image.image.width)
            self.assertEqual(639, image.image.height)

            self.q1.refresh_from_db()
            self.assertEqual('public-domain', self.q1.why_allowed)
            self.assertEqual('approved', self.q1.decision)
            las = LoggedAction.objects.all()
            self.assertEqual(1, len(las))
            la = las[0]
            self.assertEqual(la.user.username, 'jane')
            self.assertEqual(la.action_type, 'photo-approve')
            self.assertEqual(la.person.id, 2009)

            self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'approved')

Example 42

View license
    @patch('moderation_queue.views.send_mail')
    def test_photo_review_upload_rejected_privileged(
            self,
            mock_send_mail
    ):
        settings = get_current_usersettings()
        settings.DEFAULT_FROM_EMAIL = '[email protected]'
        settings.SUPPORT_EMAIL = '[email protected]'
        settings.site_id = self.site.id
        settings.save()
        with self.settings(SITE_ID=self.site.id):
            review_url = reverse(
                'photo-review',
                kwargs={'queued_image_id': self.q1.id}
            )
            review_page_response = self.app.get(
                review_url,
                user=self.test_reviewer
            )
            form = review_page_response.forms['photo-review-form']
            form['decision'] = 'rejected'
            form['rejection_reason'] = 'There\'s no clear source or copyright statement'
            response = form.submit(user=self.test_reviewer)
            self.assertEqual(response.status_code, 302)
            split_location = urlsplit(response.location)
            self.assertEqual('/moderation/photo/review', split_location.path)

            las = LoggedAction.objects.all()
            self.assertEqual(1, len(las))
            la = las[0]
            self.assertEqual(la.user.username, 'jane')
            self.assertEqual(la.action_type, 'photo-reject')
            self.assertEqual(la.person.id, 2009)
            self.assertEqual(la.source, 'Rejected a photo upload from john')
            self.assertEqual(la.note, 'There\'s no clear source or copyright statement')

            mock_send_mail.assert_called_once_with(
                'YNR image moderation results',
                "Thank-you for uploading a photo of Tessa Jowell to YNR, but\nunfortunately we can't use that image because:\n\n  There\'s no clear source or copyright statement\n\nYou can just reply to this email if you want to discuss that\nfurther, or you can try uploading a photo with a different\nreason or justification for its use using this link:\n\n  http://localhost:80/moderation/photo/upload/2009\n\nMany thanks from the YNR volunteers\n\n-- \nFor administrators' use: http://localhost:80/moderation/photo/review/{0}\n".format(self.q1.id),
                '[email protected]',
                ['[email protected]', '[email protected]'],
                fail_silently=False
            )

            self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'rejected')

Example 43

View license
    def test_fields_are_saved_when_creating(self):
        response = self.app.get(
            '/election/2015/person/create/',
            user=self.user
        )
        form = response.forms['new-candidate-form']
        form['name'] = 'Naomi Newperson'
        form['cv'] = 'http://example.org/another-cv'
        form['profession'] = 'Longshoreman'
        form['standing_2015'] = 'not-standing'
        form['reelection'] = 'yes'
        form['source'] = 'Test creating someone with additional fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        m = re.search(r'^/person/(.*)', split_location.path)
        self.assertTrue(m)

        person = Person.objects.get(id=m.group(1))
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='cv'
            ).value,
            'http://example.org/another-cv'
        )
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='profession'
            ).value,
            'Longshoreman'
        )
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='reelection'
            ).value,
            'yes'
        )

Example 44

View license
    @patch('moderation_queue.views.send_mail')
    def test_photo_review_upload_undecided_privileged(
            self,
            mock_send_mail
    ):
        settings = get_current_usersettings()
        settings.DEFAULT_FROM_EMAIL = '[email protected]'
        settings.save()
        review_url = reverse(
            'photo-review',
            kwargs={'queued_image_id': self.q1.id}
        )
        review_page_response = self.app.get(
            review_url,
            user=self.test_reviewer
        )
        form = review_page_response.forms['photo-review-form']
        form['decision'] = 'undecided'
        form['rejection_reason'] = 'No clear source or copyright statement'
        response = form.submit(user=self.test_reviewer)
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/moderation/photo/review', split_location.path)

        self.assertEqual(mock_send_mail.call_count, 0)

        self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'undecided')

Example 45

View license
    @patch('moderation_queue.views.send_mail')
    def test_photo_review_upload_ignore_privileged(
            self,
            mock_send_mail
    ):
        settings = get_current_usersettings()
        settings.DEFAULT_FROM_EMAIL = '[email protected]'
        settings.save()
        review_url = reverse(
            'photo-review',
            kwargs={'queued_image_id': self.q1.id}
        )
        review_page_response = self.app.get(
            review_url,
            user=self.test_reviewer
        )
        form = review_page_response.forms['photo-review-form']
        form['decision'] = 'ignore'
        response = form.submit(user=self.test_reviewer)
        self.assertEqual(response.status_code, 302)
        split_location = urlsplit(response.location)
        self.assertEqual('/moderation/photo/review', split_location.path)

        self.assertEqual(mock_send_mail.call_count, 0)

        self.assertEqual(QueuedImage.objects.get(pk=self.q1.id).decision, 'ignore')

        las = LoggedAction.objects.all()
        self.assertEqual(1, len(las))
        la = las[0]
        self.assertEqual(la.user.username, 'jane')
        self.assertEqual(la.action_type, 'photo-ignore')
        self.assertEqual(la.person.id, 2009)

Example 46

View license
    def test_copyright_assigned(self):
        response = self.app.get(
            '/constituency/65808/dulwich-and-west-norwood',
            user=self.user_refused,
            auto_follow=True
        )

        form = response.forms['copyright_assignment']
        form['assigned_to_dc'] = True
        form_response = form.submit()

        split_location = urlsplit(form_response.location)
        self.assertEqual(
            '/constituency/65808/dulwich-and-west-norwood',
            split_location.path
        )

        agreement = self.user_refused.terms_agreement
        agreement.refresh_from_db()
        self.assertTrue(agreement.assigned_to_dc)

Example 47

View license
    def handle_inner(self, *args, **options):
        post_label_format = options['post_label']

        mapit_url = options['MAPIT-URL']
        split_mapit_url = urlsplit(mapit_url)
        if any([split_mapit_url.path not in ('', '/'),
                split_mapit_url.query,
                split_mapit_url.fragment]):
            raise CommandError("MAPIT-URL must only be the base URL, "
                "with no path, query or fragment")
        area_type = options['AREA-TYPE']
        post_id_format = options['POST-ID-FORMAT']
        manual_area_ids = []
        if options['area_ids']:
            try:
                manual_area_ids = [
                    int(n, 10) for n in options['area_ids'].split(',')
                ]
            except ValueError:
                raise Command("The --area-ids option must be comma separated numeric IDs")
        enclosing_area_id = options['enclosing_area_id']
        if enclosing_area_id:
            # Just check that area actually exists:
            enclosing_area_url = urljoin(
                mapit_url,
                '/area/{0}'.format(enclosing_area_id))
            r = requests.get(enclosing_area_url)
            if r.status_code == 404:
                msg = "The enclosing area {0} doesn't exist"
                raise CommandError(msg.format(enclosing_area_url))

        party_set, created = PartySet.objects.get_or_create(
            slug=slugify(options['party_set']),
            defaults={'name': options['party_set']}
        )

        elections = Election.objects.all()

        if elections.count() == 0:
            raise CommandError("There must be at least one election")

        for election in elections:
            if manual_area_ids:
                all_areas_url = urljoin(mapit_url, '/areas/' + ','.join(
                    text_type(a_id) for a_id in manual_area_ids
                ))
            else:
                query = {}
                if enclosing_area_id:
                    all_areas_url = enclosing_area_url + '/covers?type=' + area_type
                    query['type'] = area_type
                else:
                    all_areas_url = urljoin(mapit_url, '/areas/{0}'.format(area_type))
                if election.area_generation:
                    query['generation'] = election.area_generation
                query_string = '&'.join('{0}={1}'.format(k, v) for k, v in query.items())
                if query_string:
                    all_areas_url += '?' + query_string

            mapit_result = requests.get(all_areas_url)
            mapit_json = mapit_result.json()

            if 'error' in mapit_json:
                raise Command("Fetching the areas failed: {0}".format(
                    mapit_json['error']))

            for_post_role = election.for_post_role
            org = election.organization

            if org is None:
                raise CommandError("Election {0} requires an organization".format(election.slug))

            for item in mapit_json.items():
                area_json = item[1]

                area_url = urljoin(mapit_url, '/area/' + text_type(area_json['id']))

                area, area_created = Area.objects.get_or_create(
                    name=area_json['name'],
                    identifier=area_url,
                    classification=area_json['type_name']
                )

                area_type, created = AreaType.objects.get_or_create(
                    name=area_json['type'],
                    source='MapIt'
                )

                if area_created:
                    area_extra, area_extra_created = AreaExtra.objects.get_or_create(
                        base=area
                    )

                if area_created and area_extra_created:
                    area_extra.type = area_type
                    area_extra.save()

                post_id = post_id_format.format(area_id=area_json['id'])
                post_name = post_label_format.format(
                    area_name=area_json['name'],
                    post_role=for_post_role
                )

                post, created = Post.objects.get_or_create(
                    label=post_name,
                    area=area,
                    organization=org
                )

                post_extra, created = PostExtra.objects.get_or_create(
                    base=post,
                    slug=post_id,
                    defaults={'party_set': party_set},
                )

                PostExtraElection.objects.get_or_create(
                    postextra=post_extra,
                    election=election,
                )

Example 48

View license
    def update_area(self, area_data):
        new_area_data = area_data.copy()
        if settings.ELECTION_APP == 'uk_general_election_2015' or \
           settings.ELECTION_APP == 'ar_elections_2015' or \
           settings.ELECTION_APP == 'bf_elections_2015':
            identifier = new_area_data['identifier']
            split_url = urlsplit(identifier)
            if not (split_url.netloc.endswith('mapit.mysociety.org') or
                    split_url.netloc.endswith('mapit.staging.mysociety.org')):
                raise Exception("Area identifers are expected to be MapIt area URLs")
            mapit_area_url = identifier
            m = re.search(r'^/area/(\d+)$', split_url.path)
            if not m:
                message = "The format of the MapIt URL was unexpected: {0}"
                raise Exception(message.format(mapit_area_url))
            mapit_area_id = m.group(1)
            # Make the Area.identifier for UK areas just the integer
            # MapIt Area ID to make it easy to keep area URLs the same:
            new_area_data['identifier'] = mapit_area_id
        elif settings.ELECTION_APP == 'st_paul_municipal_2015':
            old_identifier = new_area_data['identifier']
            if old_identifier == '/area/0':
                new_area_data['identifier'] = 'ocd-division/country:us/state:mn/place:st_paul'
            else:
                m = re.search(r'^/area/([1-7])', old_identifier)
                if m:
                    new_area_data['identifier'] = \
                        'ocd-division/country:us/state:mn/place:st_paul/ward:' + m.group(1)
                else:
                    message = "The format of the St Paul area ID was unexpected: {0}"
                    raise Exception(message.format(old_identifier))

        area_id, area = super(YNRPopItImporter, self).update_area(new_area_data)

        if settings.ELECTION_APP == 'uk_general_election_2015':
            ContentType = self.get_model_class('contenttypes', 'ContentType')
            area_content_type = ContentType.objects.get_for_model(area)
            Identifier = self.get_model_class('popolo', 'Identifier')
            # For the UK, we need to add the GSS code for each area:
            mapit_filename = get_url_cached(mapit_area_url)
            with open(mapit_filename) as f:
                mapit_area_data = json.load(f)
            self.uk_mapit_data[str(mapit_area_data['id'])] = mapit_area_data
            gss_code = mapit_area_data.get('codes', {}).get('gss')
            if gss_code:
                Identifier.objects.create(
                    scheme='gss',
                    identifier=gss_code,
                    object_id=area.id,
                    content_type_id=area_content_type.id,
                )
            # Also preserve the complete MapIt URL:
            Identifier.objects.create(
                scheme='mapit-area-url',
                identifier=mapit_area_url,
                object_id=area.id,
                content_type_id=area_content_type.id
            )
        # Create the extra area object:
        AreaExtra = self.get_model_class('candidates', 'AreaExtra')
        AreaExtra.objects.get_or_create(base=area)

        return area_id, area

Example 49

View license
    def test_fields_are_saved_when_editing(self):
        response = self.app.get(
            '/person/{person_id}/update'.format(person_id=self.person.id),
            user=self.user,
        )
        form = response.forms['person-details']
        form['cv'] = 'http://homepage.example.org/john-the-described'
        form['profession'] = 'Soda Jerk'
        form['reelection'] = 'no'
        form['source'] = 'Testing setting additional fields'
        submission_response = form.submit()

        self.assertEqual(submission_response.status_code, 302)
        split_location = urlsplit(submission_response.location)
        self.assertEqual(
            '/person/{person_id}'.format(person_id=self.person.id),
            split_location.path
        )

        person = Person.objects.get(id=self.person.id)
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='cv'
            ).value,
            'http://homepage.example.org/john-the-described'
        )
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='profession'
            ).value,
            'Soda Jerk'
        )
        self.assertEqual(
            PersonExtraFieldValue.objects.get(
                person=person, field__key='reelection'
            ).value,
            'no'
        )

Example 50

View license
    def update_area(self, area_data):
        new_area_data = area_data.copy()
        if settings.ELECTION_APP == 'uk_general_election_2015' or \
           settings.ELECTION_APP == 'ar_elections_2015' or \
           settings.ELECTION_APP == 'bf_elections_2015':
            identifier = new_area_data['identifier']
            split_url = urlsplit(identifier)
            if not (split_url.netloc.endswith('mapit.mysociety.org') or
                    split_url.netloc.endswith('mapit.staging.mysociety.org')):
                raise Exception("Area identifers are expected to be MapIt area URLs")
            mapit_area_url = identifier
            m = re.search(r'^/area/(\d+)$', split_url.path)
            if not m:
                message = "The format of the MapIt URL was unexpected: {0}"
                raise Exception(message.format(mapit_area_url))
            mapit_area_id = m.group(1)
            # Make the Area.identifier for UK areas just the integer
            # MapIt Area ID to make it easy to keep area URLs the same:
            new_area_data['identifier'] = mapit_area_id
        elif settings.ELECTION_APP == 'st_paul_municipal_2015':
            old_identifier = new_area_data['identifier']
            if old_identifier == '/area/0':
                new_area_data['identifier'] = 'ocd-division/country:us/state:mn/place:st_paul'
            else:
                m = re.search(r'^/area/([1-7])', old_identifier)
                if m:
                    new_area_data['identifier'] = \
                        'ocd-division/country:us/state:mn/place:st_paul/ward:' + m.group(1)
                else:
                    message = "The format of the St Paul area ID was unexpected: {0}"
                    raise Exception(message.format(old_identifier))

        area_id, area = super(YNRPopItImporter, self).update_area(new_area_data)

        if settings.ELECTION_APP == 'uk_general_election_2015':
            ContentType = self.get_model_class('contenttypes', 'ContentType')
            area_content_type = ContentType.objects.get_for_model(area)
            Identifier = self.get_model_class('popolo', 'Identifier')
            # For the UK, we need to add the GSS code for each area:
            mapit_filename = get_url_cached(mapit_area_url)
            with open(mapit_filename) as f:
                mapit_area_data = json.load(f)
            self.uk_mapit_data[str(mapit_area_data['id'])] = mapit_area_data
            gss_code = mapit_area_data.get('codes', {}).get('gss')
            if gss_code:
                Identifier.objects.create(
                    scheme='gss',
                    identifier=gss_code,
                    object_id=area.id,
                    content_type_id=area_content_type.id,
                )
            # Also preserve the complete MapIt URL:
            Identifier.objects.create(
                scheme='mapit-area-url',
                identifier=mapit_area_url,
                object_id=area.id,
                content_type_id=area_content_type.id
            )
        # Create the extra area object:
        AreaExtra = self.get_model_class('candidates', 'AreaExtra')
        AreaExtra.objects.get_or_create(base=area)

        return area_id, area