annotate bio/views.py @ 605:a7968613c469

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