From 424cddc1932423b41d590f9ec9b4f7d733e90f31 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 3 Dec 2022 11:36:01 +0100 Subject: [PATCH] API: ajout champ dept_name dans /departements et /departement --- app/api/departements.py | 7 ++++--- app/models/departements.py | 15 +++++++++++++-- sco_version.py | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/api/departements.py b/app/api/departements.py index 066197d3f..d9b871b89 100644 --- a/app/api/departements.py +++ b/app/api/departements.py @@ -16,7 +16,7 @@ from flask import jsonify, request from flask_login import login_required import app -from app import db, log +from app import db from app.api import api_bp as bp from app.scodoc.sco_utils import json_error from app.decorators import scodoc, permission_required @@ -43,7 +43,7 @@ def get_departement(dept_ident: str) -> Departement: @permission_required(Permission.ScoView) def departements_list(): """Liste les départements""" - return jsonify([dept.to_dict() for dept in Departement.query]) + return jsonify([dept.to_dict(with_dept_name=True) for dept in Departement.query]) @bp.route("/departements_ids") @@ -67,13 +67,14 @@ def departement(acronym: str): { "id": 1, "acronym": "TAPI", + "dept_name" : "TEST", "description": null, "visible": true, "date_creation": "Fri, 15 Apr 2022 12:19:28 GMT" } """ dept = Departement.query.filter_by(acronym=acronym).first_or_404() - return jsonify(dept.to_dict()) + return jsonify(dept.to_dict(with_dept_name=True)) @bp.route("/departement/id/") diff --git a/app/models/departements.py b/app/models/departements.py index 6786bf9fd..8c5df10ce 100644 --- a/app/models/departements.py +++ b/app/models/departements.py @@ -2,10 +2,10 @@ """ScoDoc models : departements """ -from typing import Any from app import db from app.models import SHORT_STR_LEN +from app.models.preferences import ScoPreference from app.scodoc.sco_exceptions import ScoValueError @@ -39,7 +39,7 @@ class Departement(db.Model): def __repr__(self): return f"<{self.__class__.__name__}(id={self.id}, acronym='{self.acronym}')>" - def to_dict(self): + def to_dict(self, with_dept_name=True, with_dept_preferences=False): data = { "id": self.id, "acronym": self.acronym, @@ -47,6 +47,17 @@ class Departement(db.Model): "visible": self.visible, "date_creation": self.date_creation, } + if with_dept_name: + pref = ScoPreference.query.filter_by( + dept_id=self.id, name="DeptName" + ).first() + data["dept_name"] = pref.value if pref else None + # Ceci n'est pas encore utilisé, mais pourrait être publié + # par l'API après nettoyage des préférences. + if with_dept_preferences: + data["preferences"] = { + p.name: p.value for p in ScoPreference.query.filter_by(dept_id=self.id) + } return data @classmethod diff --git a/sco_version.py b/sco_version.py index d3d81902f..43e017b61 100644 --- a/sco_version.py +++ b/sco_version.py @@ -1,7 +1,7 @@ # -*- mode: python -*- # -*- coding: utf-8 -*- -SCOVERSION = "9.4.7" +SCOVERSION = "9.4.8" SCONAME = "ScoDoc"