Fix API partitions pour encodeur flask-json

This commit is contained in:
Emmanuel Viennet 2023-04-12 13:21:13 +02:00
parent 1c0a4f28ce
commit 8e7b0839d6
2 changed files with 14 additions and 6 deletions

View File

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

View File

@ -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,6 +97,12 @@ 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
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
}