django.utils.six.moves.urllib_parse.urljoin

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

30 Examples 7

Example 1

Project: yournextrepresentative
Source File: views.py
View license
    def get_redirect_url(self, *args, **kwargs):
        new_url = urljoin(
            'https://candidates.democracyclub.org.uk',
            self.request.get_full_path(),
        )
        return new_url

Example 2

Project: yournextrepresentative
Source File: views.py
View license
    def get_redirect_url(self, *args, **kwargs):
        new_url = urljoin(
            'https://candidates.democracyclub.org.uk',
            self.request.get_full_path(),
        )
        return new_url

Example 3

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 4

Project: yournextrepresentative
Source File: mapit.py
View license
def get_areas_from_postcode(postcode):
    cache_key = 'mapit-postcode:' + postcode
    cached_result = cache.get(cache_key)
    if cached_result:
        return cached_result
    url = urljoin(settings.MAPIT_BASE_URL, '/postcode/' + urlquote(postcode))
    r = requests.get(url)
    if r.status_code == 200:
        mapit_result = r.json()
        areas = get_known_area_types(mapit_result)
        cache.set(cache_key, areas, settings.MAPIT_CACHE_SECONDS)
        return areas
    elif r.status_code == 400:
        mapit_result = r.json()
        raise BadPostcodeException(mapit_result['error'])
    elif r.status_code == 404:
        raise BadPostcodeException(
            _('The postcode “{0}” couldn’t be found').format(
                postcode
            )
        )
    else:
        raise UnknownMapitException(
            _('Unknown MapIt error for postcode "{0}"').format(
                postcode
            )
        )

Example 5

Project: yournextrepresentative
Source File: address.py
View license
def check_address(address_string, country=None):
    tidied_address_before_country = address_string.strip()
    if country is None:
        tidied_address = tidied_address_before_country
    else:
        tidied_address = tidied_address_before_country + ', ' + country
    try:
        location_results = Geocoder.geocode(tidied_address)
    except GeocoderError:
        message = _("Failed to find a location for '{0}'")
        raise ValidationError(message.format(tidied_address_before_country))
    lat, lon = location_results[0].coordinates
    all_mapit_json = []
    queries_to_try = defaultdict(set)
    for election in Election.objects.current().prefetch_related('area_types'):
        area_types = election.area_types.values_list('name', flat=True)
        queries_to_try[election.area_generation].update(area_types)
    for area_generation, area_types in queries_to_try.items():
        mapit_lookup_url = urljoin(settings.MAPIT_BASE_URL,
                                   'point/4326/{lon},{lat}'.format(
                                       lon=lon,
                                       lat=lat,
                                       ))
        mapit_lookup_url += '?type=' + ','.join(area_types)
        mapit_lookup_url += '&generation={0}'.format(election.area_generation)
        mapit_result = requests.get(mapit_lookup_url)
        mapit_json = mapit_result.json()
        if 'error' in mapit_json:
            message = _("The area lookup returned an error: '{error}'")
            raise ValidationError(message.format(error=mapit_json['error']))
        all_mapit_json += mapit_json.items()
    sorted_mapit_results = sorted(
        all_mapit_json,
        key=lambda t: (t[1]['type'], int(t[0]))
    )
    if not sorted_mapit_results:
        message = _("The address '{0}' appears to be outside the area this site knows about")
        raise ValidationError(message.format(tidied_address_before_country))
    types_and_areas = [
        {
            'area_type_code': a[1]['type'],
            'area_id': a[0],
        }
        for a in sorted_mapit_results
    ]
    if settings.AREAS_TO_ALWAYS_RETURN:
        types_and_areas += settings.AREAS_TO_ALWAYS_RETURN
    types_and_areas_joined = ','.join(
        '{area_type_code}-{area_id}'.format(**ta) for ta in types_and_areas
    )
    area_slugs = [slugify(a[1]['name']) for a in sorted_mapit_results]
    ignored_slug = '-'.join(area_slugs)
    return {
        'type_and_area_ids': types_and_areas_joined,
        'ignored_slug': ignored_slug,
    }

Example 6

