示例#1
0
文件: forms.py 项目: zullkay/kuma
class UserEditForm(forms.ModelForm):
    """
    The main form to edit user profile data.

    It dynamically adds a bunch of fields for maintaining information
    about a user's websites and handles expertise and interests fields
    specially.
    """
    timezone = TimezoneChoiceField(
        label=_(u'Timezone'),
        initial=settings.TIME_ZONE,
        choices=COMMON_GROUPED_CHOICES,
        required=False,
    )
    beta = forms.BooleanField(
        label=_(u'Beta tester'),
        required=False,
    )
    interests = forms.CharField(
        label=_(u'Interests'),
        max_length=255,
        required=False,
        widget=forms.TextInput(attrs={'class': 'tags'}),
    )
    expertise = forms.CharField(
        label=_(u'Expertise'),
        max_length=255,
        required=False,
        widget=forms.TextInput(attrs={'class': 'tags'}),
    )
    username = forms.RegexField(
        label=_(u'Username'),
        regex=USERNAME_REGEX,
        max_length=30,
        required=False,
        error_message=USERNAME_CHARACTERS,
    )
    website_url = forms.CharField(
        label=_('Website'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['website']],
        widget=forms.TextInput(attrs={
            'placeholder': 'http://',
            'data-fa-icon': 'icon-link',
        }),
    )
    twitter_url = forms.CharField(
        label=_('Twitter'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['twitter']],
        widget=forms.TextInput(
            attrs={
                'placeholder': 'https://twitter.com/',
                'data-fa-icon': 'icon-twitter',
            }),
    )
    github_url = forms.CharField(
        label=_('GitHub'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['github']],
        widget=forms.TextInput(attrs={
            'placeholder': 'https://github.com/',
            'data-fa-icon': 'icon-github',
        }),
    )
    stackoverflow_url = forms.CharField(
        label=_('Stack Overflow'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['stackoverflow']],
        widget=forms.TextInput(
            attrs={
                'placeholder': 'https://stackoverflow.com/users/',
                'data-fa-icon': 'icon-stackexchange',
            }),
    )
    linkedin_url = forms.CharField(
        label=_('LinkedIn'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['linkedin']],
        widget=forms.TextInput(
            attrs={
                'placeholder': 'https://www.linkedin.com/',
                'data-fa-icon': 'icon-linkedin',
            }),
    )
    mozillians_url = forms.CharField(
        label=_('Mozillians'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['mozillians']],
        widget=forms.TextInput(
            attrs={
                'placeholder': 'https://mozillians.org/u/',
                'data-fa-icon': 'icon-group',
            }),
    )
    facebook_url = forms.CharField(
        label=_('Facebook'),
        required=False,
        validators=[User.WEBSITE_VALIDATORS['facebook']],
        widget=forms.TextInput(
            attrs={
                'placeholder': 'https://www.facebook.com/',
                'data-fa-icon': 'icon-facebook',
            }),
    )

    class Meta:
        model = User
        fields = ('fullname', 'title', 'organization', 'location', 'locale',
                  'timezone', 'bio', 'irc_nickname', 'interests',
                  'website_url', 'twitter_url', 'github_url',
                  'stackoverflow_url', 'linkedin_url', 'mozillians_url',
                  'facebook_url')

    def __init__(self, *args, **kwargs):
        super(UserEditForm, self).__init__(*args, **kwargs)
        # in case the username is not changed and the user has a legacy
        # username we want to disarm the username regex
        if ('username' not in self.changed_data and self.instance
                and self.instance.has_legacy_username):
            self.fields['username'].regex = USERNAME_LEGACY_REGEX

    def clean_expertise(self):
        """Enforce expertise as a subset of interests"""
        # bug 709938 - don't assume interests passed validation
        interests = set(parse_tags(self.cleaned_data.get('interests', '')))
        expertise = set(parse_tags(self.cleaned_data['expertise']))

        if len(expertise) > 0 and not expertise.issubset(interests):
            raise forms.ValidationError(
                _("Areas of expertise must be a "
                  "subset of interests"))

        return self.cleaned_data['expertise']

    def clean_username(self):
        new_username = self.cleaned_data['username']

        if (self.instance is not None and User.objects.exclude(
                pk=self.instance.pk).filter(username=new_username).exists()):
            raise forms.ValidationError(_('Username already in use.'))
        return new_username
