Package madrona :: Package user_profile :: Module views
[hide private]

Source Code for Module madrona.user_profile.views

 1  from django.contrib.auth.models import User 
 2  from django.contrib.auth.decorators import login_required 
 3  from django.http import HttpResponse, HttpResponseRedirect 
 4  from django.shortcuts import render_to_response 
 5  from models import UserProfile 
 6  from forms import UserForm, UserProfileForm 
 7  from django.conf import settings 
 8  from django.core.urlresolvers import reverse 
 9  from madrona.openid.models import UserAssociation 
10 11 @login_required 12 -def profile_form(request,username,use_openid=False):
13 if request.user.username != username: 14 return HttpResponse("You cannot access another user's profile.", status=401) 15 else: 16 user = User.objects.get(username=username) 17 try: 18 user_profile = UserProfile.objects.get(user=user) 19 except UserProfile.DoesNotExist: 20 user_profile = UserProfile.objects.create(user=user) 21 22 user_assoc = UserAssociation.objects.filter(user__id=user.id) 23 24 if request.method == 'GET': 25 uform = UserForm(instance=user) 26 pform = UserProfileForm(instance=user_profile) 27 return render_to_response('user_profile/user_profile_form.html', 28 {'profile': user_profile, 'assoc': user_assoc, 'uform': uform, 'pform': pform, 29 'group_request_email': settings.GROUP_REQUEST_EMAIL, 'use_openid': use_openid, 'MEDIA_URL':settings.MEDIA_URL}) 30 31 elif request.method == 'POST': 32 uform = UserForm(data=request.POST, instance=user) 33 pform = UserProfileForm(data=request.POST, instance=user_profile) 34 if uform.is_valid(): 35 user.save() 36 if pform.is_valid(): 37 user_profile.save() 38 39 #return HttpResponseRedirect(reverse('user_profile-form', args=[username])) 40 return HttpResponseRedirect(reverse('map')) 41 else: 42 return HttpResponse("Received unexpected " + request.method + " request.", status=400)
43