forms.py 3 KB
Newer Older
Skia's avatar
Skia committed
1
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm
Skia's avatar
Skia committed
2
3
4
5
from django import forms
from django.contrib.auth import logout, login, authenticate
import logging

Skia's avatar
Skia committed
6
from .models import User, Page
Skia's avatar
Skia committed
7
8
9
10
11
12

class RegisteringForm(UserCreationForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = User
Skia's avatar
Skia committed
13
        fields = ('first_name', 'last_name', 'email', 'date_of_birth')
Skia's avatar
Skia committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

    def save(self, commit=True):
        user = super(RegisteringForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        user.generate_username()
        if commit:
            user.save()
        return user

class LoginForm(AuthenticationForm):
    def login(self):
        u = authenticate(username=self.request.POST['username'],
                         password=self.request.POST['password'])
        if u is not None:
            if u.is_active:
                login(self.request, u)
Skia's avatar
Skia committed
30
                logging.debug("Logging in "+str(u))
Skia's avatar
Skia committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
            else:
                raise forms.ValidationError(
                        self.error_messages['invalid_login'],
                        code='inactive',
                        params={'username': self.username_field.verbose_name},
                    )
        else:
            logging.debug("Login failed")
            raise forms.ValidationError(
                    self.error_messages['invalid_login'],
                    code='invalid_login',
                    params={'username': self.username_field.verbose_name},
                )

Skia's avatar
Skia committed
45
class UserEditForm(UserChangeForm):
Skia's avatar
Skia committed
46
47
48
49
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = User
Skia's avatar
Skia committed
50
51
52
53
54
55
56
57
        fields = ('first_name', 'last_name', 'nick_name', 'email', 'date_of_birth', 'groups', 'user_permissions',)

    def __init__(self, *args, **kwargs):
        super(UserEditForm, self).__init__(*args, **kwargs)

    def clean_password(self):
        """We never handle password in this form"""
        return
Skia's avatar
Skia committed
58
59
60


class PagePropForm(forms.ModelForm):
61
62
63
64
65
    error_css_class = 'error'
    required_css_class = 'required'
    parent = forms.ModelChoiceField(queryset=Page.objects.all())

    def __init__(self, *args, **kwargs):
Skia's avatar
Skia committed
66
        super(PagePropForm, self).__init__(*args, **kwargs)
67
        self.fields['parent'].required = False
Skia's avatar
Skia committed
68

Skia's avatar
Skia committed
69
70
    class Meta:
        model = Page
Skia's avatar
Skia committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        fields = ['parent', 'name', 'owner_group', 'edit_group', 'view_group']

    def save(self, commit=True):
        page = super(PagePropForm, self).save(commit=False)
        if commit:
            page.save()
        return page


class PageEditForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'

    def __init__(self, *args, **kwargs):
        super(PageEditForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Page
        fields = ['title', 'content', ]
Skia's avatar
Skia committed
90
91

    def save(self, commit=True):
Skia's avatar
Skia committed
92
        page = super(PageEditForm, self).save(commit=False)
Skia's avatar
Skia committed
93
94
95
96
        if commit:
            page.save()
        return page

Skia's avatar
Skia committed
97