Sith/core/views/forms.py
2015-12-14 10:49:15 +01:00

74 lines
2.3 KiB
Python

from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm
from django import forms
from django.contrib.auth import logout, login, authenticate
from django.forms import CheckboxSelectMultiple
import logging
from core.models import User, Page, Group
class RegisteringForm(UserCreationForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'date_of_birth')
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 UserPropForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = User
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",
}
widgets = {
'groups': CheckboxSelectMultiple,
'user_permissions': CheckboxSelectMultiple,
'edit_group': CheckboxSelectMultiple,
'view_group': CheckboxSelectMultiple,
}
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,
}
def __init__(self, *arg, **kwargs):
super(PagePropForm, self).__init__(*arg, **kwargs)
self.fields['edit_group'].required = False
self.fields['view_group'].required = False
class GroupEditForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = Group
fields = ['name', 'permissions',]
widgets = {
'permissions': CheckboxSelectMultiple,
}