annotate gpp/bio/views.py @ 467:b910cc1460c8

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