Revision 270dd48d snf-astakos-app/astakos/im/views.py
b/snf-astakos-app/astakos/im/views.py | ||
---|---|---|
51 | 51 |
from django.utils.http import urlencode |
52 | 52 |
from django.http import HttpResponseRedirect, HttpResponseBadRequest |
53 | 53 |
from django.db.utils import IntegrityError |
54 |
from django.contrib.auth.views import password_change |
|
54 | 55 |
|
55 |
from astakos.im.models import AstakosUser, Invitation |
|
56 |
from astakos.im.models import AstakosUser, Invitation, ApprovalTerms
|
|
56 | 57 |
from astakos.im.backends import get_backend |
57 |
from astakos.im.util import get_context, prepare_response, set_cookie |
|
58 |
from astakos.im.util import get_context, prepare_response, set_cookie, has_signed_terms
|
|
58 | 59 |
from astakos.im.forms import * |
59 | 60 |
from astakos.im.functions import send_greeting |
60 | 61 |
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, COOKIE_NAME, COOKIE_DOMAIN, IM_MODULES, SITENAME, BASEURL, LOGOUT_NEXT |
... | ... | |
80 | 81 |
|
81 | 82 |
def requires_anonymous(func): |
82 | 83 |
""" |
83 |
Decorator checkes whether the request.user is Anonymous and in that case |
|
84 |
Decorator checkes whether the request.user is not Anonymous and in that case
|
|
84 | 85 |
redirects to `logout`. |
85 | 86 |
""" |
86 | 87 |
@wraps(func) |
87 | 88 |
def wrapper(request, *args): |
88 | 89 |
if not request.user.is_anonymous(): |
89 | 90 |
next = urlencode({'next': request.build_absolute_uri()}) |
90 |
login_uri = reverse(logout) + '?' + next
|
|
91 |
return HttpResponseRedirect(login_uri)
|
|
91 |
logout_uri = reverse(logout) + '?' + next
|
|
92 |
return HttpResponseRedirect(logout_uri)
|
|
92 | 93 |
return func(request, *args) |
93 | 94 |
return wrapper |
94 | 95 |
|
96 |
def signed_terms_required(func): |
|
97 |
""" |
|
98 |
Decorator checkes whether the request.user is Anonymous and in that case |
|
99 |
redirects to `logout`. |
|
100 |
""" |
|
101 |
@wraps(func) |
|
102 |
def wrapper(request, *args, **kwargs): |
|
103 |
if request.user.is_authenticated() and not has_signed_terms(request.user): |
|
104 |
params = urlencode({'next': request.build_absolute_uri(), |
|
105 |
'show_form':''}) |
|
106 |
terms_uri = reverse('latest_terms') + '?' + params |
|
107 |
return HttpResponseRedirect(terms_uri) |
|
108 |
return func(request, *args, **kwargs) |
|
109 |
return wrapper |
|
110 |
|
|
111 |
@signed_terms_required |
|
95 | 112 |
def index(request, login_template_name='im/login.html', profile_template_name='im/profile.html', extra_context={}): |
96 | 113 |
""" |
97 | 114 |
If there is logged on user renders the profile page otherwise renders login page. |
... | ... | |
124 | 141 |
context_instance = get_context(request, extra_context)) |
125 | 142 |
|
126 | 143 |
@login_required |
144 |
@signed_terms_required |
|
127 | 145 |
@transaction.commit_manually |
128 | 146 |
def invite(request, template_name='im/invitations.html', extra_context={}): |
129 | 147 |
""" |
... | ... | |
197 | 215 |
context_instance = context) |
198 | 216 |
|
199 | 217 |
@login_required |
218 |
@signed_terms_required |
|
200 | 219 |
def edit_profile(request, template_name='im/profile.html', extra_context={}): |
201 | 220 |
""" |
202 | 221 |
Allows a user to edit his/her profile. |
... | ... | |
321 | 340 |
context_instance=get_context(request, extra_context)) |
322 | 341 |
|
323 | 342 |
@login_required |
343 |
@signed_terms_required |
|
324 | 344 |
def send_feedback(request, template_name='im/feedback.html', email_template_name='im/feedback_mail.txt', extra_context={}): |
325 | 345 |
""" |
326 | 346 |
Allows a user to send feedback. |
... | ... | |
426 | 446 |
messages.add_message(request, messages.ERROR, message) |
427 | 447 |
transaction.rollback() |
428 | 448 |
return signup(request, on_failure='im/signup.html') |
449 |
|
|
450 |
def approval_terms(request, term_id=None, template_name='im/approval_terms.html', extra_context={}): |
|
451 |
term = None |
|
452 |
terms = None |
|
453 |
if not term_id: |
|
454 |
try: |
|
455 |
term = ApprovalTerms.objects.order_by('-id')[0] |
|
456 |
except IndexError: |
|
457 |
pass |
|
458 |
else: |
|
459 |
try: |
|
460 |
term = ApprovalTerms.objects.get(id=term_id) |
|
461 |
except ApprovalTermDoesNotExist, e: |
|
462 |
pass |
|
463 |
|
|
464 |
if not term: |
|
465 |
return HttpResponseBadRequest(_('No approval terms found.')) |
|
466 |
f = open(term.location, 'r') |
|
467 |
terms = f.read() |
|
468 |
|
|
469 |
if request.method == 'POST': |
|
470 |
next = request.POST.get('next') |
|
471 |
if not next: |
|
472 |
return HttpResponseBadRequest(_('No next param.')) |
|
473 |
form = SignApprovalTermsForm(request.POST, instance=request.user) |
|
474 |
if not form.is_valid(): |
|
475 |
return render_response(template_name, |
|
476 |
terms = terms, |
|
477 |
form = form, |
|
478 |
context_instance = get_context(request, extra_context)) |
|
479 |
user = form.save() |
|
480 |
return HttpResponseRedirect(next) |
|
481 |
else: |
|
482 |
form = SignApprovalTermsForm(instance=request.user) if request.user.is_authenticated() else None |
|
483 |
return render_response(template_name, |
|
484 |
terms = terms, |
|
485 |
form = form, |
|
486 |
context_instance = get_context(request, extra_context)) |
|
487 |
|
|
488 |
@signed_terms_required |
|
489 |
def change_password(request): |
|
490 |
return password_change(request, post_change_redirect=reverse('astakos.im.views.edit_profile')) |
Also available in: Unified diff