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