From a54da7ac0b8988674542e850259ea9b632b7b48f Mon Sep 17 00:00:00 2001 From: Felix Blanke <info@fblanke.de> Date: Mon, 10 Feb 2025 17:03:00 +0100 Subject: [PATCH] Change funcs to export json dict instead of str --- AKModel/models.py | 10 +++++----- .../templates/admin/AKModel/ak_json_export.html | 13 +------------ AKModel/views/ak.py | 14 +++++++++----- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/AKModel/models.py b/AKModel/models.py index 7c1a920b..4b3a3ceb 100644 --- a/AKModel/models.py +++ b/AKModel/models.py @@ -4,7 +4,7 @@ import json import math from dataclasses import dataclass from datetime import datetime, timedelta -from typing import Iterable, Generator +from typing import Any, Iterable, Generator from django.db import models, transaction from django.apps import apps @@ -852,7 +852,7 @@ class Room(models.Model): def __str__(self): return self.title - def as_json(self) -> str: + def as_json_dict(self) -> dict[str, Any]: """Return a json string representation of this room object. :return: The json string representation is constructed @@ -887,7 +887,7 @@ class Room(models.Model): if not any(constr.startswith("proxy") for constr in data["fulfilled_room_constraints"]): data["fulfilled_room_constraints"].append("no-proxy") - return json.dumps(data) + return data class AKSlot(models.Model): @@ -984,7 +984,7 @@ class AKSlot(models.Model): self.duration = min(self.duration, event_duration_hours) super().save(force_insert, force_update, using, update_fields) - def as_json(self) -> str: + def as_json_dict(self) -> dict[str, Any]: """Return a json string representation of the AK object of this slot. :return: The json string representation is constructed @@ -1055,7 +1055,7 @@ class AKSlot(models.Model): if not any(constr.startswith("proxy") for constr in data["room_constraints"]): data["room_constraints"].append("no-proxy") - return json.dumps(data) + return data class AKOrgaMessage(models.Model): """ diff --git a/AKModel/templates/admin/AKModel/ak_json_export.html b/AKModel/templates/admin/AKModel/ak_json_export.html index 38e5526e..7ae242ca 100644 --- a/AKModel/templates/admin/AKModel/ak_json_export.html +++ b/AKModel/templates/admin/AKModel/ak_json_export.html @@ -4,17 +4,6 @@ {% block content %} <pre> - {"aks": [ - {% for slot in slots %}{{ slot.as_json }}{% if not forloop.last %}, - {% endif %}{% endfor %} - ], - "rooms": [ - {% for room in rooms %}{{ room.as_json }}{% if not forloop.last %}, - {% endif %}{% endfor %} - ], - "participants": {{ participants }}, - "timeslots": {{ timeslots }}, - "info": {{ info_dict }} - } +{{ json_data }} </pre> {% endblock %} diff --git a/AKModel/views/ak.py b/AKModel/views/ak.py index 40a6e102..32d840fb 100644 --- a/AKModel/views/ak.py +++ b/AKModel/views/ak.py @@ -86,10 +86,10 @@ class AKJSONExportView(AdminViewMixin, FilterByEventSlugMixin, ListView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["participants"] = json.dumps([]) + data = {} rooms = Room.objects.filter(event=self.event) - context["rooms"] = rooms + data["rooms"] = [r.as_json_dict() for r in rooms] timeslots = { "info": {"duration": float(self.event.export_slot)}, @@ -179,17 +179,21 @@ class AKJSONExportView(AdminViewMixin, FilterByEventSlugMixin, ListView): timeslots["info"]["blocknames"] = block_names - context["timeslots"] = json.dumps(timeslots) - info_dict = { "title": self.event.name, "slug": self.event.slug } + for attr in ["contact_email", "place"]: if hasattr(self.event, attr) and getattr(self.event, attr): info_dict[attr] = getattr(self.event, attr) - context["info_dict"] = json.dumps(info_dict) + data["timeslots"] = timeslots + data["info"] = info_dict + data["participants"] = [] + data["aks"] = [ak.as_json_dict() for ak in context["slots"]] + + context["json_data"] = json.dumps(data, indent=2) return context -- GitLab