From 39118b985d4c4aa49d0419cc147f61dba91255e4 Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Sun, 1 Dec 2024 21:00:50 -0500 Subject: [PATCH] Support Django 5.0 Django 5.0 dropped support for serializing sessions with PickleSerializer. brutaldon depended on this because the default JSON serializer does not support datetime objects. This commit adds a simple wrapper using the datetime and decimal handler in django.core.serializers.DjangoJSONEncoder. Brutaldon also saved user information in the session. Mastodon.py returns API data as dictionaries wrapped by a class that adds read-only attributes for every key, to allow dotted access, and brutaldon saved some of these directly to the session. A trip through the JSONSerializer turns them back into normal dictionaries. For compatibility, this commit pulls in the python-box package as a dependency to restore dotted access as used in views.py. --- Pipfile | 3 ++- brutaldon/serializers.py | 14 ++++++++++++++ brutaldon/settings.py | 3 +-- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 brutaldon/serializers.py diff --git a/Pipfile b/Pipfile index 61dd29d..bb81388 100644 --- a/Pipfile +++ b/Pipfile @@ -21,10 +21,11 @@ requests = "*" six = "*" "urllib3" = "*" webencodings = "*" -Django = "~=4.2" django-html_sanitizer = "*" inscriptis = "*" lxml = "*" +Django = "~=5.0" +python-box = {extras = ["all"], version = "~=7.0"} [dev-packages] python-lsp-server = {extras = ["pyflakes", "rope", "yapf"], version = "*"} diff --git a/brutaldon/serializers.py b/brutaldon/serializers.py new file mode 100644 index 0000000..2ff6886 --- /dev/null +++ b/brutaldon/serializers.py @@ -0,0 +1,14 @@ +import json +from django.core.serializers.json import DjangoJSONEncoder +from box import Box + +class JSONSerializer(): + """ + A wrapper around json that can handle date/time, decimal + types, and UUIDs using defaults from DjangoJSONEncoder. + """ + def dumps(self, obj): + return json.dumps(obj, separators=(",", ":"), cls=DjangoJSONEncoder).encode("latin-1") + + def loads(self, data): + return Box(json.loads(data.decode("latin-1"))) diff --git a/brutaldon/settings.py b/brutaldon/settings.py index c201001..1ea785b 100644 --- a/brutaldon/settings.py +++ b/brutaldon/settings.py @@ -202,8 +202,7 @@ SANITIZER_ALLOWED_ATTRIBUTES = ["href", "src", "title", "alt", "class", "lang", FILE_UPLOAD_HANDLERS = ["django.core.files.uploadhandler.TemporaryFileUploadHandler"] # Session serialization -# Important: whatever you choose has to be able to serialize DateTime, so not JSON. -SESSION_SERIALIZER = "django.contrib.sessions.serializers.PickleSerializer" +SESSION_SERIALIZER = "brutaldon.serializers.JSONSerializer" # URL to redirect users to when not logged in ANONYMOUS_HOME_URL = "about"