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