View license
    def as_list_of_dicts(self, election, base_url=None):
        result = []
        user_settings = get_current_usersettings()
        if not base_url:
            base_url = ''
        # Find the list of relevant candidacies. So as not to cause
        # extra queries, we don't use filter but instead iterate over
        # all objects:
        candidacies = []
        for m in self.base.memberships.all():
            try:
                m_extra = m.extra
            except ObjectDoesNotExist:
                continue
            if not m_extra.election:
                continue
            expected_role = m.extra.election.candidate_membership_role
            if election is None:
                if expected_role == m.role:
                    candidacies.append(m)
            else:
                if m_extra.election == election and expected_role == m.role:
                    candidacies.append(m)
        for candidacy in candidacies:
            candidacy_extra = candidacy.extra
            party = candidacy.on_behalf_of
            post = candidacy.post
            elected = candidacy_extra.elected
            elected_for_csv = ''
            image_copyright = ''
            image_uploading_user = ''
            image_uploading_user_notes = ''
            proxy_image_url_template = ''
            if elected is not None:
                elected_for_csv = str(elected)
            mapit_identifier = None
            for identifier in post.area.other_identifiers.all():
                if identifier.scheme == 'mapit-area-url':
                    mapit_identifier = identifier
            if mapit_identifier:
                mapit_url = mapit_identifier.identifier
            else:
                mapit_url = ''
            primary_image = None
            for image in self.images.all():
                if image.is_primary:
                    primary_image = image
            primary_image_url = None
            if primary_image:
                primary_image_url = urljoin(base_url, primary_image.image.url)
                if user_settings.IMAGE_PROXY_URL and base_url:
                    encoded_url = quote_plus(primary_image_url)
                    proxy_image_url_template = user_settings.IMAGE_PROXY_URL + \
                        encoded_url + '/{height}/{width}.{extension}'

                try:
                    image_copyright = primary_image.extra.copyright
                    user = primary_image.extra.uploading_user
                    if user is not None:
                        image_uploading_user = primary_image.extra.uploading_user.username
                    image_uploading_user_notes = primary_image.extra.user_notes
                except ObjectDoesNotExist:
                    pass
            twitter_user_id = ''
            for identifier in self.base.identifiers.all():
                if identifier.scheme == 'twitter':
                    twitter_user_id = identifier.identifier

            row = {
                'id': self.base.id,
                'name': self.base.name,
                'honorific_prefix': self.base.honorific_prefix,
                'honorific_suffix': self.base.honorific_suffix,
                'gender': self.base.gender,
                'birth_date': self.base.birth_date,
                'election': candidacy_extra.election.slug,
                'election_date': candidacy_extra.election.election_date,
                'election_current': candidacy_extra.election.current,
                'party_id': party.extra.slug,
                'party_lists_in_use': candidacy_extra.election.party_lists_in_use,
                'party_list_position': candidacy_extra.party_list_position,
                'party_name': party.name,
                'post_id': post.extra.slug,
                'post_label': post.extra.short_label,
                'mapit_url': mapit_url,
                'elected': elected_for_csv,
                'email': self.base.email,
                'twitter_username': self.twitter_username,
                'twitter_user_id': twitter_user_id,
                'facebook_page_url': self.facebook_page_url,
                'linkedin_url': self.linkedin_url,
                'party_ppc_page_url': self.party_ppc_page_url,
                'facebook_personal_url': self.facebook_personal_url,
                'homepage_url': self.homepage_url,
                'wikipedia_url': self.wikipedia_url,
                'image_url': primary_image_url,
                'proxy_image_url_template': proxy_image_url_template,
                'image_copyright': image_copyright,
                'image_uploading_user': image_uploading_user,
                'image_uploading_user_notes': image_uploading_user_notes,
            }
            from ..election_specific import get_extra_csv_values
            extra_csv_data = get_extra_csv_values(self.base, election, post)
            row.update(extra_csv_data)
            result.append(row)

        return result

Example 7

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 8

Project: yournextrepresentative
Source File: mapit.py
View license
def get_areas_from_postcode(postcode):
    cache_key = 'mapit-postcode:' + postcode
    cached_result = cache.get(cache_key)
    if cached_result:
        return cached_result
    url = urljoin(settings.MAPIT_BASE_URL, '/postcode/' + urlquote(postcode))
    r = requests.get(url)
    if r.status_code == 200:
        mapit_result = r.json()
        areas = get_known_area_types(mapit_result)
        cache.set(cache_key, areas, settings.MAPIT_CACHE_SECONDS)
        return areas
    elif r.status_code == 400:
        mapit_result = r.json()
        raise BadPostcodeException(mapit_result['error'])
    elif r.status_code == 404:
        raise BadPostcodeException(
            _('The postcode “{0}” couldn’t be found').format(
                postcode
            )
        )
    else:
        raise UnknownMapitException(
            _('Unknown MapIt error for postcode "{0}"').format(
                postcode
            )
        )

Example 9

View license
def fake_requests_for_mapit(url):
    """Return reduced MapIt output for some known URLs"""
    if url == urljoin(settings.MAPIT_BASE_URL, '/postcode/sw1a1aa'):
        status_code = 200
        json_result = sw1a1aa_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/se240ag'):
        status_code = 200
        json_result = se240ag_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/cb28rq'):
        status_code = 404
        json_result = {
            "code": 404,
            "error": "No Postcode matches the given query."
        }
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/foobar'):
        status_code = 400
        json_result = {
            "code": 400,
            "error": "Postcode 'FOOBAR' is not valid."
        }
    else:
        raise Exception("URL that hasn't been mocked yet: " + url)
    return Mock(**{
        'json.return_value': json_result,
        'status_code': status_code
    })

Example 10

Project: yournextrepresentative
Source File: address.py
View license
def check_address(address_string, country=None):
    tidied_address_before_country = address_string.strip()
    if country is None:
        tidied_address = tidied_address_before_country
    else:
        tidied_address = tidied_address_before_country + ', ' + country
    try:
        location_results = Geocoder.geocode(tidied_address)
    except GeocoderError:
        message = _("Failed to find a location for '{0}'")
        raise ValidationError(message.format(tidied_address_before_country))
    lat, lon = location_results[0].coordinates
    all_mapit_json = []
    queries_to_try = defaultdict(set)
    for election in Election.objects.current().prefetch_related('area_types'):
        area_types = election.area_types.values_list('name', flat=True)
        queries_to_try[election.area_generation].update(area_types)
    for area_generation, area_types in queries_to_try.items():
        mapit_lookup_url = urljoin(settings.MAPIT_BASE_URL,
                                   'point/4326/{lon},{lat}'.format(
                                       lon=lon,
                                       lat=lat,
                                       ))
        mapit_lookup_url += '?type=' + ','.join(area_types)
        mapit_lookup_url += '&generation={0}'.format(election.area_generation)
        mapit_result = requests.get(mapit_lookup_url)
        mapit_json = mapit_result.json()
        if 'error' in mapit_json:
            message = _("The area lookup returned an error: '{error}'")
            raise ValidationError(message.format(error=mapit_json['error']))
        all_mapit_json += mapit_json.items()
    sorted_mapit_results = sorted(
        all_mapit_json,
        key=lambda t: (t[1]['type'], int(t[0]))
    )
    if not sorted_mapit_results:
        message = _("The address '{0}' appears to be outside the area this site knows about")
        raise ValidationError(message.format(tidied_address_before_country))
    types_and_areas = [
        {
            'area_type_code': a[1]['type'],
            'area_id': a[0],
        }
        for a in sorted_mapit_results
    ]
    if settings.AREAS_TO_ALWAYS_RETURN:
        types_and_areas += settings.AREAS_TO_ALWAYS_RETURN
    types_and_areas_joined = ','.join(
        '{area_type_code}-{area_id}'.format(**ta) for ta in types_and_areas
    )
    area_slugs = [slugify(a[1]['name']) for a in sorted_mapit_results]
    ignored_slug = '-'.join(area_slugs)
    return {
        'type_and_area_ids': types_and_areas_joined,
        'ignored_slug': ignored_slug,
    }

