Sith/core/views/forms.py

65 lines
2.1 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
2016-08-10 03:48:06 +00:00
from django.forms import CheckboxSelectMultiple, Select
from django.utils.translation import ugettext as _
2015-11-18 16:09:06 +00:00
import logging
2016-03-29 10:45:10 +00:00
from core.models import User, Page, RealGroup
2015-11-18 08:44:06 +00:00
class RegisteringForm(UserCreationForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
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
2015-12-09 10:01:11 +00:00
class UserPropForm(forms.ModelForm):
2015-11-24 13:01:10 +00:00
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = User
fields = ['groups']
2015-12-14 09:49:15 +00:00
help_texts = {
'groups': "Which groups this user belongs to",
}
2015-11-26 15:32:56 +00:00
widgets = {
'groups': CheckboxSelectMultiple,
}
2015-11-24 13:01:10 +00:00
class PagePropForm(forms.ModelForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta:
model = Page
2016-02-05 15:59:42 +00:00
fields = ['parent', 'name', 'owner_group', 'edit_groups', 'view_groups', ]
widgets = {
2016-02-05 15:59:42 +00:00
'edit_groups': CheckboxSelectMultiple,
'view_groups': CheckboxSelectMultiple,
}
2015-11-27 15:09:47 +00:00
def __init__(self, *arg, **kwargs):
super(PagePropForm, self).__init__(*arg, **kwargs)
2016-02-05 15:59:42 +00:00
self.fields['edit_groups'].required = False
self.fields['view_groups'].required = False
2015-11-27 15:09:47 +00:00
2016-08-10 03:48:06 +00:00
class SelectFile(Select):
def render(self, name, value, attrs=None):
output = '<span class="choose_file_widget" title="%(title)s">%(content)s</span>' % {
'title': _("Choose file"),
'content': super(SelectFile, self).render(name, value, attrs),
}
output += '<span class="choose_file_button">' + _("Choose file") + '</span>'
print(output)
return output