generate username with first and lastname

This commit is contained in:
Oskar Hahn 2011-09-04 09:57:23 +02:00
parent d07aa3ac3b
commit a1d92dc1bb
2 changed files with 29 additions and 20 deletions

View File

@ -16,7 +16,18 @@ from django.contrib.auth.forms import AdminPasswordChangeForm
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from participant.models import Profile from participant.models import Profile
class UserForm(ModelForm): class UserNewForm(ModelForm):
error_css_class = 'error'
required_css_class = 'required'
first_name = CharField(label=_("First name"))
last_name = CharField(label=_("Last name"))
class Meta:
model = User
exclude = ('username', 'password', 'is_staff', 'last_login', 'date_joined', 'user_permissions')
class UserEditForm(ModelForm):
error_css_class = 'error' error_css_class = 'error'
required_css_class = 'required' required_css_class = 'required'
@ -49,10 +60,7 @@ class GroupForm(ModelForm):
class Meta: class Meta:
model = Group model = Group
class UsersettingsForm(UserForm): class UsersettingsForm(UserEditForm):
error_css_class = 'error'
required_css_class = 'required'
class Meta: class Meta:
model = User model = User
fields = ('username', 'first_name', 'last_name', 'email') fields = ('username', 'first_name', 'last_name', 'email')

View File

@ -23,7 +23,7 @@ from django.utils.translation import ugettext as _
from participant.models import Profile, set_first_user_passwords from participant.models import Profile, set_first_user_passwords
from participant.api import gen_username from participant.api import gen_username
from participant.forms import UserForm, UsernameForm, ProfileForm, UsersettingsForm, UserImportForm, GroupForm, AdminPasswordChangeForm from participant.forms import UserNewForm, UserEditForm, ProfileForm, UsersettingsForm, UserImportForm, GroupForm, AdminPasswordChangeForm
from utils.utils import template, permission_required, gen_confirm_form from utils.utils import template, permission_required, gen_confirm_form
from utils.pdf import print_userlist, print_passwords from utils.pdf import print_userlist, print_passwords
@ -81,19 +81,18 @@ def edit(request, user_id=None):
user = None user = None
if request.method == 'POST': if request.method == 'POST':
userform = UserForm(request.POST, instance=user, prefix="user") if user_id is None:
try: userform = UserNewForm(request.POST, prefix="user")
profileform = ProfileForm(request.POST, instance=user.profile, prefix="profile")
except:
profileform = ProfileForm(request.POST, prefix="profile") profileform = ProfileForm(request.POST, prefix="profile")
else:
userform = UserEditForm(request.POST, instance=user, prefix="user")
profileform = ProfileForm(request.POST, instance=user.profile, prefix="profile")
formlist = [userform, profileform] if userform.is_valid and profileform.is_valid:
formerror = 0
for f in formlist:
if not f.is_valid():
formerror += 1
if formerror == 0:
user = userform.save() user = userform.save()
if user_id is None:
user.username = gen_username(user.first_name, user.last_name)
user.save()
profile = profileform.save(commit=False) profile = profileform.save(commit=False)
profile.user = user profile.user = user
profile.save() profile.save()
@ -108,11 +107,13 @@ def edit(request, user_id=None):
else: else:
messages.error(request, _('Please check the form for errors.')) messages.error(request, _('Please check the form for errors.'))
else: else:
userform = UserForm(instance=user, prefix="user") if user_id is None:
try: userform = UserNewForm(prefix="user")
profileform = ProfileForm(instance=user.profile, prefix="profile")
except AttributeError:
profileform = ProfileForm(prefix="profile") profileform = ProfileForm(prefix="profile")
else:
userform = UserEditForm(instance=user, prefix="user")
profileform = ProfileForm(instance=user.profile, prefix="profile")
return { return {
'userform': userform, 'userform': userform,
'profileform': profileform, 'profileform': profileform,