def post(self, request, *args, **kwargs): response = HttpResponseRedirect("/") redirect_url = request.META.get("HTTP_REFERER", request.build_absolute_uri()) args = [redirect_url] kwargs = {"require_https": request.is_secure()} allowed_hosts = self.get_success_url_allowed_hosts() if __version__.startswith("2.0"): kwargs["allowed_hosts"] = allowed_hosts else: args.append(allowed_hosts) url_is_safe = is_safe_url(*args, **kwargs) if url_is_safe: response = HttpResponseRedirect(redirect_url) form = ConsentForm(request.POST) if form.is_valid(): set_consent( request, response, {key: str(value).lower() for key, value in form.cleaned_data.items()}, explicit=True, ) return response
def post(self, request, *args, **kwargs): response = HttpResponseRedirect("/") redirect_url = request.META.get("HTTP_REFERER", request.build_absolute_uri()) url_is_safe = is_safe_url(redirect_url, settings.ALLOWED_HOSTS, require_https=request.is_secure()) if url_is_safe: response = HttpResponseRedirect(redirect_url) form = ConsentForm(request.POST) if form.is_valid(): for key, value in form.cleaned_data.items(): set_cookie(response, f"wtm_{key}", str(value).lower()) return response
def wtm_cookie_bar(context, include_form=False): request = context.get("request") cookie_state = TagStrategy(request).cookie_state return { "manage_view": getattr(settings, "WTM_MANAGE_VIEW", True), "include_form": include_form, "form": ConsentForm(initial=cookie_state), }
def test_consent_form(): form = ConsentForm() assert "functional" in form.fields assert "analytical" in form.fields assert "traceable" in form.fields assert form.fields["functional"].required is True assert form.fields["functional"].disabled is True assert form.fields["functional"].initial is True assert form.fields["analytical"].required is False assert form.fields["analytical"].disabled is False assert form.fields["analytical"].initial is True assert form.fields["traceable"].required is False assert form.fields["traceable"].disabled is False assert form.fields["traceable"].initial is False
def test_consent_form_initial(rf, site): request = rf.get(site.root_page.url) request.COOKIES = { **request.COOKIES, "wtm": "functional:true|analytical:false|traceable:true", } cookie_state = TagStrategy(request).cookie_state form = ConsentForm(initial=cookie_state) assert "functional" in form.fields assert "analytical" in form.fields assert "traceable" in form.fields assert form.fields["functional"].initial is True assert form.fields["analytical"].initial is False assert form.fields["traceable"].initial is True
def test_consent_form(): form = ConsentForm() assert "necessary" in form.fields assert "preferences" in form.fields assert "marketing" in form.fields assert form.fields["necessary"].required is True assert form.fields["necessary"].disabled is True assert form.fields["necessary"].initial is True assert form.fields["preferences"].required is False assert form.fields["preferences"].disabled is False assert form.fields["preferences"].initial is True assert form.fields["marketing"].required is False assert form.fields["marketing"].disabled is False assert form.fields["marketing"].initial is False
def test_consent_form_initial(rf, site): request = rf.get(site.root_page.url) request.COOKIES = { **request.COOKIES, "wtm": "necessary:true|preferences:false|marketing:true", } cookie_state = TagStrategy(request).cookie_state form = ConsentForm(initial=cookie_state) assert "necessary" in form.fields assert "preferences" in form.fields assert "marketing" in form.fields assert form.fields["necessary"].initial is True assert form.fields["preferences"].initial is False assert form.fields["marketing"].initial is True
def wtm_cookie_bar(context): request = context.get("request", None) if request: cookie_bar_settings = {} if getattr(request, "site", None): cookie_bar_settings = CookieBarSettings.for_site(request.site) manage_view = getattr(settings, "WTM_MANAGE_VIEW", True) if manage_view and hasattr(request, "resolver_match"): manage_view = (getattr(request.resolver_match, "view_name", "") != "wtm:manage") return { "manage_view": manage_view, "form": ConsentForm(initial=TagStrategy(request).cookie_state), "settings": cookie_bar_settings, "declarations": CookieDeclaration.objects.all().sorted(), } return ""
def wtm_manage_form(context): request = context.get("request") if request: return {"form": ConsentForm(initial=TagStrategy(request).cookie_state)} return ""