Exemple #1
0
 def save(self, domain_override=""):
     """
     Create the new ``User`` and ``RegistrationProfile``, and
     returns the ``User`` (by calling
     ``RegistrationProfile.objects.create_inactive_user()``).
     
     """
     email = self.cleaned_data['email']
     postcode = self.cleaned_data['postcode']
     can_cc = self.cleaned_data['can_cc']
     first_name = self.cleaned_data['first_name']
     last_name = self.cleaned_data['last_name']
     user = CustomUser.objects.create(username=email,
                                      email=email,
                                      postcode=postcode,
                                      can_cc=can_cc,
                                      first_name=first_name,
                                      last_name=last_name,
                                      is_active=False)
     constituency = Constituency.objects.all()\
                    .filter(name=twfy.getConstituency(postcode))\
                    .filter(year=CONSTITUENCY_YEAR)
     if constituency:
         user.constituencies.add(constituency[0])
     user.save()
     profile = RegistrationProfile.objects.create_profile(user)
     return profile
Exemple #2
0
def constituency(place):
    try:
        if POSTCODE_RE.match(place):
            return twfy.getConstituency(place)
        else:
            _, (lat, lng) = geocode(place)
            consts = twfy.getConstituencies(latitude=lat, longitude=lng, distance=10)
            if len(consts) > 0:
                return consts[0]
    except Exception:
        return None
Exemple #3
0
def constituency(place):
    try:
        if POSTCODE_RE.match(place):
            return twfy.getConstituency(place)
        else:
            _, (lat, lng) = geocode(place)
            consts = twfy.getConstituencies(latitude=lat,
                                            longitude=lng,
                                            distance=10)
            if len(consts) > 0:
                return consts[0]
    except Exception:
        return None
    def clean_postcode(self):
        code = self.cleaned_data['postcode']
        if not POSTCODE_RE.match(code):
            raise forms.ValidationError("Please enter a valid postcode")
        try:
            constituency_name = twfy.getConstituency(code)
        except (IOError, ValueError): # ValueError may be thrown by
                                      # JSON decoding                                      
            raise forms.ValidationError("Sorry, there was a problem connecting to TheyWorkForYou to look up your constituency. Please try again in a few minutes.")

        if constituency_name:
            constituency = Constituency.objects.all()\
                           .filter(name=constituency_name)\
                           .filter(year=CONSTITUENCY_YEAR)
            if constituency:
                self.cleaned_data['constituency'] = constituency[0]
            else:
                raise forms.ValidationError("Internal error: Constituency '%s', year '%s' not found in DemocracyClub database" % (constituency_name, CONSTITUENCY_YEAR))
        else:
            raise forms.ValidationError("Unknown postcode")
        return code
def constituency(place):
    try:
        if POSTCODE_RE.match(place):
            const = twfy.getConstituency(place)
            if const == None:
                return None
            else:
                return [const]
        else:
            _, (lat, lng) = geocode(place)
            
            consts = None
            distance = 10
            while not consts:
                # Only likely to actually loop in extreme, circumstances,
                # e.g. "Haltwhistle". See issue 19
                consts = twfy.getConstituencies(latitude=lat,
                                                longitude=lng,
                                                distance=distance)
                distance = distance * 2
            
            return [x['name'] for x in consts]
    except Exception:
        return None