Example 11

View license
    def as_list_of_dicts(self, election, base_url=None):
        result = []
        user_settings = get_current_usersettings()
        if not base_url:
            base_url = ''
        # Find the list of relevant candidacies. So as not to cause
        # extra queries, we don't use filter but instead iterate over
        # all objects:
        candidacies = []
        for m in self.base.memberships.all():
            try:
                m_extra = m.extra
            except ObjectDoesNotExist:
                continue
            if not m_extra.election:
                continue
            expected_role = m.extra.election.candidate_membership_role
            if election is None:
                if expected_role == m.role:
                    candidacies.append(m)
            else:
                if m_extra.election == election and expected_role == m.role:
                    candidacies.append(m)
        for candidacy in candidacies:
            candidacy_extra = candidacy.extra
            party = candidacy.on_behalf_of
            post = candidacy.post
            elected = candidacy_extra.elected
            elected_for_csv = ''
            image_copyright = ''
            image_uploading_user = ''
            image_uploading_user_notes = ''
            proxy_image_url_template = ''
            if elected is not None:
                elected_for_csv = str(elected)
            mapit_identifier = None
            for identifier in post.area.other_identifiers.all():
                if identifier.scheme == 'mapit-area-url':
                    mapit_identifier = identifier
            if mapit_identifier:
                mapit_url = mapit_identifier.identifier
            else:
                mapit_url = ''
            primary_image = None
            for image in self.images.all():
                if image.is_primary:
                    primary_image = image
            primary_image_url = None
            if primary_image:
                primary_image_url = urljoin(base_url, primary_image.image.url)
                if user_settings.IMAGE_PROXY_URL and base_url:
                    encoded_url = quote_plus(primary_image_url)
                    proxy_image_url_template = user_settings.IMAGE_PROXY_URL + \
                        encoded_url + '/{height}/{width}.{extension}'

                try:
                    image_copyright = primary_image.extra.copyright
                    user = primary_image.extra.uploading_user
                    if user is not None:
                        image_uploading_user = primary_image.extra.uploading_user.username
                    image_uploading_user_notes = primary_image.extra.user_notes
                except ObjectDoesNotExist:
                    pass
            twitter_user_id = ''
            for identifier in self.base.identifiers.all():
                if identifier.scheme == 'twitter':
                    twitter_user_id = identifier.identifier

            row = {
                'id': self.base.id,
                'name': self.base.name,
                'honorific_prefix': self.base.honorific_prefix,
                'honorific_suffix': self.base.honorific_suffix,
                'gender': self.base.gender,
                'birth_date': self.base.birth_date,
                'election': candidacy_extra.election.slug,
                'election_date': candidacy_extra.election.election_date,
                'election_current': candidacy_extra.election.current,
                'party_id': party.extra.slug,
                'party_lists_in_use': candidacy_extra.election.party_lists_in_use,
                'party_list_position': candidacy_extra.party_list_position,
                'party_name': party.name,
                'post_id': post.extra.slug,
                'post_label': post.extra.short_label,
                'mapit_url': mapit_url,
                'elected': elected_for_csv,
                'email': self.base.email,
                'twitter_username': self.twitter_username,
                'twitter_user_id': twitter_user_id,
                'facebook_page_url': self.facebook_page_url,
                'linkedin_url': self.linkedin_url,
                'party_ppc_page_url': self.party_ppc_page_url,
                'facebook_personal_url': self.facebook_personal_url,
                'homepage_url': self.homepage_url,
                'wikipedia_url': self.wikipedia_url,
                'image_url': primary_image_url,
                'proxy_image_url_template': proxy_image_url_template,
                'image_copyright': image_copyright,
                'image_uploading_user': image_uploading_user,
                'image_uploading_user_notes': image_uploading_user_notes,
            }
            from ..election_specific import get_extra_csv_values
            extra_csv_data = get_extra_csv_values(self.base, election, post)
            row.update(extra_csv_data)
            result.append(row)

        return result

Example 12

Project: yournextrepresentative
Source File: frontpage.py
View license
    def get(self, request, **kwargs):
        latitude = kwargs['latitude']
        longitude = kwargs['longitude']

        generation_with_types = defaultdict(list)
        for t in AreaType.objects.filter(election__current=True) \
                .values_list('election__area_generation', 'name'):
            generation_with_types[t[0]].append(t[1])

        mapit_base_url = urljoin(settings.MAPIT_BASE_URL,
                                 'point/4326/{lon},{lat}'.format(
                                     lon=longitude,
                                     lat=latitude,
                                 ))

        mapit_json = []
        for generation, types in generation_with_types.items():
            lookup_url = mapit_base_url + '?type=' \
                + ','.join(types)
            lookup_url += '&generation={0}'.format(generation)
            mapit_result = requests.get(lookup_url)
            mapit_result = mapit_result.json()
            if 'error' in mapit_result:
                message = _("The area lookup returned an error: '{0}'") \
                    .format(mapit_result['error'])
                return HttpResponse(
                    json.dumps({'error': message}),
                    content_type='application/json',
                )
            mapit_json += mapit_result.items()

        if len(mapit_json) == 0:
            message = _("Your location does not seem to be covered by this site")
            return HttpResponse(
                json.dumps({'error': message}),
                content_type='application/json',
            )

        ids_and_areas = [
            "{0}-{1}".format(
                area[1]['type'],
                mapit.format_code_from_area(area[1]))
            for area in mapit_json
        ]

        url = reverse('areas-view', kwargs={
            'type_and_area_ids': ','.join(sorted(ids_and_areas))
        })

        return HttpResponse(
            json.dumps({'url': url}),
            content_type='application/json',
        )

