Move 'mark view restriction as passed' logic to the BaseViewRestriction model

pull/3644/merge
Matt Westcott 2017-06-07 23:48:06 +01:00
rodzic 049b15f3fe
commit 60f40f10ee
2 zmienionych plików z 16 dodań i 10 usunięć

Wyświetl plik

@ -1908,6 +1908,21 @@ class BaseViewRestriction(models.Model):
return True
def mark_as_passed(self, request):
"""
Update the session data in the request to mark the user as having passed this
view restriction
"""
has_existing_session = (settings.SESSION_COOKIE_NAME in request.COOKIES)
passed_restrictions = request.session.setdefault(self.passed_view_restrictions_session_key, [])
if self.id not in passed_restrictions:
passed_restrictions.append(self.id)
request.session[self.passed_view_restrictions_session_key] = passed_restrictions
if not has_existing_session:
# if this is a session we've created, set it to expire at the end
# of the browser session
request.session.set_expiry(0)
class Meta:
abstract = True
verbose_name = _('view restriction')

Wyświetl plik

@ -1,6 +1,5 @@
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
@ -38,15 +37,7 @@ def authenticate_with_password(request, page_view_restriction_id, page_id):
if request.method == 'POST':
form = PasswordPageViewRestrictionForm(request.POST, instance=restriction)
if form.is_valid():
has_existing_session = (settings.SESSION_COOKIE_NAME in request.COOKIES)
passed_restrictions = request.session.setdefault('passed_page_view_restrictions', [])
if restriction.id not in passed_restrictions:
passed_restrictions.append(restriction.id)
request.session['passed_page_view_restrictions'] = passed_restrictions
if not has_existing_session:
# if this is a session we've created, set it to expire at the end
# of the browser session
request.session.set_expiry(0)
restriction.mark_as_passed(request)
return redirect(form.cleaned_data['return_url'])
else: