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.
This commit is contained in:
Jason McBrayer 2024-12-01 21:00:50 -05:00
parent 8cf6be956e
commit 39118b985d
3 changed files with 17 additions and 3 deletions

View File

@ -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 = "*"}

14
brutaldon/serializers.py Normal file
View File

@ -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")))

View File

@ -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"