def process_request(self, request): """Process each request to app to ensure terms & conditions have been accepted""" LOGGER.debug('termsandconditions.middleware') current_path = request.META['PATH_INFO'] protected_path = True for exclude_path in TERMS_EXCLUDE_URL_PREFIX_LIST: if current_path.startswith(exclude_path): protected_path = False if current_path in TERMS_EXCLUDE_URL_LIST: protected_path = False if current_path.startswith(ACCEPT_TERMS_PATH): protected_path = False if request.user.is_authenticated() and protected_path: if MULTIPLE_ACTIVE_TERMS: for term in TermsAndConditions.get_active_list(): if not TermsAndConditions.agreed_to_latest( request.user, term): return redirect_to_terms_accept(current_path, term) else: if not TermsAndConditions.agreed_to_latest(request.user): return redirect_to_terms_accept(current_path)
def test_accept(self): """Validate that accepting terms works""" LOGGER.debug('Test user1 login for accept') login_response = self.client.login(username='******', password='******') self.assertTrue(login_response) LOGGER.debug('Test /terms/accept/ get') accept_response = self.client.get('/terms/accept/', follow=True) self.assertContains(accept_response, "Accept") LOGGER.debug('Test /terms/accept/ post') chained_terms_response = self.client.post('/terms/accept/', {'terms': 2, 'returnTo': '/secure/'}, follow=True) self.assertContains(chained_terms_response, "Contributor") self.assertEquals(True, TermsAndConditions.agreed_to_latest(user=self.user1, slug='site-terms')) LOGGER.debug('Test /terms/accept/contrib-terms/1.5/ post') accept_version_response = accept_response = self.client.get('/terms/accept/contrib-terms/1.5/', follow=True) self.assertContains(accept_version_response, "Contributor Terms and Conditions 1.5") LOGGER.debug('Test /terms/accept/contrib-terms/3/ post') accept_version_post_response = self.client.post('/terms/accept/', {'terms': 3, 'returnTo': '/secure/'}, follow=True) self.assertContains(accept_version_post_response, "Secure") self.assertTrue(TermsAndConditions.agreed_to_terms(user=self.user1, terms=self.terms3))
def process_request(self, request): """Process each request to app to ensure terms & conditions have been accepted""" LOGGER.debug('termsandconditions.middleware') current_path = request.META['PATH_INFO'] protected_path = True for exclude_path in TERMS_EXCLUDE_URL_PREFIX_LIST: if current_path.startswith(exclude_path): protected_path = False if current_path in TERMS_EXCLUDE_URL_LIST: protected_path = False if current_path.startswith(ACCEPT_TERMS_PATH): protected_path = False if request.user.is_authenticated() and protected_path: if MULTIPLE_ACTIVE_TERMS: for term in TermsAndConditions.get_active_list(): if not TermsAndConditions.agreed_to_latest(request.user, term): return redirect_to_terms_accept(current_path, term) else: if not TermsAndConditions.agreed_to_latest(request.user): return redirect_to_terms_accept(current_path)
def process_request(self, request): """Process each request to app to ensure terms have been accepted""" LOGGER.debug('termsandconditions.middleware') current_path = request.META['PATH_INFO'] protected_path = is_path_protected(current_path) if request.user.is_authenticated() and protected_path: for term in TermsAndConditions.get_active_list(): if not TermsAndConditions.agreed_to_latest(request.user, term): return redirect_to_terms_accept(current_path, term) return None
def user_accept_terms(backend, user, uid, social_user=None, *args, **kwargs): """Check if the user has accepted the terms and conditions after creation.""" LOGGER.debug('user_accept_terms') if not TermsAndConditions.agreed_to_latest(user): return redirect_to_terms_accept('/') else: return {'social_user': social_user, 'user': user}
def get_terms(kwargs): """Checks URL parameters for slug and/or version to pull the right TermsAndConditions object""" slug = kwargs.get("slug", DEFAULT_TERMS_SLUG) if kwargs.get("version"): terms = TermsAndConditions.objects.get(slug=slug, version_number=kwargs.get("version")) else: terms = TermsAndConditions.get_active(slug) return terms
def user_accept_terms(backend, user, uid, social_user=None, *args, **kwargs): """Check the user has accepted the terms and conditions after creation.""" LOGGER.debug('user_accept_terms') if not TermsAndConditions.agreed_to_latest(user): complete_url = reverse('socialauth_complete', args=[backend.name]) return redirect_to_terms_accept(complete_url) else: return {'social_user': social_user, 'user': user}
def _wrapped_view(request, *args, **kwargs): if not request.user.is_authenticated() or TermsAndConditions.agreed_to_latest(request.user): return view_func(request, *args, **kwargs) currentPath = request.META['PATH_INFO'] login_url_parts = list(urlparse.urlparse(ACCEPT_TERMS_PATH)) querystring = QueryDict(login_url_parts[4], mutable=True) querystring['returnTo'] = currentPath login_url_parts[4] = querystring.urlencode(safe='/') return HttpResponseRedirect(urlparse.urlunparse(login_url_parts))
def get_terms(kwargs): """Checks URL parameters for slug and/or version to pull the right TermsAndConditions object""" slug = kwargs.get("slug", DEFAULT_TERMS_SLUG) if kwargs.get("version"): terms = TermsAndConditions.objects.get( slug=slug, version_number=kwargs.get("version")) else: terms = TermsAndConditions.get_active(slug) return terms
def _wrapped_view(request, *args, **kwargs): """Method to wrap the view passed in""" if not request.user.is_authenticated() or TermsAndConditions.agreed_to_latest(request.user): return view_func(request, *args, **kwargs) currentPath = request.path login_url_parts = list(urlparse.urlparse(ACCEPT_TERMS_PATH)) querystring = QueryDict(login_url_parts[4], mutable=True) querystring['returnTo'] = currentPath login_url_parts[4] = querystring.urlencode(safe='/') return HttpResponseRedirect(urlparse.urlunparse(login_url_parts))
def test_terms_and_conditions_models(self): """Various tests of the TermsAndConditions Module""" # Testing Direct Assignment of Acceptance UserTermsAndConditions.objects.create(user=self.user1, terms=self.terms1) UserTermsAndConditions.objects.create(user=self.user2, terms=self.terms3) self.assertEquals(1.0, self.user1.userterms.get().terms.version_number) self.assertEquals(1.5, self.user2.userterms.get().terms.version_number) self.assertEquals('user1', self.terms1.users.all()[0].username) # Testing the get_active static method of TermsAndConditions self.assertEquals(2.0, TermsAndConditions.get_active(slug='site-terms').version_number) self.assertEquals(1.5, TermsAndConditions.get_active(slug='contrib-terms').version_number) # Testing the agreed_to_latest static method of TermsAndConditions self.assertEquals(False, TermsAndConditions.agreed_to_latest(user=self.user1, slug='site-terms')) self.assertEquals(True, TermsAndConditions.agreed_to_latest(user=self.user2, slug='contrib-terms')) # Testing the unicode method of TermsAndConditions self.assertEquals('site-terms-2.00', str(TermsAndConditions.get_active(slug='site-terms'))) self.assertEquals('contrib-terms-1.50', str(TermsAndConditions.get_active(slug='contrib-terms')))
def test_get_active_list(self): """Test get list of active T&Cs""" active_list = TermsAndConditions.get_active_list() self.assertEqual(2, len(active_list))