gremmie@1: """ gremmie@1: Views for the bio application. bgneal@471: gremmie@1: """ bgneal@769: from django.shortcuts import render, redirect gremmie@1: from django.shortcuts import get_object_or_404 bgneal@178: from django.contrib import messages bgneal@259: from django.contrib.auth.models import User bgneal@138: from django.http import HttpResponse bgneal@138: from django.http import HttpResponseBadRequest bgneal@562: from django.http import HttpResponseServerError bgneal@243: from django.http import Http404 bgneal@92: from django.core.paginator import InvalidPage gremmie@1: from django.contrib.auth.decorators import login_required bgneal@138: from django.views.decorators.http import require_POST gremmie@1: bgneal@29: from elsewhere.models import SocialNetworkForm bgneal@29: from elsewhere.models import InstantMessengerForm bgneal@29: from elsewhere.models import WebsiteForm bgneal@29: gremmie@1: from bio.models import UserProfile bgneal@138: from bio.models import UserProfileFlag bgneal@204: from bio.models import BadgeOwnership gremmie@1: from bio.forms import UploadAvatarForm gremmie@1: from bio.forms import EditUserForm gremmie@1: from bio.forms import EditUserProfileForm bgneal@149: from bio.forms import SearchUsersForm bgneal@471: from bio.signals import notify_profile_content_update gremmie@1: from core.paginator import DiggPaginator bgneal@138: from core.functions import email_admins bgneal@243: from core.functions import get_page gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@125: @login_required bgneal@243: def member_list(request, type='user'): bgneal@147: """ bgneal@147: This view displays the member list. Only active members are displayed. bgneal@147: """ bgneal@789: qs = User.objects.filter(is_active=True).select_related('profile') gremmie@1: if type == 'user': bgneal@147: qs = qs.order_by('username') gremmie@1: else: bgneal@147: qs = qs.order_by('date_joined') bgneal@147: num_members = qs.count() gremmie@1: bgneal@147: paginator = DiggPaginator(qs, 20, body=5, tail=3, margin=3, padding=2) bgneal@243: page = get_page(request.GET) gremmie@1: try: bgneal@243: the_page = paginator.page(page) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: bgneal@769: return render(request, 'bio/members.html', { gremmie@1: 'page': the_page, gremmie@1: 'type': type, bgneal@147: 'num_members': num_members, bgneal@1074: 'V3_DESIGN': True, bgneal@769: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def my_profile(request): bgneal@789: profile = request.user.profile bgneal@204: badge_collection = BadgeOwnership.objects.filter( bgneal@204: profile=profile).select_related("badge") gremmie@1: bgneal@769: return render(request, 'bio/view_profile.html', { bgneal@471: 'subject': request.user, bgneal@471: 'profile': profile, gremmie@1: 'hide_email': False, gremmie@1: 'this_is_me': True, bgneal@204: 'badge_collection': badge_collection, bgneal@1120: 'V3_DESIGN': True, bgneal@769: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def view_profile(request, username): gremmie@1: bgneal@789: user = get_object_or_404(User.objects.select_related('profile'), bgneal@789: username=username) gremmie@1: if user == request.user: bgneal@769: return redirect('bio-me') gremmie@1: bgneal@789: profile = user.profile bgneal@204: hide_email = profile.hide_email gremmie@1: bgneal@204: badge_collection = BadgeOwnership.objects.filter( bgneal@204: profile=profile).select_related("badge") bgneal@471: bgneal@769: return render(request, 'bio/view_profile.html', { bgneal@471: 'subject': user, bgneal@471: 'profile': profile, gremmie@1: 'hide_email': hide_email, gremmie@1: 'this_is_me': False, bgneal@204: 'badge_collection': badge_collection, bgneal@1120: 'V3_DESIGN': True, bgneal@769: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def edit_profile(request): gremmie@1: if request.method == 'POST': gremmie@1: if request.POST.get('submit_button', 'Cancel') == 'Cancel': bgneal@769: return redirect('bio-me') bgneal@789: profile = request.user.profile gremmie@1: user_form = EditUserForm(request.POST, instance=request.user) gremmie@1: profile_form = EditUserProfileForm(request.POST, instance=profile) gremmie@1: if user_form.is_valid() and profile_form.is_valid(): gremmie@1: user_form.save() gremmie@1: profile = profile_form.save(commit=False) gremmie@1: profile.user = request.user gremmie@1: profile.save() bgneal@769: return redirect('bio-me') gremmie@1: else: bgneal@789: profile = request.user.profile gremmie@1: user_form = EditUserForm(instance=request.user) gremmie@1: profile_form = EditUserProfileForm(instance=profile) gremmie@1: bgneal@769: return render(request, 'bio/edit_profile.html', { gremmie@1: 'user_form': user_form, gremmie@1: 'profile_form': profile_form, bgneal@769: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def change_avatar(request): gremmie@1: if request.method == 'POST': gremmie@1: form = UploadAvatarForm(request.POST, request.FILES) gremmie@1: if form.is_valid(): bgneal@265: # Update the profile with the new avatar bgneal@789: profile = request.user.profile bgneal@265: bgneal@265: # First delete any old avatar file gremmie@1: if profile.avatar.name != '': gremmie@1: profile.avatar.delete(save=False) bgneal@265: bgneal@265: try: bgneal@265: name, avatar = form.save() bgneal@265: except IOError: bgneal@265: messages.error(request, 'A file error occurred.') bgneal@769: return redirect('bio-me') bgneal@265: bgneal@265: if avatar is not None: bgneal@265: profile.avatar.save(name, avatar, save=False) gremmie@1: profile.save() gremmie@1: bgneal@178: messages.success(request, 'Avatar updated') bgneal@769: return redirect('bio-me') gremmie@1: else: gremmie@1: form = UploadAvatarForm() gremmie@1: bgneal@769: return render(request, 'bio/avatar.html', { gremmie@1: 'form': form, bgneal@1122: 'V3_DESIGN': True, bgneal@769: }) gremmie@1: bgneal@29: ####################################################################### gremmie@1: bgneal@138: @require_POST bgneal@138: def flag_profile(request, profile_id): bgneal@138: """ bgneal@138: This function handles the flagging of profiles by users. This function should bgneal@138: be the target of an AJAX post. bgneal@138: """ bgneal@138: if not request.user.is_authenticated(): bgneal@138: return HttpResponse('Please login or register to flag a profile.') bgneal@138: bgneal@138: try: bgneal@138: profile = UserProfile.objects.get(pk=profile_id) bgneal@138: except UserProfile.DoesNotExist: bgneal@138: return HttpResponseBadRequest("That profile doesn't exist.") bgneal@138: bgneal@138: flag = UserProfileFlag(user=request.user, profile=profile) bgneal@138: flag.save() bgneal@138: email_admins('A Profile Has Been Flagged', """Hello, bgneal@138: bgneal@138: A user has flagged a profile for review. bgneal@138: """) bgneal@769: return HttpResponse('The profile was flagged. A moderator will review the' bgneal@769: ' profile shortly. Thanks for helping to improve the content on this ' bgneal@138: 'site.') bgneal@138: bgneal@138: ####################################################################### bgneal@138: bgneal@29: @login_required bgneal@29: def edit_elsewhere(request): bgneal@29: im_id = 'id_im_%s' # to prevent duplicate ID in HTML output bgneal@29: if request.method == 'POST': bgneal@29: new_data = request.POST.copy() bgneal@29: bgneal@29: # Add forms bgneal@29: if new_data.get('sn-form') or new_data.get('im-form') or new_data.get('w-form'): bgneal@29: bgneal@29: if new_data.get('sn-form'): bgneal@29: sn_form = SocialNetworkForm(new_data) bgneal@29: im_form = InstantMessengerForm(auto_id=im_id) bgneal@29: w_form = WebsiteForm() bgneal@29: form = sn_form bgneal@29: elif new_data.get('im-form'): bgneal@29: sn_form = SocialNetworkForm() bgneal@29: im_form = InstantMessengerForm(new_data, auto_id=im_id) bgneal@29: w_form = WebsiteForm() bgneal@29: form = im_form bgneal@29: elif new_data.get('w-form'): bgneal@29: sn_form = SocialNetworkForm() bgneal@29: im_form = InstantMessengerForm(auto_id=im_id) bgneal@29: w_form = WebsiteForm(new_data) bgneal@29: form = w_form bgneal@29: bgneal@29: if form.is_valid(): bgneal@29: profile = form.save(commit=False) bgneal@29: profile.user = request.user bgneal@29: profile.save() bgneal@769: return redirect(request.path) bgneal@29: bgneal@29: # Delete forms bgneal@29: elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'): bgneal@29: delete_id = request.POST['delete_id'] bgneal@29: bgneal@471: update_occurred = True bgneal@29: if new_data.get('delete-sn-form'): bgneal@29: request.user.social_network_profiles.get(id=delete_id).delete() bgneal@29: elif new_data.get('delete-im-form'): bgneal@29: request.user.instant_messenger_profiles.get(id=delete_id).delete() bgneal@29: elif new_data.get('delete-w-form'): bgneal@29: request.user.website_profiles.get(id=delete_id).delete() bgneal@471: else: bgneal@471: update_occurred = False bgneal@471: bgneal@471: if update_occurred: bgneal@789: notify_profile_content_update(request.user.profile) bgneal@29: bgneal@769: return redirect(request.path) bgneal@29: bgneal@29: # WTF? bgneal@29: else: bgneal@29: return HttpResponseServerError bgneal@29: bgneal@29: else: bgneal@29: # Create blank forms bgneal@29: sn_form = SocialNetworkForm() bgneal@29: im_form = InstantMessengerForm(auto_id=im_id) bgneal@29: w_form = WebsiteForm() bgneal@29: bgneal@769: return render(request, 'bio/edit_elsewhere.html', { bgneal@471: 'sn_form': sn_form, bgneal@471: 'im_form': im_form, bgneal@29: 'w_form': w_form, bgneal@769: }) bgneal@29: bgneal@149: ####################################################################### bgneal@149: bgneal@149: @login_required bgneal@149: def member_search(request): bgneal@769: data = request.GET if request.GET else None bgneal@769: form = SearchUsersForm(data) bgneal@769: if form.is_valid(): bgneal@769: username = form.cleaned_data['username'] bgneal@769: return redirect('bio-view_profile', username=username) bgneal@149: bgneal@1078: return render(request, 'bio/member_search.html', { bgneal@1078: 'form': form, bgneal@1078: 'V3_DESIGN': True, bgneal@1078: })