1 # -*- coding: utf-8 -*- |
|
2 from __future__ import absolute_import |
|
3 |
|
4 #python |
|
5 from urlparse import urlparse |
|
6 import json |
|
7 import urllib |
|
8 import os |
|
9 |
|
10 #django |
|
11 from django.conf import settings |
|
12 from django.db.models import Q |
|
13 from django.http import HttpResponse |
|
14 from django.shortcuts import render_to_response |
|
15 from django.template import RequestContext |
|
16 from django.core.urlresolvers import reverse |
|
17 from django.db.models.signals import post_save |
|
18 |
|
19 #django.contrib |
|
20 from django.contrib.auth.decorators import login_required |
|
21 from django.contrib.auth.forms import AuthenticationForm |
|
22 from django.contrib.auth.forms import PasswordChangeForm |
|
23 from django.contrib.auth.models import User |
|
24 from django.core.exceptions import ObjectDoesNotExist |
|
25 |
|
26 #PIL |
|
27 from PIL import Image |
|
28 |
|
29 #kiwipycon |
|
30 from project.kiwipycon.utils import set_message_cookie |
|
31 from project.kiwipycon.talk.models import Talk |
|
32 from project.kiwipycon.registration.models import Registration |
|
33 from project.kiwipycon.registration.models import Wifi |
|
34 from project.kiwipycon.registration.forms import WifiForm |
|
35 from project.kiwipycon.sponsor.models import Sponsor |
|
36 |
|
37 from .utils import kiwipycon_createuser |
|
38 from .utils import handle_uploaded_photo |
|
39 from .forms import RegisterForm |
|
40 from .forms import EditProfileForm |
|
41 from .forms import UsernameForm |
|
42 |
|
43 @login_required |
|
44 def account(request, template_name="user/account.html"): |
|
45 """Displays the main screen of the current user's account. |
|
46 """ |
|
47 user = request.user |
|
48 profile = user.get_profile() |
|
49 |
|
50 talks = Talk.objects.filter(speaker=user) |
|
51 try: |
|
52 registration = Registration.objects.get(registrant=user) |
|
53 except ObjectDoesNotExist: |
|
54 registration = None |
|
55 try: |
|
56 wifiobj = Wifi.objects.get(user=user) |
|
57 except ObjectDoesNotExist: |
|
58 wifiobj = None |
|
59 |
|
60 if profile.photo: |
|
61 photo = os.path.join(settings.USER_MEDIA_URL, profile.photo) |
|
62 else: |
|
63 photo = '/img/user-default.png' |
|
64 |
|
65 qstring = "" |
|
66 |
|
67 wifi_comment = None |
|
68 if wifiobj: |
|
69 wifi_form = False |
|
70 else: |
|
71 if request.method == "POST": |
|
72 wifi_form = WifiForm(request.POST) |
|
73 if wifi_form.is_valid(): |
|
74 wifi_form.save(user) |
|
75 wifi_comment = 'Thanks, your wifi preference has been saved' |
|
76 wifi_form = None |
|
77 else: |
|
78 wifi_form = WifiForm() |
|
79 |
|
80 return render_to_response(template_name, RequestContext(request, { |
|
81 "form" : wifi_form, "comment": wifi_comment, |
|
82 "user" : user, "profile" : profile, "photo" : photo, |
|
83 "talks" : talks, "registration" : registration, |
|
84 })) |
|
85 |
|
86 @login_required |
|
87 def edit_profile(request, template_name="user/editprofile.html"): |
|
88 """Allows user to edit profile |
|
89 """ |
|
90 user = request.user |
|
91 profile = user.get_profile() |
|
92 |
|
93 if request.method == "POST": |
|
94 form = EditProfileForm(data=request.POST, |
|
95 files=request.FILES) |
|
96 |
|
97 if form.is_valid(): |
|
98 photo = request.FILES.get('photo', None) |
|
99 filename= None |
|
100 if photo: |
|
101 filename = handle_uploaded_photo(user, request.FILES['photo']) |
|
102 if filename: |
|
103 profile.photo = filename |
|
104 |
|
105 user.email = form.data.get("email") |
|
106 user.first_name = form.data.get("first_name") |
|
107 user.last_name = form.data.get("last_name") |
|
108 user.save() |
|
109 |
|
110 profile.url = form.data.get("url") |
|
111 profile.about = form.data.get("about") |
|
112 profile.save() |
|
113 |
|
114 redirect_to = reverse("kiwipycon_account") |
|
115 return set_message_cookie(redirect_to, |
|
116 msg = u"Your profile has been changed.") |
|
117 |
|
118 else: |
|
119 form = EditProfileForm(initial={"email" : user.email, |
|
120 "email2" : user.email, # hidden field |
|
121 "first_name" : user.first_name, |
|
122 "last_name" : user.last_name, |
|
123 "url" : profile.url, |
|
124 "about" : profile.about, |
|
125 }) |
|
126 |
|
127 return render_to_response(template_name, RequestContext(request, { |
|
128 "form": form |
|
129 })) |
|
130 |
|
131 def login(request, template_name="user/login.html"): |
|
132 """Custom view to login or register/login a user. |
|
133 Integration of register and login form |
|
134 It uses Django's standard AuthenticationForm, though. |
|
135 """ |
|
136 user = request.user |
|
137 if user.is_authenticated(): |
|
138 redirect_to = reverse("kiwipycon_account") |
|
139 return set_message_cookie(redirect_to, |
|
140 msg = u"Redirected to account from login form.") |
|
141 |
|
142 # Using Djangos default AuthenticationForm |
|
143 login_form = AuthenticationForm() |
|
144 register_form = RegisterForm() |
|
145 |
|
146 if request.POST.get("action") == "login": |
|
147 login_form = AuthenticationForm(data=request.POST) |
|
148 |
|
149 if login_form.is_valid(): |
|
150 redirect_to = request.POST.get("next") |
|
151 # Light security check -- make sure redirect_to isn't garbage. |
|
152 if not redirect_to or '//' in redirect_to or ' ' in redirect_to: |
|
153 redirect_to = reverse("kiwipycon_account") |
|
154 |
|
155 from django.contrib.auth import login |
|
156 login(request, login_form.get_user()) |
|
157 |
|
158 return set_message_cookie(redirect_to, msg = u"You have been logged in.") |
|
159 |
|
160 elif request.POST.get("action") == "register": |
|
161 register_form = RegisterForm(data=request.POST) |
|
162 if register_form.is_valid(): |
|
163 |
|
164 user = kiwipycon_createuser(request, register_form.data) |
|
165 |
|
166 redirect_to = request.POST.get("next") |
|
167 if not redirect_to or '//' in redirect_to or ' ' in redirect_to: |
|
168 redirect_to = reverse("kiwipycon_account") |
|
169 |
|
170 return set_message_cookie( |
|
171 redirect_to, msg = u"You have been registered and logged in.") |
|
172 |
|
173 # Get next_url |
|
174 next_url = request.REQUEST.get("next") |
|
175 if next_url is None: |
|
176 next_url = request.META.get("HTTP_REFERER") |
|
177 if next_url is None: |
|
178 next_url = reverse("kiwipycon_account") |
|
179 # Get just the path of the url. See django.contrib.auth.views.login for more |
|
180 next_url = urlparse(next_url) |
|
181 next_url = next_url[2] |
|
182 |
|
183 try: |
|
184 login_form_errors = login_form.errors["__all__"] |
|
185 except KeyError: |
|
186 login_form_errors = None |
|
187 |
|
188 return render_to_response(template_name, RequestContext(request, { |
|
189 "login_form" : login_form, |
|
190 "login_form_errors" : login_form_errors, |
|
191 "register_form" : register_form, |
|
192 "next_url" : next_url, |
|
193 })) |
|
194 |
|
195 def logout(request): |
|
196 """Custom method to logout a user. |
|
197 |
|
198 The reason to use a custom logout method is just to provide a login and a |
|
199 logoutmethod on one place. |
|
200 """ |
|
201 from django.contrib.auth import logout |
|
202 logout(request) |
|
203 |
|
204 redirect_to = '/' |
|
205 return set_message_cookie(redirect_to, msg = u"You have been logged out.") |
|
206 |
|
207 @login_required |
|
208 def password(request, template_name="user/password.html"): |
|
209 """Changes the password of current user. |
|
210 """ |
|
211 if request.method == "POST": |
|
212 form = PasswordChangeForm(request.user, request.POST) |
|
213 if form.is_valid(): |
|
214 form.save() |
|
215 redirect_to = reverse("kiwipycon_account") |
|
216 return set_message_cookie(redirect_to, |
|
217 msg = u"Your password has been changed.") |
|
218 else: |
|
219 form = PasswordChangeForm(request.user) |
|
220 |
|
221 return render_to_response(template_name, RequestContext(request, { |
|
222 "form" : form |
|
223 })) |
|
224 |
|
225 @login_required |
|
226 def username(request, template_name="user/username.html"): |
|
227 """Saves the username from the data form. |
|
228 """ |
|
229 if request.method == "POST": |
|
230 username_form = UsernameForm(initial={"username" : request.user.username}, data=request.POST) |
|
231 if username_form.is_valid(): |
|
232 request.user.username = username_form.cleaned_data.get("username") |
|
233 request.user.save() |
|
234 redirect_to = reverse("kiwipycon_account") |
|
235 return set_message_cookie(redirect_to, |
|
236 msg = u"Your username has been changed.") |
|
237 else: |
|
238 username_form = UsernameForm(initial={"username" : request.user.username}) |
|
239 |
|
240 return render_to_response(template_name, RequestContext(request, { |
|
241 "form": username_form |
|
242 })) |
|
243 |
|
244 |
|
245 def get_usernames(request): |
|
246 """Returns in json the list of ten possible usernames |
|
247 starting with the last pattern in the comma separated string |
|
248 """ |
|
249 |
|
250 get_params = request.GET |
|
251 authors_str = get_params.get('input') |
|
252 |
|
253 if not authors_str: |
|
254 return HttpResponse(json.dumps('')) |
|
255 |
|
256 authors = authors_str.split(',') |
|
257 search_author = authors[-1].strip() |
|
258 |
|
259 users = User.objects.filter( |
|
260 Q(username__istartswith=search_author) | Q( |
|
261 first_name__istartswith=search_author) | Q( |
|
262 last_name__istartswith=search_author)) |
|
263 |
|
264 results = [{'id': '', |
|
265 'info': 'plugin_header', |
|
266 'value': 'User Names' |
|
267 }] |
|
268 |
|
269 for user in users: |
|
270 results.append( |
|
271 {'id': 'author_name', |
|
272 'info': str(user.get_full_name()), |
|
273 'value': str(user.username) |
|
274 }) |
|
275 |
|
276 json_response = {'results': results} |
|
277 |
|
278 return HttpResponse(json.dumps(json_response)) |
|