def __call__(self, request): response = self.get_response(request) if not is_cookie_consent_enabled(request): return response cookie_dic = get_cookie_dict_from_request(request) for cookie_group in all_cookie_groups().values(): if not cookie_group.is_deletable: continue group_version = cookie_dic.get(cookie_group.varname, None) for cookie in cookie_group.cookie_set.all(): if cookie.name not in request.COOKIES: continue if not group_version: if not settings.COOKIE_CONSENT_OPT_OUT: response.delete_cookie(smart_str(cookie.name), cookie.path, cookie.domain) continue if group_version == settings.COOKIE_CONSENT_DECLINE: response.delete_cookie(smart_str(cookie.name), cookie.path, cookie.domain) if group_version < cookie.get_version( ) and not settings.COOKIE_CONSENT_OPT_OUT: response.delete_cookie(smart_str(cookie.name), cookie.path, cookie.domain) return response
def process_response(self, request, response): if not is_cookie_consent_enabled(request): return response cookie_dic = get_cookie_dict_from_request(request) for cookie_group in all_cookie_groups().values(): if not cookie_group.is_deletable: continue group_version = cookie_dic.get(cookie_group.varname, None) for cookie in cookie_group.cookie_set.all(): if cookie.name not in request.COOKIES: continue if group_version == settings.COOKIE_CONSENT_DECLINE: response.delete_cookie(smart_str(cookie.name), cookie.path, cookie.domain) if group_version < cookie.get_version(): if not settings.COOKIE_CONSENT_OPT_OUT: response.delete_cookie(smart_str(cookie.name), cookie.path, cookie.domain) return response
def js_type_for_cookie_consent(request, varname, cookie=None): """ Tag returns "x/cookie_consent" when processing javascript will create an cookie and consent does not exists yet. Example:: <script type="{% js_type_for_cookie_consent request "social" %}" data-varname="social"> alert("Social cookie accepted"); </script> """ enabled = is_cookie_consent_enabled(request) if not enabled: res = True else: value = get_cookie_value_from_request(request, varname, cookie) if value is None: res = settings.COOKIE_CONSENT_OPT_OUT else: res = value return "text/javascript" if res else "x/cookie_consent"
def cookie_consent_enabled(request): """ Filter returns if cookie consent enabled for this request. """ return is_cookie_consent_enabled(request)
def test_is_cookie_consent_enabled_callable(self): self.assertFalse(is_cookie_consent_enabled(None))
def test_is_cookie_consent_enabled(self): self.assertTrue(is_cookie_consent_enabled(None))