annotate bio/views.py @ 787:7e17b9e45356

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