annotate gpp/bio/views.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000 (2009-04-06)
parents
children 74f04122295e
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
gremmie@1 8 from django.contrib import auth
gremmie@1 9 from django.http import HttpResponseRedirect
gremmie@1 10 from django.core.urlresolvers import reverse
gremmie@1 11 from django.contrib.auth.decorators import login_required
gremmie@1 12
gremmie@1 13 from bio.models import UserProfile
gremmie@1 14 from bio.forms import UploadAvatarForm
gremmie@1 15 from bio.forms import EditUserForm
gremmie@1 16 from bio.forms import EditUserProfileForm
gremmie@1 17 from core.paginator import DiggPaginator
gremmie@1 18
gremmie@1 19 #######################################################################
gremmie@1 20
gremmie@1 21 def get_profile(user):
gremmie@1 22 try:
gremmie@1 23 profile = user.get_profile()
gremmie@1 24 except:
gremmie@1 25 profile = UserProfile()
gremmie@1 26 profile.user = user
gremmie@1 27 return profile
gremmie@1 28
gremmie@1 29 #######################################################################
gremmie@1 30
gremmie@1 31 def member_list(request, type='user', page=1):
gremmie@1 32 if type == 'user':
gremmie@1 33 users = auth.models.User.objects.all().order_by('username')
gremmie@1 34 else:
gremmie@1 35 users = auth.models.User.objects.all().order_by('date_joined')
gremmie@1 36
gremmie@1 37 paginator = DiggPaginator(users, 10, body=5, tail=3, margin=3, padding=2)
gremmie@1 38 try:
gremmie@1 39 the_page = paginator.page(int(page))
gremmie@1 40 except InvalidPage:
gremmie@1 41 raise Http404
gremmie@1 42
gremmie@1 43 return render_to_response('bio/members.html', {
gremmie@1 44 'page': the_page,
gremmie@1 45 'type': type,
gremmie@1 46 },
gremmie@1 47 context_instance = RequestContext(request))
gremmie@1 48
gremmie@1 49 #######################################################################
gremmie@1 50
gremmie@1 51 @login_required
gremmie@1 52 def my_profile(request):
gremmie@1 53 profile = get_profile(request.user)
gremmie@1 54
gremmie@1 55 return render_to_response('bio/view_profile.html', {
gremmie@1 56 'subject': request.user,
gremmie@1 57 'profile': profile,
gremmie@1 58 'hide_email': False,
gremmie@1 59 'this_is_me': True,
gremmie@1 60 },
gremmie@1 61 context_instance = RequestContext(request))
gremmie@1 62
gremmie@1 63 #######################################################################
gremmie@1 64
gremmie@1 65 @login_required
gremmie@1 66 def view_profile(request, username):
gremmie@1 67
gremmie@1 68 user = get_object_or_404(auth.models.User, username = username)
gremmie@1 69 if user == request.user:
gremmie@1 70 return HttpResponseRedirect(reverse('bio.views.my_profile'))
gremmie@1 71
gremmie@1 72 profile = get_profile(user)
gremmie@1 73
gremmie@1 74 # work around MySQL's handling of Boolean
gremmie@1 75 hide_email = bool(profile.hide_email)
gremmie@1 76
gremmie@1 77 return render_to_response('bio/view_profile.html', {
gremmie@1 78 'subject': user,
gremmie@1 79 'profile': profile,
gremmie@1 80 'hide_email': hide_email,
gremmie@1 81 'this_is_me': False,
gremmie@1 82 },
gremmie@1 83 context_instance = RequestContext(request))
gremmie@1 84
gremmie@1 85 #######################################################################
gremmie@1 86
gremmie@1 87 @login_required
gremmie@1 88 def edit_profile(request):
gremmie@1 89 if request.method == 'POST':
gremmie@1 90 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
gremmie@1 91 return HttpResponseRedirect(reverse('bio.views.my_profile'))
gremmie@1 92 profile = get_profile(request.user)
gremmie@1 93 user_form = EditUserForm(request.POST, instance=request.user)
gremmie@1 94 profile_form = EditUserProfileForm(request.POST, instance=profile)
gremmie@1 95 if user_form.is_valid() and profile_form.is_valid():
gremmie@1 96 user_form.save()
gremmie@1 97 profile = profile_form.save(commit=False)
gremmie@1 98 profile.user = request.user
gremmie@1 99 profile.save()
gremmie@1 100 return HttpResponseRedirect(reverse('bio.views.my_profile'))
gremmie@1 101 else:
gremmie@1 102 profile = get_profile(request.user)
gremmie@1 103 user_form = EditUserForm(instance=request.user)
gremmie@1 104 profile_form = EditUserProfileForm(instance=profile)
gremmie@1 105
gremmie@1 106 return render_to_response('bio/edit_profile.html', {
gremmie@1 107 'user_form': user_form,
gremmie@1 108 'profile_form': profile_form,
gremmie@1 109 },
gremmie@1 110 context_instance = RequestContext(request))
gremmie@1 111
gremmie@1 112 #######################################################################
gremmie@1 113
gremmie@1 114 @login_required
gremmie@1 115 def change_avatar(request):
gremmie@1 116 if request.method == 'POST':
gremmie@1 117 form = UploadAvatarForm(request.POST, request.FILES)
gremmie@1 118 if form.is_valid():
gremmie@1 119 profile = get_profile(request.user)
gremmie@1 120 file = form.get_file()
gremmie@1 121 if profile.avatar.name != '':
gremmie@1 122 profile.avatar.delete(save=False)
gremmie@1 123 if file is not None:
gremmie@1 124 profile.avatar.save(form.get_filename(), file, save=False)
gremmie@1 125 profile.save()
gremmie@1 126
gremmie@1 127 request.user.message_set.create(message='Avatar updated.')
gremmie@1 128 return HttpResponseRedirect(reverse('bio-me'))
gremmie@1 129 else:
gremmie@1 130 form = UploadAvatarForm()
gremmie@1 131
gremmie@1 132 return render_to_response('bio/avatar.html', {
gremmie@1 133 'form': form,
gremmie@1 134 },
gremmie@1 135 context_instance = RequestContext(request))
gremmie@1 136
gremmie@1 137
gremmie@1 138 # vim: ts=4 sw=4