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