Example 13

View license
def fake_requests_for_mapit(url):
    """Return reduced MapIt output for some known URLs"""
    if url == urljoin(settings.MAPIT_BASE_URL, '/postcode/sw1a1aa'):
        status_code = 200
        json_result = sw1a1aa_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/se240ag'):
        status_code = 200
        json_result = se240ag_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/cb28rq'):
        status_code = 404
        json_result = {
            "code": 404,
            "error": "No Postcode matches the given query."
        }
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/foobar'):
        status_code = 400
        json_result = {
            "code": 400,
            "error": "Postcode 'FOOBAR' is not valid."
        }
    else:
        raise Exception("URL that hasn't been mocked yet: " + url)
    return Mock(**{
        'json.return_value': json_result,
        'status_code': status_code
    })

Example 14

Project: yournextrepresentative
Source File: frontpage.py
View license
    def get(self, request, **kwargs):
        latitude = kwargs['latitude']
        longitude = kwargs['longitude']

        generation_with_types = defaultdict(list)
        for t in AreaType.objects.filter(election__current=True) \
                .values_list('election__area_generation', 'name'):
            generation_with_types[t[0]].append(t[1])

        mapit_base_url = urljoin(settings.MAPIT_BASE_URL,
                                 'point/4326/{lon},{lat}'.format(
                                     lon=longitude,
                                     lat=latitude,
                                 ))

        mapit_json = []
        for generation, types in generation_with_types.items():
            lookup_url = mapit_base_url + '?type=' \
                + ','.join(types)
            lookup_url += '&generation={0}'.format(generation)
            mapit_result = requests.get(lookup_url)
            mapit_result = mapit_result.json()
            if 'error' in mapit_result:
                message = _("The area lookup returned an error: '{0}'") \
                    .format(mapit_result['error'])
                return HttpResponse(
                    json.dumps({'error': message}),
                    content_type='application/json',
                )
            mapit_json += mapit_result.items()

        if len(mapit_json) == 0:
            message = _("Your location does not seem to be covered by this site")
            return HttpResponse(
                json.dumps({'error': message}),
                content_type='application/json',
            )

        ids_and_areas = [
            "{0}-{1}".format(
                area[1]['type'],
                mapit.format_code_from_area(area[1]))
            for area in mapit_json
        ]

        url = reverse('areas-view', kwargs={
            'type_and_area_ids': ','.join(sorted(ids_and_areas))
        })

        return HttpResponse(
            json.dumps({'url': url}),
            content_type='application/json',
        )

Example 15

View license
    def handle(self, **options):

        mapit_url = settings.MAPIT_BASE_URL

        self.gb_parties, _ = PartySet.objects.get_or_create(
            slug='gb', defaults={'name': 'Great Britain'}
        )

        self.organizations = {}

        self.base_election_info = {
            'name': 'Police and Crime Commissioner Elections 2016',
            'for_post_role': 'Police and Crime Commissioner',
            'label_format': 'Police and Crime Commissioner for {area_name}',
            'area_generation': 1,
            'election_date': date(2016, 5, 5),
            'party_lists_in_use': False,
            'mapit_code': 'PDG',
            'electon_id_prefix': 'pcc',
            # 'area_type_description': 'Police Force',
        }

        url_path = '/areas/' + self.base_election_info['mapit_code']
        url = urljoin(mapit_url, url_path)
        r = requests.get(url)
        mapit_results = r.json().items()

        # First make all the organisations
        for mapit_area_id, mapit_area_data in mapit_results:
            if mapit_area_data['codes']['police_id'] == "metropolitan":
                continue
            self.add_police_force_orgs(mapit_area_id, mapit_area_data)

        # Create a single election
        self.create_pcc_election()
        # Add all the areas for that election
        for mapit_area_id, mapit_area_data in mapit_results:
            if mapit_area_data['codes']['police_id'] == "metropolitan":
                # The Met doesn't have a PCCge
                continue
            self.add_pcc_areas(mapit_area_id, mapit_area_data)

Example 16

