From 8e7b0839d6f60b54d1dcd3b0a341824f529ae8a9 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 12 Apr 2023 13:21:13 +0200 Subject: [PATCH] Fix API partitions pour encodeur flask-json --- app/api/partitions.py | 2 +- app/models/groups.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/api/partitions.py b/app/api/partitions.py index b31ff80d8..1f5ef8a1e 100644 --- a/app/api/partitions.py +++ b/app/api/partitions.py @@ -92,7 +92,7 @@ def formsemestre_partitions(formsemestre_id: int): formsemestre: FormSemestre = query.first_or_404(formsemestre_id) partitions = sorted(formsemestre.partitions, key=attrgetter("numero")) return { - partition.id: partition.to_dict(with_groups=True) + str(partition.id): partition.to_dict(with_groups=True, str_keys=True) for partition in partitions if partition.partition_name is not None } diff --git a/app/models/groups.py b/app/models/groups.py index 5c9ae1688..1d07ed214 100644 --- a/app/models/groups.py +++ b/app/models/groups.py @@ -85,8 +85,10 @@ class Partition(db.Model): "Vrai s'il s'agit de la partition de parcours" return self.partition_name == scu.PARTITION_PARCOURS - def to_dict(self, with_groups=False) -> dict: - """as a dict, with or without groups""" + def to_dict(self, with_groups=False, str_keys: bool = False) -> dict: + """as a dict, with or without groups. + If str_keys, convert integer dict keys to strings (useful for JSON) + """ d = dict(self.__dict__) d["partition_id"] = self.id d.pop("_sa_instance_state", None) @@ -95,9 +97,15 @@ class Partition(db.Model): if with_groups: groups = sorted(self.groups, key=attrgetter("numero", "group_name")) # un dict et non plus une liste, pour JSON - d["groups"] = { - group.id: group.to_dict(with_partition=False) for group in groups - } + if str_keys: + d["groups"] = { + str(group.id): group.to_dict(with_partition=False) + for group in groups + } + else: + d["groups"] = { + group.id: group.to_dict(with_partition=False) for group in groups + } return d def get_etud_group(self, etudid: int) -> "GroupDescr":