forms.py 1.97 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
30
        fields = ['edit_group', 'view_group', 'groups', 'user_permissions',]
Skia's avatar
Skia committed
31 32 33
        widgets = {
            'groups': CheckboxSelectMultiple,
            'user_permissions': CheckboxSelectMultiple,
34 35
            'edit_group': CheckboxSelectMultiple,
            'view_group': CheckboxSelectMultiple,
Skia's avatar
Skia committed
36
        }
Skia's avatar
Skia committed
37

38 39 40 41 42 43 44 45 46 47 48
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
49 50 51 52 53 54
    def __init__(self, *arg, **kwargs):
        super(PagePropForm, self).__init__(*arg, **kwargs)
        self.fields['edit_group'].required = False
        self.fields['view_group'].required = False


55 56 57 58 59 60 61 62 63 64
class GroupEditForm(forms.ModelForm):
    error_css_class = 'error'
    required_css_class = 'required'
    class Meta:
        model = Group
        fields = ['name', 'permissions',]
        widgets = {
            'permissions': CheckboxSelectMultiple,
        }