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) formsemestre: FormSemestre = query.first_or_404(formsemestre_id)
partitions = sorted(formsemestre.partitions, key=attrgetter("numero")) partitions = sorted(formsemestre.partitions, key=attrgetter("numero"))
return { 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 for partition in partitions
if partition.partition_name is not None 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" "Vrai s'il s'agit de la partition de parcours"
return self.partition_name == scu.PARTITION_PARCOURS return self.partition_name == scu.PARTITION_PARCOURS
def to_dict(self, with_groups=False) -> dict: def to_dict(self, with_groups=False, str_keys: bool = False) -> dict:
"""as a dict, with or without groups""" """as a dict, with or without groups.
If str_keys, convert integer dict keys to strings (useful for JSON)
"""
d = dict(self.__dict__) d = dict(self.__dict__)
d["partition_id"] = self.id d["partition_id"] = self.id
d.pop("_sa_instance_state", None) d.pop("_sa_instance_state", None)
@ -95,9 +97,15 @@ class Partition(db.Model):
if with_groups: if with_groups:
groups = sorted(self.groups, key=attrgetter("numero", "group_name")) groups = sorted(self.groups, key=attrgetter("numero", "group_name"))
# un dict et non plus une liste, pour JSON # un dict et non plus une liste, pour JSON
d["groups"] = { if str_keys:
group.id: group.to_dict(with_partition=False) for group in groups 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 return d
def get_etud_group(self, etudid: int) -> "GroupDescr": def get_etud_group(self, etudid: int) -> "GroupDescr":