Exemplo n.º 1
0
class StationProfileForm(forms.Form, Id2Model):
    name = forms.CharField(label=_("Station name"))
    password = forms.CharField(label=_("Change password"), widget=forms.PasswordInput(), required=False)
    password2 = forms.CharField(label=_("Re-enter password"), widget=forms.PasswordInput(), required=False)
    country_id = forms.IntegerField(widget=forms.Select(choices=Country.country_choices()), label=_("Country"))
    city_id = forms.IntegerField(widget=forms.Select(choices=[("", "-------------")]), label=_("City"))
    address = forms.CharField(label=_("Address"))
    number_of_taxis = forms.RegexField(regex=r'^\d+$',
                                       max_length=4,
                                       widget=forms.TextInput(),
                                       label=_("Number of taxis"),
                                       error_messages={'invalid': _("The value must contain only numbers.")})

    website_url = forms.CharField(label=_("Website URL"), required=False)
    #    language = form.
    email = forms.EmailField(label=_("Email"))
    #    logo = AppEngineImageField(label=_("Logo"), required=False)
    description = forms.CharField(label=_("Description"), widget=forms.Textarea, required=False)
    lon = forms.FloatField(widget=forms.HiddenInput(), required=False)
    lat = forms.FloatField(widget=forms.HiddenInput(), required=False)

    class Ajax:
        rules = [
                ('password2', {'equal_to_field': 'password'}),
                ]
        messages = [
                ('password2', {'equal_to_field': _("The two password fields didn't match.")}),
                ]

    def clean_address(self):
        if not INITIAL_DATA in self.data:
            if not (self.data['lon'] and self.data['lat']):
                raise forms.ValidationError(_("Invalid address"))

        return self.cleaned_data['address']


    def clean(self):
        """
        """
        self.id_field_to_model('country_id', Country)
        self.id_field_to_model('city_id', City)

        self.cleaned_data['city'] = self.cleaned_data['city_id']
        self.cleaned_data['country'] = self.cleaned_data['country_id']

        return self.cleaned_data
Exemplo n.º 2
0
class StationRegistrationForm(forms.Form):
    """
    Form for registering a new user account.

    Validates that the requested username is not already in use, and
    requires the password to be entered twice to catch typos.

    Subclasses should feel free to add any additional validation they
    need, but should avoid defining a ``save()`` method -- the actual
    saving of collected user data is delegated to the active
    registration backend.

    """
    first_name = forms.CharField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=30)),
        label=_("first name"))
    last_name = forms.CharField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=30)),
        label=_("last name"))
    name = forms.CharField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=50)),
        label=_("station name"))

    license_number = forms.CharField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=30)),
        label=_("license number"))

    username = forms.RegexField(
        regex=r'^\w+$',
        max_length=30,
        widget=forms.TextInput(attrs=attrs_dict),
        label=_("username"),
        error_messages={
            'invalid':
            _("This value must contain only letters, numbers and underscores.")
        })

    password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict,
                                                           render_value=False),
                                label=_("password"))

    password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict,
                                                           render_value=False),
                                label=_("password (again)"))
    email = forms.EmailField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=75)),
        label=_("email address"))

    default_country = Country.objects.get(code=settings.DEFAULT_COUNTRY_CODE)
    country = forms.IntegerField(widget=forms.Select(
        attrs=attrs_dict, choices=Country.country_choices()),
                                 label=_("country"))
    country.initial = default_country.id

    city_choices = [(c.id, c.name)
                    for c in City.objects.filter(country=default_country)]
    city = forms.IntegerField(widget=forms.Select(attrs=attrs_dict,
                                                  choices=city_choices),
                              label=_("City"))

    address = forms.CharField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=80)),
        label=_("address"))
    language_choice = LANGUAGE_CHOICES
    language = forms.IntegerField(widget=forms.Select(attrs=attrs_dict,
                                                      choices=language_choice),
                                  label=_("language"))

    website_url = forms.CharField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=255)),
        label=_("website url"),
        required=False)

    local_phone = forms.RegexField(
        regex=r'^\d+$',
        max_length=20,
        widget=forms.TextInput(attrs=attrs_dict),
        label=_("local phone number"),
        error_messages={'invalid': _("The value must contain only numbers.")})

    logo = forms.FileField(widget=BlobWidget(attrs=dict(attrs_dict)),
                           label=_("logo"),
                           required=False)

    number_of_taxis = forms.IntegerField(
        widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=30)),
        label=_("number of taxis"))

    description = forms.CharField(
        widget=forms.Textarea(attrs=dict(attrs_dict, maxlength=80)),
        label=_("description"),
        required=False)

    def clean_username(self):
        """
        Validate that the username is alphanumeric and is not already
        in use.
        
        """
        try:
            user = User.objects.get(username=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(
            _("A user with that username already exists."))

    def clean(self):
        """
        Verifiy that the values entered into the two password fields
        match. Note that an error here will end up in
        ``non_field_errors()`` because it doesn't apply to a single
        field.
        
        """
        if 'city' in self.cleaned_data:
            city = City.objects.get(id=self.cleaned_data['city'])
            self.cleaned_data['city'] = city

        if 'country' in self.cleaned_data:
            country = Country.objects.get(id=self.cleaned_data['country'])
            self.cleaned_data['country'] = country

        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(
                    _("The two password fields didn't match."))
        return self.cleaned_data