root / snf-astakos-app / astakos / im / views.py @ d2633501
History | View | Annotate | Download (25.3 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import logging |
35 |
import socket |
36 |
|
37 |
from smtplib import SMTPException |
38 |
from urllib import quote |
39 |
from functools import wraps |
40 |
|
41 |
from django.core.mail import send_mail |
42 |
from django.http import ( |
43 |
HttpResponse, HttpResponseBadRequest, HttpResponseRedirect |
44 |
) |
45 |
from django.shortcuts import redirect |
46 |
from django.template.loader import render_to_string |
47 |
from django.utils.translation import ugettext as _ |
48 |
from django.core.urlresolvers import reverse |
49 |
from django.contrib.auth.decorators import login_required |
50 |
from django.contrib import messages |
51 |
from django.db import transaction |
52 |
from django.utils.http import urlencode |
53 |
from django.db.utils import IntegrityError |
54 |
from django.contrib.auth.views import password_change |
55 |
from django.core.exceptions import ValidationError, PermissionDenied |
56 |
from django.views.decorators.http import require_http_methods |
57 |
|
58 |
from astakos.im.models import AstakosUser, Invitation, ApprovalTerms |
59 |
from astakos.im.activation_backends import get_backend, SimpleBackend |
60 |
from astakos.im.util import ( |
61 |
get_context, prepare_response, get_query, restrict_next |
62 |
) |
63 |
from astakos.im.forms import * |
64 |
from astakos.im.functions import (send_greeting, send_feedback, SendMailError, |
65 |
invite as invite_func, logout as auth_logout, activate as activate_func, |
66 |
send_activation as send_activation_func
|
67 |
) |
68 |
from astakos.im.settings import ( |
69 |
DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, COOKIE_DOMAIN, IM_MODULES, |
70 |
SITENAME, LOGOUT_NEXT, LOGGING_LEVEL |
71 |
) |
72 |
from astakos.im import settings |
73 |
from astakos.im import auth_providers |
74 |
|
75 |
logger = logging.getLogger(__name__) |
76 |
|
77 |
def render_response(template, tab=None, status=200, context_instance=None, **kwargs): |
78 |
"""
|
79 |
Calls ``django.template.loader.render_to_string`` with an additional ``tab``
|
80 |
keyword argument and returns an ``django.http.HttpResponse`` with the
|
81 |
specified ``status``.
|
82 |
"""
|
83 |
if tab is None: |
84 |
tab = template.partition('_')[0].partition('.html')[0] |
85 |
kwargs.setdefault('tab', tab)
|
86 |
html = render_to_string(template, kwargs, context_instance=context_instance) |
87 |
response = HttpResponse(html, status=status) |
88 |
return response
|
89 |
|
90 |
def requires_auth_provider(provider_id, **perms): |
91 |
"""
|
92 |
"""
|
93 |
def decorator(func, *args, **kwargs): |
94 |
@wraps(func)
|
95 |
def wrapper(request, *args, **kwargs): |
96 |
provider = auth_providers.get_provider(provider_id) |
97 |
|
98 |
if not provider or not provider.is_active(): |
99 |
raise PermissionDenied
|
100 |
|
101 |
if provider:
|
102 |
for pkey, value in perms.iteritems(): |
103 |
attr = 'is_available_for_%s' % pkey.lower()
|
104 |
if getattr(provider, attr)() != value: |
105 |
raise PermissionDenied
|
106 |
return func(request, *args)
|
107 |
return wrapper
|
108 |
return decorator
|
109 |
|
110 |
|
111 |
def requires_anonymous(func): |
112 |
"""
|
113 |
Decorator checkes whether the request.user is not Anonymous and in that case
|
114 |
redirects to `logout`.
|
115 |
"""
|
116 |
@wraps(func)
|
117 |
def wrapper(request, *args): |
118 |
if not request.user.is_anonymous(): |
119 |
next = urlencode({'next': request.build_absolute_uri()})
|
120 |
logout_uri = reverse(logout) + '?' + next |
121 |
return HttpResponseRedirect(logout_uri)
|
122 |
return func(request, *args)
|
123 |
return wrapper
|
124 |
|
125 |
def signed_terms_required(func): |
126 |
"""
|
127 |
Decorator checkes whether the request.user is Anonymous and in that case
|
128 |
redirects to `logout`.
|
129 |
"""
|
130 |
@wraps(func)
|
131 |
def wrapper(request, *args, **kwargs): |
132 |
if request.user.is_authenticated() and not request.user.signed_terms(): |
133 |
params = urlencode({'next': request.build_absolute_uri(),
|
134 |
'show_form':''}) |
135 |
terms_uri = reverse('latest_terms') + '?' + params |
136 |
return HttpResponseRedirect(terms_uri)
|
137 |
return func(request, *args, **kwargs)
|
138 |
return wrapper
|
139 |
|
140 |
@require_http_methods(["GET", "POST"]) |
141 |
@signed_terms_required
|
142 |
def index(request, login_template_name='im/login.html', profile_template_name='im/profile.html', extra_context=None): |
143 |
"""
|
144 |
If there is logged on user renders the profile page otherwise renders login page.
|
145 |
|
146 |
**Arguments**
|
147 |
|
148 |
``login_template_name``
|
149 |
A custom login template to use. This is optional; if not specified,
|
150 |
this will default to ``im/login.html``.
|
151 |
|
152 |
``profile_template_name``
|
153 |
A custom profile template to use. This is optional; if not specified,
|
154 |
this will default to ``im/profile.html``.
|
155 |
|
156 |
``extra_context``
|
157 |
An dictionary of variables to add to the template context.
|
158 |
|
159 |
**Template:**
|
160 |
|
161 |
im/profile.html or im/login.html or ``template_name`` keyword argument.
|
162 |
|
163 |
"""
|
164 |
extra_context = extra_context or {}
|
165 |
template_name = login_template_name |
166 |
if request.user.is_authenticated():
|
167 |
return HttpResponseRedirect(reverse('astakos.im.views.edit_profile')) |
168 |
|
169 |
return render_response(
|
170 |
template_name, |
171 |
login_form = LoginForm(request=request), |
172 |
context_instance = get_context(request, extra_context) |
173 |
) |
174 |
|
175 |
@require_http_methods(["GET", "POST"]) |
176 |
@login_required
|
177 |
@signed_terms_required
|
178 |
@transaction.commit_manually
|
179 |
def invite(request, template_name='im/invitations.html', extra_context=None): |
180 |
"""
|
181 |
Allows a user to invite somebody else.
|
182 |
|
183 |
In case of GET request renders a form for providing the invitee information.
|
184 |
In case of POST checks whether the user has not run out of invitations and then
|
185 |
sends an invitation email to singup to the service.
|
186 |
|
187 |
The view uses commit_manually decorator in order to ensure the number of the
|
188 |
user invitations is going to be updated only if the email has been successfully sent.
|
189 |
|
190 |
If the user isn't logged in, redirects to settings.LOGIN_URL.
|
191 |
|
192 |
**Arguments**
|
193 |
|
194 |
``template_name``
|
195 |
A custom template to use. This is optional; if not specified,
|
196 |
this will default to ``im/invitations.html``.
|
197 |
|
198 |
``extra_context``
|
199 |
An dictionary of variables to add to the template context.
|
200 |
|
201 |
**Template:**
|
202 |
|
203 |
im/invitations.html or ``template_name`` keyword argument.
|
204 |
|
205 |
**Settings:**
|
206 |
|
207 |
The view expectes the following settings are defined:
|
208 |
|
209 |
* LOGIN_URL: login uri
|
210 |
* ASTAKOS_DEFAULT_CONTACT_EMAIL: service support email
|
211 |
* ASTAKOS_DEFAULT_FROM_EMAIL: from email
|
212 |
"""
|
213 |
extra_context = extra_context or {}
|
214 |
status = None
|
215 |
message = None
|
216 |
form = InvitationForm() |
217 |
|
218 |
inviter = request.user |
219 |
if request.method == 'POST': |
220 |
form = InvitationForm(request.POST) |
221 |
if inviter.invitations > 0: |
222 |
if form.is_valid():
|
223 |
try:
|
224 |
invitation = form.save() |
225 |
invite_func(invitation, inviter) |
226 |
status = messages.SUCCESS |
227 |
message = _('Invitation sent to %s' % invitation.username)
|
228 |
except SendMailError, e:
|
229 |
status = messages.ERROR |
230 |
message = e.message |
231 |
transaction.rollback() |
232 |
except BaseException, e: |
233 |
status = messages.ERROR |
234 |
message = _('Something went wrong.')
|
235 |
logger.exception(e) |
236 |
transaction.rollback() |
237 |
else:
|
238 |
transaction.commit() |
239 |
else:
|
240 |
status = messages.ERROR |
241 |
message = _('No invitations left')
|
242 |
messages.add_message(request, status, message) |
243 |
|
244 |
sent = [{'email': inv.username,
|
245 |
'realname': inv.realname,
|
246 |
'is_consumed': inv.is_consumed}
|
247 |
for inv in request.user.invitations_sent.all()] |
248 |
kwargs = {'inviter': inviter,
|
249 |
'sent':sent}
|
250 |
context = get_context(request, extra_context, **kwargs) |
251 |
return render_response(template_name,
|
252 |
invitation_form = form, |
253 |
context_instance = context) |
254 |
|
255 |
@require_http_methods(["GET", "POST"]) |
256 |
@login_required
|
257 |
@signed_terms_required
|
258 |
def edit_profile(request, template_name='im/profile.html', extra_context=None): |
259 |
"""
|
260 |
Allows a user to edit his/her profile.
|
261 |
|
262 |
In case of GET request renders a form for displaying the user information.
|
263 |
In case of POST updates the user informantion and redirects to ``next``
|
264 |
url parameter if exists.
|
265 |
|
266 |
If the user isn't logged in, redirects to settings.LOGIN_URL.
|
267 |
|
268 |
**Arguments**
|
269 |
|
270 |
``template_name``
|
271 |
A custom template to use. This is optional; if not specified,
|
272 |
this will default to ``im/profile.html``.
|
273 |
|
274 |
``extra_context``
|
275 |
An dictionary of variables to add to the template context.
|
276 |
|
277 |
**Template:**
|
278 |
|
279 |
im/profile.html or ``template_name`` keyword argument.
|
280 |
|
281 |
**Settings:**
|
282 |
|
283 |
The view expectes the following settings are defined:
|
284 |
|
285 |
* LOGIN_URL: login uri
|
286 |
"""
|
287 |
extra_context = extra_context or {}
|
288 |
form = ProfileForm( |
289 |
instance=request.user, |
290 |
session_key=request.session.session_key |
291 |
) |
292 |
extra_context['next'] = request.GET.get('next') |
293 |
if request.method == 'POST': |
294 |
form = ProfileForm( |
295 |
request.POST, |
296 |
instance=request.user, |
297 |
session_key=request.session.session_key |
298 |
) |
299 |
if form.is_valid():
|
300 |
try:
|
301 |
prev_token = request.user.auth_token |
302 |
user = form.save() |
303 |
form = ProfileForm( |
304 |
instance=user, |
305 |
session_key=request.session.session_key |
306 |
) |
307 |
next = restrict_next( |
308 |
request.POST.get('next'),
|
309 |
domain=COOKIE_DOMAIN |
310 |
) |
311 |
if next: |
312 |
return redirect(next) |
313 |
msg = _('<p>Profile has been updated successfully</p>')
|
314 |
messages.add_message(request, messages.SUCCESS, msg) |
315 |
except ValueError, ve: |
316 |
messages.add_message(request, messages.ERROR, ve) |
317 |
elif request.method == "GET": |
318 |
request.user.is_verified = True
|
319 |
request.user.save() |
320 |
|
321 |
# existing providers
|
322 |
user_providers = request.user.get_active_auth_providers() |
323 |
|
324 |
# providers that user can add
|
325 |
user_available_providers = request.user.get_available_auth_providers() |
326 |
|
327 |
return render_response(template_name,
|
328 |
profile_form = form, |
329 |
user_providers = user_providers, |
330 |
user_available_providers = user_available_providers, |
331 |
context_instance = get_context(request, |
332 |
extra_context)) |
333 |
|
334 |
@require_http_methods(["GET", "POST"]) |
335 |
def signup(request, template_name='im/signup.html', on_success='im/signup_complete.html', extra_context=None, backend=None): |
336 |
"""
|
337 |
Allows a user to create a local account.
|
338 |
|
339 |
In case of GET request renders a form for entering the user information.
|
340 |
In case of POST handles the signup.
|
341 |
|
342 |
The user activation will be delegated to the backend specified by the ``backend`` keyword argument
|
343 |
if present, otherwise to the ``astakos.im.activation_backends.InvitationBackend``
|
344 |
if settings.ASTAKOS_INVITATIONS_ENABLED is True or ``astakos.im.activation_backends.SimpleBackend`` if not
|
345 |
(see activation_backends);
|
346 |
|
347 |
Upon successful user creation, if ``next`` url parameter is present the user is redirected there
|
348 |
otherwise renders the same page with a success message.
|
349 |
|
350 |
On unsuccessful creation, renders ``template_name`` with an error message.
|
351 |
|
352 |
**Arguments**
|
353 |
|
354 |
``template_name``
|
355 |
A custom template to render. This is optional;
|
356 |
if not specified, this will default to ``im/signup.html``.
|
357 |
|
358 |
``on_success``
|
359 |
A custom template to render in case of success. This is optional;
|
360 |
if not specified, this will default to ``im/signup_complete.html``.
|
361 |
|
362 |
``extra_context``
|
363 |
An dictionary of variables to add to the template context.
|
364 |
|
365 |
**Template:**
|
366 |
|
367 |
im/signup.html or ``template_name`` keyword argument.
|
368 |
im/signup_complete.html or ``on_success`` keyword argument.
|
369 |
"""
|
370 |
extra_context = extra_context or {}
|
371 |
if request.user.is_authenticated():
|
372 |
return HttpResponseRedirect(reverse('astakos.im.views.edit_profile')) |
373 |
|
374 |
provider = get_query(request).get('provider', 'local') |
375 |
if not auth_providers.get_provider(provider).is_available_for_create(): |
376 |
raise PermissionDenied
|
377 |
|
378 |
id = get_query(request).get('id')
|
379 |
try:
|
380 |
instance = AstakosUser.objects.get(id=id) if id else None |
381 |
except AstakosUser.DoesNotExist:
|
382 |
instance = None
|
383 |
|
384 |
try:
|
385 |
if not backend: |
386 |
backend = get_backend(request) |
387 |
form = backend.get_signup_form(provider, instance) |
388 |
except Exception, e: |
389 |
form = SimpleBackend(request).get_signup_form(provider) |
390 |
messages.add_message(request, messages.ERROR, e) |
391 |
if request.method == 'POST': |
392 |
if form.is_valid():
|
393 |
user = form.save(commit=False)
|
394 |
try:
|
395 |
result = backend.handle_activation(user) |
396 |
status = messages.SUCCESS |
397 |
message = result.message |
398 |
|
399 |
form.store_user(user, request) |
400 |
|
401 |
if 'additional_email' in form.cleaned_data: |
402 |
additional_email = form.cleaned_data['additional_email']
|
403 |
if additional_email != user.email:
|
404 |
user.additionalmail_set.create(email=additional_email) |
405 |
msg = 'Additional email: %s saved for user %s.' % (
|
406 |
additional_email, |
407 |
user.email |
408 |
) |
409 |
logger._log(LOGGING_LEVEL, msg, []) |
410 |
if user and user.is_active: |
411 |
next = request.POST.get('next', '') |
412 |
return prepare_response(request, user, next=next) |
413 |
messages.add_message(request, status, message) |
414 |
return render_response(
|
415 |
on_success, |
416 |
context_instance=get_context( |
417 |
request, |
418 |
extra_context |
419 |
) |
420 |
) |
421 |
except SendMailError, e:
|
422 |
logger.exception(e) |
423 |
status = messages.ERROR |
424 |
message = e.message |
425 |
messages.add_message(request, status, message) |
426 |
except BaseException, e: |
427 |
logger.exception(e) |
428 |
status = messages.ERROR |
429 |
message = _('Something went wrong.')
|
430 |
messages.add_message(request, status, message) |
431 |
logger.exception(e) |
432 |
return render_response(template_name,
|
433 |
signup_form = form, |
434 |
provider = provider, |
435 |
context_instance=get_context(request, extra_context)) |
436 |
|
437 |
@require_http_methods(["GET", "POST"]) |
438 |
@login_required
|
439 |
@signed_terms_required
|
440 |
def feedback(request, template_name='im/feedback.html', email_template_name='im/feedback_mail.txt', extra_context=None): |
441 |
"""
|
442 |
Allows a user to send feedback.
|
443 |
|
444 |
In case of GET request renders a form for providing the feedback information.
|
445 |
In case of POST sends an email to support team.
|
446 |
|
447 |
If the user isn't logged in, redirects to settings.LOGIN_URL.
|
448 |
|
449 |
**Arguments**
|
450 |
|
451 |
``template_name``
|
452 |
A custom template to use. This is optional; if not specified,
|
453 |
this will default to ``im/feedback.html``.
|
454 |
|
455 |
``extra_context``
|
456 |
An dictionary of variables to add to the template context.
|
457 |
|
458 |
**Template:**
|
459 |
|
460 |
im/signup.html or ``template_name`` keyword argument.
|
461 |
|
462 |
**Settings:**
|
463 |
|
464 |
* LOGIN_URL: login uri
|
465 |
* ASTAKOS_DEFAULT_CONTACT_EMAIL: List of feedback recipients
|
466 |
"""
|
467 |
extra_context = extra_context or {}
|
468 |
if request.method == 'GET': |
469 |
form = FeedbackForm() |
470 |
if request.method == 'POST': |
471 |
if not request.user: |
472 |
return HttpResponse('Unauthorized', status=401) |
473 |
|
474 |
form = FeedbackForm(request.POST) |
475 |
if form.is_valid():
|
476 |
msg = form.cleaned_data['feedback_msg']
|
477 |
data = form.cleaned_data['feedback_data']
|
478 |
try:
|
479 |
send_feedback(msg, data, request.user, email_template_name) |
480 |
except SendMailError, e:
|
481 |
message = e.message |
482 |
status = messages.ERROR |
483 |
else:
|
484 |
message = _('Feedback successfully sent')
|
485 |
status = messages.SUCCESS |
486 |
messages.add_message(request, status, message) |
487 |
return render_response(template_name,
|
488 |
feedback_form = form, |
489 |
context_instance = get_context(request, extra_context)) |
490 |
|
491 |
@require_http_methods(["GET"]) |
492 |
def logout(request, template='registration/logged_out.html', extra_context=None): |
493 |
"""
|
494 |
Wraps `django.contrib.auth.logout`.
|
495 |
"""
|
496 |
extra_context = extra_context or {}
|
497 |
response = HttpResponse() |
498 |
if request.user.is_authenticated():
|
499 |
email = request.user.email |
500 |
auth_logout(request) |
501 |
next = restrict_next( |
502 |
request.GET.get('next'),
|
503 |
domain=COOKIE_DOMAIN |
504 |
) |
505 |
if next: |
506 |
response['Location'] = next |
507 |
response.status_code = 302
|
508 |
elif LOGOUT_NEXT:
|
509 |
response['Location'] = LOGOUT_NEXT
|
510 |
response.status_code = 301
|
511 |
else:
|
512 |
messages.add_message(request, messages.SUCCESS, _('<p>You have successfully logged out.</p>'))
|
513 |
context = get_context(request, extra_context) |
514 |
response.write(render_to_string(template, context_instance=context)) |
515 |
return response
|
516 |
|
517 |
@require_http_methods(["GET", "POST"]) |
518 |
@transaction.commit_manually
|
519 |
def activate(request, greeting_email_template_name='im/welcome_email.txt', helpdesk_email_template_name='im/helpdesk_notification.txt'): |
520 |
"""
|
521 |
Activates the user identified by the ``auth`` request parameter, sends a welcome email
|
522 |
and renews the user token.
|
523 |
|
524 |
The view uses commit_manually decorator in order to ensure the user state will be updated
|
525 |
only if the email will be send successfully.
|
526 |
"""
|
527 |
token = request.GET.get('auth')
|
528 |
next = request.GET.get('next')
|
529 |
try:
|
530 |
user = AstakosUser.objects.get(auth_token=token) |
531 |
except AstakosUser.DoesNotExist:
|
532 |
return HttpResponseBadRequest(_('No such user')) |
533 |
|
534 |
if user.is_active:
|
535 |
message = _('Account already active.')
|
536 |
messages.add_message(request, messages.ERROR, message) |
537 |
return index(request)
|
538 |
|
539 |
try:
|
540 |
activate_func(user, greeting_email_template_name, helpdesk_email_template_name, verify_email=True)
|
541 |
response = prepare_response(request, user, next, renew=True) |
542 |
transaction.commit() |
543 |
return response
|
544 |
except SendMailError, e:
|
545 |
message = e.message |
546 |
messages.add_message(request, messages.ERROR, message) |
547 |
transaction.rollback() |
548 |
return index(request)
|
549 |
except BaseException, e: |
550 |
status = messages.ERROR |
551 |
message = _('Something went wrong.')
|
552 |
messages.add_message(request, messages.ERROR, message) |
553 |
logger.exception(e) |
554 |
transaction.rollback() |
555 |
return index(request)
|
556 |
|
557 |
@require_http_methods(["GET", "POST"]) |
558 |
def approval_terms(request, term_id=None, template_name='im/approval_terms.html', extra_context=None): |
559 |
extra_context = extra_context or {}
|
560 |
term = None
|
561 |
terms = None
|
562 |
if not term_id: |
563 |
try:
|
564 |
term = ApprovalTerms.objects.order_by('-id')[0] |
565 |
except IndexError: |
566 |
pass
|
567 |
else:
|
568 |
try:
|
569 |
term = ApprovalTerms.objects.get(id=term_id) |
570 |
except ApprovalTermDoesNotExist, e:
|
571 |
pass
|
572 |
|
573 |
if not term: |
574 |
return HttpResponseRedirect(reverse('astakos.im.views.index')) |
575 |
f = open(term.location, 'r') |
576 |
terms = f.read() |
577 |
|
578 |
if request.method == 'POST': |
579 |
next = restrict_next( |
580 |
request.POST.get('next'),
|
581 |
domain=COOKIE_DOMAIN |
582 |
) |
583 |
if not next: |
584 |
next = reverse('astakos.im.views.index')
|
585 |
form = SignApprovalTermsForm(request.POST, instance=request.user) |
586 |
if not form.is_valid(): |
587 |
return render_response(template_name,
|
588 |
terms = terms, |
589 |
approval_terms_form = form, |
590 |
context_instance = get_context(request, extra_context)) |
591 |
user = form.save() |
592 |
return HttpResponseRedirect(next) |
593 |
else:
|
594 |
form = None
|
595 |
if request.user.is_authenticated() and not request.user.signed_terms(): |
596 |
form = SignApprovalTermsForm(instance=request.user) |
597 |
return render_response(template_name,
|
598 |
terms = terms, |
599 |
approval_terms_form = form, |
600 |
context_instance = get_context(request, extra_context)) |
601 |
|
602 |
@require_http_methods(["GET", "POST"]) |
603 |
@login_required
|
604 |
@signed_terms_required
|
605 |
@transaction.commit_manually
|
606 |
def change_email(request, activation_key=None, |
607 |
email_template_name='registration/email_change_email.txt',
|
608 |
form_template_name='registration/email_change_form.html',
|
609 |
confirm_template_name='registration/email_change_done.html',
|
610 |
extra_context=None):
|
611 |
extra_context = extra_context or {}
|
612 |
if activation_key:
|
613 |
try:
|
614 |
user = EmailChange.objects.change_email(activation_key) |
615 |
if request.user.is_authenticated() and request.user == user: |
616 |
msg = _('Email changed successfully.')
|
617 |
messages.add_message(request, messages.SUCCESS, msg) |
618 |
auth_logout(request) |
619 |
response = prepare_response(request, user) |
620 |
transaction.commit() |
621 |
return response
|
622 |
except ValueError, e: |
623 |
messages.add_message(request, messages.ERROR, e) |
624 |
return render_response(confirm_template_name,
|
625 |
modified_user = user if 'user' in locals() else None, |
626 |
context_instance = get_context(request, |
627 |
extra_context)) |
628 |
|
629 |
if not request.user.is_authenticated(): |
630 |
path = quote(request.get_full_path()) |
631 |
url = request.build_absolute_uri(reverse('astakos.im.views.index'))
|
632 |
return HttpResponseRedirect(url + '?next=' + path) |
633 |
form = EmailChangeForm(request.POST or None) |
634 |
if request.method == 'POST' and form.is_valid(): |
635 |
try:
|
636 |
ec = form.save(email_template_name, request) |
637 |
except SendMailError, e:
|
638 |
status = messages.ERROR |
639 |
msg = e |
640 |
transaction.rollback() |
641 |
except IntegrityError, e:
|
642 |
status = messages.ERROR |
643 |
msg = _('There is already a pending change email request.')
|
644 |
else:
|
645 |
status = messages.SUCCESS |
646 |
msg = _('Change email request has been registered succefully.\
|
647 |
You are going to receive a verification email in the new address.')
|
648 |
transaction.commit() |
649 |
messages.add_message(request, status, msg) |
650 |
return render_response(form_template_name,
|
651 |
form = form,) |
652 |
|
653 |
|
654 |
def send_activation(request, user_id, template_name='im/login.html', extra_context=None): |
655 |
|
656 |
if settings.MODERATION_ENABLED:
|
657 |
raise PermissionDenied
|
658 |
|
659 |
extra_context = extra_context or {}
|
660 |
try:
|
661 |
u = AstakosUser.objects.get(id=user_id) |
662 |
except AstakosUser.DoesNotExist:
|
663 |
messages.error(request, _('Invalid user id'))
|
664 |
else:
|
665 |
try:
|
666 |
send_activation_func(u) |
667 |
msg = _('Activation sent.')
|
668 |
messages.success(request, msg) |
669 |
except SendMailError, e:
|
670 |
messages.error(request, e) |
671 |
return render_response(
|
672 |
template_name, |
673 |
login_form = LoginForm(request=request), |
674 |
context_instance = get_context( |
675 |
request, |
676 |
extra_context |
677 |
) |
678 |
) |
679 |
|
680 |
@require_http_methods(["GET", "POST"]) |
681 |
@login_required
|
682 |
@signed_terms_required
|
683 |
def remove_auth_provider(request, pk): |
684 |
provider = request.user.auth_providers.get(pk=pk) |
685 |
print provider
|
686 |
if provider.can_remove():
|
687 |
provider.delete() |
688 |
return HttpResponseRedirect(reverse('edit_profile')) |
689 |
else:
|
690 |
messages.error(_('Authentication method cannot be removed'))
|
691 |
return HttpResponseRedirect(reverse('edit_profile')) |
692 |
|