gremmie@1: """ gremmie@1: Views for the bio application. gremmie@1: """ gremmie@1: gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.shortcuts import get_object_or_404 gremmie@1: from django.template import RequestContext gremmie@1: from django.contrib import auth bgneal@138: from django.http import HttpResponse bgneal@138: from django.http import HttpResponseBadRequest gremmie@1: from django.http import HttpResponseRedirect bgneal@92: from django.core.paginator import InvalidPage gremmie@1: from django.core.urlresolvers import reverse 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 gremmie@1: from bio.forms import UploadAvatarForm gremmie@1: from bio.forms import EditUserForm gremmie@1: from bio.forms import EditUserProfileForm gremmie@1: from core.paginator import DiggPaginator bgneal@138: from core.functions import email_admins gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@125: @login_required gremmie@1: def member_list(request, type='user', page=1): gremmie@1: if type == 'user': gremmie@1: users = auth.models.User.objects.all().order_by('username') gremmie@1: else: gremmie@1: users = auth.models.User.objects.all().order_by('date_joined') gremmie@1: gremmie@1: paginator = DiggPaginator(users, 10, body=5, tail=3, margin=3, padding=2) gremmie@1: try: gremmie@1: the_page = paginator.page(int(page)) gremmie@1: except InvalidPage: gremmie@1: raise Http404 gremmie@1: bgneal@118: # Attach user profiles to each post to avoid using get_user_profile() in bgneal@118: # the template. bgneal@118: users = set(user.id for user in the_page.object_list) bgneal@118: bgneal@118: profiles = UserProfile.objects.filter(user__id__in=users).select_related() bgneal@118: user_profiles = dict((profile.user.id, profile) for profile in profiles) bgneal@118: bgneal@118: for user in the_page.object_list: bgneal@118: user.user_profile = user_profiles[user.id] bgneal@118: gremmie@1: return render_to_response('bio/members.html', { gremmie@1: 'page': the_page, gremmie@1: 'type': type, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def my_profile(request): bgneal@44: profile = request.user.get_profile() gremmie@1: gremmie@1: return render_to_response('bio/view_profile.html', { gremmie@1: 'subject': request.user, gremmie@1: 'profile': profile, gremmie@1: 'hide_email': False, gremmie@1: 'this_is_me': True, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: @login_required gremmie@1: def view_profile(request, username): gremmie@1: gremmie@1: user = get_object_or_404(auth.models.User, username = username) gremmie@1: if user == request.user: gremmie@1: return HttpResponseRedirect(reverse('bio.views.my_profile')) gremmie@1: bgneal@44: profile = user.get_profile() gremmie@1: gremmie@1: # work around MySQL's handling of Boolean gremmie@1: hide_email = bool(profile.hide_email) gremmie@1: gremmie@1: return render_to_response('bio/view_profile.html', { gremmie@1: 'subject': user, gremmie@1: 'profile': profile, gremmie@1: 'hide_email': hide_email, gremmie@1: 'this_is_me': False, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) 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': gremmie@1: return HttpResponseRedirect(reverse('bio.views.my_profile')) bgneal@44: profile = request.user.get_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() gremmie@1: return HttpResponseRedirect(reverse('bio.views.my_profile')) gremmie@1: else: bgneal@44: profile = request.user.get_profile() gremmie@1: user_form = EditUserForm(instance=request.user) gremmie@1: profile_form = EditUserProfileForm(instance=profile) gremmie@1: gremmie@1: return render_to_response('bio/edit_profile.html', { gremmie@1: 'user_form': user_form, gremmie@1: 'profile_form': profile_form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) 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@44: profile = request.user.get_profile() gremmie@1: file = form.get_file() gremmie@1: if profile.avatar.name != '': gremmie@1: profile.avatar.delete(save=False) gremmie@1: if file is not None: gremmie@1: profile.avatar.save(form.get_filename(), file, save=False) gremmie@1: profile.save() gremmie@1: gremmie@1: request.user.message_set.create(message='Avatar updated.') gremmie@1: return HttpResponseRedirect(reverse('bio-me')) gremmie@1: else: gremmie@1: form = UploadAvatarForm() gremmie@1: gremmie@1: return render_to_response('bio/avatar.html', { gremmie@1: 'form': form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) 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@138: return HttpResponse('The profile was flagged. A moderator will review the' \ bgneal@138: ' 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@29: return HttpResponseRedirect(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@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@29: bgneal@29: return HttpResponseRedirect(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@29: return render_to_response('bio/edit_elsewhere.html', { bgneal@29: 'sn_form': sn_form, bgneal@29: 'im_form': im_form, bgneal@29: 'w_form': w_form, bgneal@29: }, bgneal@29: context_instance=RequestContext(request)) bgneal@29: