Mercurial > public > sg101
comparison bio/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/bio/views.py@98b373ca09f3 |
children | 22d158ef2217 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the bio application. | |
3 | |
4 """ | |
5 from django.shortcuts import render_to_response | |
6 from django.shortcuts import get_object_or_404 | |
7 from django.template import RequestContext | |
8 from django.contrib import messages | |
9 from django.contrib.auth.models import User | |
10 from django.http import HttpResponse | |
11 from django.http import HttpResponseBadRequest | |
12 from django.http import HttpResponseRedirect | |
13 from django.http import HttpResponseServerError | |
14 from django.http import Http404 | |
15 from django.core.paginator import InvalidPage | |
16 from django.core.urlresolvers import reverse | |
17 from django.contrib.auth.decorators import login_required | |
18 from django.views.decorators.http import require_POST | |
19 | |
20 from elsewhere.models import SocialNetworkForm | |
21 from elsewhere.models import InstantMessengerForm | |
22 from elsewhere.models import WebsiteForm | |
23 | |
24 from bio.models import UserProfile | |
25 from bio.models import UserProfileFlag | |
26 from bio.models import BadgeOwnership | |
27 from bio.forms import UploadAvatarForm | |
28 from bio.forms import EditUserForm | |
29 from bio.forms import EditUserProfileForm | |
30 from bio.forms import SearchUsersForm | |
31 from bio.signals import notify_profile_content_update | |
32 from core.paginator import DiggPaginator | |
33 from core.functions import email_admins | |
34 from core.functions import get_page | |
35 | |
36 ####################################################################### | |
37 | |
38 @login_required | |
39 def member_list(request, type='user'): | |
40 """ | |
41 This view displays the member list. Only active members are displayed. | |
42 """ | |
43 qs = User.objects.filter(is_active=True) | |
44 if type == 'user': | |
45 qs = qs.order_by('username') | |
46 else: | |
47 qs = qs.order_by('date_joined') | |
48 num_members = qs.count() | |
49 | |
50 paginator = DiggPaginator(qs, 20, body=5, tail=3, margin=3, padding=2) | |
51 page = get_page(request.GET) | |
52 try: | |
53 the_page = paginator.page(page) | |
54 except InvalidPage: | |
55 raise Http404 | |
56 | |
57 # Attach user profiles to each user to avoid using get_user_profile() in | |
58 # the template. | |
59 users = set(user.id for user in the_page.object_list) | |
60 | |
61 profiles = UserProfile.objects.filter(user__id__in=users).select_related() | |
62 user_profiles = dict((profile.user.id, profile) for profile in profiles) | |
63 | |
64 for user in the_page.object_list: | |
65 user.user_profile = user_profiles[user.id] | |
66 | |
67 return render_to_response('bio/members.html', { | |
68 'page': the_page, | |
69 'type': type, | |
70 'num_members': num_members, | |
71 }, | |
72 context_instance = RequestContext(request)) | |
73 | |
74 ####################################################################### | |
75 | |
76 @login_required | |
77 def my_profile(request): | |
78 profile = request.user.get_profile() | |
79 badge_collection = BadgeOwnership.objects.filter( | |
80 profile=profile).select_related("badge") | |
81 | |
82 return render_to_response('bio/view_profile.html', { | |
83 'subject': request.user, | |
84 'profile': profile, | |
85 'hide_email': False, | |
86 'this_is_me': True, | |
87 'badge_collection': badge_collection, | |
88 }, | |
89 context_instance = RequestContext(request)) | |
90 | |
91 ####################################################################### | |
92 | |
93 @login_required | |
94 def view_profile(request, username): | |
95 | |
96 user = get_object_or_404(User, username=username) | |
97 if user == request.user: | |
98 return HttpResponseRedirect(reverse('bio.views.my_profile')) | |
99 | |
100 profile = user.get_profile() | |
101 hide_email = profile.hide_email | |
102 | |
103 badge_collection = BadgeOwnership.objects.filter( | |
104 profile=profile).select_related("badge") | |
105 | |
106 return render_to_response('bio/view_profile.html', { | |
107 'subject': user, | |
108 'profile': profile, | |
109 'hide_email': hide_email, | |
110 'this_is_me': False, | |
111 'badge_collection': badge_collection, | |
112 }, | |
113 context_instance = RequestContext(request)) | |
114 | |
115 ####################################################################### | |
116 | |
117 @login_required | |
118 def edit_profile(request): | |
119 if request.method == 'POST': | |
120 if request.POST.get('submit_button', 'Cancel') == 'Cancel': | |
121 return HttpResponseRedirect(reverse('bio.views.my_profile')) | |
122 profile = request.user.get_profile() | |
123 user_form = EditUserForm(request.POST, instance=request.user) | |
124 profile_form = EditUserProfileForm(request.POST, instance=profile) | |
125 if user_form.is_valid() and profile_form.is_valid(): | |
126 user_form.save() | |
127 profile = profile_form.save(commit=False) | |
128 profile.user = request.user | |
129 profile.save() | |
130 return HttpResponseRedirect(reverse('bio.views.my_profile')) | |
131 else: | |
132 profile = request.user.get_profile() | |
133 user_form = EditUserForm(instance=request.user) | |
134 profile_form = EditUserProfileForm(instance=profile) | |
135 | |
136 return render_to_response('bio/edit_profile.html', { | |
137 'user_form': user_form, | |
138 'profile_form': profile_form, | |
139 }, | |
140 context_instance = RequestContext(request)) | |
141 | |
142 ####################################################################### | |
143 | |
144 @login_required | |
145 def change_avatar(request): | |
146 if request.method == 'POST': | |
147 form = UploadAvatarForm(request.POST, request.FILES) | |
148 if form.is_valid(): | |
149 # Update the profile with the new avatar | |
150 profile = request.user.get_profile() | |
151 | |
152 # First delete any old avatar file | |
153 if profile.avatar.name != '': | |
154 profile.avatar.delete(save=False) | |
155 | |
156 try: | |
157 name, avatar = form.save() | |
158 except IOError: | |
159 messages.error(request, 'A file error occurred.') | |
160 return HttpResponseRedirect(reverse('bio-me')) | |
161 | |
162 if avatar is not None: | |
163 profile.avatar.save(name, avatar, save=False) | |
164 profile.save() | |
165 | |
166 messages.success(request, 'Avatar updated') | |
167 return HttpResponseRedirect(reverse('bio-me')) | |
168 else: | |
169 form = UploadAvatarForm() | |
170 | |
171 return render_to_response('bio/avatar.html', { | |
172 'form': form, | |
173 }, | |
174 context_instance = RequestContext(request)) | |
175 | |
176 ####################################################################### | |
177 | |
178 @require_POST | |
179 def flag_profile(request, profile_id): | |
180 """ | |
181 This function handles the flagging of profiles by users. This function should | |
182 be the target of an AJAX post. | |
183 """ | |
184 if not request.user.is_authenticated(): | |
185 return HttpResponse('Please login or register to flag a profile.') | |
186 | |
187 try: | |
188 profile = UserProfile.objects.get(pk=profile_id) | |
189 except UserProfile.DoesNotExist: | |
190 return HttpResponseBadRequest("That profile doesn't exist.") | |
191 | |
192 flag = UserProfileFlag(user=request.user, profile=profile) | |
193 flag.save() | |
194 email_admins('A Profile Has Been Flagged', """Hello, | |
195 | |
196 A user has flagged a profile for review. | |
197 """) | |
198 return HttpResponse('The profile was flagged. A moderator will review the' \ | |
199 ' profile shortly. Thanks for helping to improve the content on this ' \ | |
200 'site.') | |
201 | |
202 ####################################################################### | |
203 | |
204 @login_required | |
205 def edit_elsewhere(request): | |
206 im_id = 'id_im_%s' # to prevent duplicate ID in HTML output | |
207 if request.method == 'POST': | |
208 new_data = request.POST.copy() | |
209 | |
210 # Add forms | |
211 if new_data.get('sn-form') or new_data.get('im-form') or new_data.get('w-form'): | |
212 | |
213 if new_data.get('sn-form'): | |
214 sn_form = SocialNetworkForm(new_data) | |
215 im_form = InstantMessengerForm(auto_id=im_id) | |
216 w_form = WebsiteForm() | |
217 form = sn_form | |
218 elif new_data.get('im-form'): | |
219 sn_form = SocialNetworkForm() | |
220 im_form = InstantMessengerForm(new_data, auto_id=im_id) | |
221 w_form = WebsiteForm() | |
222 form = im_form | |
223 elif new_data.get('w-form'): | |
224 sn_form = SocialNetworkForm() | |
225 im_form = InstantMessengerForm(auto_id=im_id) | |
226 w_form = WebsiteForm(new_data) | |
227 form = w_form | |
228 | |
229 if form.is_valid(): | |
230 profile = form.save(commit=False) | |
231 profile.user = request.user | |
232 profile.save() | |
233 return HttpResponseRedirect(request.path) | |
234 | |
235 # Delete forms | |
236 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'] | |
238 | |
239 update_occurred = True | |
240 if new_data.get('delete-sn-form'): | |
241 request.user.social_network_profiles.get(id=delete_id).delete() | |
242 elif new_data.get('delete-im-form'): | |
243 request.user.instant_messenger_profiles.get(id=delete_id).delete() | |
244 elif new_data.get('delete-w-form'): | |
245 request.user.website_profiles.get(id=delete_id).delete() | |
246 else: | |
247 update_occurred = False | |
248 | |
249 if update_occurred: | |
250 notify_profile_content_update(request.user.get_profile()) | |
251 | |
252 return HttpResponseRedirect(request.path) | |
253 | |
254 # WTF? | |
255 else: | |
256 return HttpResponseServerError | |
257 | |
258 else: | |
259 # Create blank forms | |
260 sn_form = SocialNetworkForm() | |
261 im_form = InstantMessengerForm(auto_id=im_id) | |
262 w_form = WebsiteForm() | |
263 | |
264 return render_to_response('bio/edit_elsewhere.html', { | |
265 'sn_form': sn_form, | |
266 'im_form': im_form, | |
267 'w_form': w_form, | |
268 }, | |
269 context_instance=RequestContext(request)) | |
270 | |
271 ####################################################################### | |
272 | |
273 @login_required | |
274 def member_search(request): | |
275 if request.method == "POST": | |
276 form = SearchUsersForm(request.POST) | |
277 if form.is_valid(): | |
278 username = form.cleaned_data['username'] | |
279 return HttpResponseRedirect(reverse("bio-view_profile", | |
280 kwargs={'username': username})) | |
281 else: | |
282 form = SearchUsersForm() | |
283 | |
284 return render_to_response('bio/member_search.html', { | |
285 'form': form, | |
286 }, | |
287 context_instance=RequestContext(request)) | |
288 |