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_cookie(self): return { "name": "wtm", "value": "|".join([f"{tag_type}:true" for tag_type in Tag.get_types()]), "path": "", "secure": False, }
def test_tag_lazy_necessary(): produced_tag = tag_lazy_necessary() tag = Tag(name="necessary lazy", content='<script>console.log("necessary lazy")</script>') assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_tag_create(): produced_tag = TagFactory() tag = Tag( name="functional instant", content='<script>console.log("functional instant")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_tag_lazy_functional(): produced_tag = tag_lazy_functional() tag = Tag( name="functional lazy", content='<script>console.log("functional lazy")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_tag_instant_traceable(): produced_tag = tag_instant_traceable() tag = Tag( name="traceable instant", tag_type="traceable", content='<script>console.log("traceable instant")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_tag_lazy_preferences(): produced_tag = tag_lazy_preferences() tag = Tag( name="preferences lazy", tag_type="preferences", content='<script>console.log("preferences lazy")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_tag_instant_analytical(): produced_tag = tag_instant_analytical() tag = Tag( name="analytical instant", tag_type="analytical", content='<script>console.log("analytical instant")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_tag_instant_marketing(): produced_tag = tag_instant_marketing() tag = Tag( name="marketing instant", tag_type="marketing", content='<script>console.log("marketing instant")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def __init__(self, request, consent=None): self._request = request self._consent = consent self._context = Tag.create_context(request) self._cookies = request.COOKIES self._config = TagTypeSettings.all() self._tags = [] self.cookies = {} self.define_strategy()
def should_include(self, tag_type, tag_config): cookie_name = Tag.get_cookie_name(tag_type) cookie = self._cookies.get(cookie_name, None) if tag_config == "required": return True elif tag_config == "initial": if not cookie or cookie == "unset" or cookie == "true": return True else: if cookie == "true": return True
def post(self, tag_type, tag_config): cookie_name = Tag.get_cookie_name(tag_type) cookie = self._cookies.get(cookie_name, None) if tag_config == "required": # Include required lazy tags # Include required cookie if self._consent is None: self._tags.append((Tag.LAZY_LOAD, tag_type)) if cookie != "true": self.cookies[cookie_name] = "true" elif self._consent is None: if tag_config == "initial": if cookie == "unset": # Include initial lazy tags # Include initial instant tags self._tags.append((Tag.LAZY_LOAD, tag_type)) self._tags.append((Tag.INSTANT_LOAD, tag_type)) elif cookie == "true": # Include initial lazy tags self._tags.append((Tag.LAZY_LOAD, tag_type)) else: if cookie == "true": # Include generic lazy tags self._tags.append((Tag.LAZY_LOAD, tag_type)) elif self._consent is True: if tag_config == "initial": if cookie == "false": # Include initial lazy tags # Include initial instant tags # Include initial cookie self._tags.append((Tag.LAZY_LOAD, tag_type)) self._tags.append((Tag.INSTANT_LOAD, tag_type)) self.cookies[cookie_name] = "true" else: if cookie == "true": pass else: # Include generic lazy tags # Include generic instant tags # Include generic cookie self._tags.append((Tag.LAZY_LOAD, tag_type)) self._tags.append((Tag.INSTANT_LOAD, tag_type)) self.cookies[cookie_name] = "true" elif self._consent is False: self.cookies[cookie_name] = "false"
def __init__(self, request, payload=None): self._request = request self._context = Tag.create_context(request) self._payload = payload or {} self._config = TagTypeSettings.all() self._tags = [] from wagtail_tag_manager.utils import get_consent self.consent_state = get_consent(request) self.consent = {} if request: self.define_strategy()
def get(self, tag_type, tag_config): cookie_name = Tag.get_cookie_name(tag_type) cookie = self._cookies.get(cookie_name, None) if tag_config == "required": # Include required instant tags # Include required cookie self._tags.append((Tag.INSTANT_LOAD, tag_type)) self.cookies[cookie_name] = "true" elif tag_config == "initial": if not cookie or cookie == "unset": # Include initial cookie self.cookies[cookie_name] = "unset" elif cookie == "true": # Include initial instant tags self._tags.append((Tag.INSTANT_LOAD, tag_type)) self.cookies[cookie_name] = "true" else: if cookie == "true": # Include generic instant tags self._tags.append((Tag.INSTANT_LOAD, tag_type)) self.cookies[cookie_name] = "true"
def cookie_state(self): return { tag_type: self.cookies.get(Tag.get_cookie_name(tag_type), "false") != "false" for tag_type in Tag.get_types() }
def scan_cookies(request): # pragma: no cover def chop_microseconds(delta): return delta - timedelta(microseconds=delta.microseconds) try: options = webdriver.ChromeOptions() options.add_argument("headless") browser = webdriver.Chrome(options=options) browser.implicitly_wait(30) browser.get(request.site.root_page.full_url) browser.delete_all_cookies() for tag in Tag.get_types(): browser.add_cookie( { "name": Tag.get_cookie_name(tag), "value": "true", "path": "", "secure": False, } ) browser.get(request.site.root_page.full_url) now = datetime.utcnow() created = 0 updated = 0 for cookie in browser.get_cookies(): expiry = datetime.fromtimestamp(cookie.get("expiry", now)) obj, created = CookieDeclaration.objects.update_or_create( name=cookie.get("name"), domain=cookie.get("domain"), defaults={ "security": CookieDeclaration.INSECURE_COOKIE if cookie.get("httpOnly") else CookieDeclaration.SECURE_COOKIE, "purpose": _("Unknown"), "duration": chop_microseconds(expiry - now), }, ) if created: created = created + 1 else: updated = updated + 1 browser.quit() messages.success( request, _("Created %d declaration(s) and updated %d." % (created, updated)) ) except NotADirectoryError: messages.warning( request, mark_safe( _( "Could not instantiate WebDriver session. Please ensure " "<a href='http://chromedriver.chromium.org/' target='_blank' rel='noopener'>ChromeDriver</a> " "is installed and available in your path." ) ), ) except Exception as e: messages.error(request, e)
def sorted(self): from wagtail_tag_manager.models import Tag order = [*Tag.get_types(), None] return sorted(self, key=lambda x: order.index(x.cookie_type))
def cookie_state(self): return { tag_type: self.consent.get(tag_type, CONSENT_FALSE) != CONSENT_FALSE for tag_type in Tag.get_types() }