def setUp(self): # Clear cache between tests cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) cache.clear() # User that has agreed to TOS self.user1 = get_runtime_user_model().objects.create_user( 'user1', '*****@*****.**', 'user1pass') # User that has not yet agreed to TOS self.user2 = get_runtime_user_model().objects.create_user( 'user2', '*****@*****.**', 'user2pass') self.user3 = get_runtime_user_model().objects.create_user( 'user3', '*****@*****.**', 'user3pass') self.tos1 = TermsOfService.objects.create( content="first edition of the terms of service", active=True) self.tos2 = TermsOfService.objects.create( content="second edition of the terms of service", active=False) self.login_url = getattr(settings, 'LOGIN_URL', '/login/') UserAgreement.objects.create(terms_of_service=self.tos1, user=self.user1) self.redirect_page = '{0}?{1}={2}'.format( reverse('tos_check_tos'), REDIRECT_FIELD_NAME, reverse('index'), )
def test_skip_for_user(self): cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) key_version = cache.get('django:tos:key_version') cache.set('django:tos:skip_tos_check:{0}'.format(str(self.user1.id)), True, version=key_version) self.client.login(username='******', password='******') response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200)
def test_invalidate_cached_agreements(self): cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) invalidate_cached_agreements(TermsOfService, {}) key_version = cache.get('django:tos:key_version') invalidate_cached_agreements(TermsOfService, {}) self.assertEqual(cache.get('django:tos:key_version'), key_version+1) invalidate_cached_agreements(TermsOfService, {}, raw=True) self.assertEqual(cache.get('django:tos:key_version'), key_version+1)
def test_invalidate_cached_agreements(self): cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) invalidate_cached_agreements(TermsOfService, {}) key_version = cache.get('django:tos:key_version') invalidate_cached_agreements(TermsOfService, {}) self.assertEqual(cache.get('django:tos:key_version'), key_version + 1) invalidate_cached_agreements(TermsOfService, {}, raw=True) self.assertEqual(cache.get('django:tos:key_version'), key_version + 1)
def check_tos( request, template_name='tos/tos_check.html', redirect_field_name=REDIRECT_FIELD_NAME, ): redirect_to = _redirect_to( request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, ''))) tos = TermsOfService.objects.get_current_tos() if request.method == "POST": if request.POST.get("accept", "") == "accept": user = get_runtime_user_model().objects.get( pk=request.session['tos_user']) user.backend = request.session['tos_backend'] # Save the user agreement to the new TOS UserAgreement.objects.create(terms_of_service=tos, user=user) # Update the cache cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) key_version = cache.get('django:tos:key_version') user_id = int(request.session['_auth_user_id']) cache.set('django:tos:agreed:{0}'.format(user_id), True, version=key_version) # Log the user in auth_login(request, user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() return HttpResponseRedirect(redirect_to) else: messages.error( request, _(u"You cannot login without agreeing to the terms of this site." )) if DJANGO_VERSION >= (1, 10, 0): return render(request, template_name, { 'tos': tos, redirect_field_name: redirect_to, }) else: return render_to_response(template_name, { 'tos': tos, redirect_field_name: redirect_to, }, RequestContext(request))
from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.forms import AuthenticationForm from django.contrib.sites.models import Site from django.http import HttpResponseRedirect from django.shortcuts import render, render_to_response from django.template import RequestContext from django.views.decorators.cache import never_cache from django.views.decorators.csrf import csrf_protect from django.views.generic import TemplateView from django.utils.translation import ugettext_lazy as _ from tos.compat import get_cache, get_runtime_user_model, get_request_site from tos.models import has_user_agreed_latest_tos, TermsOfService, UserAgreement cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) class TosView(TemplateView): template_name = "tos/tos.html" def get_context_data(self, **kwargs): context = super(TosView, self).get_context_data(**kwargs) context['tos'] = TermsOfService.objects.get_current_tos() return context def _redirect_to(redirect_to): """ Moved redirect_to logic here to avoid duplication in views""" # Light security check -- make sure redirect_to isn't garbage.