Sith/core/forms.py

91 lines
2.8 KiB
Python
Raw Normal View History

2015-11-24 13:01:10 +00:00
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm
2015-11-18 16:09:06 +00:00
from django import forms
from django.contrib.auth import logout, login, authenticate
import logging
2015-11-20 14:47:01 +00:00
from .models import User, Page
2015-11-18 08:44:06 +00:00
class RegisteringForm(UserCreationForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = User
2015-11-22 17:23:21 +00:00
fields = ('first_name', 'last_name', 'email', 'date_of_birth')
2015-11-18 16:09:06 +00:00
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)
2015-11-19 13:44:48 +00:00
logging.debug("Logging in "+str(u))
2015-11-18 16:09:06 +00:00
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},
)
2015-11-24 13:01:10 +00:00
class EditUserForm(UserChangeForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'date_of_birth', 'groups', 'user_permissions')
class PagePropForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
parent = forms.ModelChoiceField(queryset=Page.objects.all())
def __init__(self, *args, **kwargs):
2015-11-24 13:01:10 +00:00
super(PagePropForm, self).__init__(*args, **kwargs)
self.fields['parent'].required = False
2015-11-23 12:30:30 +00:00
2015-11-20 14:47:01 +00:00
class Meta:
model = Page
2015-11-24 13:01:10 +00:00
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', ]
2015-11-20 14:47:01 +00:00
def save(self, commit=True):
2015-11-24 13:01:10 +00:00
page = super(PageEditForm, self).save(commit=False)
2015-11-20 14:47:01 +00:00
if commit:
page.save()
return page
2015-11-18 16:09:06 +00:00