diff options
author | Dan McGee <dan@archlinux.org> | 2011-03-29 14:46:25 -0500 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-03-29 14:46:25 -0500 |
commit | 325b9d63d07cff73117258507e7cde592d2f824c (patch) | |
tree | a79dce474238840b5df00e51346c5a6ef505844b /devel | |
parent | 1ce650d3c3850020c6ba54766cb70ad049c6e0bd (diff) |
Work out kinks in create new user view
We allowed repo selection before, but never actually called save_m2m()
so selections would have to be repeated on the next page. Add in group
selection as well to the form, and ensure we do all of our database
operations in one transaction so it is a lot easier to test and roll
back if things go wrong.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'devel')
-rw-r--r-- | devel/views.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/devel/views.py b/devel/views.py index a6a51f22..a83c3bbc 100644 --- a/devel/views.py +++ b/devel/views.py @@ -2,9 +2,10 @@ from django import forms from django.http import HttpResponseRedirect from django.contrib.auth.decorators import \ login_required, permission_required, user_passes_test -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.contrib.sites.models import Site from django.core.mail import send_mail +from django.db import transaction from django.shortcuts import get_object_or_404 from django.template import loader, Context from django.views.decorators.cache import never_cache @@ -123,6 +124,8 @@ class NewUserForm(forms.ModelForm): private_email = forms.EmailField() first_name = forms.CharField(required=False) last_name = forms.CharField(required=False) + groups = forms.ModelMultipleChoiceField(required=False, + queryset=Group.objects.all()) class Meta: model = UserProfile @@ -145,8 +148,8 @@ class NewUserForm(forms.ModelForm): "A user with that username already exists.") return username - def save(self): - profile = forms.ModelForm.save(self, False) + def save(self, commit=True): + profile = super(NewUserForm, self).save(False) pwletters = ascii_letters + digits password = ''.join([random.choice(pwletters) for i in xrange(8)]) user = User.objects.create_user(username=self.cleaned_data['username'], @@ -154,8 +157,13 @@ class NewUserForm(forms.ModelForm): user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() + # sucks that the MRM.add() method can't take a list directly... we have + # to resort to dirty * magic. + user.groups.add(*self.cleaned_data['groups']) profile.user = user - profile.save() + if commit: + profile.save() + self.save_m2m() t = loader.get_template('devel/new_account.txt') c = Context({ @@ -190,8 +198,11 @@ def new_user_form(request): if request.POST: form = NewUserForm(request.POST) if form.is_valid(): - form.save() - log_addition(request, form.instance.user) + @transaction.commit_on_success + def inner_save(): + form.save() + log_addition(request, form.instance.user) + inner_save() return HttpResponseRedirect('/admin/auth/user/%d/' % \ form.instance.user.id) else: |