diff --git a/app/api/partitions.py b/app/api/partitions.py index b31ff80d..1f5ef8a1 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 5c9ae168..1d07ed21 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":