コード例 #1
0
ファイル: views.py プロジェクト: andymckay/openparliament
def try_postcode_first(request):
    match = r_postcode.search(request.GET['q'].upper())
    if match:
        postcode = match.group(1) + " " + match.group(2)
        try:
            x = InternalXref.objects.filter(schema='edid_postcode', text_value=postcode)[0]
            edid = x.target_id
        except IndexError:
            edid = postcode_to_edid(postcode)
            if edid:
                InternalXref.objects.get_or_create(schema='edid_postcode', text_value=postcode, target_id=edid)
        if edid:
            try:
                member = ElectedMember.objects.get(end_date__isnull=True, riding__edid=edid)
                return adaptive_redirect(request, member.politician.get_absolute_url())
            except ElectedMember.DoesNotExist:
                return flatpage_response(request, u"Ain’t nobody lookin’ out for you",
                    mark_safe(u"""It looks like that postal code is in the riding of %s. There is no current
                    Member of Parliament for that riding. By law, a byelection must be called within
                    180 days of a resignation causing a vacancy. (If you think we’ve got our facts
                    wrong about your riding or MP, please send an <a class='maillink'>e-mail</a>.)"""
                    % Riding.objects.get(edid=edid).dashed_name))
            except ElectedMember.MultipleObjectsReturned:
                raise Exception("Too many MPs for postcode %s" % postcode)
    return False
コード例 #2
0
ファイル: views.py プロジェクト: annieying/openparliament
def try_postcode_first(request):
    match = r_postcode.search(request.GET['q'].upper())
    if match:
        postcode = match.group(1) + " " + match.group(2)
        try:
            x = InternalXref.objects.filter(schema='edid_postcode',
                                            text_value=postcode)[0]
            edid = x.target_id
        except IndexError:
            edid = postcode_to_edid(postcode)
            if edid:
                InternalXref.objects.get_or_create(schema='edid_postcode',
                                                   text_value=postcode,
                                                   target_id=edid)
        if edid:
            try:
                member = ElectedMember.objects.get(end_date__isnull=True,
                                                   riding__edid=edid)
                return adaptive_redirect(request,
                                         member.politician.get_absolute_url())
            except ElectedMember.DoesNotExist:
                return flatpage_response(
                    request, u"Ain’t nobody lookin’ out for you",
                    mark_safe(
                        u"""It looks like that postal code is in the riding of %s. There is no current
                    Member of Parliament for that riding. By law, a byelection must be called within
                    180 days of a resignation causing a vacancy. (If you think we’ve got our facts
                    wrong about your riding or MP, please send an <a class='maillink'>e-mail</a>.)"""
                        % Riding.objects.get(edid=edid).dashed_name))
            except ElectedMember.MultipleObjectsReturned:
                raise Exception("Too many MPs for postcode %s" % postcode)
    return False
コード例 #3
0
def try_postcode_first(request):
    match = r_postcode.search(request.GET['q'].upper())
    if not match:
        return False
    postcode = match.group(1) + match.group(2)
    try:
        x = InternalXref.objects.filter(schema='edid_postcode',
                                        text_value=postcode)[0]
        edid = x.target_id
    except IndexError:
        try:
            edid = postcode_to_edid_ec(postcode)
            assert edid
            InternalXref.objects.get_or_create(schema='edid_postcode',
                                               text_value=postcode,
                                               target_id=edid)
        except AmbiguousPostcodeException as e:
            ec_url = e.ec_url if e.ec_url else 'http://elections.ca/'
            return flatpage_response(
                request, u"You’ve got a confusing postcode",
                mark_safe(
                    u"""Some postal codes might cross riding boundaries. It looks like yours is one of them.
                    If you need to find out who your MP is, visit <a href="%s">this Elections Canada page</a> and
                    tell them your full address.""" % ec_url))
        except Exception:
            logger.exception("elections.ca problem",
                             extra={'request': request})
            edid = postcode_to_edid_represent(postcode)
    if not edid:
        return flatpage_response(
            request, u"Can’t find that postcode",
            mark_safe(
                u"""We’re having trouble figuring out where that postcode is.
                Try asking <a href="http://elections.ca/">Elections Canada</a> who your MP is."""
            ))
    try:
        member = ElectedMember.objects.get(end_date__isnull=True,
                                           riding__edid=edid)
        return adaptive_redirect(request, member.politician.get_absolute_url())
    except ElectedMember.DoesNotExist:
        return flatpage_response(
            request, u"Ain’t nobody lookin’ out for you",
            mark_safe(
                u"""It looks like that postal code is in the riding of %s. There is no current
            Member of Parliament for that riding. By law, a byelection must be called within
            180 days of a resignation causing a vacancy. (If you think we’ve got our facts
            wrong about your riding or MP, please send an <a class='maillink'>e-mail</a>.)"""
                % Riding.objects.get(edid=edid).dashed_name))
    except ElectedMember.MultipleObjectsReturned:
        raise Exception("Too many MPs for postcode %s" % postcode)
コード例 #4
0
ファイル: views.py プロジェクト: mtanjir/openparliament
def try_postcode_first(request):
    match = r_postcode.search(request.GET['q'].upper())
    if not match:
        return False
    postcode = match.group(1) + match.group(2)
    try:
        x = InternalXref.objects.filter(schema='edid_postcode', text_value=postcode)[0]
        edid = x.target_id
    except IndexError:
        try:
            edid = postcode_to_edid_ec(postcode)
            assert edid
            InternalXref.objects.get_or_create(schema='edid_postcode', text_value=postcode, target_id=edid)
        except AmbiguousPostcodeException as e:
            ec_url = e.ec_url if e.ec_url else 'http://elections.ca/'
            return flatpage_response(request, u"You’ve got a confusing postcode",
                mark_safe(u"""Some postal codes might cross riding boundaries. It looks like yours is one of them.
                    If you need to find out who your MP is, visit <a href="%s">this Elections Canada page</a> and
                    tell them your full address.""" % ec_url))
        except Exception:
            logger.exception("elections.ca problem")
            edid = postcode_to_edid_represent(postcode)
    if not edid:
        return flatpage_response(request, u"Can’t find that postcode",
            mark_safe(u"""We’re having trouble figuring out where that postcode is.
                Try asking <a href="http://elections.ca/">Elections Canada</a> who your MP is."""))
    try:
        member = ElectedMember.objects.get(end_date__isnull=True, riding__edid=edid)
        return adaptive_redirect(request, member.politician.get_absolute_url())
    except ElectedMember.DoesNotExist:
        return flatpage_response(request, u"Ain’t nobody lookin’ out for you",
            mark_safe(u"""It looks like that postal code is in the riding of %s. There is no current
            Member of Parliament for that riding. By law, a byelection must be called within
            180 days of a resignation causing a vacancy. (If you think we’ve got our facts
            wrong about your riding or MP, please send an <a class='maillink'>e-mail</a>.)"""
            % Riding.objects.get(edid=edid).dashed_name))
    except ElectedMember.MultipleObjectsReturned:
        raise Exception("Too many MPs for postcode %s" % postcode)