View license
    def handle(self, **options):

        mapit_url = settings.MAPIT_BASE_URL

        gb_parties, _ = PartySet.objects.get_or_create(
            slug='gb', defaults={'name': 'Great Britain'}
        )
        ni_parties, _ = PartySet.objects.get_or_create(
            slug='ni', defaults={'name': 'Northern Ireland'}
        )

        elections = {
            'sp-2016-05-05-r': {
                'name': '2016 Scottish Parliament Election (Regions)',
                'for_post_role': 'Member of the Scottish Parliament',
                'label_format': 'Member of the Scottish Parliament for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': True,
                'mapit_code': 'SPE',
                'area_type_description': 'Scottish Parliament region',
                'organization_slug': 'scottish-parliament',
                'organization_name': 'Scottish Parliament',
            },
            'sp-2016-05-05-c': {
                'name': '2016 Scottish Parliament Election (Constituencies)',
                'for_post_role': 'Member of the Scottish Parliament',
                'label_format': 'Member of the Scottish Parliament for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'SPC',
                'area_type_description': 'Scottish Parliament constituency',
                'organization_slug': 'scottish-parliament',
                'organization_name': 'Scottish Parliament',
            },
            'naw-2016-05-05-r': {
                'name': '2016 Welsh Assembly Election (Regions)',
                'for_post_role': 'Member of the National Assembly for Wales',
                'label_format': 'Assembly Member for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': True,
                'mapit_code': 'WAE',
                'area_type_description': 'Welsh Assembly region',
                'organization_slug': 'welsh-assembly',
                'organization_name': 'National Assembly for Wales',
            },
            'naw-2016-05-05-c': {
                'name': '2016 Welsh Assembly Election (Constituencies)',
                'for_post_role': 'Member of the National Assembly for Wales',
                'label_format': 'Assembly Member for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'WAC',
                'area_type_description': 'Welsh Assembly constituency',
                'organization_slug': 'welsh-assembly',
                'organization_name': 'National Assembly for Wales',
            },
            'nia-2016-05-05': {
                'name': '2016 Northern Ireland Assembly Election',
                'for_post_role': 'Member of the Legislative Assembly',
                'label_format': 'Member of the Legislative Assembly for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'NIE',
                'area_type_description': 'Northern Ireland Assembly constituency',
                'organization_slug': 'northern-ireland-assembly',
                'organization_name': 'Northern Ireland Assembly',
            },
            'gla-2016-05-05-c': {
                'name': '2016 London Assembly Election (Constituencies)',
                'for_post_role': 'Member of the London Assembly',
                'label_format': 'Assembly Member for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'LAC',
                'area_type_description': 'London Assembly constituency',
                'organization_slug': 'london-assembly',
                'organization_name': 'London Assembly',
            },
            'gla-2016-05-05-a': {
                'name': '2016 London Assembly Election (Additional)',
                'for_post_role': 'Member of the London Assembly',
                'label_format': 'Assembly Member',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'GLA',
                'area_type_description': 'London Assembly constituency',
                'organization_slug': 'london-assembly',
                'organization_name': 'London Assembly',
            },
        }

        for election_slug, data in elections.items():
            # Make sure the parliament Organization and
            # OrganizationExtra objects exist:
            try:
                organization_extra = OrganizationExtra.objects.get(
                    slug=data['organization_slug']
                )
                organization = organization_extra.base
            except OrganizationExtra.DoesNotExist:
                organization = Organization.objects.create(
                    name=data['organization_name']
                )
                organization_extra = OrganizationExtra.objects.create(
                    base=organization,
                    slug=data['organization_slug']
                )
            # Make sure the Election object exists:
            election_defaults = {
                k: data[k] for k in
                [
                    'name', 'for_post_role', 'area_generation', 'election_date',
                    'party_lists_in_use',
                ]
            }
            election_defaults['current'] = True
            election_defaults['candidate_membership_role'] = 'Candidate'
            print('Creating:', election_defaults['name'], '...',)
            election, created = Election.objects.update_or_create(
                slug=election_slug,
                defaults=election_defaults
            )
            if created:
                print('[created]')
            else:
                print('[already existed]')

            area_type, _ = AreaType.objects.update_or_create(
                name=data['mapit_code'], defaults={'source': 'MapIt'}
            )
            if not election.area_types.filter(name=area_type.name).exists():
                election.area_types.add(area_type)
            url_path = '/areas/' + data['mapit_code']
            url = urljoin(mapit_url, url_path)
            r = requests.get(url)
            for mapit_area_id, mapit_area_data in r.json().items():

                area, _ = Area.objects.update_or_create(
                    identifier=str(mapit.format_code_from_area(mapit_area_data)),
                    defaults={'name': mapit_area_data['name']}
                )
                # Now make sure that the MapIt codes are present as identifiers:
                for scheme, identifier in mapit_area_data['codes'].items():
                    area.other_identifiers.update_or_create(
                        scheme=scheme,
                        defaults={'identifier': identifier},
                    )

                AreaExtra.objects.get_or_create(base=area, type=area_type)
                post, _ = Post.objects.update_or_create(
                    organization=organization,
                    area=area,
                    role='Member of the Scottish Parliament',
                    defaults={
                        'label': data['label_format'].format(
                            area_name=area.name
                        )
                    }
                )
                if mapit_area_data['country_name'] == 'Northern Ireland':
                    party_set = ni_parties
                else:
                    party_set = gb_parties
                post_extra, _ = PostExtra.objects.update_or_create(
                    base=post,
                    defaults={
                        'slug': str(mapit.format_code_from_area(mapit_area_data)),
                        'party_set': party_set,
                    },
                )
                PostExtraElection.objects.update_or_create(
                    postextra=post_extra,
                    election=election
                )

Example 17

View license
    def upload_images(self, emblems, party_extra):
        content_type = ContentType.objects.get_for_model(party_extra)
        sort_emblems(emblems, party_extra.slug)
        primary = True
        for emblem in emblems:
            emblem_id = str(emblem['Id'])
            ntf = NamedTemporaryFile(delete=False)
            image_url = urljoin(base_emblem_url, emblem_id)
            r = requests.get(image_url)
            with open(ntf.name, 'w') as f:
                f.write(r.content)
            mime_type = self.mime_type_magic.from_file(ntf.name)
            extension = mimetypes.guess_extension(mime_type)
            leafname = 'Emblem_{0}{1}'.format(emblem_id, extension)
            desired_storage_path = join('images', leafname)
            fname = join(emblem_directory, leafname)
            move(ntf.name, fname)
            md5sum = get_file_md5sum(fname)
            ImageExtra.objects.update_or_create_from_file(
                fname,
                desired_storage_path,
                md5sum=md5sum,
                base__object_id=party_extra.id,
                base__content_type_id=content_type.id,
                defaults={
                    'uploading_user':None,
                    'notes': emblem['MonochromeDescription'],
                    'base__source': 'The Electoral Commission',
                    'base__is_primary': primary,
                }
            )
            primary = False

Example 18

