chore: use Django JSON serializer when calling json.dumps

This commit is contained in:
Alexis Métaireau 2024-02-22 11:52:29 +01:00
parent c46f59e3dd
commit c5fd72fe2b
4 changed files with 10 additions and 8 deletions

View file

@ -1,6 +1,7 @@
import json import json
import six import six
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models from django.db import models
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
@ -14,7 +15,7 @@ class DictField(models.TextField):
if not value: if not value:
value = {} value = {}
if not isinstance(value, six.string_types): if not isinstance(value, six.string_types):
value = json.dumps(value) value = json.dumps(value, cls=DjangoJSONEncoder)
return value return value
def from_db_value(self, value, expression, connection): def from_db_value(self, value, expression, connection):

View file

@ -111,7 +111,7 @@ def test_update(client, datalayer, map, post_data):
# Test response is a json # Test response is a json
j = json.loads(response.content.decode()) j = json.loads(response.content.decode())
assert "id" in j assert "id" in j
assert datalayer.pk == j["id"] assert str(datalayer.pk) == j["id"]
assert j["browsable"] is True assert j["browsable"] is True
assert Path(modified_datalayer.geojson.path).exists() assert Path(modified_datalayer.geojson.path).exists()

View file

@ -85,7 +85,7 @@ i18n_urls += decorated_patterns(
name="datalayer_versions", name="datalayer_versions",
), ),
re_path( re_path(
r"^datalayer/(?P<map_id>\d+)/(?P<pk>" + uuid + r" )/(?P<name>[_\w]+.geojson)$", r"^datalayer/(?P<map_id>\d+)/(?P<pk>" + uuid + r")/(?P<name>" + uuid + r"[_\w]+.geojson)$",
views.DataLayerVersion.as_view(), views.DataLayerVersion.as_view(),
name="datalayer_version", name="datalayer_version",
), ),

View file

@ -23,6 +23,7 @@ from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail from django.core.mail import send_mail
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.core.serializers.json import DjangoJSONEncoder
from django.core.signing import BadSignature, Signer from django.core.signing import BadSignature, Signer
from django.core.validators import URLValidator, ValidationError from django.core.validators import URLValidator, ValidationError
from django.http import ( from django.http import (
@ -314,7 +315,7 @@ class UserDownload(DetailView, SearchMixin):
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file: with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
for map_ in self.get_maps(): for map_ in self.get_maps():
umapjson = map_.generate_umapjson(self.request) umapjson = map_.generate_umapjson(self.request)
geojson_file = io.StringIO(json.dumps(umapjson)) geojson_file = io.StringIO(json.dumps(umapjson, cls=DjangoJSONEncoder))
file_name = f"umap_backup_{map_.slug}_{map_.pk}.umap" file_name = f"umap_backup_{map_.slug}_{map_.pk}.umap"
zip_file.writestr(file_name, geojson_file.getvalue()) zip_file.writestr(file_name, geojson_file.getvalue())
@ -353,7 +354,7 @@ class MapsShowCase(View):
} }
geojson = {"type": "FeatureCollection", "features": [make(m) for m in maps]} geojson = {"type": "FeatureCollection", "features": [make(m) for m in maps]}
return HttpResponse(smart_bytes(json.dumps(geojson))) return HttpResponse(smart_bytes(json.dumps(geojson, cls=DjangoJSONEncoder)))
showcase = MapsShowCase.as_view() showcase = MapsShowCase.as_view()
@ -440,7 +441,7 @@ ajax_proxy = AjaxProxy.as_view()
def simple_json_response(**kwargs): def simple_json_response(**kwargs):
return HttpResponse(json.dumps(kwargs), content_type="application/json") return HttpResponse(json.dumps(kwargs, cls=DjangoJSONEncoder), content_type="application/json")
# ############## # # ############## #
@ -536,7 +537,7 @@ class MapDetailMixin:
geojson["properties"] = {} geojson["properties"] = {}
geojson["properties"].update(properties) geojson["properties"].update(properties)
geojson["properties"]["datalayers"] = self.get_datalayers() geojson["properties"]["datalayers"] = self.get_datalayers()
context["map_settings"] = json.dumps(geojson, indent=settings.DEBUG) context["map_settings"] = json.dumps(geojson, indent=settings.DEBUG, cls=DjangoJSONEncoder)
self.set_preconnect(geojson["properties"], context) self.set_preconnect(geojson["properties"], context)
return context return context
@ -1100,7 +1101,7 @@ class DataLayerUpdate(FormLessEditMixin, GZipMixin, UpdateView):
# Replace the uploaded file by the merged version. # Replace the uploaded file by the merged version.
self.request.FILES["geojson"].file = BytesIO( self.request.FILES["geojson"].file = BytesIO(
json.dumps(merged).encode("utf-8") json.dumps(merged, cls=DjangoJSONEncoder).encode("utf-8")
) )
# Mark the data to be reloaded by form_valid # Mark the data to be reloaded by form_valid