API: ajout champ dept_name dans /departements et /departement

This commit is contained in:
Emmanuel Viennet 2022-12-03 11:36:01 +01:00
parent 5001fbf4c8
commit 424cddc193
3 changed files with 18 additions and 6 deletions

View File

@ -16,7 +16,7 @@ from flask import jsonify, request
from flask_login import login_required from flask_login import login_required
import app import app
from app import db, log from app import db
from app.api import api_bp as bp from app.api import api_bp as bp
from app.scodoc.sco_utils import json_error from app.scodoc.sco_utils import json_error
from app.decorators import scodoc, permission_required from app.decorators import scodoc, permission_required
@ -43,7 +43,7 @@ def get_departement(dept_ident: str) -> Departement:
@permission_required(Permission.ScoView) @permission_required(Permission.ScoView)
def departements_list(): def departements_list():
"""Liste les départements""" """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") @bp.route("/departements_ids")
@ -67,13 +67,14 @@ def departement(acronym: str):
{ {
"id": 1, "id": 1,
"acronym": "TAPI", "acronym": "TAPI",
"dept_name" : "TEST",
"description": null, "description": null,
"visible": true, "visible": true,
"date_creation": "Fri, 15 Apr 2022 12:19:28 GMT" "date_creation": "Fri, 15 Apr 2022 12:19:28 GMT"
} }
""" """
dept = Departement.query.filter_by(acronym=acronym).first_or_404() 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/<int:dept_id>") @bp.route("/departement/id/<int:dept_id>")

View File

@ -2,10 +2,10 @@
"""ScoDoc models : departements """ScoDoc models : departements
""" """
from typing import Any
from app import db from app import db
from app.models import SHORT_STR_LEN from app.models import SHORT_STR_LEN
from app.models.preferences import ScoPreference
from app.scodoc.sco_exceptions import ScoValueError from app.scodoc.sco_exceptions import ScoValueError
@ -39,7 +39,7 @@ class Departement(db.Model):
def __repr__(self): def __repr__(self):
return f"<{self.__class__.__name__}(id={self.id}, acronym='{self.acronym}')>" 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 = { data = {
"id": self.id, "id": self.id,
"acronym": self.acronym, "acronym": self.acronym,
@ -47,6 +47,17 @@ class Departement(db.Model):
"visible": self.visible, "visible": self.visible,
"date_creation": self.date_creation, "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 return data
@classmethod @classmethod

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
SCOVERSION = "9.4.7" SCOVERSION = "9.4.8"
SCONAME = "ScoDoc" SCONAME = "ScoDoc"