Project: yournextrepresentative
Source File: mapit.py
View license
def get_areas_from_postcode(original_postcode):
    postcode = re.sub(r'(?ms)\s*', '', original_postcode.lower())
    if re.search(r'[^a-z0-9]', postcode):
        raise BadPostcodeException(
            _('There were disallowed characters in "{0}"').format(original_postcode)
        )
    cache_key = 'mapit-postcode:' + postcode
    cached_result = cache.get(cache_key)
    if cached_result:
        return cached_result
    url = urljoin(settings.MAPIT_BASE_URL,
                  '/postcode/{0}'.format(urlquote(postcode)))
    r = requests.get(url)
    if r.status_code == 200:
        mapit_result = r.json()

        result = sorted(
            [
                (a['type'], format_code_from_area(a))
                for a in mapit_result['areas'].values()
                if Area.objects.filter(
                        extra__type__name=a['type'],
                        identifier=format_code_from_area(a)
                ).exists()
            ],
            key=area_sort_key
        )

        cache.set(cache_key, result, settings.MAPIT_CACHE_SECONDS)
        return result
    elif r.status_code == 400:
        mapit_result = r.json()
        raise BadPostcodeException(mapit_result['error'])
    elif r.status_code == 404:
        raise BadPostcodeException(
            _('The postcode “{0}” couldn’t be found').format(original_postcode)
        )
    else:
        raise UnknownMapitException(
            _('Unknown MapIt error for postcode "{0}"').format(
                original_postcode
            )
        )

Example 19

Project: yournextrepresentative
Source File: mapit.py
View license
def get_wmc_from_postcode(original_postcode):
    postcode = re.sub(r'(?ms)\s*', '', original_postcode.lower())
    if re.search(r'[^a-z0-9]', postcode):
        raise BadPostcodeException(
            _('There were disallowed characters in "{0}"').format(original_postcode)
        )
    cached_result = cache.get(postcode)
    if cached_result:
        return cached_result
    url = urljoin(settings.MAPIT_BASE_URL,
                  '/postcode/{0}'.format(urlquote(postcode)))
    r = requests.get(url)
    if r.status_code == 200:
        mapit_result = r.json()
        wmc = mapit_result.get('shortcuts', {}).get('WMC')
        if not wmc:
            raise NoConstituencyForPostcodeException(
                _('No constituency found for the postcode "{0}"').format(
                    original_postcode
                )
            )
        cache.set(postcode, wmc, settings.MAPIT_CACHE_SECONDS)
        return wmc
    elif r.status_code == 400:
        mapit_result = r.json()
        raise BadPostcodeException(mapit_result['error'])
    elif r.status_code == 404:
        raise BadPostcodeException(
            _('The postcode “{0}” couldn’t be found').format(original_postcode)
        )
    else:
        raise UnknownMapitException(
            _('Unknown MapIt error for postcode "{0}"').format(
                original_postcode
            )
        )

Example 20

Project: yournextrepresentative
Source File: mapit.py
View license
    def load_data(self, code):
        data = requests.get(urljoin(self.mapit_base_url, "areas/{code}".format(
            mapit_base_url=self.mapit_base_url, code=code
        ))).json()
        self.update(data)

Example 21

Project: yournextrepresentative
Source File: mapit.py
View license
    def __getitem__(self, key):
        key = str(key)
        try:
            return super(MapitLookup, self).__getitem__(key)
        except KeyError:
            try:
                logging.info("Making extra request to Mapit for ID {0}".format(
                    key
                ))
                url = urljoin(self.mapit_base_url,
                              "area/{key}".format(key=key))
                req = requests.get(url)
                if req.status_code == 404 or \
                        req.json()[key].get('code', 200) == 404:
                    raise MapItAreaNotFoundException
                self.update({key: req.json()})
                return self[key]
            except MapItAreaNotFoundException:
                raise KeyError
            except Exception:
                raise

Example 22

View license
def fake_requests_for_mapit(url):
    """Return reduced MapIt output for some known URLs"""
    if url == urljoin(settings.MAPIT_BASE_URL, '/postcode/sw1a1aa'):
        status_code = 200
        json_result = sw1a1aa_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/se240ag'):
        status_code = 200
        json_result = se240ag_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/cb28rq'):
        status_code = 404
        json_result = {
            "code": 404,
            "error": "No Postcode matches the given query."
        }
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/foobar'):
        status_code = 400
        json_result = {
            "code": 400,
            "error": "Postcode 'FOOBAR' is not valid."
        }
    else:
        raise Exception("URL that hasn't been mocked yet: " + url)
    return Mock(**{
        'json.return_value': json_result,
        'status_code': status_code
    })

Example 23

View license
    def handle(self, **options):

        mapit_url = settings.MAPIT_BASE_URL

        self.gb_parties, _ = PartySet.objects.get_or_create(
            slug='gb', defaults={'name': 'Great Britain'}
        )

        self.organizations = {}

        self.base_election_info = {
            'name': 'Police and Crime Commissioner Elections 2016',
            'for_post_role': 'Police and Crime Commissioner',
            'label_format': 'Police and Crime Commissioner for {area_name}',
            'area_generation': 1,
            'election_date': date(2016, 5, 5),
            'party_lists_in_use': False,
            'mapit_code': 'PDG',
            'electon_id_prefix': 'pcc',
            # 'area_type_description': 'Police Force',
        }

        url_path = '/areas/' + self.base_election_info['mapit_code']
        url = urljoin(mapit_url, url_path)
        r = requests.get(url)
        mapit_results = r.json().items()

        # First make all the organisations
        for mapit_area_id, mapit_area_data in mapit_results:
            if mapit_area_data['codes']['police_id'] == "metropolitan":
                continue
            self.add_police_force_orgs(mapit_area_id, mapit_area_data)

        # Create a single election
        self.create_pcc_election()
        # Add all the areas for that election
        for mapit_area_id, mapit_area_data in mapit_results:
            if mapit_area_data['codes']['police_id'] == "metropolitan":
                # The Met doesn't have a PCCge
                continue
            self.add_pcc_areas(mapit_area_id, mapit_area_data)

Example 24

