Ejemplo n.º 1
0
class LoginForm(forms.Form):
    username = UsernameField(required=True, max_length=12, min_length=3)
    password = PasswordField(required=True, max_length=12, min_length=6)

    def __init__(self, request=None, *args, **kwargs):
        self.request = request
        self.user_cache = None
        super(LoginForm, self).__init__(*args, **kwargs)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password:
            self.user_cache = authenticate(username=username,
                                           password=password)
            #如果成功返回对应的User对象,否则返回None(只是判断此用户是否存在,不判断是否is_active或者is_staff)
            if self.user_cache is None:
                raise forms.ValidationError(u"您输入的用户名或密码不正确!")
            elif not self.user_cache.is_active or not self.user_cache.is_staff:
                raise forms.ValidationError(u"您输入的用户名或密码不正确!")
            else:
                login(self.request, self.user_cache)
        return self.cleaned_data

    def get_user_id(self):
        if self.user_cache:
            return self.user_cache.id
        return None

    def get_user(self):
        return self.user_cache
Ejemplo n.º 2
0
class LoginForm(forms.Form):
    username = UsernameField(required=True, max_length=12, min_length=6)
    password = PasswordField(required=True, max_length=12, min_length=6)
    captcha = CaptchaField(required=True, error_messages={'invalid': u'密码不正确'})

    def __init__(self, request=None, *args, **kwargs):
        self.request = request
        self.user_cache = None
        super(LoginForm, self).__init__(*args, **kwargs)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        if username and password:
            self.user_cache = authenticate(username=username,
                                           password=password)
            if self.user_cache is None:
                raise forms.ValidationError(u'用户名或密码不正确')
            elif not (self.user_cache.is_active or self.user_cache.is_staff):
                raise forms.ValidationError(u'用户名未激活')
            else:
                login(self.request, self.user_cache)
        return self.cleaned_data

    def get_user_id(self):
        if self.user_cache:
            return self.user_cache.id
        return None

    def get_user(self):
        return self.user_cache
Ejemplo n.º 3
0
class UserForm(forms.Form):
    username = UsernameField(
        max_length=30,
        help_text="Max. 30 characters. Letters, digits and .@+-_")
    password = forms.CharField(min_length=5,
                               widget=forms.PasswordInput(
                                   render_value=True,
                                   attrs={'autocomplete': 'off'}),
                               help_text="At least 5 characters.")
    email = forms.EmailField(
        label="E-mail",
        help_text="Project support requests will be sent to this e-mail.")
    name = forms.CharField(
        max_length=100,
        help_text="Either your full name (John Doe) or organization (Acme).")
    info = forms.CharField(
        required=False,
        widget=forms.Textarea(attrs={'rows': 3}),
        label="Profile info",
        help_text="Optional. This will be displayed in your public profile.")
Ejemplo n.º 4
0
class LoginForm(forms.Form):
    username = UsernameField(required=True, max_length=12, min_length=4)
    password = PasswordField(required=True, max_length=12, min_length=6)

    def __init__(self, request=None, *args, **kwargs):
        self.request = request
        self.user_cache = None
        super(LoginForm, self).__init__(*args, **kwargs)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        if username and password:
            self.user_cache = authenticate(username=username,
                                           password=password)
            print self.user_cache
            if self.user_cache is None:
                raise forms.ValidationError(u"您输入的用户名或密码不正确!")
            elif not self.user_cache.is_active or not self.user_cache.is_staff:
                raise forms.ValidationError(u"您输入的用户名或密码不正确!")
            else:
                login(self.request, self.user_cache)
        return self.cleaned_data