forms.py 2.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
from django import forms
from django.contrib.auth import logout, login, authenticate
Skia's avatar
Skia committed
4
from django.forms import CheckboxSelectMultiple
Skia's avatar
Skia committed
5
6
import logging

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

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

    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


Skia's avatar
Skia committed
25
class UserPropForm(forms.ModelForm):
Skia's avatar
Skia committed
26
27
28
29
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = User
Skia's avatar
Skia committed
30
31
32
33
34
35
36
37
38
39
        fields = ['groups', 'edit_group', 'view_group']
        labels = {
            'edit_group': "Edit profile group",
            'view_group': "View profile group",
        }
        help_texts = {
            'edit_group': "Groups that can edit this user's profile",
            'view_group': "Groups that can view this user's profile",
            'groups': "Which groups this user belongs to",
        }
Skia's avatar
Skia committed
40
41
42
        widgets = {
            'groups': CheckboxSelectMultiple,
            'user_permissions': CheckboxSelectMultiple,
43
44
            'edit_group': CheckboxSelectMultiple,
            'view_group': CheckboxSelectMultiple,
Skia's avatar
Skia committed
45
        }
Skia's avatar
Skia committed
46

47
48
49
50
51
52
53
54
55
56
57
class PagePropForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = Page
        fields = ['parent', 'name', 'owner_group', 'edit_group', 'view_group', ]
        widgets = {
            'edit_group': CheckboxSelectMultiple,
            'view_group': CheckboxSelectMultiple,
        }

Skia's avatar
Skia committed
58
59
60
61
62
63
    def __init__(self, *arg, **kwargs):
        super(PagePropForm, self).__init__(*arg, **kwargs)
        self.fields['edit_group'].required = False
        self.fields['view_group'].required = False


64
65
66
67
68
69
70
71
72
73
class GroupEditForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = Group
        fields = ['name', 'permissions',]
        widgets = {
            'permissions': CheckboxSelectMultiple,
        }