Mercurial > public > sg101
comparison bio/views.py @ 769:cd3343abca9d
For issue #66, member search function should use GET method.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 22 Jan 2014 20:07:39 -0600 |
parents | 22d158ef2217 |
children | 9e803323a0d0 |
comparison
equal
deleted
inserted
replaced
768:1b1a12abde3b | 769:cd3343abca9d |
---|---|
1 """ | 1 """ |
2 Views for the bio application. | 2 Views for the bio application. |
3 | 3 |
4 """ | 4 """ |
5 from django.shortcuts import render_to_response | 5 from django.shortcuts import render, redirect |
6 from django.shortcuts import get_object_or_404 | 6 from django.shortcuts import get_object_or_404 |
7 from django.template import RequestContext | |
8 from django.contrib import messages | 7 from django.contrib import messages |
9 from django.contrib.auth.models import User | 8 from django.contrib.auth.models import User |
10 from django.http import HttpResponse | 9 from django.http import HttpResponse |
11 from django.http import HttpResponseBadRequest | 10 from django.http import HttpResponseBadRequest |
12 from django.http import HttpResponseRedirect | |
13 from django.http import HttpResponseServerError | 11 from django.http import HttpResponseServerError |
14 from django.http import Http404 | 12 from django.http import Http404 |
15 from django.core.paginator import InvalidPage | 13 from django.core.paginator import InvalidPage |
16 from django.core.urlresolvers import reverse | |
17 from django.contrib.auth.decorators import login_required | 14 from django.contrib.auth.decorators import login_required |
18 from django.views.decorators.http import require_POST | 15 from django.views.decorators.http import require_POST |
19 | 16 |
20 from elsewhere.models import SocialNetworkForm | 17 from elsewhere.models import SocialNetworkForm |
21 from elsewhere.models import InstantMessengerForm | 18 from elsewhere.models import InstantMessengerForm |
62 user_profiles = dict((profile.user.id, profile) for profile in profiles) | 59 user_profiles = dict((profile.user.id, profile) for profile in profiles) |
63 | 60 |
64 for user in the_page.object_list: | 61 for user in the_page.object_list: |
65 user.user_profile = user_profiles[user.id] | 62 user.user_profile = user_profiles[user.id] |
66 | 63 |
67 return render_to_response('bio/members.html', { | 64 return render(request, 'bio/members.html', { |
68 'page': the_page, | 65 'page': the_page, |
69 'type': type, | 66 'type': type, |
70 'num_members': num_members, | 67 'num_members': num_members, |
71 }, | 68 }) |
72 context_instance = RequestContext(request)) | |
73 | 69 |
74 ####################################################################### | 70 ####################################################################### |
75 | 71 |
76 @login_required | 72 @login_required |
77 def my_profile(request): | 73 def my_profile(request): |
78 profile = request.user.get_profile() | 74 profile = request.user.get_profile() |
79 badge_collection = BadgeOwnership.objects.filter( | 75 badge_collection = BadgeOwnership.objects.filter( |
80 profile=profile).select_related("badge") | 76 profile=profile).select_related("badge") |
81 | 77 |
82 return render_to_response('bio/view_profile.html', { | 78 return render(request, 'bio/view_profile.html', { |
83 'subject': request.user, | 79 'subject': request.user, |
84 'profile': profile, | 80 'profile': profile, |
85 'hide_email': False, | 81 'hide_email': False, |
86 'this_is_me': True, | 82 'this_is_me': True, |
87 'badge_collection': badge_collection, | 83 'badge_collection': badge_collection, |
88 }, | 84 }) |
89 context_instance = RequestContext(request)) | |
90 | 85 |
91 ####################################################################### | 86 ####################################################################### |
92 | 87 |
93 @login_required | 88 @login_required |
94 def view_profile(request, username): | 89 def view_profile(request, username): |
95 | 90 |
96 user = get_object_or_404(User, username=username) | 91 user = get_object_or_404(User, username=username) |
97 if user == request.user: | 92 if user == request.user: |
98 return HttpResponseRedirect(reverse('bio-me')) | 93 return redirect('bio-me') |
99 | 94 |
100 profile = user.get_profile() | 95 profile = user.get_profile() |
101 hide_email = profile.hide_email | 96 hide_email = profile.hide_email |
102 | 97 |
103 badge_collection = BadgeOwnership.objects.filter( | 98 badge_collection = BadgeOwnership.objects.filter( |
104 profile=profile).select_related("badge") | 99 profile=profile).select_related("badge") |
105 | 100 |
106 return render_to_response('bio/view_profile.html', { | 101 return render(request, 'bio/view_profile.html', { |
107 'subject': user, | 102 'subject': user, |
108 'profile': profile, | 103 'profile': profile, |
109 'hide_email': hide_email, | 104 'hide_email': hide_email, |
110 'this_is_me': False, | 105 'this_is_me': False, |
111 'badge_collection': badge_collection, | 106 'badge_collection': badge_collection, |
112 }, | 107 }) |
113 context_instance = RequestContext(request)) | |
114 | 108 |
115 ####################################################################### | 109 ####################################################################### |
116 | 110 |
117 @login_required | 111 @login_required |
118 def edit_profile(request): | 112 def edit_profile(request): |
119 if request.method == 'POST': | 113 if request.method == 'POST': |
120 if request.POST.get('submit_button', 'Cancel') == 'Cancel': | 114 if request.POST.get('submit_button', 'Cancel') == 'Cancel': |
121 return HttpResponseRedirect(reverse('bio-me')) | 115 return redirect('bio-me') |
122 profile = request.user.get_profile() | 116 profile = request.user.get_profile() |
123 user_form = EditUserForm(request.POST, instance=request.user) | 117 user_form = EditUserForm(request.POST, instance=request.user) |
124 profile_form = EditUserProfileForm(request.POST, instance=profile) | 118 profile_form = EditUserProfileForm(request.POST, instance=profile) |
125 if user_form.is_valid() and profile_form.is_valid(): | 119 if user_form.is_valid() and profile_form.is_valid(): |
126 user_form.save() | 120 user_form.save() |
127 profile = profile_form.save(commit=False) | 121 profile = profile_form.save(commit=False) |
128 profile.user = request.user | 122 profile.user = request.user |
129 profile.save() | 123 profile.save() |
130 return HttpResponseRedirect(reverse('bio-me')) | 124 return redirect('bio-me') |
131 else: | 125 else: |
132 profile = request.user.get_profile() | 126 profile = request.user.get_profile() |
133 user_form = EditUserForm(instance=request.user) | 127 user_form = EditUserForm(instance=request.user) |
134 profile_form = EditUserProfileForm(instance=profile) | 128 profile_form = EditUserProfileForm(instance=profile) |
135 | 129 |
136 return render_to_response('bio/edit_profile.html', { | 130 return render(request, 'bio/edit_profile.html', { |
137 'user_form': user_form, | 131 'user_form': user_form, |
138 'profile_form': profile_form, | 132 'profile_form': profile_form, |
139 }, | 133 }) |
140 context_instance = RequestContext(request)) | |
141 | 134 |
142 ####################################################################### | 135 ####################################################################### |
143 | 136 |
144 @login_required | 137 @login_required |
145 def change_avatar(request): | 138 def change_avatar(request): |
155 | 148 |
156 try: | 149 try: |
157 name, avatar = form.save() | 150 name, avatar = form.save() |
158 except IOError: | 151 except IOError: |
159 messages.error(request, 'A file error occurred.') | 152 messages.error(request, 'A file error occurred.') |
160 return HttpResponseRedirect(reverse('bio-me')) | 153 return redirect('bio-me') |
161 | 154 |
162 if avatar is not None: | 155 if avatar is not None: |
163 profile.avatar.save(name, avatar, save=False) | 156 profile.avatar.save(name, avatar, save=False) |
164 profile.save() | 157 profile.save() |
165 | 158 |
166 messages.success(request, 'Avatar updated') | 159 messages.success(request, 'Avatar updated') |
167 return HttpResponseRedirect(reverse('bio-me')) | 160 return redirect('bio-me') |
168 else: | 161 else: |
169 form = UploadAvatarForm() | 162 form = UploadAvatarForm() |
170 | 163 |
171 return render_to_response('bio/avatar.html', { | 164 return render(request, 'bio/avatar.html', { |
172 'form': form, | 165 'form': form, |
173 }, | 166 }) |
174 context_instance = RequestContext(request)) | |
175 | 167 |
176 ####################################################################### | 168 ####################################################################### |
177 | 169 |
178 @require_POST | 170 @require_POST |
179 def flag_profile(request, profile_id): | 171 def flag_profile(request, profile_id): |
193 flag.save() | 185 flag.save() |
194 email_admins('A Profile Has Been Flagged', """Hello, | 186 email_admins('A Profile Has Been Flagged', """Hello, |
195 | 187 |
196 A user has flagged a profile for review. | 188 A user has flagged a profile for review. |
197 """) | 189 """) |
198 return HttpResponse('The profile was flagged. A moderator will review the' \ | 190 return HttpResponse('The profile was flagged. A moderator will review the' |
199 ' profile shortly. Thanks for helping to improve the content on this ' \ | 191 ' profile shortly. Thanks for helping to improve the content on this ' |
200 'site.') | 192 'site.') |
201 | 193 |
202 ####################################################################### | 194 ####################################################################### |
203 | 195 |
204 @login_required | 196 @login_required |
228 | 220 |
229 if form.is_valid(): | 221 if form.is_valid(): |
230 profile = form.save(commit=False) | 222 profile = form.save(commit=False) |
231 profile.user = request.user | 223 profile.user = request.user |
232 profile.save() | 224 profile.save() |
233 return HttpResponseRedirect(request.path) | 225 return redirect(request.path) |
234 | 226 |
235 # Delete forms | 227 # Delete forms |
236 elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'): | 228 elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'): |
237 delete_id = request.POST['delete_id'] | 229 delete_id = request.POST['delete_id'] |
238 | 230 |
247 update_occurred = False | 239 update_occurred = False |
248 | 240 |
249 if update_occurred: | 241 if update_occurred: |
250 notify_profile_content_update(request.user.get_profile()) | 242 notify_profile_content_update(request.user.get_profile()) |
251 | 243 |
252 return HttpResponseRedirect(request.path) | 244 return redirect(request.path) |
253 | 245 |
254 # WTF? | 246 # WTF? |
255 else: | 247 else: |
256 return HttpResponseServerError | 248 return HttpResponseServerError |
257 | 249 |
259 # Create blank forms | 251 # Create blank forms |
260 sn_form = SocialNetworkForm() | 252 sn_form = SocialNetworkForm() |
261 im_form = InstantMessengerForm(auto_id=im_id) | 253 im_form = InstantMessengerForm(auto_id=im_id) |
262 w_form = WebsiteForm() | 254 w_form = WebsiteForm() |
263 | 255 |
264 return render_to_response('bio/edit_elsewhere.html', { | 256 return render(request, 'bio/edit_elsewhere.html', { |
265 'sn_form': sn_form, | 257 'sn_form': sn_form, |
266 'im_form': im_form, | 258 'im_form': im_form, |
267 'w_form': w_form, | 259 'w_form': w_form, |
268 }, | 260 }) |
269 context_instance=RequestContext(request)) | |
270 | 261 |
271 ####################################################################### | 262 ####################################################################### |
272 | 263 |
273 @login_required | 264 @login_required |
274 def member_search(request): | 265 def member_search(request): |
275 if request.method == "POST": | 266 data = request.GET if request.GET else None |
276 form = SearchUsersForm(request.POST) | 267 form = SearchUsersForm(data) |
277 if form.is_valid(): | 268 if form.is_valid(): |
278 username = form.cleaned_data['username'] | 269 username = form.cleaned_data['username'] |
279 return HttpResponseRedirect(reverse("bio-view_profile", | 270 return redirect('bio-view_profile', username=username) |
280 kwargs={'username': username})) | 271 |
281 else: | 272 return render(request, 'bio/member_search.html', {'form': form}) |
282 form = SearchUsersForm() | |
283 | |
284 return render_to_response('bio/member_search.html', { | |
285 'form': form, | |
286 }, | |
287 context_instance=RequestContext(request)) | |
288 |