2021-12-22 10:23:17 +01:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Gestion scolarite IUT
|
|
|
|
#
|
2023-12-31 23:04:06 +01:00
|
|
|
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
|
2021-12-22 10:23:17 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
# Emmanuel Viennet emmanuel.viennet@viennet.net
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""API: gestion des logos
|
|
|
|
Contrib @jmp
|
|
|
|
"""
|
|
|
|
|
|
|
|
from datetime import datetime
|
2023-04-06 16:10:32 +02:00
|
|
|
from flask import Response, send_file
|
|
|
|
from flask_json import as_json
|
2021-12-22 10:23:17 +01:00
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
from app.api import api_bp as bp
|
2024-07-17 12:03:08 +02:00
|
|
|
from app.api import api_permission_required as permission_required
|
|
|
|
from app.decorators import scodoc
|
2021-12-22 10:23:17 +01:00
|
|
|
from app.models import Departement
|
|
|
|
from app.scodoc.sco_logos import list_logos, find_logo
|
|
|
|
from app.scodoc.sco_permissions import Permission
|
2024-07-17 12:03:08 +02:00
|
|
|
from app.scodoc.sco_utils import json_error
|
|
|
|
|
2021-12-22 10:23:17 +01:00
|
|
|
|
2022-07-27 16:03:14 +02:00
|
|
|
# Note: l'API logos n'est accessible qu'en mode global (avec jeton, sans dept)
|
2021-12-22 10:23:17 +01:00
|
|
|
|
2022-07-27 16:03:14 +02:00
|
|
|
|
|
|
|
@bp.route("/logos")
|
|
|
|
@scodoc
|
2022-08-21 09:17:45 +02:00
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-17 15:04:51 +02:00
|
|
|
def logo_list_globals():
|
2024-07-27 13:30:02 +02:00
|
|
|
"""Liste des noms des logos définis pour le site ScoDoc.
|
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/logos
|
|
|
|
"""
|
2021-12-22 10:23:17 +01:00
|
|
|
logos = list_logos()[None]
|
2023-04-06 16:10:32 +02:00
|
|
|
return list(logos.keys())
|
2021-12-22 10:23:17 +01:00
|
|
|
|
|
|
|
|
2022-08-21 09:17:45 +02:00
|
|
|
@bp.route("/logo/<string:logoname>")
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
2022-08-21 09:17:45 +02:00
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2024-07-17 15:04:51 +02:00
|
|
|
def logo_get_global(logoname):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Renvoie le logo global de nom donné.
|
|
|
|
|
|
|
|
L'image est au format png ou jpg; le format retourné dépend du format sous lequel
|
|
|
|
l'image a été initialement enregistrée.
|
2024-07-27 13:30:02 +02:00
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/logo/B
|
2024-07-24 17:34:30 +02:00
|
|
|
"""
|
2021-12-22 10:23:17 +01:00
|
|
|
logo = find_logo(logoname=logoname)
|
|
|
|
if logo is None:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, message="logo not found")
|
2021-12-22 10:23:17 +01:00
|
|
|
logo.select()
|
|
|
|
return send_file(
|
|
|
|
logo.filepath,
|
|
|
|
mimetype=f"image/{logo.suffix}",
|
|
|
|
last_modified=datetime.now(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
def _core_get_logos(dept_id) -> list:
|
2021-12-22 10:23:17 +01:00
|
|
|
logos = list_logos().get(dept_id, dict())
|
2023-04-06 16:10:32 +02:00
|
|
|
return list(logos.keys())
|
2021-12-22 10:23:17 +01:00
|
|
|
|
|
|
|
|
2024-07-27 13:30:02 +02:00
|
|
|
@bp.route("/departement/<string:dept_acronym>/logos")
|
2022-07-27 16:03:14 +02:00
|
|
|
@scodoc
|
2022-08-21 09:17:45 +02:00
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-27 13:30:02 +02:00
|
|
|
def departement_logos(dept_acronym: str):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Liste des noms des logos définis pour le département
|
|
|
|
désigné par son acronyme.
|
2024-07-27 13:30:02 +02:00
|
|
|
|
|
|
|
SAMPLES
|
|
|
|
-------
|
|
|
|
/departement/TAPI/logos
|
2024-07-24 17:34:30 +02:00
|
|
|
"""
|
2024-07-27 13:30:02 +02:00
|
|
|
dept_id = Departement.from_acronym(dept_acronym).id
|
2023-04-06 16:10:32 +02:00
|
|
|
return _core_get_logos(dept_id)
|
2022-08-21 09:17:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/departement/id/<int:dept_id>/logos")
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2023-04-06 16:10:32 +02:00
|
|
|
@as_json
|
2024-07-27 13:30:02 +02:00
|
|
|
def departement_logos_by_id(dept_id):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Liste des noms des logos définis pour le département
|
|
|
|
désigné par son id.
|
|
|
|
"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return _core_get_logos(dept_id)
|
2022-08-21 09:17:45 +02:00
|
|
|
|
|
|
|
|
2023-04-06 16:10:32 +02:00
|
|
|
def _core_get_logo(dept_id, logoname) -> Response:
|
2021-12-22 10:23:17 +01:00
|
|
|
logo = find_logo(logoname=logoname, dept_id=dept_id)
|
|
|
|
if logo is None:
|
2022-08-07 19:56:25 +02:00
|
|
|
return json_error(404, message="logo not found")
|
2021-12-22 10:23:17 +01:00
|
|
|
logo.select()
|
|
|
|
return send_file(
|
|
|
|
logo.filepath,
|
|
|
|
mimetype=f"image/{logo.suffix}",
|
|
|
|
last_modified=datetime.now(),
|
|
|
|
)
|
2022-08-21 09:17:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/departement/<string:departement>/logo/<string:logoname>")
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2024-07-17 15:04:51 +02:00
|
|
|
def logo_get_local_dept_by_acronym(departement, logoname):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Le logo: image (format png ou jpg).
|
|
|
|
|
|
|
|
**Exemple d'utilisation:**
|
|
|
|
|
|
|
|
* `/ScoDoc/api/departement/MMI/logo/header`
|
|
|
|
"""
|
2022-08-21 09:17:45 +02:00
|
|
|
dept_id = Departement.from_acronym(departement).id
|
2023-04-06 16:10:32 +02:00
|
|
|
return _core_get_logo(dept_id, logoname)
|
2022-08-21 09:17:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/departement/id/<int:dept_id>/logo/<string:logoname>")
|
|
|
|
@scodoc
|
|
|
|
@permission_required(Permission.ScoSuperAdmin)
|
2024-07-17 15:04:51 +02:00
|
|
|
def logo_get_local_dept_by_id(dept_id, logoname):
|
2024-07-24 17:34:30 +02:00
|
|
|
"""Le logo: image (format png ou jpg).
|
|
|
|
|
|
|
|
**Exemple d'utilisation:**
|
|
|
|
|
|
|
|
* `/ScoDoc/api/departement/id/3/logo/header`
|
|
|
|
"""
|
2023-04-06 16:10:32 +02:00
|
|
|
return _core_get_logo(dept_id, logoname)
|