View license
    def handle(self, **options):

        mapit_url = settings.MAPIT_BASE_URL

        gb_parties, _ = PartySet.objects.get_or_create(
            slug='gb', defaults={'name': 'Great Britain'}
        )
        ni_parties, _ = PartySet.objects.get_or_create(
            slug='ni', defaults={'name': 'Northern Ireland'}
        )

        elections = {
            'sp-2016-05-05-r': {
                'name': '2016 Scottish Parliament Election (Regions)',
                'for_post_role': 'Member of the Scottish Parliament',
                'label_format': 'Member of the Scottish Parliament for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': True,
                'mapit_code': 'SPE',
                'area_type_description': 'Scottish Parliament region',
                'organization_slug': 'scottish-parliament',
                'organization_name': 'Scottish Parliament',
            },
            'sp-2016-05-05-c': {
                'name': '2016 Scottish Parliament Election (Constituencies)',
                'for_post_role': 'Member of the Scottish Parliament',
                'label_format': 'Member of the Scottish Parliament for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'SPC',
                'area_type_description': 'Scottish Parliament constituency',
                'organization_slug': 'scottish-parliament',
                'organization_name': 'Scottish Parliament',
            },
            'naw-2016-05-05-r': {
                'name': '2016 Welsh Assembly Election (Regions)',
                'for_post_role': 'Member of the National Assembly for Wales',
                'label_format': 'Assembly Member for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': True,
                'mapit_code': 'WAE',
                'area_type_description': 'Welsh Assembly region',
                'organization_slug': 'welsh-assembly',
                'organization_name': 'National Assembly for Wales',
            },
            'naw-2016-05-05-c': {
                'name': '2016 Welsh Assembly Election (Constituencies)',
                'for_post_role': 'Member of the National Assembly for Wales',
                'label_format': 'Assembly Member for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'WAC',
                'area_type_description': 'Welsh Assembly constituency',
                'organization_slug': 'welsh-assembly',
                'organization_name': 'National Assembly for Wales',
            },
            'nia-2016-05-05': {
                'name': '2016 Northern Ireland Assembly Election',
                'for_post_role': 'Member of the Legislative Assembly',
                'label_format': 'Member of the Legislative Assembly for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'NIE',
                'area_type_description': 'Northern Ireland Assembly constituency',
                'organization_slug': 'northern-ireland-assembly',
                'organization_name': 'Northern Ireland Assembly',
            },
            'gla-2016-05-05-c': {
                'name': '2016 London Assembly Election (Constituencies)',
                'for_post_role': 'Member of the London Assembly',
                'label_format': 'Assembly Member for {area_name}',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'LAC',
                'area_type_description': 'London Assembly constituency',
                'organization_slug': 'london-assembly',
                'organization_name': 'London Assembly',
            },
            'gla-2016-05-05-a': {
                'name': '2016 London Assembly Election (Additional)',
                'for_post_role': 'Member of the London Assembly',
                'label_format': 'Assembly Member',
                'area_generation': 22,
                'election_date': date(2016, 5, 5),
                'party_lists_in_use': False,
                'mapit_code': 'GLA',
                'area_type_description': 'London Assembly constituency',
                'organization_slug': 'london-assembly',
                'organization_name': 'London Assembly',
            },
        }

        for election_slug, data in elections.items():
            # Make sure the parliament Organization and
            # OrganizationExtra objects exist:
            try:
                organization_extra = OrganizationExtra.objects.get(
                    slug=data['organization_slug']
                )
                organization = organization_extra.base
            except OrganizationExtra.DoesNotExist:
                organization = Organization.objects.create(
                    name=data['organization_name']
                )
                organization_extra = OrganizationExtra.objects.create(
                    base=organization,
                    slug=data['organization_slug']
                )
            # Make sure the Election object exists:
            election_defaults = {
                k: data[k] for k in
                [
                    'name', 'for_post_role', 'area_generation', 'election_date',
                    'party_lists_in_use',
                ]
            }
            election_defaults['current'] = True
            election_defaults['candidate_membership_role'] = 'Candidate'
            print('Creating:', election_defaults['name'], '...',)
            election, created = Election.objects.update_or_create(
                slug=election_slug,
                defaults=election_defaults
            )
            if created:
                print('[created]')
            else:
                print('[already existed]')

            area_type, _ = AreaType.objects.update_or_create(
                name=data['mapit_code'], defaults={'source': 'MapIt'}
            )
            if not election.area_types.filter(name=area_type.name).exists():
                election.area_types.add(area_type)
            url_path = '/areas/' + data['mapit_code']
            url = urljoin(mapit_url, url_path)
            r = requests.get(url)
            for mapit_area_id, mapit_area_data in r.json().items():

                area, _ = Area.objects.update_or_create(
                    identifier=str(mapit.format_code_from_area(mapit_area_data)),
                    defaults={'name': mapit_area_data['name']}
                )
                # Now make sure that the MapIt codes are present as identifiers:
                for scheme, identifier in mapit_area_data['codes'].items():
                    area.other_identifiers.update_or_create(
                        scheme=scheme,
                        defaults={'identifier': identifier},
                    )

                AreaExtra.objects.get_or_create(base=area, type=area_type)
                post, _ = Post.objects.update_or_create(
                    organization=organization,
                    area=area,
                    role='Member of the Scottish Parliament',
                    defaults={
                        'label': data['label_format'].format(
                            area_name=area.name
                        )
                    }
                )
                if mapit_area_data['country_name'] == 'Northern Ireland':
                    party_set = ni_parties
                else:
                    party_set = gb_parties
                post_extra, _ = PostExtra.objects.update_or_create(
                    base=post,
                    defaults={
                        'slug': str(mapit.format_code_from_area(mapit_area_data)),
                        'party_set': party_set,
                    },
                )
                PostExtraElection.objects.update_or_create(
                    postextra=post_extra,
                    election=election
                )

Example 25

