示例#1
0
class PartyValidationForm(forms.Form):
    description = forms.URLField(required=False,
                                 validators=[validate_user_url])
    name = forms.CharField(required=False, max_length=100)
    organization = forms.CharField(max_length=200, required=False)
    email = forms.EmailField(required=False)
    address = forms.CharField(max_length=250, required=False)
    phone = forms.CharField(max_length=25, required=False)
    homepage = forms.URLField(required=False)

    def clean_description(self):
        user_absolute_url = self.cleaned_data['description']
        if user_absolute_url:
            url_parts = user_absolute_url.split('/')
            return '/user/{user_id}/'.format(user_id=url_parts[4])
        return user_absolute_url

    def clean(self):
        cleaned_data = super(PartyValidationForm, self).clean()
        name = cleaned_data.get('name', None)
        org = cleaned_data.get('organization', None)
        if not org:
            if not name or len(name.strip()) == 0:
                self._errors['name'] = [
                    "A value for name or organization is required but both "
                    "are missing"
                ]

        return self.cleaned_data
示例#2
0
class PartyValidationForm(forms.Form):
    """Validate form for Party models."""

    description = forms.CharField(required=False,
                                  validators=[validate_user_url])
    name = forms.CharField(required=False, max_length=100)
    organization = forms.CharField(max_length=200, required=False)
    email = forms.EmailField(required=False)
    address = forms.CharField(max_length=250, required=False)
    phone = forms.CharField(max_length=25, required=False)
    homepage = forms.URLField(required=False)
    identifiers = forms.CharField(required=False)

    def clean_description(self):
        """Create absolute URL for Party.description field."""
        user_absolute_url = self.cleaned_data['description']
        if user_absolute_url:
            url_parts = user_absolute_url.split('/')
            if len(url_parts) > 4:
                return '/user/{user_id}/'.format(user_id=url_parts[4])
        return user_absolute_url

    def clean_identifiers(self):
        data = self.cleaned_data['identifiers']
        return Party.validate_identifiers(data)

    def clean(self):
        """Validate that name and/or organization are present in form data."""
        cleaned_data = super(PartyValidationForm, self).clean()
        name = cleaned_data.get('name', None)
        org = cleaned_data.get('organization', None)
        if not org:
            if not name or len(name.strip()) == 0:
                self._errors['name'] = [
                    "A value for name or organization is required but both "
                    "are missing"
                ]

        return self.cleaned_data