Skip to content

Commit 4321263

Browse files
🚨 Enable the pyupgrade rules
Automatic upgrade for the modern type annotations. Some manual fixes were required for the string interpolation. Along the way, the varname for an individual cookie is deprecated. A code search in Github revealed that the accepted_cookies template tag does not appear to be used in any public projects. Deprecating it should be safe - I also can't really imagine how/why you'd use this tag as it's documented and more common to check whether an entire cookie group is accepted.
1 parent e7bafcc commit 4321263

File tree

18 files changed

+17
-28
lines changed

18 files changed

+17
-28
lines changed

cookie_consent/admin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from django.contrib import admin
32

43
from .conf import settings

cookie_consent/cache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from django.core.cache import caches
32

43
from .conf import settings

cookie_consent/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from django.conf import settings # NOQA
32

43
from appconf import AppConf

cookie_consent/middleware.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
# -*- coding: utf-8 -*-
2-
from typing import Optional
3-
41
from .cache import all_cookie_groups
52
from .conf import settings
63
from .util import get_cookie_dict_from_request, is_cookie_consent_enabled
74

85

9-
def _should_delete_cookie(group_version: Optional[str]) -> bool:
6+
def _should_delete_cookie(group_version: str | None) -> bool:
107
# declined after it was accepted (and set) before
118
if group_version == settings.COOKIE_CONSENT_DECLINE:
129
return True

cookie_consent/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import re
32
from typing import TypedDict
43

@@ -144,7 +143,7 @@ class Meta:
144143
ordering = ["-created"]
145144

146145
def __str__(self):
147-
return "%s %s%s" % (self.name, self.domain, self.path)
146+
return f"{self.name} {self.domain}{self.path}"
148147

149148
@clear_cache_after
150149
def save(self, *args, **kwargs):
@@ -161,7 +160,8 @@ def natural_key(self):
161160

162161
@property
163162
def varname(self):
164-
return "%s=%s:%s" % (self.cookiegroup.varname, self.name, self.domain)
163+
group_varname = self.cookiegroup.varname
164+
return f"{group_varname}={self.name}:{self.domain}"
165165

166166
def get_version(self):
167167
return self.created.isoformat()
@@ -191,4 +191,4 @@ class Meta:
191191
ordering = ["-created"]
192192

193193
def __str__(self):
194-
return "%s %s" % (self.cookiegroup.name, self.version)
194+
return f"{self.cookiegroup.name} {self.version}"
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-

cookie_consent/templatetags/cookie_consent_tags.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ def accepted_cookies(request):
166166
{{ request|accepted_cookies }}
167167
168168
"""
169+
warnings.warn(
170+
"The 'accepted_cookies' template filter is deprecated and will be removed"
171+
"in django-cookie-consent 1.0.",
172+
DeprecationWarning,
173+
stacklevel=1,
174+
)
169175
return [c.varname for c in get_accepted_cookies(request)]
170176

171177

cookie_consent/urls.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from django.urls import path, re_path
32
from django.views.decorators.csrf import csrf_exempt
43

cookie_consent/util.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# -*- coding: utf-8 -*-
21
import datetime
32
import logging
4-
from typing import Dict, Union
53

64
from .cache import all_cookie_groups, get_cookie, get_cookie_group
75
from .conf import settings
@@ -13,7 +11,7 @@
1311
KEY_VALUE_SEP = "="
1412

1513

16-
def parse_cookie_str(cookie: str) -> Dict[str, str]:
14+
def parse_cookie_str(cookie: str) -> dict[str, str]:
1715
if not cookie:
1816
return {}
1917

@@ -171,7 +169,7 @@ def are_all_cookies_accepted(request):
171169
)
172170

173171

174-
def _get_cookie_groups_by_state(request, state: Union[bool, None]):
172+
def _get_cookie_groups_by_state(request, state: bool | None):
175173
return [
176174
cookie_group
177175
for cookie_group in get_cookie_groups()
@@ -219,7 +217,7 @@ def get_cookie_string(cookie_dic):
219217
expires = datetime.datetime.now() + datetime.timedelta(
220218
seconds=settings.COOKIE_CONSENT_MAX_AGE
221219
)
222-
cookie_str = "%s=%s; expires=%s; path=/" % (
220+
cookie_str = "{}={}; expires={}; path=/".format(
223221
settings.COOKIE_CONSENT_NAME,
224222
dict_to_cookie_str(cookie_dic),
225223
expires.strftime("%a, %d %b %Y %H:%M:%S GMT"),

cookie_consent/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from django.contrib.auth.views import RedirectURLMixin
32
from django.core.exceptions import SuspiciousOperation
43
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse

0 commit comments

Comments
 (0)