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
|
bgneal@92
|
10 from django.core.paginator import InvalidPage
|
gremmie@1
|
11 from django.core.urlresolvers import reverse
|
gremmie@1
|
12 from django.contrib.auth.decorators import login_required
|
gremmie@1
|
13
|
bgneal@29
|
14 from elsewhere.models import SocialNetworkForm
|
bgneal@29
|
15 from elsewhere.models import InstantMessengerForm
|
bgneal@29
|
16 from elsewhere.models import WebsiteForm
|
bgneal@29
|
17
|
gremmie@1
|
18 from bio.models import UserProfile
|
gremmie@1
|
19 from bio.forms import UploadAvatarForm
|
gremmie@1
|
20 from bio.forms import EditUserForm
|
gremmie@1
|
21 from bio.forms import EditUserProfileForm
|
gremmie@1
|
22 from core.paginator import DiggPaginator
|
gremmie@1
|
23
|
gremmie@1
|
24 #######################################################################
|
gremmie@1
|
25
|
gremmie@1
|
26 def member_list(request, type='user', page=1):
|
gremmie@1
|
27 if type == 'user':
|
gremmie@1
|
28 users = auth.models.User.objects.all().order_by('username')
|
gremmie@1
|
29 else:
|
gremmie@1
|
30 users = auth.models.User.objects.all().order_by('date_joined')
|
gremmie@1
|
31
|
gremmie@1
|
32 paginator = DiggPaginator(users, 10, body=5, tail=3, margin=3, padding=2)
|
gremmie@1
|
33 try:
|
gremmie@1
|
34 the_page = paginator.page(int(page))
|
gremmie@1
|
35 except InvalidPage:
|
gremmie@1
|
36 raise Http404
|
gremmie@1
|
37
|
gremmie@1
|
38 return render_to_response('bio/members.html', {
|
gremmie@1
|
39 'page': the_page,
|
gremmie@1
|
40 'type': type,
|
gremmie@1
|
41 },
|
gremmie@1
|
42 context_instance = RequestContext(request))
|
gremmie@1
|
43
|
gremmie@1
|
44 #######################################################################
|
gremmie@1
|
45
|
gremmie@1
|
46 @login_required
|
gremmie@1
|
47 def my_profile(request):
|
bgneal@44
|
48 profile = request.user.get_profile()
|
gremmie@1
|
49
|
gremmie@1
|
50 return render_to_response('bio/view_profile.html', {
|
gremmie@1
|
51 'subject': request.user,
|
gremmie@1
|
52 'profile': profile,
|
gremmie@1
|
53 'hide_email': False,
|
gremmie@1
|
54 'this_is_me': True,
|
gremmie@1
|
55 },
|
gremmie@1
|
56 context_instance = RequestContext(request))
|
gremmie@1
|
57
|
gremmie@1
|
58 #######################################################################
|
gremmie@1
|
59
|
gremmie@1
|
60 @login_required
|
gremmie@1
|
61 def view_profile(request, username):
|
gremmie@1
|
62
|
gremmie@1
|
63 user = get_object_or_404(auth.models.User, username = username)
|
gremmie@1
|
64 if user == request.user:
|
gremmie@1
|
65 return HttpResponseRedirect(reverse('bio.views.my_profile'))
|
gremmie@1
|
66
|
bgneal@44
|
67 profile = user.get_profile()
|
gremmie@1
|
68
|
gremmie@1
|
69 # work around MySQL's handling of Boolean
|
gremmie@1
|
70 hide_email = bool(profile.hide_email)
|
gremmie@1
|
71
|
gremmie@1
|
72 return render_to_response('bio/view_profile.html', {
|
gremmie@1
|
73 'subject': user,
|
gremmie@1
|
74 'profile': profile,
|
gremmie@1
|
75 'hide_email': hide_email,
|
gremmie@1
|
76 'this_is_me': False,
|
gremmie@1
|
77 },
|
gremmie@1
|
78 context_instance = RequestContext(request))
|
gremmie@1
|
79
|
gremmie@1
|
80 #######################################################################
|
gremmie@1
|
81
|
gremmie@1
|
82 @login_required
|
gremmie@1
|
83 def edit_profile(request):
|
gremmie@1
|
84 if request.method == 'POST':
|
gremmie@1
|
85 if request.POST.get('submit_button', 'Cancel') == 'Cancel':
|
gremmie@1
|
86 return HttpResponseRedirect(reverse('bio.views.my_profile'))
|
bgneal@44
|
87 profile = request.user.get_profile()
|
gremmie@1
|
88 user_form = EditUserForm(request.POST, instance=request.user)
|
gremmie@1
|
89 profile_form = EditUserProfileForm(request.POST, instance=profile)
|
gremmie@1
|
90 if user_form.is_valid() and profile_form.is_valid():
|
gremmie@1
|
91 user_form.save()
|
gremmie@1
|
92 profile = profile_form.save(commit=False)
|
gremmie@1
|
93 profile.user = request.user
|
gremmie@1
|
94 profile.save()
|
gremmie@1
|
95 return HttpResponseRedirect(reverse('bio.views.my_profile'))
|
gremmie@1
|
96 else:
|
bgneal@44
|
97 profile = request.user.get_profile()
|
gremmie@1
|
98 user_form = EditUserForm(instance=request.user)
|
gremmie@1
|
99 profile_form = EditUserProfileForm(instance=profile)
|
gremmie@1
|
100
|
gremmie@1
|
101 return render_to_response('bio/edit_profile.html', {
|
gremmie@1
|
102 'user_form': user_form,
|
gremmie@1
|
103 'profile_form': profile_form,
|
gremmie@1
|
104 },
|
gremmie@1
|
105 context_instance = RequestContext(request))
|
gremmie@1
|
106
|
gremmie@1
|
107 #######################################################################
|
gremmie@1
|
108
|
gremmie@1
|
109 @login_required
|
gremmie@1
|
110 def change_avatar(request):
|
gremmie@1
|
111 if request.method == 'POST':
|
gremmie@1
|
112 form = UploadAvatarForm(request.POST, request.FILES)
|
gremmie@1
|
113 if form.is_valid():
|
bgneal@44
|
114 profile = request.user.get_profile()
|
gremmie@1
|
115 file = form.get_file()
|
gremmie@1
|
116 if profile.avatar.name != '':
|
gremmie@1
|
117 profile.avatar.delete(save=False)
|
gremmie@1
|
118 if file is not None:
|
gremmie@1
|
119 profile.avatar.save(form.get_filename(), file, save=False)
|
gremmie@1
|
120 profile.save()
|
gremmie@1
|
121
|
gremmie@1
|
122 request.user.message_set.create(message='Avatar updated.')
|
gremmie@1
|
123 return HttpResponseRedirect(reverse('bio-me'))
|
gremmie@1
|
124 else:
|
gremmie@1
|
125 form = UploadAvatarForm()
|
gremmie@1
|
126
|
gremmie@1
|
127 return render_to_response('bio/avatar.html', {
|
gremmie@1
|
128 'form': form,
|
gremmie@1
|
129 },
|
gremmie@1
|
130 context_instance = RequestContext(request))
|
gremmie@1
|
131
|
bgneal@29
|
132 #######################################################################
|
gremmie@1
|
133
|
bgneal@29
|
134 @login_required
|
bgneal@29
|
135 def edit_elsewhere(request):
|
bgneal@29
|
136 im_id = 'id_im_%s' # to prevent duplicate ID in HTML output
|
bgneal@29
|
137 if request.method == 'POST':
|
bgneal@29
|
138 new_data = request.POST.copy()
|
bgneal@29
|
139
|
bgneal@29
|
140 # Add forms
|
bgneal@29
|
141 if new_data.get('sn-form') or new_data.get('im-form') or new_data.get('w-form'):
|
bgneal@29
|
142
|
bgneal@29
|
143 if new_data.get('sn-form'):
|
bgneal@29
|
144 sn_form = SocialNetworkForm(new_data)
|
bgneal@29
|
145 im_form = InstantMessengerForm(auto_id=im_id)
|
bgneal@29
|
146 w_form = WebsiteForm()
|
bgneal@29
|
147 form = sn_form
|
bgneal@29
|
148 elif new_data.get('im-form'):
|
bgneal@29
|
149 sn_form = SocialNetworkForm()
|
bgneal@29
|
150 im_form = InstantMessengerForm(new_data, auto_id=im_id)
|
bgneal@29
|
151 w_form = WebsiteForm()
|
bgneal@29
|
152 form = im_form
|
bgneal@29
|
153 elif new_data.get('w-form'):
|
bgneal@29
|
154 sn_form = SocialNetworkForm()
|
bgneal@29
|
155 im_form = InstantMessengerForm(auto_id=im_id)
|
bgneal@29
|
156 w_form = WebsiteForm(new_data)
|
bgneal@29
|
157 form = w_form
|
bgneal@29
|
158
|
bgneal@29
|
159 if form.is_valid():
|
bgneal@29
|
160 profile = form.save(commit=False)
|
bgneal@29
|
161 profile.user = request.user
|
bgneal@29
|
162 profile.save()
|
bgneal@29
|
163 return HttpResponseRedirect(request.path)
|
bgneal@29
|
164
|
bgneal@29
|
165 # Delete forms
|
bgneal@29
|
166 elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'):
|
bgneal@29
|
167 delete_id = request.POST['delete_id']
|
bgneal@29
|
168
|
bgneal@29
|
169 if new_data.get('delete-sn-form'):
|
bgneal@29
|
170 request.user.social_network_profiles.get(id=delete_id).delete()
|
bgneal@29
|
171 elif new_data.get('delete-im-form'):
|
bgneal@29
|
172 request.user.instant_messenger_profiles.get(id=delete_id).delete()
|
bgneal@29
|
173 elif new_data.get('delete-w-form'):
|
bgneal@29
|
174 request.user.website_profiles.get(id=delete_id).delete()
|
bgneal@29
|
175
|
bgneal@29
|
176 return HttpResponseRedirect(request.path)
|
bgneal@29
|
177
|
bgneal@29
|
178 # WTF?
|
bgneal@29
|
179 else:
|
bgneal@29
|
180 return HttpResponseServerError
|
bgneal@29
|
181
|
bgneal@29
|
182 else:
|
bgneal@29
|
183 # Create blank forms
|
bgneal@29
|
184 sn_form = SocialNetworkForm()
|
bgneal@29
|
185 im_form = InstantMessengerForm(auto_id=im_id)
|
bgneal@29
|
186 w_form = WebsiteForm()
|
bgneal@29
|
187
|
bgneal@29
|
188 return render_to_response('bio/edit_elsewhere.html', {
|
bgneal@29
|
189 'sn_form': sn_form,
|
bgneal@29
|
190 'im_form': im_form,
|
bgneal@29
|
191 'w_form': w_form,
|
bgneal@29
|
192 },
|
bgneal@29
|
193 context_instance=RequestContext(request))
|
bgneal@29
|
194
|