django-cookie-consent 0.8.0__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. cookie_consent/__init__.py +1 -1
  2. cookie_consent/admin.py +32 -4
  3. cookie_consent/cache.py +13 -8
  4. cookie_consent/conf.py +21 -13
  5. cookie_consent/forms.py +50 -0
  6. cookie_consent/middleware.py +7 -6
  7. cookie_consent/migrations/0001_initial.py +2 -1
  8. cookie_consent/migrations/0003_alter_cookiegroup_varname.py +2 -2
  9. cookie_consent/migrations/0004_cookie_natural_key.py +0 -1
  10. cookie_consent/models.py +30 -20
  11. cookie_consent/processor.py +77 -0
  12. cookie_consent/py.typed +0 -0
  13. cookie_consent/static/cookie_consent/cookiebar.module.js +8 -3
  14. cookie_consent/static/cookie_consent/cookiebar.module.js.map +2 -2
  15. cookie_consent/templates/cookie_consent/_cookie_group.html +4 -2
  16. cookie_consent/templatetags/__init__.py +0 -1
  17. cookie_consent/templatetags/cookie_consent_tags.py +25 -116
  18. cookie_consent/urls.py +3 -25
  19. cookie_consent/util.py +87 -119
  20. cookie_consent/views.py +46 -40
  21. django_cookie_consent-1.0.0.dist-info/METADATA +96 -0
  22. django_cookie_consent-1.0.0.dist-info/RECORD +31 -0
  23. {django_cookie_consent-0.8.0.dist-info → django_cookie_consent-1.0.0.dist-info}/WHEEL +1 -1
  24. cookie_consent/static/cookie_consent/cookiebar.js +0 -67
  25. django_cookie_consent-0.8.0.dist-info/METADATA +0 -125
  26. django_cookie_consent-0.8.0.dist-info/RECORD +0 -30
  27. django_cookie_consent-0.8.0.dist-info/licenses/AUTHORS +0 -14
  28. {django_cookie_consent-0.8.0.dist-info → django_cookie_consent-1.0.0.dist-info}/licenses/LICENSE +0 -0
  29. {django_cookie_consent-0.8.0.dist-info → django_cookie_consent-1.0.0.dist-info}/top_level.txt +0 -0
@@ -1 +1 @@
1
- __version__ = "0.8.0"
1
+ __version__ = "1.0.0"
cookie_consent/admin.py CHANGED
@@ -1,10 +1,16 @@
1
- # -*- coding: utf-8 -*-
2
1
  from django.contrib import admin
2
+ from django.db.models import Count
3
+ from django.http.request import HttpRequest
4
+ from django.templatetags.l10n import localize
5
+ from django.templatetags.static import static
6
+ from django.utils.html import format_html
7
+ from django.utils.translation import gettext_lazy as _
3
8
 
4
9
  from .conf import settings
5
10
  from .models import Cookie, CookieGroup, LogItem
6
11
 
7
12
 
13
+ @admin.register(Cookie)
8
14
  class CookieAdmin(admin.ModelAdmin):
9
15
  list_display = ("varname", "name", "cookiegroup", "path", "domain", "get_version")
10
16
  search_fields = ("name", "domain", "cookiegroup__varname", "cookiegroup__name")
@@ -12,8 +18,16 @@ class CookieAdmin(admin.ModelAdmin):
12
18
  list_filter = ("cookiegroup",)
13
19
 
14
20
 
21
+ @admin.register(CookieGroup)
15
22
  class CookieGroupAdmin(admin.ModelAdmin):
16
- list_display = ("varname", "name", "is_required", "is_deletable", "get_version")
23
+ list_display = (
24
+ "varname",
25
+ "name",
26
+ "is_required",
27
+ "is_deletable",
28
+ "num_cookies",
29
+ "get_version",
30
+ )
17
31
  search_fields = (
18
32
  "varname",
19
33
  "name",
@@ -23,6 +37,22 @@ class CookieGroupAdmin(admin.ModelAdmin):
23
37
  "is_deletable",
24
38
  )
25
39
 
40
+ def get_queryset(self, request: HttpRequest):
41
+ qs = super().get_queryset(request)
42
+ return qs.annotate(num_cookies=Count("cookie"))
43
+
44
+ @admin.display(ordering="num_cookies", description=_("# cookies"))
45
+ def num_cookies(self, obj: CookieGroup):
46
+ if (count := obj.num_cookies) > 0:
47
+ return localize(count)
48
+
49
+ return format_html(
50
+ '{count} <img src="{src}" alt="{alt}">',
51
+ count=localize(count),
52
+ src=static("admin/img/icon-alert.svg"),
53
+ alt=_("Warning icon for missing cookies in cookie group."),
54
+ )
55
+
26
56
 
27
57
  class LogItemAdmin(admin.ModelAdmin):
28
58
  list_display = ("action", "cookiegroup", "version", "created")
@@ -31,7 +61,5 @@ class LogItemAdmin(admin.ModelAdmin):
31
61
  date_hierarchy = "created"
32
62
 
33
63
 
34
- admin.site.register(Cookie, CookieAdmin)
35
- admin.site.register(CookieGroup, CookieGroupAdmin)
36
64
  if settings.COOKIE_CONSENT_LOG_ENABLED:
37
65
  admin.site.register(LogItem, LogItemAdmin)
cookie_consent/cache.py CHANGED
@@ -1,8 +1,9 @@
1
- # -*- coding: utf-8 -*-
1
+ from collections.abc import Mapping
2
+
2
3
  from django.core.cache import caches
3
4
 
4
5
  from .conf import settings
5
- from .models import CookieGroup
6
+ from .models import Cookie, CookieGroup
6
7
 
7
8
  CACHE_KEY = "cookie_consent_cache"
8
9
  CACHE_TIMEOUT = 60 * 60 # 60 minutes
@@ -19,17 +20,17 @@ def _get_cache():
19
20
  return caches[settings.COOKIE_CONSENT_CACHE_BACKEND]
20
21
 
21
22
 
22
- def delete_cache():
23
+ def delete_cache() -> None:
23
24
  cache = _get_cache()
24
25
  cache.delete(CACHE_KEY)
25
26
 
26
27
 
27
- def _get_cookie_groups_from_db():
28
+ def _get_cookie_groups_from_db() -> Mapping[str, CookieGroup]:
28
29
  qs = CookieGroup.objects.filter(is_required=False).prefetch_related("cookie_set")
29
30
  return qs.in_bulk(field_name="varname")
30
31
 
31
32
 
32
- def all_cookie_groups():
33
+ def all_cookie_groups() -> Mapping[str, CookieGroup]:
33
34
  """
34
35
  Get all cookie groups that are optional.
35
36
 
@@ -37,16 +38,20 @@ def all_cookie_groups():
37
38
  cache miss.
38
39
  """
