comparison gpp/bio/views.py @ 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 74f04122295e
children 62eb9cbbcffc
comparison
equal deleted inserted replaced
43:2763977301c2 44:08cd19c1ee50
17 from bio.models import UserProfile 17 from bio.models import UserProfile
18 from bio.forms import UploadAvatarForm 18 from bio.forms import UploadAvatarForm
19 from bio.forms import EditUserForm 19 from bio.forms import EditUserForm
20 from bio.forms import EditUserProfileForm 20 from bio.forms import EditUserProfileForm
21 from core.paginator import DiggPaginator 21 from core.paginator import DiggPaginator
22
23 #######################################################################
24
25 def get_profile(user):
26 try:
27 profile = user.get_profile()
28 except:
29 profile = UserProfile()
30 profile.user = user
31 return profile
32 22
33 ####################################################################### 23 #######################################################################
34 24
35 def member_list(request, type='user', page=1): 25 def member_list(request, type='user', page=1):
36 if type == 'user': 26 if type == 'user':
52 42
53 ####################################################################### 43 #######################################################################
54 44
55 @login_required 45 @login_required
56 def my_profile(request): 46 def my_profile(request):
57 profile = get_profile(request.user) 47 profile = request.user.get_profile()
58 48
59 return render_to_response('bio/view_profile.html', { 49 return render_to_response('bio/view_profile.html', {
60 'subject': request.user, 50 'subject': request.user,
61 'profile': profile, 51 'profile': profile,
62 'hide_email': False, 52 'hide_email': False,
71 61
72 user = get_object_or_404(auth.models.User, username = username) 62 user = get_object_or_404(auth.models.User, username = username)
73 if user == request.user: 63 if user == request.user:
74 return HttpResponseRedirect(reverse('bio.views.my_profile')) 64 return HttpResponseRedirect(reverse('bio.views.my_profile'))
75 65
76 profile = get_profile(user) 66 profile = user.get_profile()
77 67
78 # work around MySQL's handling of Boolean 68 # work around MySQL's handling of Boolean
79 hide_email = bool(profile.hide_email) 69 hide_email = bool(profile.hide_email)
80 70
81 return render_to_response('bio/view_profile.html', { 71 return render_to_response('bio/view_profile.html', {
91 @login_required 81 @login_required
92 def edit_profile(request): 82 def edit_profile(request):
93 if request.method == 'POST': 83 if request.method == 'POST':
94 if request.POST.get('submit_button', 'Cancel') == 'Cancel': 84 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
95 return HttpResponseRedirect(reverse('bio.views.my_profile')) 85 return HttpResponseRedirect(reverse('bio.views.my_profile'))
96 profile = get_profile(request.user) 86 profile = request.user.get_profile()
97 user_form = EditUserForm(request.POST, instance=request.user) 87 user_form = EditUserForm(request.POST, instance=request.user)
98 profile_form = EditUserProfileForm(request.POST, instance=profile) 88 profile_form = EditUserProfileForm(request.POST, instance=profile)
99 if user_form.is_valid() and profile_form.is_valid(): 89 if user_form.is_valid() and profile_form.is_valid():
100 user_form.save() 90 user_form.save()
101 profile = profile_form.save(commit=False) 91 profile = profile_form.save(commit=False)
102 profile.user = request.user 92 profile.user = request.user
103 profile.save() 93 profile.save()
104 return HttpResponseRedirect(reverse('bio.views.my_profile')) 94 return HttpResponseRedirect(reverse('bio.views.my_profile'))
105 else: 95 else:
106 profile = get_profile(request.user) 96 profile = request.user.get_profile()
107 user_form = EditUserForm(instance=request.user) 97 user_form = EditUserForm(instance=request.user)
108 profile_form = EditUserProfileForm(instance=profile) 98 profile_form = EditUserProfileForm(instance=profile)
109 99
110 return render_to_response('bio/edit_profile.html', { 100 return render_to_response('bio/edit_profile.html', {
111 'user_form': user_form, 101 'user_form': user_form,
118 @login_required 108 @login_required
119 def change_avatar(request): 109 def change_avatar(request):
120 if request.method == 'POST': 110 if request.method == 'POST':
121 form = UploadAvatarForm(request.POST, request.FILES) 111 form = UploadAvatarForm(request.POST, request.FILES)
122 if form.is_valid(): 112 if form.is_valid():
123 profile = get_profile(request.user) 113 profile = request.user.get_profile()
124 file = form.get_file() 114 file = form.get_file()
125 if profile.avatar.name != '': 115 if profile.avatar.name != '':
126 profile.avatar.delete(save=False) 116 profile.avatar.delete(save=False)
127 if file is not None: 117 if file is not None:
128 profile.avatar.save(form.get_filename(), file, save=False) 118 profile.avatar.save(form.get_filename(), file, save=False)