annotate bio/views.py @ 994:504ec8af166d

Use https for Twitter badge image.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Nov 2015 19:22:13 -0600
parents 9e803323a0d0
children 650ab160cbb9
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@789 40 qs = User.objects.filter(is_active=True).select_related('profile')
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@769 54 return render(request, 'bio/members.html', {
gremmie@1 55 'page': the_page,
gremmie@1 56 'type': type,
bgneal@147 57 'num_members': num_members,
bgneal@769 58 })
gremmie@1 59
gremmie@1 60 #######################################################################
gremmie@1 61
gremmie@1 62 @login_required
gremmie@1 63 def my_profile(request):
bgneal@789 64 profile = request.user.profile
bgneal@204 65 badge_collection = BadgeOwnership.objects.filter(
bgneal@204 66 profile=profile).select_related("badge")
gremmie@1 67
bgneal@769 68 return render(request, 'bio/view_profile.html', {
bgneal@471 69 'subject': request.user,
bgneal@471 70 'profile': profile,
gremmie@1 71 'hide_email': False,
gremmie@1 72 'this_is_me': True,
bgneal@204 73 'badge_collection': badge_collection,
bgneal@769 74 })
gremmie@1 75
gremmie@1 76 #######################################################################
gremmie@1 77
gremmie@1 78 @login_required
gremmie@1 79 def view_profile(request, username):
gremmie@1 80
bgneal@789 81 user = get_object_or_404(User.objects.select_related('profile'),
bgneal@789 82 username=username)
gremmie@1 83 if user == request.user:
bgneal@769 84 return redirect('bio-me')
gremmie@1 85
bgneal@789 86 profile = user.profile
bgneal@204 87 hide_email = profile.hide_email
gremmie@1 88
bgneal@204 89 badge_collection = BadgeOwnership.objects.filter(
bgneal@204 90 profile=profile).select_related("badge")
bgneal@471 91
bgneal@769 92 return render(request, 'bio/view_profile.html', {
bgneal@471 93 'subject': user,
bgneal@471 94 'profile': profile,
gremmie@1 95 'hide_email': hide_email,
gremmie@1 96 'this_is_me': False,
bgneal@204 97 'badge_collection': badge_collection,
bgneal@769 98 })
gremmie@1 99
gremmie@1 100 #######################################################################
gremmie@1 101
gremmie@1 102 @login_required
gremmie@1 103 def edit_profile(request):
gremmie@1 104 if request.method == 'POST':
gremmie@1 105 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
bgneal@769 106 return redirect('bio-me')
bgneal@789 107 profile = request.user.profile
gremmie@1 108 user_form = EditUserForm(request.POST, instance=request.user)
gremmie@1 109 profile_form = EditUserProfileForm(request.POST, instance=profile)
gremmie@1 110 if user_form.is_valid() and profile_form.is_valid():
gremmie@1 111 user_form.save()
gremmie@1 112 profile = profile_form.save(commit=False)
gremmie@1 113 profile.user = request.user
gremmie@1 114 profile.save()
bgneal@769 115 return redirect('bio-me')
gremmie@1 116 else:
bgneal@789 117 profile = request.user.profile
gremmie@1 118 user_form = EditUserForm(instance=request.user)
gremmie@1 119 profile_form = EditUserProfileForm(instance=profile)
gremmie@1 120
bgneal@769 121 return render(request, 'bio/edit_profile.html', {
gremmie@1 122 'user_form': user_form,
gremmie@1 123 'profile_form': profile_form,
bgneal@769 124 })
gremmie@1 125
gremmie@1 126 #######################################################################
gremmie@1 127
gremmie@1 128 @login_required
gremmie@1 129 def change_avatar(request):
gremmie@1 130 if request.method == 'POST':
gremmie@1 131 form = UploadAvatarForm(request.POST, request.FILES)
gremmie@1 132 if form.is_valid():
bgneal@265 133 # Update the profile with the new avatar
bgneal@789 134 profile = request.user.profile
bgneal@265 135
bgneal@265 136 # First delete any old avatar file
gremmie@1 137 if profile.avatar.name != '':
gremmie@1 138 profile.avatar.delete(save=False)
bgneal@265 139
bgneal@265 140 try:
bgneal@265 141 name, avatar = form.save()
bgneal@265 142 except IOError:
bgneal@265 143 messages.error(request, 'A file error occurred.')
bgneal@769 144 return redirect('bio-me')
bgneal@265 145
bgneal@265 146 if avatar is not None:
bgneal@265 147 profile.avatar.save(name, avatar, save=False)
gremmie@1 148 profile.save()
gremmie@1 149
bgneal@178 150 messages.success(request, 'Avatar updated')
bgneal@769 151 return redirect('bio-me')
gremmie@1 152 else:
gremmie@1 153 form = UploadAvatarForm()
gremmie@1 154
bgneal@769 155 return render(request, 'bio/avatar.html', {
gremmie@1 156 'form': form,
bgneal@769 157 })
gremmie@1 158
bgneal@29 159 #######################################################################
gremmie@1 160
bgneal@138 161 @require_POST
bgneal@138 162 def flag_profile(request, profile_id):
bgneal@138 163 """
bgneal@138 164 This function handles the flagging of profiles by users. This function should
bgneal@138 165 be the target of an AJAX post.
bgneal@138 166 """
bgneal@138 167 if not request.user.is_authenticated():
bgneal@138 168 return HttpResponse('Please login or register to flag a profile.')
bgneal@138 169
bgneal@138 170 try:
bgneal@138 171 profile = UserProfile.objects.get(pk=profile_id)
bgneal@138 172 except UserProfile.DoesNotExist:
bgneal@138 173 return HttpResponseBadRequest("That profile doesn't exist.")
bgneal@138 174
bgneal@138 175 flag = UserProfileFlag(user=request.user, profile=profile)
bgneal@138 176 flag.save()
bgneal@138 177 email_admins('A Profile Has Been Flagged', """Hello,
bgneal@138 178
bgneal@138 179 A user has flagged a profile for review.
bgneal@138 180 """)
bgneal@769 181 return HttpResponse('The profile was flagged. A moderator will review the'
bgneal@769 182 ' profile shortly. Thanks for helping to improve the content on this '
bgneal@138 183 'site.')
bgneal@138 184
bgneal@138 185 #######################################################################
bgneal@138 186
bgneal@29 187 @login_required
bgneal@29 188 def edit_elsewhere(request):
bgneal@29 189 im_id = 'id_im_%s' # to prevent duplicate ID in HTML output
bgneal@29 190 if request.method == 'POST':
bgneal@29 191 new_data = request.POST.copy()
bgneal@29 192
bgneal@29 193 # Add forms
bgneal@29 194 if new_data.get('sn-form') or new_data.get('im-form') or new_data.get('w-form'):
bgneal@29 195
bgneal@29 196 if new_data.get('sn-form'):
bgneal@29 197 sn_form = SocialNetworkForm(new_data)
bgneal@29 198 im_form = InstantMessengerForm(auto_id=im_id)
bgneal@29 199 w_form = WebsiteForm()
bgneal@29 200 form = sn_form
bgneal@29 201 elif new_data.get('im-form'):
bgneal@29 202 sn_form = SocialNetworkForm()
bgneal@29 203 im_form = InstantMessengerForm(new_data, auto_id=im_id)
bgneal@29 204 w_form = WebsiteForm()
bgneal@29 205 form = im_form
bgneal@29 206 elif new_data.get('w-form'):
bgneal@29 207 sn_form = SocialNetworkForm()
bgneal@29 208 im_form = InstantMessengerForm(auto_id=im_id)
bgneal@29 209 w_form = WebsiteForm(new_data)
bgneal@29 210 form = w_form
bgneal@29 211
bgneal@29 212 if form.is_valid():
bgneal@29 213 profile = form.save(commit=False)
bgneal@29 214 profile.user = request.user
bgneal@29 215 profile.save()
bgneal@769 216 return redirect(request.path)
bgneal@29 217
bgneal@29 218 # Delete forms
bgneal@29 219 elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'):
bgneal@29 220 delete_id = request.POST['delete_id']
bgneal@29 221
bgneal@471 222 update_occurred = True
bgneal@29 223 if new_data.get('delete-sn-form'):
bgneal@29 224 request.user.social_network_profiles.get(id=delete_id).delete()
bgneal@29 225 elif new_data.get('delete-im-form'):
bgneal@29 226 request.user.instant_messenger_profiles.get(id=delete_id).delete()
bgneal@29 227 elif new_data.get('delete-w-form'):
bgneal@29 228 request.user.website_profiles.get(id=delete_id).delete()
bgneal@471 229 else:
bgneal@471 230 update_occurred = False
bgneal@471 231
bgneal@471 232 if update_occurred:
bgneal@789 233 notify_profile_content_update(request.user.profile)
bgneal@29 234
bgneal@769 235 return redirect(request.path)
bgneal@29 236
bgneal@29 237 # WTF?
bgneal@29 238 else:
bgneal@29 239 return HttpResponseServerError
bgneal@29 240
bgneal@29 241 else:
bgneal@29 242 # Create blank forms
bgneal@29 243 sn_form = SocialNetworkForm()
bgneal@29 244 im_form = InstantMessengerForm(auto_id=im_id)
bgneal@29 245 w_form = WebsiteForm()
bgneal@29 246
bgneal@769 247 return render(request, 'bio/edit_elsewhere.html', {
bgneal@471 248 'sn_form': sn_form,
bgneal@471 249 'im_form': im_form,
bgneal@29 250 'w_form': w_form,
bgneal@769 251 })
bgneal@29 252
bgneal@149 253 #######################################################################
bgneal@149 254
bgneal@149 255 @login_required
bgneal@149 256 def member_search(request):
bgneal@769 257 data = request.GET if request.GET else None
bgneal@769 258 form = SearchUsersForm(data)
bgneal@769 259 if form.is_valid():
bgneal@769 260 username = form.cleaned_data['username']
bgneal@769 261 return redirect('bio-view_profile', username=username)
bgneal@149 262
bgneal@769 263 return render(request, 'bio/member_search.html', {'form': form})