def wtm_instant_tags(context, location=None): context["tags"] = [] request = context.get("request", None) found = False if location is not None: location_choices = [ location_choice[0][2:] for location_choice in Tag.LOCATION_CHOICES ] if location in location_choices: found = True if not found: raise KeyError( "'{}' is not an allowed location. Select one of {}".format( location, ", ".join(location_choices))) if request is not None: for tag in TagStrategy(request).result: obj = tag.get("object") element = tag.get("element") if (location is not None and obj.tag_location[2:] == location or location is None): context["tags"].append(element.decode()) return context
def _add_instant_tags(self, request, response): if hasattr(response, "content") and getattr(settings, "WTM_INJECT_TAGS", True): strategy = TagStrategy(request) content = response.content.decode(response.charset) doc = BeautifulSoup(content, "html.parser") head = getattr(doc, "head", []) body = getattr(doc, "body", []) for tag in strategy.result: obj = tag.get("object") element = tag.get("element") if head and obj.tag_location == Tag.TOP_HEAD: head.insert(1, element) elif head and obj.tag_location == Tag.BOTTOM_HEAD: head.append(element) elif body and obj.tag_location == Tag.TOP_BODY: body.insert(1, element) elif body and obj.tag_location == Tag.BOTTOM_BODY: body.append(element) doc.head = head doc.body = body response.content = doc.encode(formatter=None) return response return response
def render(self, context): request = context.get("request", None) if request: tag_config = TagTypeSettings().get(self.tag_type) if TagStrategy(request=request).should_include( self.tag_type, tag_config): ctx = Tag.create_context(request=request, context=context) if self.src: if self.src.endswith(".html"): return render_to_string(self.src, ctx) elif self.src.endswith(".css"): tag = BeautifulSoup("", "html.parser").new_tag("link") tag["rel"] = "stylesheet" tag["type"] = "text/css" tag["href"] = static(self.src) elif self.src.endswith(".js"): tag = BeautifulSoup("", "html.parser").new_tag("script") tag["type"] = "text/javascript" tag["src"] = static(self.src) return mark_safe(tag.decode()) output = self.nodelist.render(ctx) return output return ""
def wtm_instant_tags(context): context["tags"] = [] request = context.get("request", None) if request is not None: strategy = TagStrategy(request) context["tags"] = [tag.get("element").decode() for tag in strategy.result] return context
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 __call__(self, request): self.request = request self.response = self.get_response(request) self.strategy = TagStrategy(request) if (getattr(self.request, "method", None) == "GET" and getattr(self.response, "status_code", None) == 200 and isinstance(self.response, TemplateResponse)): self._add_instant_tags() self._add_lazy_manager() return self.response
def process_template_response(self, request, response): if (getattr(request, "method", None) == "GET" and getattr(response, "status_code", None) == 200 and not getattr(response, "streaming", False)): strategy = TagStrategy(request) set_consent( request, response, {key: value for key, value in strategy.consent.items()}, ) return response
def __call__(self, request): response = self.get_response(request) strategy = TagStrategy(request) if (getattr(request, "method", None) == "GET" and getattr(response, "status_code", None) == 200): set_consent( request, response, {key: value for key, value in strategy.consent.items()}, ) return response
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_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 __call__(self, request): self.request = request self.response = self.get_response(request) if (not hasattr(self.request, "wtm_injected") # Only once per request and self.request.method == "GET" and self.response.status_code == 200 and isinstance(self.response, TemplateResponse)): self.strategy = TagStrategy(request) for cookie_name, value in self.strategy.cookies.items(): set_cookie(self.response, cookie_name, value) self._add_instant_tags() self._add_lazy_manager() self.request.wtm_injected = True return self.response
def __call__(self, request): self.request = request self.response = self.get_response(request) self.strategy = TagStrategy(request) if ( getattr(self.request, "method", None) == "GET" and getattr(self.response, "status_code", None) == 200 ): set_consent( self.response, {key: value for key, value in self.strategy.consent.items()}, ) if isinstance(self.response, TemplateResponse): self._add_instant_tags() self._add_lazy_manager() return self.response
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 lazy_endpoint(request): data = {"tags": []} response = JsonResponse(data) if request.method == "POST" and request.body: try: payload = json.loads(request.body) except json.JSONDecodeError: return HttpResponseBadRequest() consent = payload.get("consent", None) request.path = payload.get("pathname", request.path) request.META["QUERY_STRING"] = payload.get("search", "") strategy = TagStrategy(request, consent) for cookie_name, value in strategy.cookies.items(): set_cookie(response, cookie_name, value) for tag in strategy.result: element = tag.get("element") for content in element.contents: if content.name: data["tags"].append({ "name": content.name, "attributes": getattr(content, "attrs", {}), "string": content.string, }) response.content = json.dumps(data) return response return HttpResponseBadRequest()
def wtm_manage_form(context): request = context.get("request") if request: return {"form": ConsentForm(initial=TagStrategy(request).cookie_state)} return ""
def consent_state(request): strategy = TagStrategy(request=request) return {"wtm_consent_state": getattr(strategy, "cookie_state", {})}