View license
    def upload_images(self, emblems, party_extra):
        content_type = ContentType.objects.get_for_model(party_extra)
        sort_emblems(emblems, party_extra.slug)
        primary = True
        for emblem in emblems:
            emblem_id = str(emblem['Id'])
            ntf = NamedTemporaryFile(delete=False)
            image_url = urljoin(base_emblem_url, emblem_id)
            r = requests.get(image_url)
            with open(ntf.name, 'w') as f:
                f.write(r.content)
            mime_type = self.mime_type_magic.from_file(ntf.name)
            extension = mimetypes.guess_extension(mime_type)
            leafname = 'Emblem_{0}{1}'.format(emblem_id, extension)
            desired_storage_path = join('images', leafname)
            fname = join(emblem_directory, leafname)
            move(ntf.name, fname)
            md5sum = get_file_md5sum(fname)
            ImageExtra.objects.update_or_create_from_file(
                fname,
                desired_storage_path,
                md5sum=md5sum,
                base__object_id=party_extra.id,
                base__content_type_id=content_type.id,
                defaults={
                    'uploading_user':None,
                    'notes': emblem['MonochromeDescription'],
                    'base__source': 'The Electoral Commission',
                    'base__is_primary': primary,
                }
            )
            primary = False

Example 26

Project: yournextrepresentative
Source File: mapit.py
View license
def get_areas_from_postcode(original_postcode):
    postcode = re.sub(r'(?ms)\s*', '', original_postcode.lower())
    if re.search(r'[^a-z0-9]', postcode):
        raise BadPostcodeException(
            _('There were disallowed characters in "{0}"').format(original_postcode)
        )
    cache_key = 'mapit-postcode:' + postcode
    cached_result = cache.get(cache_key)
    if cached_result:
        return cached_result
    url = urljoin(settings.MAPIT_BASE_URL,
                  '/postcode/{0}'.format(urlquote(postcode)))
    r = requests.get(url)
    if r.status_code == 200:
        mapit_result = r.json()

        result = sorted(
            [
                (a['type'], format_code_from_area(a))
                for a in mapit_result['areas'].values()
                if Area.objects.filter(
                        extra__type__name=a['type'],
                        identifier=format_code_from_area(a)
                ).exists()
            ],
            key=area_sort_key
        )

        cache.set(cache_key, result, settings.MAPIT_CACHE_SECONDS)
        return result
    elif r.status_code == 400:
        mapit_result = r.json()
        raise BadPostcodeException(mapit_result['error'])
    elif r.status_code == 404:
        raise BadPostcodeException(
            _('The postcode “{0}” couldn’t be found').format(original_postcode)
        )
    else:
        raise UnknownMapitException(
            _('Unknown MapIt error for postcode "{0}"').format(
                original_postcode
            )
        )

Example 27

Project: yournextrepresentative
Source File: mapit.py
View license
def get_wmc_from_postcode(original_postcode):
    postcode = re.sub(r'(?ms)\s*', '', original_postcode.lower())
    if re.search(r'[^a-z0-9]', postcode):
        raise BadPostcodeException(
            _('There were disallowed characters in "{0}"').format(original_postcode)
        )
    cached_result = cache.get(postcode)
    if cached_result:
        return cached_result
    url = urljoin(settings.MAPIT_BASE_URL,
                  '/postcode/{0}'.format(urlquote(postcode)))
    r = requests.get(url)
    if r.status_code == 200:
        mapit_result = r.json()
        wmc = mapit_result.get('shortcuts', {}).get('WMC')
        if not wmc:
            raise NoConstituencyForPostcodeException(
                _('No constituency found for the postcode "{0}"').format(
                    original_postcode
                )
            )
        cache.set(postcode, wmc, settings.MAPIT_CACHE_SECONDS)
        return wmc
    elif r.status_code == 400:
        mapit_result = r.json()
        raise BadPostcodeException(mapit_result['error'])
    elif r.status_code == 404:
        raise BadPostcodeException(
            _('The postcode “{0}” couldn’t be found').format(original_postcode)
        )
    else:
        raise UnknownMapitException(
            _('Unknown MapIt error for postcode "{0}"').format(
                original_postcode
            )
        )

Example 28

Project: yournextrepresentative
Source File: mapit.py
View license
    def load_data(self, code):
        data = requests.get(urljoin(self.mapit_base_url, "areas/{code}".format(
            mapit_base_url=self.mapit_base_url, code=code
        ))).json()
        self.update(data)

Example 29

Project: yournextrepresentative
Source File: mapit.py
View license
    def __getitem__(self, key):
        key = str(key)
        try:
            return super(MapitLookup, self).__getitem__(key)
        except KeyError:
            try:
                logging.info("Making extra request to Mapit for ID {0}".format(
                    key
                ))
                url = urljoin(self.mapit_base_url,
                              "area/{key}".format(key=key))
                req = requests.get(url)
                if req.status_code == 404 or \
                        req.json()[key].get('code', 200) == 404:
                    raise MapItAreaNotFoundException
                self.update({key: req.json()})
                return self[key]
            except MapItAreaNotFoundException:
                raise KeyError
            except Exception:
                raise

Example 30

View license
def fake_requests_for_mapit(url):
    """Return reduced MapIt output for some known URLs"""
    if url == urljoin(settings.MAPIT_BASE_URL, '/postcode/sw1a1aa'):
        status_code = 200
        json_result = sw1a1aa_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/se240ag'):
        status_code = 200
        json_result = se240ag_result
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/cb28rq'):
        status_code = 404
        json_result = {
            "code": 404,
            "error": "No Postcode matches the given query."
        }
    elif url == urljoin(settings.MAPIT_BASE_URL, '/postcode/foobar'):
        status_code = 400
        json_result = {
            "code": 400,
            "error": "Postcode 'FOOBAR' is not valid."
        }
    else:
        raise Exception("URL that hasn't been mocked yet: " + url)
    return Mock(**{
        'json.return_value': json_result,
        'status_code': status_code
    })