39
40
  cache = _get_cache()
40
- return cache.get_or_set(
41
+ result = cache.get_or_set(
41
42
  CACHE_KEY, _get_cookie_groups_from_db, timeout=CACHE_TIMEOUT
42
43
  )
44
+ assert result is not None
45
+ return result
43
46
 
44
47
 
45
- def get_cookie_group(varname):
48
+ def get_cookie_group(varname: str) -> CookieGroup | None:
46
49
  return all_cookie_groups().get(varname)
47
50
 
48
51
 
49
- def get_cookie(cookie_group, name, domain):
52
+ def get_cookie(cookie_group: CookieGroup, name: str, domain: str) -> Cookie | None:
53
+ # loop over cookie set relation instead of doing a lookup query, as this should
54
+ # come from the cache and avoid hitting the database
50
55
  for cookie in cookie_group.cookie_set.all():
51
56
  if cookie.name == name and cookie.domain == domain:
52
57
  return cookie
cookie_consent/conf.py CHANGED
@@ -1,5 +1,8 @@
1
- # -*- coding: utf-8 -*-
2
- from django.conf import settings # NOQA
1
+ from typing import Literal
2
+
3
+ from django.conf import settings
4
+ from django.urls import reverse_lazy
5
+ from django.utils.functional import Promise
3
6
 
4
7
  from appconf import AppConf
5
8
 
@@ -8,20 +11,25 @@ __all__ = ["settings"]
8
11
 
9
12
  class CookieConsentConf(AppConf):
10
13
  # django-cookie-consent cookie settings that store the configuration
11
- NAME = "cookie_consent"
14
+ NAME: str = "cookie_consent"
12
15
  # TODO: rename to AGE for parity with django settings
13
- MAX_AGE = 60 * 60 * 24 * 365 * 1 # 1 year,
14
- DOMAIN = None
15
- SECURE = False
16
- HTTPONLY = True
17
- SAMESITE = "Lax"
16
+ MAX_AGE: int = 60 * 60 * 24 * 365 * 1 # 1 year,
17
+ DOMAIN: str | None = None
18
+ SECURE: bool = False
19
+ HTTPONLY: bool = True
20
+ SAMESITE: Literal["Strict", "Lax", "None", False] = "Lax"
21
+
22
+ DECLINE: str = "-1"
18
23
 
19
- DECLINE = "-1"
24
+ ENABLED: bool = True
20
25
 
21
- ENABLED = True
26
+ OPT_OUT: bool = False
22
27
 
23
- OPT_OUT = False
28
+ CACHE_BACKEND: str = "default"
24
29
 
25
- CACHE_BACKEND = "default"
30
+ LOG_ENABLED: bool = True
31
+ """
32
+ DeprecationWarning: in future versions the default may switch to log disabled.
33
+ """
26
34
 
27
- LOG_ENABLED = True
35
+ SUCCESS_URL: str | Promise = reverse_lazy("cookie_consent_cookie_group_list")
@@ -0,0 +1,50 @@
1
+ from collections.abc import Collection, Iterator
2
+
3
+ from django import forms
4
+ from django.utils.translation import gettext_lazy as _
5
+
6
+ from .cache import all_cookie_groups
7
+ from .models import CookieGroup
8
+
9
+
10
+ def iter_cookie_group_choices() -> Iterator[tuple[str, str]]:
11
+ """
12
+ Use the cached cookie group instances to get a list of choices.
13
+ """
14
+ for varname, cookie_group in all_cookie_groups().items():
15
+ yield varname, cookie_group.name
16
+
17
+
18
+ class CookieGroupsChoiceField(forms.TypedMultipleChoiceField):
19
+ def __init__(self, **kwargs):
20
+ kwargs["coerce"] = self._coerce_choice
21
+ kwargs["choices"] = iter_cookie_group_choices
22
+ super().__init__(**kwargs)
23
+
24
+ def _coerce_choice(self, varname: str) -> CookieGroup:
25
+ all_groups = all_cookie_groups()
26
+ return all_groups[varname]
27
+
28
+
29
+ class ProcessCookiesForm(forms.Form):
30
+ all_groups = forms.BooleanField(
31
+ label=_("Apply to all cookie groups"),
32
+ required=False,
33
+ )
34
+ cookie_groups = CookieGroupsChoiceField(
35
+ label=_("Cookie group varnames"),
36
+ choices=iter_cookie_group_choices,
37
+ required=False,
38
+ )
39
+
40
+ def get_cookie_groups(self) -> Collection[CookieGroup]:
41
+ """
42
+ Build the collection of specified cookies.
43
+ """
44
+ match self.cleaned_data:
45
+ case {"all_groups": True}:
46
+ return all_cookie_groups().values()
47
+ case {"cookie_groups": [*groups]}:
48
+ return groups
49
+ case _:
50
+ return []
@@ -1,12 +1,13 @@
1
- # -*- coding: utf-8 -*-
2
- from typing import Optional
1
+ from collections.abc import Callable
2
+
3
+ from django.http import HttpRequest, HttpResponseBase
3
4
 
4
5
  from .cache import all_cookie_groups
5
6
  from .conf import settings
6
7
  from .util import get_cookie_dict_from_request, is_cookie_consent_enabled
7
8
 
8
9
 
9
- def _should_delete_cookie(group_version: Optional[str]) -> bool:
10
+ def _should_delete_cookie(group_version: str | None) -> bool:
10
11
  # declined after it was accepted (and set) before
11
12
  if group_version == settings.COOKIE_CONSENT_DECLINE:
12
13
  return True
@@ -31,16 +32,16 @@ class CleanCookiesMiddleware:
31
32
  Note that this only applies if COOKIE_CONSENT_OPT_OUT is not set.
32
33
  """
33
34
 
34
- def __init__(self, get_response):
35
+ def __init__(self, get_response: Callable[[HttpRequest], HttpResponseBase]):
35
36
  self.get_response = get_response
36
37
 
37
- def __call__(self, request):
38
+ def __call__(self, request: HttpRequest):
38
39
  response = self.get_response(request)
39
40
  if is_cookie_consent_enabled(request):
40
41
  self.process_response(request, response)
41
42
  return response
42
43
 
43
- def process_response(self, request, response):
44
+ def process_response(self, request: HttpRequest, response: HttpResponseBase):
44
45
  cookie_dic = get_cookie_dict_from_request(request)
45
46
 
46
47
  cookies_to_delete = []
@@ -68,7 +68,8 @@ class Migration(migrations.Migration):
68
68
  validators=[
69
69
  django.core.validators.RegexValidator(
70
70
  re.compile("^[-_a-zA-Z0-9]+$"),
71
- "Enter a valid 'varname' consisting of letters, numbers, underscores or hyphens.",
71
+ "Enter a valid 'varname' consisting of letters, "
72
+ "numbers, underscores or hyphens.",
72
73
  "invalid",
73
74
  )
74
75
  ],
@@ -7,7 +7,6 @@ from django.db import migrations, models
7
7
 
8
8
 
9
9
  class Migration(migrations.Migration):
10
-
11
10
  dependencies = [
12
11
  ("cookie_consent", "0002_auto__add_logitem"),
13
12
  ]
@@ -22,7 +21,8 @@ class Migration(migrations.Migration):
22
21
  validators=[
23
22
  django.core.validators.RegexValidator(
24
23
  re.compile("^[-_a-zA-Z0-9]+$"),
25
- "Enter a valid 'varname' consisting of letters, numbers, underscores or hyphens.",
24
+ "Enter a valid 'varname' consisting of letters, numbers, "
25
+ "underscores or hyphens.",
26
26
  "invalid",
27
27
  )
28
28
  ],
@@ -4,7 +4,6 @@ from django.db import migrations, models
4
4
 
5
5
 
6
6
  class Migration(migrations.Migration):
7
-
8
7
  dependencies = [
9
8
  ("cookie_consent", "0003_alter_cookiegroup_varname"),
10
9
  ]
cookie_consent/models.py CHANGED
@@ -1,6 +1,8 @@
1
- # -*- coding: utf-8 -*-
1
+ from __future__ import annotations
2
+
2
3
  import re
3
- from typing import TypedDict
4
+ from collections.abc import Callable
5
+ from typing import ClassVar, ParamSpec, TypedDict, TypeVar
4
6
 
5
7
  from django.core.validators import RegexValidator
6
8
  from django.db import models
@@ -16,9 +18,12 @@ validate_cookie_name = RegexValidator(
16
18
  "invalid",
17
19
  )
18
20
 
21
+ P = ParamSpec("P")
22
+ T = TypeVar("T")
23
+
19
24
 
20
- def clear_cache_after(func):
21
- def wrapper(*args, **kwargs):
25
+ def clear_cache_after(func: Callable[P, T]) -> Callable[P, T]:
26
+ def wrapper(*args: P.args, **kwargs: P.kwargs):
22
27
  from .cache import delete_cache
23
28
 
24
29
  return_value = func(*args, **kwargs)
@@ -33,9 +38,8 @@ class CookieGroupDict(TypedDict):
33
38
  name: str
34
39
  description: str
35
40
  is_required: bool
36
- # TODO: should we output this? page cache busting would be
37
- # required if we do this. Alternatively, set up a JSONView to output these?
38
- # version: str
41
+ # The version is deliberately not included because it requires page/view cache
42
+ # busting if a new cookie gets added to the group, which we don't control.
39
43
 
40
44
 
41
45
  class BaseQueryset(models.query.QuerySet):
@@ -49,7 +53,7 @@ class BaseQueryset(models.query.QuerySet):
49
53
 
50
54
 
51
55
  class CookieGroupManager(models.Manager.from_queryset(BaseQueryset)):
52
- def get_by_natural_key(self, varname):
56
+ def get_by_natural_key(self, varname: str) -> CookieGroup:
53
57
  return self.get(varname=varname)
54
58
 
55
59
 
@@ -75,7 +79,8 @@ class CookieGroup(models.Model):
75
79
  ordering = models.IntegerField(_("Ordering"), default=0)
76
80
  created = models.DateTimeField(_("Created"), auto_now_add=True, blank=True)
77
81
 
78
- objects = CookieGroupManager()
82
+ objects: ClassVar[CookieGroupManager] = CookieGroupManager() # pyright: ignore[reportIncompatibleVariableOverride]
83
+ cookie_set: ClassVar[CookieManager]
79
84
 
80
85
  class Meta:
81
86
  verbose_name = _("Cookie Group")
@@ -93,11 +98,15 @@ class CookieGroup(models.Model):
93
98
  def delete(self, *args, **kwargs):
94
99
  return super().delete(*args, **kwargs)
95
100
 
96
- def natural_key(self):
101
+ def natural_key(self) -> tuple[str]:
97
102
  return (self.varname,)
98
103
 
99
104
  def get_version(self) -> str:
100
105
  try:
106
+ # this relies on the cookie set being ordered by most-recently created
107
+ # first.
108
+ # Note that we don't use `.first()` as that's a new query and bypasses
109
+ # the cache.
101
110
  return str(self.cookie_set.all()[0].get_version())
102
111
  except IndexError:
103
112
  return ""
@@ -113,7 +122,7 @@ class CookieGroup(models.Model):
113
122
 
114
123
 
115
124
  class CookieManager(models.Manager.from_queryset(BaseQueryset)):
116
- def get_by_natural_key(self, name, domain, cookiegroup):
125
+ def get_by_natural_key(self, name: str, domain: str, cookiegroup: str) -> Cookie:
117
126
  group = CookieGroup.objects.get_by_natural_key(cookiegroup)
118
127
  return self.get(cookiegroup=group, name=name, domain=domain)
119
128
 
@@ -144,7 +153,7 @@ class Cookie(models.Model):
144
153
  ordering = ["-created"]
145
154
 
146
155
  def __str__(self):
147
- return "%s %s%s" % (self.name, self.domain, self.path)
156
+ return f"{self.name} {self.domain}{self.path}"
148
157
 
149
158
  @clear_cache_after
150
159
  def save(self, *args, **kwargs):
@@ -154,16 +163,17 @@ class Cookie(models.Model):
154
163
  def delete(self, *args, **kwargs):
155
164
  return super().delete(*args, **kwargs)
156
165
 
157
- def natural_key(self):
166
+ def natural_key(self) -> tuple[str, str, str]:
158
167
  return (self.name, self.domain) + self.cookiegroup.natural_key()
159
168
 
160
- natural_key.dependencies = ["cookie_consent.cookiegroup"]
169
+ natural_key.dependencies = ["cookie_consent.cookiegroup"] # pyright: ignore[reportFunctionMemberAccess]
161
170
 
162
171
  @property
163
- def varname(self):
164
- return "%s=%s:%s" % (self.cookiegroup.varname, self.name, self.domain)
172
+ def varname(self) -> str:
173
+ group_varname = self.cookiegroup.varname
174
+ return f"{group_varname}={self.name}:{self.domain}"
165
175
 
166
- def get_version(self):
176
+ def get_version(self) -> str:
167
177
  return self.created.isoformat()
168
178
 
169
179
 
@@ -185,10 +195,10 @@ class LogItem(models.Model):
185
195
  version = models.CharField(_("Version"), max_length=32)
186
196
  created = models.DateTimeField(_("Created"), auto_now_add=True, blank=True)
187
197
 
188
- def __str__(self):
189
- return "%s %s" % (self.cookiegroup.name, self.version)
190
-
191
198
  class Meta:
192
199
  verbose_name = _("Log item")
193
200
  verbose_name_plural = _("Log items")
194
201
  ordering = ["-created"]
202
+
203
+ def __str__(self):
204
+ return f"{self.cookiegroup.name} {self.version}"
@@ -0,0 +1,77 @@
1
+ from collections.abc import Collection
2
+ from typing import Literal
3
+
4
+ from django.http import HttpRequest, HttpResponseBase
5
+
6
+ from .conf import settings
7
+ from .models import ACTION_ACCEPTED, ACTION_DECLINED, CookieGroup, LogItem
8
+ from .util import get_cookie_dict_from_request, set_cookie_dict_to_response
9
+
10
+
11
+ class CookiesProcessor:
12
+ """
13
+ Process the accept/decline logic for cookie groups.
14
+ """
15
+
16
+ def __init__(self, request: HttpRequest, response: HttpResponseBase):
17
+ self.request = request
18
+ self.response = response
19
+
20
+ def process(
21
+ self,
22
+ cookie_groups: Collection[CookieGroup],
23
+ action: Literal["accept", "decline"],
24
+ ) -> None:
25
+ """
26
+ Apply ``action`` to the specified ``cookie_groups``.
27
+
28
+ Mutates the response by updating the cookie tracking the cookie group status. If
29
+ there are no cookie groups provided, nothing happens.
30
+ """
31
+ if not cookie_groups:
32
+ return
33
+
34
+ cookie_dic = get_cookie_dict_from_request(self.request)
35
+
36
+ match action:
37
+ case "accept":
38
+ for cookie_group in cookie_groups:
39
+ cookie_dic[cookie_group.varname] = cookie_group.get_version()
40
+ case "decline":
41
+ self._delete_cookies(cookie_groups)
42
+ for cookie_group in cookie_groups:
43
+ cookie_dic[cookie_group.varname] = settings.COOKIE_CONSENT_DECLINE
44
+
45
+ self._log_action(cookie_groups, action)
46
+ set_cookie_dict_to_response(self.response, cookie_dic)
47
+
48
+ def _log_action(
49
+ self,
50
+ cookie_groups: Collection[CookieGroup],
51
+ action: Literal["accept", "decline"],
52
+ ) -> None:
53
+ if not settings.COOKIE_CONSENT_LOG_ENABLED:
54
+ return
55
+ # TODO: replace with stdlib logging call/helper instead of creating DB records
56
+ # directly.
57
+
58
+ action_map: dict[Literal["accept", "decline"], int] = {
59
+ "accept": ACTION_ACCEPTED,
60
+ "decline": ACTION_DECLINED,
61
+ }
62
+ log_items: list[LogItem] = [
63
+ LogItem(
64
+ action=action_map[action],
65
+ cookiegroup=cookie_group,
66
+ version=cookie_group.get_version(),
67
+ )
68
+ for cookie_group in cookie_groups
69
+ ]
70
+ LogItem.objects.bulk_create(log_items)
71
+
72
+ def _delete_cookies(self, cookie_groups: Collection[CookieGroup]) -> None:
73
+ for cookie_group in cookie_groups:
74
+ if not cookie_group.is_deletable:
75
+ continue
76
+ for cookie in cookie_group.cookie_set.all():
77
+ self.response.delete_cookie(cookie.name, cookie.path, cookie.domain)
File without changes
@@ -25,14 +25,19 @@ var FetchClient = class {
25
25
  }
26
26
  return this.cookieStatus;
27
27
  }
28
- async saveCookiesStatusBackend(urlProperty) {
28
+ async saveCookiesStatusBackend(urlProperty, cookieGroups) {
29
29
  const cookieStatus = await this.getCookieStatus();
30
30
  const url = cookieStatus[urlProperty];
31
31
  if (!url) {
32
32
  throw new Error(`Missing url for ${urlProperty} - was the cookie status not loaded properly?`);
33
33
  }
34
+ const formData = new FormData();
35
+ for (const group of cookieGroups) {
36
+ formData.append("cookie_groups", group.varname);
37
+ }
34
38
  await window.fetch(url, {
35
39
  method: "POST",
40
+ body: formData,
36
41
  credentials: "same-origin",
37
42
  headers: {
38
43
  ...DEFAULT_FETCH_HEADERS,
@@ -71,7 +76,7 @@ var registerEvents = ({
71
76
  event.preventDefault();
72
77
  const acceptedGroups = filterCookieGroups(cookieGroups, accepted.concat(undecided));
73
78
  onAccept == null ? void 0 : onAccept(acceptedGroups, event);
74
- client.saveCookiesStatusBackend("acceptUrl");
79
+ client.saveCookiesStatusBackend("acceptUrl", acceptedGroups);
75
80
  cookieBarNode.parentNode.removeChild(cookieBarNode);
76
81
  });
77
82
  }
@@ -81,7 +86,7 @@ var registerEvents = ({
81
86
  event.preventDefault();
82
87
  const declinedGroups = filterCookieGroups(cookieGroups, declined.concat(undecided));
83
88
  onDecline == null ? void 0 : onDecline(declinedGroups, event);
84
- client.saveCookiesStatusBackend("declineUrl");
89
+ client.saveCookiesStatusBackend("declineUrl", declinedGroups);
85
90
  cookieBarNode.parentNode.removeChild(cookieBarNode);
86
91
  });
87
92
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../js/src/cookiebar.ts"],
4
- "sourcesContent": ["/**\n * Cookiebar functionality, as a TS/JS module.\n *\n * About modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules\n *\n * The code is organized here in a way to make the templates work with Django's page\n * cache. This means that anything user-specific (so different django session and even\n * cookie consent cookies) cannot be baked into the templates, as that breaks caches.\n *\n * The cookie bar operates on the following principles:\n *\n * - The developer using the library includes the desired template in their django\n * templates, using the HTML <template> element. This contains the content for the\n * cookie bar.\n * - The developer is responsible for loading some Javascript that loads this script.\n * - The main export of this script needs to be called (showCookieBar), with the\n * appropriate options.\n * - The options include the backend URLs where the retrieve data, which selectors/DOM\n * nodes to use for various functionality and the hooks to tap into the accept/decline\n * life-cycle.\n * - When a user accepts or declines (all) cookies, the call to the backend is made via\n * a fetch request, bypassing any page caches and preventing full-page reloads.\n */\n\n/**\n * A serialized cookie group.\n *\n * See the backend model method `CookieGroup.as_json()`.\n */\nexport interface CookieGroup {\n varname: string;\n name: string;\n description: string;\n is_required: boolean;\n}\n\nexport interface Options {\n statusUrl: string;\n // TODO: also accept element rather than selector?\n templateSelector: string;\n /**\n * DOM selector to the (script) tag holding the JSON-serialized cookie groups.\n *\n * This is typically rendered in a template with a template tag, e.g.\n *\n * ```django\n * {% all_cookie_groups 'cookie-consent__cookie-groups' %}\n * ```\n *\n * resulting in the selector: `'#cookie-consent__cookie-groups'`.\n */\n cookieGroupsSelector: string;\n acceptSelector: string;\n declineSelector: string;\n /**\n * Either a string (selector), DOMNode or null.\n *\n * If null, the bar is appended to the body. If provided, the node is used or looked\n * up.\n */\n insertBefore: string | HTMLElement | null;\n /**\n * Optional callback for when the cookie bar is being shown.\n *\n * You can use this to add a CSS class name to the body, for example.\n */\n onShow?: () => void;\n /**\n * Optional callback called when cookies are accepted.\n */\n onAccept?: (acceptedGroups: CookieGroup[], event?: MouseEvent) => void;\n /**\n * Optional callback called when cookies are accepted.\n */\n onDecline?: (declinedGroups: CookieGroup[], event?: MouseEvent) => void;\n /**\n * Name of the header to use for the CSRF token.\n *\n * If needed, this can be read/set via `settings.CSRF_HEADER_NAME` in the backend.\n */\n csrfHeaderName: string;\n};\n\nexport interface CookieStatus {\n csrftoken: string;\n /**\n * Backend endpoint to POST to to accept the cookie groups.\n */\n acceptUrl: string;\n /**\n * Backend endpoint to POST to to decline the cookie groups.\n */\n declineUrl: string;\n /**\n * Array of accepted cookie group varnames.\n */\n acceptedCookieGroups: string[];\n /**\n * Array of declined cookie group varnames.\n */\n declinedCookieGroups: string[];\n /**\n * Array of undecided cookie group varnames.\n */\n notAcceptedOrDeclinedCookieGroups: string[];\n}\n\nconst DEFAULT_FETCH_HEADERS: Record<string, string> = {\n 'X-Cookie-Consent-Fetch': '1'\n};\n\n/**\n * A simple wrapper around window.fetch that understands the django-cookie-consent\n * backend endpoints.\n *\n * @private - while exported, use at your own risk. This class is not part of the\n * public API covered by SemVer.\n */\nexport class FetchClient {\n protected statusUrl: string;\n protected csrfHeaderName: string;\n protected cookieStatus: CookieStatus | null;\n\n constructor(statusUrl: string, csrfHeaderName: string) {\n this.statusUrl = statusUrl;\n this.csrfHeaderName = csrfHeaderName;\n this.cookieStatus = null;\n }\n\n async getCookieStatus(): Promise<CookieStatus> {\n if (this.cookieStatus === null) {\n const response = await window.fetch(\n this.statusUrl,\n {\n method: 'GET',\n credentials: 'same-origin',\n headers: DEFAULT_FETCH_HEADERS,\n }\n );\n this.cookieStatus = await response.json();\n }\n\n // type checker sanity check\n if (this.cookieStatus === null) {\n throw new Error('Unexpectedly received null cookie status');\n }\n return this.cookieStatus;\n };\n\n async saveCookiesStatusBackend (urlProperty: 'acceptUrl' | 'declineUrl') {\n const cookieStatus = await this.getCookieStatus();\n const url = cookieStatus[urlProperty];\n if (!url) {\n throw new Error(`Missing url for ${urlProperty} - was the cookie status not loaded properly?`);\n }\n\n await window.fetch(url, {\n method: 'POST',\n credentials: 'same-origin',\n headers: {\n ...DEFAULT_FETCH_HEADERS,\n [this.csrfHeaderName]: cookieStatus.csrftoken\n }\n });\n }\n}\n\n/**\n * Read the JSON script node contents and parse the content as JSON.\n *\n * The result is the list of available/configured cookie groups.\n * Use the status URL to get the accepted/declined status for an individual user.\n */\nexport const loadCookieGroups = (selector: string): CookieGroup[] => {\n const node = document.querySelector<HTMLScriptElement>(selector);\n if (!node) {\n throw new Error(`No cookie groups (script) tag found, using selector: '${selector}'`);\n }\n return JSON.parse(node.innerText);\n};\n\nconst doInsertBefore = (beforeNode: HTMLElement, newNode: Node): void => {\n const parent = beforeNode.parentNode;\n if (parent === null) throw new Error('Reference node doesn\\'t have a parent.');\n parent.insertBefore(newNode, beforeNode);\n}\n\ntype RegisterEventsOptions = Pick<\n Options,\n 'acceptSelector' | 'onAccept' | 'declineSelector' | 'onDecline'\n> & Pick<\n CookieStatus,\n 'acceptedCookieGroups' | 'declinedCookieGroups' | 'notAcceptedOrDeclinedCookieGroups'\n> & {\n client: FetchClient,\n cookieBarNode: Element;\n cookieGroups: CookieGroup[];\n}\n\n/**\n * Register the accept/decline event handlers.\n *\n * Note that we can't just set the decline or accept cookie purely client-side, as the\n * cookie possibly has the httpOnly flag set.\n */\nconst registerEvents = ({\n client,\n cookieBarNode,\n cookieGroups,\n acceptSelector,\n onAccept,\n declineSelector,\n onDecline,\n acceptedCookieGroups: accepted,\n declinedCookieGroups: declined,\n notAcceptedOrDeclinedCookieGroups: undecided,\n}: RegisterEventsOptions): void => {\n\n const acceptNode = cookieBarNode.querySelector<HTMLElement>(acceptSelector);\n if (acceptNode) {\n acceptNode.addEventListener('click', event => {\n event.preventDefault();\n const acceptedGroups = filterCookieGroups(cookieGroups, accepted.concat(undecided));\n onAccept?.(acceptedGroups, event);\n // trigger async action, but don't wait for completion\n client.saveCookiesStatusBackend('acceptUrl');\n cookieBarNode.parentNode!.removeChild(cookieBarNode);\n });\n }\n\n const declineNode = cookieBarNode.querySelector<HTMLElement>(declineSelector);\n if (declineNode) {\n declineNode.addEventListener('click', event => {\n event.preventDefault();\n const declinedGroups = filterCookieGroups(cookieGroups, declined.concat(undecided));\n onDecline?.(declinedGroups, event);\n // trigger async action, but don't wait for completion\n client.saveCookiesStatusBackend('declineUrl');\n cookieBarNode.parentNode!.removeChild(cookieBarNode);\n });\n }\n};\n\n/**\n * Filter the cookie groups down to a subset of specified varnames.\n */\nconst filterCookieGroups = (cookieGroups: CookieGroup[], varNames: string[]) => {\n return cookieGroups.filter(group => varNames.includes(group.varname));\n};\n\n// See https://github.com/microsoft/TypeScript/issues/283\nfunction cloneNode<T extends Node>(node: T) {\n return <T>node.cloneNode(true);\n}\n\nexport const showCookieBar = async (options: Partial<Options> = {}): Promise<void> => {\n const {\n templateSelector = '#cookie-consent__cookie-bar',\n cookieGroupsSelector = '#cookie-consent__cookie-groups',\n acceptSelector = '.cookie-consent__accept',\n declineSelector = '.cookie-consent__decline',\n insertBefore = null,\n onShow,\n onAccept,\n onDecline,\n statusUrl = '',\n csrfHeaderName = 'X-CSRFToken', // Django's default, can be overridden with settings.CSRF_HEADER_NAME\n } = options;\n\n const cookieGroups = loadCookieGroups(cookieGroupsSelector);\n\n // no cookie groups -> abort, nothing to do\n if (!cookieGroups.length) return;\n\n const templateNode = document.querySelector<HTMLTemplateElement>(templateSelector);\n if (!templateNode) {\n throw new Error(`No (template) element found for selector '${templateSelector}'.`)\n }\n\n // insert before a given node, if specified, or append to the body as default behaviour\n const doInsert = insertBefore === null\n ? (cookieBarNode: Node) => document.querySelector('body')!.appendChild(cookieBarNode)\n : typeof insertBefore === 'string'\n ? (cookieBarNode: Node) => {\n const referenceNode = document.querySelector<HTMLElement>(insertBefore);\n if (referenceNode === null) throw new Error(`No element found for selector '${insertBefore}'.`)\n doInsertBefore(referenceNode, cookieBarNode);\n }\n : (cookieBarNode: Node) => doInsertBefore(insertBefore, cookieBarNode)\n ;\n\n if (!statusUrl) throw new Error('Missing status URL option, did you forget to pass the `statusUrl` option?');\n\n const client = new FetchClient(statusUrl, csrfHeaderName);\n const cookieStatus = await client.getCookieStatus();\n\n // calculate the cookie groups to invoke the callbacks. We deliberately fire those\n // without awaiting so that our cookie bar is shown/hidden as soon as possible.\n const {\n acceptedCookieGroups,\n declinedCookieGroups,\n notAcceptedOrDeclinedCookieGroups\n } = cookieStatus;\n\n const acceptedGroups = filterCookieGroups(cookieGroups, acceptedCookieGroups);\n if (acceptedGroups.length) onAccept?.(acceptedGroups);\n const declinedGroups = filterCookieGroups(cookieGroups, declinedCookieGroups);\n if (declinedGroups.length) onDecline?.(declinedGroups);\n\n // there are no (more) cookie groups to accept, don't show the bar\n if (!notAcceptedOrDeclinedCookieGroups.length) return;\n\n // grab the contents from the template node and add them to the DOM, optionally\n // calling the onShow callback\n const childToClone = templateNode.content.firstElementChild;\n if (childToClone === null) throw new Error('The cookie bar template element may not be empty.');\n const cookieBarNode = cloneNode(childToClone);\n registerEvents({\n client,\n cookieBarNode,\n cookieGroups,\n acceptSelector,\n onAccept,\n declineSelector,\n onDecline,\n acceptedCookieGroups,\n declinedCookieGroups,\n notAcceptedOrDeclinedCookieGroups,\n });\n doInsert(cookieBarNode);\n onShow?.();\n};\n"],
5
- "mappings": ";AA2GA,IAAM,wBAAgD;AAAA,EACpD,0BAA0B;AAC5B;AASO,IAAM,cAAN,MAAkB;AAAA,EAKvB,YAAY,WAAmB,gBAAwB;AACrD,SAAK,YAAY;AACjB,SAAK,iBAAiB;AACtB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAM,kBAAyC;AAC7C,QAAI,KAAK,iBAAiB,MAAM;AAC9B,YAAM,WAAW,MAAM,OAAO;AAAA,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M,gBAAgB,SAAS,cAA2B,YAAY;AACtE,QAAI,kBAAkB,KAAM,OAAM,IAAI,MAAM,kCAAkC,YAAY,IAAI;AAC9F,mBAAe,eAAeA,cAAa;AAAA,EAC7C,IACE,CAACA,mBAAwB,eAAe,cAAcA,cAAa;AAGzE,MAAI,CAAC,UAAW,OAAM,IAAI,MAAM,2EAA2E;AAE3G,QAAM,SAAS,IAAI,YAAY,WAAW,cAAc;AACxD,QAAM,eAAe,MAAM,OAAO,gBAAgB;AAIlD,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,iBAAiB,mBAAmB,cAAc,oBAAoB;AAC5E,MAAI,eAAe,OAAQ,sCAAW;AACtC,QAAM,iBAAiB,mBAAmB,cAAc,oBAAoB;AAC5E,MAAI,eAAe,OAAQ,wCAAY;AAGvC,MAAI,CAAC,kCAAkC,OAAQ;AAI/C,QAAM,eAAe,aAAa,QAAQ;AAC1C,MAAI,iBAAiB,KAAM,OAAM,IAAI,MAAM,mDAAmD;AAC9F,QAAM,gBAAgB,UAAU,YAAY;AAC5C,iBAAe;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACD,WAAS,aAAa;AACtB;AACF;",
4
+ "sourcesContent": ["/**\n * Cookiebar functionality, as a TS/JS module.\n *\n * About modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules\n *\n * The code is organized here in a way to make the templates work with Django's page\n * cache. This means that anything user-specific (so different django session and even\n * cookie consent cookies) cannot be baked into the templates, as that breaks caches.\n *\n * The cookie bar operates on the following principles:\n *\n * - The developer using the library includes the desired template in their django\n * templates, using the HTML <template> element. This contains the content for the\n * cookie bar.\n * - The developer is responsible for loading some Javascript that loads this script.\n * - The main export of this script needs to be called (showCookieBar), with the\n * appropriate options.\n * - The options include the backend URLs where the retrieve data, which selectors/DOM\n * nodes to use for various functionality and the hooks to tap into the accept/decline\n * life-cycle.\n * - When a user accepts or declines (all) cookies, the call to the backend is made via\n * a fetch request, bypassing any page caches and preventing full-page reloads.\n */\n\n/**\n * A serialized cookie group.\n *\n * See the backend model method `CookieGroup.as_json()`.\n */\nexport interface CookieGroup {\n varname: string;\n name: string;\n description: string;\n is_required: boolean;\n}\n\nexport interface Options {\n statusUrl: string;\n // TODO: also accept element rather than selector?\n templateSelector: string;\n /**\n * DOM selector to the (script) tag holding the JSON-serialized cookie groups.\n *\n * This is typically rendered in a template with a template tag, e.g.\n *\n * ```django\n * {% all_cookie_groups 'cookie-consent__cookie-groups' %}\n * ```\n *\n * resulting in the selector: `'#cookie-consent__cookie-groups'`.\n */\n cookieGroupsSelector: string;\n acceptSelector: string;\n declineSelector: string;\n /**\n * Either a string (selector), DOMNode or null.\n *\n * If null, the bar is appended to the body. If provided, the node is used or looked\n * up.\n */\n insertBefore: string | HTMLElement | null;\n /**\n * Optional callback for when the cookie bar is being shown.\n *\n * You can use this to add a CSS class name to the body, for example.\n */\n onShow?: () => void;\n /**\n * Optional callback called when cookies are accepted.\n */\n onAccept?: (acceptedGroups: CookieGroup[], event?: MouseEvent) => void;\n /**\n * Optional callback called when cookies are accepted.\n */\n onDecline?: (declinedGroups: CookieGroup[], event?: MouseEvent) => void;\n /**\n * Name of the header to use for the CSRF token.\n *\n * If needed, this can be read/set via `settings.CSRF_HEADER_NAME` in the backend.\n */\n csrfHeaderName: string;\n};\n\nexport interface CookieStatus {\n csrftoken: string;\n /**\n * Backend endpoint to POST to to accept the cookie groups.\n */\n acceptUrl: string;\n /**\n * Backend endpoint to POST to to decline the cookie groups.\n */\n declineUrl: string;\n /**\n * Array of accepted cookie group varnames.\n */\n acceptedCookieGroups: string[];\n /**\n * Array of declined cookie group varnames.\n */\n declinedCookieGroups: string[];\n /**\n * Array of undecided cookie group varnames.\n */\n notAcceptedOrDeclinedCookieGroups: string[];\n}\n\nconst DEFAULT_FETCH_HEADERS: Record<string, string> = {\n 'X-Cookie-Consent-Fetch': '1'\n};\n\n/**\n * A simple wrapper around window.fetch that understands the django-cookie-consent\n * backend endpoints.\n *\n * @private - while exported, use at your own risk. This class is not part of the\n * public API covered by SemVer.\n */\nexport class FetchClient {\n protected statusUrl: string;\n protected csrfHeaderName: string;\n protected cookieStatus: CookieStatus | null;\n\n constructor(statusUrl: string, csrfHeaderName: string) {\n this.statusUrl = statusUrl;\n this.csrfHeaderName = csrfHeaderName;\n this.cookieStatus = null;\n }\n\n async getCookieStatus(): Promise<CookieStatus> {\n if (this.cookieStatus === null) {\n const response = await window.fetch(\n this.statusUrl,\n {\n method: 'GET',\n credentials: 'same-origin',\n headers: DEFAULT_FETCH_HEADERS,\n }\n );\n this.cookieStatus = await response.json();\n }\n\n // type checker sanity check\n if (this.cookieStatus === null) {\n throw new Error('Unexpectedly received null cookie status');\n }\n return this.cookieStatus;\n };\n\n async saveCookiesStatusBackend (\n urlProperty: 'acceptUrl' | 'declineUrl',\n cookieGroups: CookieGroup[],\n ) {\n const cookieStatus = await this.getCookieStatus();\n const url = cookieStatus[urlProperty];\n if (!url) {\n throw new Error(`Missing url for ${urlProperty} - was the cookie status not loaded properly?`);\n }\n\n const formData = new FormData();\n for (const group of cookieGroups) {\n formData.append('cookie_groups', group.varname);\n }\n\n await window.fetch(url, {\n method: 'POST',\n body: formData,\n credentials: 'same-origin',\n headers: {\n ...DEFAULT_FETCH_HEADERS,\n [this.csrfHeaderName]: cookieStatus.csrftoken\n }\n });\n }\n}\n\n/**\n * Read the JSON script node contents and parse the content as JSON.\n *\n * The result is the list of available/configured cookie groups.\n * Use the status URL to get the accepted/declined status for an individual user.\n */\nexport const loadCookieGroups = (selector: string): CookieGroup[] => {\n const node = document.querySelector<HTMLScriptElement>(selector);\n if (!node) {\n throw new Error(`No cookie groups (script) tag found, using selector: '${selector}'`);\n }\n return JSON.parse(node.innerText);\n};\n\nconst doInsertBefore = (beforeNode: HTMLElement, newNode: Node): void => {\n const parent = beforeNode.parentNode;\n if (parent === null) throw new Error('Reference node doesn\\'t have a parent.');\n parent.insertBefore(newNode, beforeNode);\n}\n\ntype RegisterEventsOptions = Pick<\n Options,\n 'acceptSelector' | 'onAccept' | 'declineSelector' | 'onDecline'\n> & Pick<\n CookieStatus,\n 'acceptedCookieGroups' | 'declinedCookieGroups' | 'notAcceptedOrDeclinedCookieGroups'\n> & {\n client: FetchClient,\n cookieBarNode: Element;\n cookieGroups: CookieGroup[];\n}\n\n/**\n * Register the accept/decline event handlers.\n *\n * Note that we can't just set the decline or accept cookie purely client-side, as the\n * cookie possibly has the httpOnly flag set.\n */\nconst registerEvents = ({\n client,\n cookieBarNode,\n cookieGroups,\n acceptSelector,\n onAccept,\n declineSelector,\n onDecline,\n acceptedCookieGroups: accepted,\n declinedCookieGroups: declined,\n notAcceptedOrDeclinedCookieGroups: undecided,\n}: RegisterEventsOptions): void => {\n\n const acceptNode = cookieBarNode.querySelector<HTMLElement>(acceptSelector);\n if (acceptNode) {\n acceptNode.addEventListener('click', event => {\n event.preventDefault();\n const acceptedGroups = filterCookieGroups(cookieGroups, accepted.concat(undecided));\n onAccept?.(acceptedGroups, event);\n // trigger async action, but don't wait for completion\n client.saveCookiesStatusBackend('acceptUrl', acceptedGroups);\n cookieBarNode.parentNode!.removeChild(cookieBarNode);\n });\n }\n\n const declineNode = cookieBarNode.querySelector<HTMLElement>(declineSelector);\n if (declineNode) {\n declineNode.addEventListener('click', event => {\n event.preventDefault();\n const declinedGroups = filterCookieGroups(cookieGroups, declined.concat(undecided));\n onDecline?.(declinedGroups, event);\n // trigger async action, but don't wait for completion\n client.saveCookiesStatusBackend('declineUrl', declinedGroups);\n cookieBarNode.parentNode!.removeChild(cookieBarNode);\n });\n }\n};\n\n/**\n * Filter the cookie groups down to a subset of specified varnames.\n */\nconst filterCookieGroups = (cookieGroups: CookieGroup[], varNames: string[]) => {\n return cookieGroups.filter(group => varNames.includes(group.varname));\n};\n\n// See https://github.com/microsoft/TypeScript/issues/283\nfunction cloneNode<T extends Node>(node: T) {\n return <T>node.cloneNode(true);\n}\n\nexport const showCookieBar = async (options: Partial<Options> = {}): Promise<void> => {\n const {\n templateSelector = '#cookie-consent__cookie-bar',\n cookieGroupsSelector = '#cookie-consent__cookie-groups',\n acceptSelector = '.cookie-consent__accept',\n declineSelector = '.cookie-consent__decline',\n insertBefore = null,\n onShow,\n onAccept,\n onDecline,\n statusUrl = '',\n csrfHeaderName = 'X-CSRFToken', // Django's default, can be overridden with settings.CSRF_HEADER_NAME\n } = options;\n\n const cookieGroups = loadCookieGroups(cookieGroupsSelector);\n\n // no cookie groups -> abort, nothing to do\n if (!cookieGroups.length) return;\n\n const templateNode = document.querySelector<HTMLTemplateElement>(templateSelector);\n if (!templateNode) {\n throw new Error(`No (template) element found for selector '${templateSelector}'.`)\n }\n\n // insert before a given node, if specified, or append to the body as default behaviour\n const doInsert = insertBefore === null\n ? (cookieBarNode: Node) => document.querySelector('body')!.appendChild(cookieBarNode)\n : typeof insertBefore === 'string'\n ? (cookieBarNode: Node) => {\n const referenceNode = document.querySelector<HTMLElement>(insertBefore);\n if (referenceNode === null) throw new Error(`No element found for selector '${insertBefore}'.`)\n doInsertBefore(referenceNode, cookieBarNode);\n }\n : (cookieBarNode: Node) => doInsertBefore(insertBefore, cookieBarNode)\n ;\n\n if (!statusUrl) throw new Error('Missing status URL option, did you forget to pass the `statusUrl` option?');\n\n const client = new FetchClient(statusUrl, csrfHeaderName);\n const cookieStatus = await client.getCookieStatus();\n\n // calculate the cookie groups to invoke the callbacks. We deliberately fire those\n // without awaiting so that our cookie bar is shown/hidden as soon as possible.\n const {\n acceptedCookieGroups,\n declinedCookieGroups,\n notAcceptedOrDeclinedCookieGroups\n } = cookieStatus;\n\n const acceptedGroups = filterCookieGroups(cookieGroups, acceptedCookieGroups);\n if (acceptedGroups.length) onAccept?.(acceptedGroups);\n const declinedGroups = filterCookieGroups(cookieGroups, declinedCookieGroups);\n if (declinedGroups.length) onDecline?.(declinedGroups);\n\n // there are no (more) cookie groups to accept, don't show the bar\n if (!notAcceptedOrDeclinedCookieGroups.length) return;\n\n // grab the contents from the template node and add them to the DOM, optionally\n // calling the onShow callback\n const childToClone = templateNode.content.firstElementChild;\n if (childToClone === null) throw new Error('The cookie bar template element may not be empty.');\n const cookieBarNode = cloneNode(childToClone);\n registerEvents({\n client,\n cookieBarNode,\n cookieGroups,\n acceptSelector,\n onAccept,\n declineSelector,\n onDecline,\n acceptedCookieGroups,\n declinedCookieGroups,\n notAcceptedOrDeclinedCookieGroups,\n });\n doInsert(cookieBarNode);\n onShow?.();\n};\n"],
5
+ "mappings": ";AA2GA,IAAM,wBAAgD;AAAA,EACpD,0BAA0B;AAC5B;AASO,IAAM,cAAN,MAAkB;AAAA,EAKvB,YAAY,WAAmB,gBAAwB;AACrD,SAAK,YAAY;AACjB,SAAK,iBAAiB;AACtB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,MAAM,kBAAyC;AAC7C,QAAI,KAAK,iBAAiB,MAAM;AAC9B,YAAM,WAAW,MAAM,OAAO;AAAA,QAC5B,KAAK;AAAA,QACL;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,SAAS;AAAA,QACX;AAAA,MACF;AACA,WAAK,eAAe,MAAM,SAAS,KAAK;AAAA,IAC1C;AAGA,QAAI,KAAK,iBAAiB,MAAM;AAC9B,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC5D;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,MAAM,yBACJ,aACA,cACA;AACA,UAAM,eAAe,MAAM,KAAK,gBAAgB;AAChD,UAAM,MAAM,aAAa,WAAW;AACpC,QAAI,CAAC,KAAK;AACR,YAAM,IAAI,MAAM,mBAAmB,WAAW,+CAA+C;AAAA,IAC/F;AAEA,UAAM,WAAW,IAAI,SAAS;AAC9B,eAAW,SAAS,cAAc;AAChC,eAAS,OAAO,iBAAiB,MAAM,OAAO;AAAA,IAChD;AAEA,UAAM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5E,MAAI,eAAe,OAAQ,sCAAW;AACtC,QAAM,iBAAiB,mBAAmB,cAAc,oBAAoB;AAC5E,MAAI,eAAe,OAAQ,wCAAY;AAGvC,MAAI,CAAC,kCAAkC,OAAQ;AAI/C,QAAM,eAAe,aAAa,QAAQ;AAC1C,MAAI,iBAAiB,KAAM,OAAM,IAAI,MAAM,mDAAmD;AAC9F,QAAM,gBAAgB,UAAU,YAAY;AAC5C,iBAAe;AAAA,IACb;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACD,WAAS,aAAa;AACtB;AACF;",
6
6
  "names": ["cookieBarNode"]
7
7
  }
@@ -11,8 +11,9 @@
11
11
  {% if request|cookie_group_accepted:cookie_group.varname %}
12
12
  <span class="cookie-consent-accepted">{% trans "Accepted" %}</span>
13
13
  {% else %}
14
- <form class="cookie-consent-accept" action="{% url "cookie_consent_accept" cookie_group.varname %}" method="POST">
14
+ <form class="cookie-consent-accept" action="{% url "cookie_consent_accept" %}" method="post">
15
15
  {% csrf_token %}
16
+ <input type="hidden" name="cookie_groups" value="{{ cookie_group.varname }}">
16
17
  <input type="submit" value="{% trans "Accept" %}">
17
18
  </form>
18
19
  {% endif %}
@@ -20,8 +21,9 @@
20
21
  {% if request|cookie_group_declined:cookie_group.varname %}
21
22
  <span class="cookie-consent-declined">{% trans "Declined" %}</span>
22
23
  {% else %}
23
- <form class="cookie-consent-decline" action="{% url "cookie_consent_decline" cookie_group.varname %}" method="POST">
24
+ <form class="cookie-consent-decline" action="{% url "cookie_consent_decline" %}" method="post">
24
25
  {% csrf_token %}
26
+ <input type="hidden" name="cookie_groups" value="{{ cookie_group.varname }}">
25
27
  <input type="submit" value="{% trans "Decline" %}">
26
28
  </form>
27
29
  {% endif %}
@@ -1,2 +1 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-