1 # -*- coding: utf-8 -*- |
|
2 from __future__ import absolute_import |
|
3 |
|
4 #python |
|
5 from urlparse import urlparse |
1 from urlparse import urlparse |
|
2 |
6 import simplejson as json |
3 import simplejson as json |
7 import urllib |
4 import urllib |
8 import os |
5 import os |
9 |
6 |
10 #django |
|
11 from django.conf import settings |
7 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 |
8 from django.contrib.auth.decorators import login_required |
21 from django.contrib.auth.forms import AuthenticationForm |
9 from django.contrib.auth.forms import AuthenticationForm |
22 from django.contrib.auth.forms import PasswordChangeForm |
10 from django.contrib.auth.forms import PasswordChangeForm |
23 from django.contrib.auth.models import User |
11 from django.contrib.auth.models import User |
24 from django.core.exceptions import ObjectDoesNotExist |
12 from django.core.exceptions import ObjectDoesNotExist |
25 |
13 from django.core.urlresolvers import reverse |
26 #PIL |
14 from django.db.models import Q |
|
15 from django.db.models.signals import post_save |
|
16 from django.http import HttpResponse |
|
17 from django.shortcuts import render_to_response |
|
18 from django.template import RequestContext |
|
19 |
27 from PIL import Image |
20 from PIL import Image |
28 |
21 |
29 #scipycon |
|
30 from project.scipycon.utils import set_message_cookie |
|
31 from project.scipycon.talk.models import Talk |
|
32 from project.scipycon.registration.models import Registration |
22 from project.scipycon.registration.models import Registration |
33 from project.scipycon.registration.models import Wifi |
23 from project.scipycon.registration.models import Wifi |
34 from project.scipycon.registration.forms import WifiForm |
24 from project.scipycon.registration.forms import WifiForm |
35 |
25 from project.scipycon.talk.models import Talk |
36 from .utils import scipycon_createuser |
26 from project.scipycon.utils import set_message_cookie |
37 from .utils import handle_uploaded_photo |
27 |
38 from .forms import RegisterForm |
28 from project.scipycon.user.forms import EditProfileForm |
39 from .forms import EditProfileForm |
29 from project.scipycon.user.utils import handle_uploaded_photo |
40 from .forms import UsernameForm |
30 from project.scipycon.user.forms import RegisterForm |
41 |
31 from project.scipycon.user.utils import scipycon_createuser |
42 @login_required |
32 from project.scipycon.user.forms import UsernameForm |
43 def account(request, template_name="user/account.html"): |
33 |
|
34 |
|
35 @login_required |
|
36 def account(request, scope, template_name="user/account.html"): |
44 """Displays the main screen of the current user's account. |
37 """Displays the main screen of the current user's account. |
45 """ |
38 """ |
|
39 |
46 user = request.user |
40 user = request.user |
47 profile = user.get_profile() |
41 profile = user.get_profile() |
48 |
42 |
49 talks = Talk.objects.filter(speaker=user) |
43 talks = Talk.objects.filter(speaker=user) |
50 try: |
44 try: |
75 wifi_form = None |
69 wifi_form = None |
76 else: |
70 else: |
77 wifi_form = WifiForm() |
71 wifi_form = WifiForm() |
78 |
72 |
79 return render_to_response(template_name, RequestContext(request, { |
73 return render_to_response(template_name, RequestContext(request, { |
80 "form" : wifi_form, "comment": wifi_comment, |
74 'params': {'scope': scope}, |
81 "user" : user, "profile" : profile, "photo" : photo, |
75 'form' : wifi_form, 'comment': wifi_comment, |
82 "talks" : talks, "registration" : registration, |
76 'user' : user, 'profile' : profile, 'photo' : photo, |
83 })) |
77 'talks' : talks, 'registration' : registration, |
84 |
78 })) |
85 @login_required |
79 |
86 def edit_profile(request, template_name="user/editprofile.html"): |
80 @login_required |
|
81 def edit_profile(request, scope, template_name="user/editprofile.html"): |
87 """Allows user to edit profile |
82 """Allows user to edit profile |
88 """ |
83 """ |
89 user = request.user |
84 user = request.user |
90 profile = user.get_profile() |
85 profile = user.get_profile() |
91 |
86 |
125 |
120 |
126 return render_to_response(template_name, RequestContext(request, { |
121 return render_to_response(template_name, RequestContext(request, { |
127 "form": form |
122 "form": form |
128 })) |
123 })) |
129 |
124 |
130 def login(request, template_name="user/login.html"): |
125 def login(request, scope, template_name="user/login.html"): |
131 """Custom view to login or register/login a user. |
126 """Custom view to login or register/login a user. |
132 Integration of register and login form |
127 Integration of register and login form |
133 It uses Django's standard AuthenticationForm, though. |
128 It uses Django's standard AuthenticationForm, though. |
134 """ |
129 """ |
135 user = request.user |
130 user = request.user |
136 if user.is_authenticated(): |
131 if user.is_authenticated(): |
137 redirect_to = reverse("scipycon_account") |
132 redirect_to = reverse("scipycon_account", kwargs={'scope': scope}) |
138 return set_message_cookie(redirect_to, |
133 return set_message_cookie(redirect_to, |
139 msg = u"Redirected to account from login form.") |
134 msg = u"Redirected to account from login form.") |
140 |
135 |
141 # Using Djangos default AuthenticationForm |
136 # Using Djangos default AuthenticationForm |
142 login_form = AuthenticationForm() |
137 login_form = AuthenticationForm() |
158 |
153 |
159 elif request.POST.get("action") == "register": |
154 elif request.POST.get("action") == "register": |
160 register_form = RegisterForm(data=request.POST) |
155 register_form = RegisterForm(data=request.POST) |
161 if register_form.is_valid(): |
156 if register_form.is_valid(): |
162 |
157 |
163 user = scipycon_createuser(request, register_form.data) |
158 user = scipycon_createuser(request, register_form.data, scope) |
164 |
159 |
165 redirect_to = request.POST.get("next") |
160 redirect_to = request.POST.get("next") |
166 if not redirect_to or '//' in redirect_to or ' ' in redirect_to: |
161 if not redirect_to or '//' in redirect_to or ' ' in redirect_to: |
167 redirect_to = reverse("scipycon_account") |
162 redirect_to = reverse("scipycon_account") |
168 |
163 |
183 login_form_errors = login_form.errors["__all__"] |
178 login_form_errors = login_form.errors["__all__"] |
184 except KeyError: |
179 except KeyError: |
185 login_form_errors = None |
180 login_form_errors = None |
186 |
181 |
187 return render_to_response(template_name, RequestContext(request, { |
182 return render_to_response(template_name, RequestContext(request, { |
188 "login_form" : login_form, |
183 'params': {'scope': scope}, |
189 "login_form_errors" : login_form_errors, |
184 'login_form' : login_form, |
190 "register_form" : register_form, |
185 'login_form_errors' : login_form_errors, |
191 "next_url" : next_url, |
186 'register_form' : register_form, |
192 })) |
187 'next_url' : next_url, |
193 |
188 })) |
194 def logout(request): |
189 |
|
190 def logout(request, scope): |
195 """Custom method to logout a user. |
191 """Custom method to logout a user. |
196 |
192 |
197 The reason to use a custom logout method is just to provide a login and a |
193 The reason to use a custom logout method is just to provide a login and a |
198 logoutmethod on one place. |
194 logoutmethod on one place. |
199 """ |
195 """ |
202 |
198 |
203 redirect_to = '/' |
199 redirect_to = '/' |
204 return set_message_cookie(redirect_to, msg = u"You have been logged out.") |
200 return set_message_cookie(redirect_to, msg = u"You have been logged out.") |
205 |
201 |
206 @login_required |
202 @login_required |
207 def password(request, template_name="user/password.html"): |
203 def password(request, scope, template_name="user/password.html"): |
208 """Changes the password of current user. |
204 """Changes the password of current user. |
209 """ |
205 """ |
210 if request.method == "POST": |
206 if request.method == "POST": |
211 form = PasswordChangeForm(request.user, request.POST) |
207 form = PasswordChangeForm(request.user, request.POST) |
212 if form.is_valid(): |
208 if form.is_valid(): |
216 msg = u"Your password has been changed.") |
212 msg = u"Your password has been changed.") |
217 else: |
213 else: |
218 form = PasswordChangeForm(request.user) |
214 form = PasswordChangeForm(request.user) |
219 |
215 |
220 return render_to_response(template_name, RequestContext(request, { |
216 return render_to_response(template_name, RequestContext(request, { |
221 "form" : form |
217 'params': {'scope': scope}, |
222 })) |
218 'form' : form |
223 |
219 })) |
224 @login_required |
220 |
225 def username(request, template_name="user/username.html"): |
221 @login_required |
|
222 def username(request, scope, template_name="user/username.html"): |
226 """Saves the username from the data form. |
223 """Saves the username from the data form. |
227 """ |
224 """ |
228 if request.method == "POST": |
225 if request.method == "POST": |
229 username_form = UsernameForm(initial={"username" : request.user.username}, data=request.POST) |
226 username_form = UsernameForm(initial={"username" : request.user.username}, data=request.POST) |
230 if username_form.is_valid(): |
227 if username_form.is_valid(): |
239 return render_to_response(template_name, RequestContext(request, { |
236 return render_to_response(template_name, RequestContext(request, { |
240 "form": username_form |
237 "form": username_form |
241 })) |
238 })) |
242 |
239 |
243 |
240 |
244 def get_usernames(request): |
241 def get_usernames(request, scope): |
245 """Returns in json the list of ten possible usernames |
242 """Returns in json the list of ten possible usernames |
246 starting with the last pattern in the comma separated string |
243 starting with the last pattern in the comma separated string |
247 """ |
244 """ |
248 |
245 |
249 get_params = request.GET |
246 get_params = request.GET |