示例#2
0
 def test_invalid_value(self):
     field = TimezoneChoiceField(choices=ALL_CHOICES)
     self.assertRaises(ValidationError, field.clean, 'invalid')
示例#3
0
 def test_coercing(self):
     field = TimezoneChoiceField(choices=ALL_CHOICES)
     self.assertEqual(field.clean(settings.TIME_ZONE),
                      pytz.timezone(settings.TIME_ZONE))
示例#4
0
 def test_empty_values(self):
     field = TimezoneChoiceField(choices=ALL_CHOICES, required=False)
     self.assertEqual(field.clean(''), '')
     self.assertEqual(field.clean(None), '')
示例#5
0
class UserEditForm(forms.ModelForm):
    """
    The main form to edit user profile data.

    It dynamically adds a bunch of fields for maintaining information
    about a user's websites
    """

    timezone = TimezoneChoiceField(
        label=_("Timezone"),
        initial=settings.TIME_ZONE,
        choices=COMMON_GROUPED_CHOICES,
        required=False,
    )
    beta = forms.BooleanField(
        label=_("Beta tester"),
        required=False,
    )
    username = forms.RegexField(
        label=_("Username"),
        regex=USERNAME_REGEX,
        max_length=30,
        required=False,
        error_messages={"invalid": USERNAME_CHARACTERS},
    )
    twitter_url = forms.CharField(
        label=_("Twitter"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["twitter"]],
        widget=forms.TextInput(
            attrs={
                "placeholder": "https://twitter.com/",
                "data-fa-icon": "icon-twitter",
            }),
    )
    github_url = forms.CharField(
        label=_("GitHub"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["github"]],
        widget=forms.TextInput(attrs={
            "placeholder": "https://github.com/",
            "data-fa-icon": "icon-github"
        }),
    )
    is_github_url_public = forms.BooleanField(
        label=_("Display a link to GitHub on my profile"), required=False)
    stackoverflow_url = forms.CharField(
        label=_("Stack Overflow"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["stackoverflow"]],
        widget=forms.TextInput(
            attrs={
                "placeholder": "https://stackoverflow.com/users/",
                "data-fa-icon": "icon-stackexchange",
            }),
    )
    linkedin_url = forms.CharField(
        label=_("LinkedIn"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["linkedin"]],
        widget=forms.TextInput(
            attrs={
                "placeholder": "https://www.linkedin.com/",
                "data-fa-icon": "icon-linkedin",
            }),
    )
    mozillians_url = forms.CharField(
        label=_("Mozillians"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["mozillians"]],
        widget=forms.TextInput(
            attrs={
                "placeholder": "https://mozillians.org/u/",
                "data-fa-icon": "icon-group",
            }),
    )
    facebook_url = forms.CharField(
        label=_("Facebook"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["facebook"]],
        widget=forms.TextInput(
            attrs={
                "placeholder": "https://www.facebook.com/",
                "data-fa-icon": "icon-facebook",
            }),
    )
    discourse_url = forms.CharField(
        label=_("Discourse"),
        required=False,
        validators=[User.WEBSITE_VALIDATORS["discourse"]],
        widget=forms.TextInput(
            attrs={
                "placeholder": "https://discourse.mozilla.org/u/",
                "data-fa-icon": "icon-discourse",
            }),
    )

    class Meta:
        model = User
        fields = (
            "fullname",
            "title",
            "organization",
            "location",
            "locale",
            "timezone",
            "irc_nickname",
            "twitter_url",
            "github_url",
            "is_github_url_public",
            "stackoverflow_url",
            "linkedin_url",
            "mozillians_url",
            "facebook_url",
            "discourse_url",
            "username",
        )

    def __init__(self, *args, **kwargs):
        super(UserEditForm, self).__init__(*args, **kwargs)
        # in case the username is not changed and the user has a legacy
        # username we want to disarm the username regex
        if ("username" not in self.changed_data and self.instance
                and self.instance.has_legacy_username):
            self.fields["username"].regex = USERNAME_LEGACY_REGEX

    def clean_username(self):
        new_username = self.cleaned_data["username"]

        if not new_username:
            raise forms.ValidationError(_("This field cannot be blank."))

        if (self.instance is not None and User.objects.exclude(
                pk=self.instance.pk).filter(username=new_username).exists()):
            raise forms.ValidationError(_("Username already in use."))
        return new_username