Mercurial > public > sg101
changeset 44:08cd19c1ee50
Added a signal handler for the user model to create a UserProfile for new users.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 17 Jun 2009 00:47:33 +0000 |
parents | 2763977301c2 |
children | a5b4c5ce0658 |
files | gpp/bio/__init__.py gpp/bio/signals.py gpp/bio/views.py |
diffstat | 3 files changed, 28 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/bio/__init__.py Sun Jun 14 20:25:22 2009 +0000 +++ b/gpp/bio/__init__.py Wed Jun 17 00:47:33 2009 +0000 @@ -0,0 +1,1 @@ +import signals
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/bio/signals.py Wed Jun 17 00:47:33 2009 +0000 @@ -0,0 +1,22 @@ +""" +Signal handler(s) for the bio application. +""" +from django.db.models.signals import post_save +from django.contrib.auth.models import User +from bio.models import UserProfile + +def on_user_save(sender, **kwargs): + """ + This signal handler ensures that every User has a corresonding + UserProfile. It is called after User instance is saved. It creates + a UserProfile for the User if the created argument is True. + """ + created = kwargs['created'] + if created: + user = kwargs['instance'] + profile = UserProfile() + profile.user = user + profile.save() + + +post_save.connect(on_user_save, sender=User)
--- a/gpp/bio/views.py Sun Jun 14 20:25:22 2009 +0000 +++ b/gpp/bio/views.py Wed Jun 17 00:47:33 2009 +0000 @@ -22,16 +22,6 @@ ####################################################################### -def get_profile(user): - try: - profile = user.get_profile() - except: - profile = UserProfile() - profile.user = user - return profile - -####################################################################### - def member_list(request, type='user', page=1): if type == 'user': users = auth.models.User.objects.all().order_by('username') @@ -54,7 +44,7 @@ @login_required def my_profile(request): - profile = get_profile(request.user) + profile = request.user.get_profile() return render_to_response('bio/view_profile.html', { 'subject': request.user, @@ -73,7 +63,7 @@ if user == request.user: return HttpResponseRedirect(reverse('bio.views.my_profile')) - profile = get_profile(user) + profile = user.get_profile() # work around MySQL's handling of Boolean hide_email = bool(profile.hide_email) @@ -93,7 +83,7 @@ if request.method == 'POST': if request.POST.get('submit_button', 'Cancel') == 'Cancel': return HttpResponseRedirect(reverse('bio.views.my_profile')) - profile = get_profile(request.user) + profile = request.user.get_profile() user_form = EditUserForm(request.POST, instance=request.user) profile_form = EditUserProfileForm(request.POST, instance=profile) if user_form.is_valid() and profile_form.is_valid(): @@ -103,7 +93,7 @@ profile.save() return HttpResponseRedirect(reverse('bio.views.my_profile')) else: - profile = get_profile(request.user) + profile = request.user.get_profile() user_form = EditUserForm(instance=request.user) profile_form = EditUserProfileForm(instance=profile) @@ -120,7 +110,7 @@ if request.method == 'POST': form = UploadAvatarForm(request.POST, request.FILES) if form.is_valid(): - profile = get_profile(request.user) + profile = request.user.get_profile() file = form.get_file() if profile.avatar.name != '': profile.avatar.delete(save=False)