MonScoDocEssai/app/api/logos.py

115 lines
3.5 KiB
Python
Raw Normal View History

2021-12-22 10:23:17 +01:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2022-01-01 14:49:42 +01:00
# Copyright (c) 1999 - 2022 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
from flask import jsonify, g, send_file
from flask_login import login_required
2021-12-22 10:23:17 +01:00
from app.api import api_bp as bp, api_web_bp
2021-12-22 10:23:17 +01:00
from app.api import requested_format
from app.scodoc.sco_utils import json_error
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.decorators import scodoc, permission_required
2021-12-22 10:23:17 +01:00
from app.scodoc.sco_permissions import Permission
# Note: l'API logos n'est accessible qu'en mode global (avec jeton, sans dept)
2021-12-22 10:23:17 +01:00
@bp.route("/logos")
@scodoc
@permission_required(Permission.ScoSuperAdmin)
2021-12-22 10:23:17 +01:00
def api_get_glob_logos():
logos = list_logos()[None]
return jsonify(list(logos.keys()))
@bp.route("/logo/<string:logoname>")
@scodoc
@permission_required(Permission.ScoSuperAdmin)
2021-12-22 10:23:17 +01:00
def api_get_glob_logo(logoname):
logo = find_logo(logoname=logoname)
if logo is None:
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(),
)
def core_get_logos(dept_id):
2021-12-22 10:23:17 +01:00
logos = list_logos().get(dept_id, dict())
return jsonify(list(logos.keys()))
@bp.route("/departement/<string:departement>/logos")
@scodoc
@permission_required(Permission.ScoSuperAdmin)
def api_get_local_logos_by_acronym(departement):
2021-12-22 10:23:17 +01:00
dept_id = Departement.from_acronym(departement).id
return core_get_logos(dept_id)
@bp.route("/departement/id/<int:dept_id>/logos")
@scodoc
@permission_required(Permission.ScoSuperAdmin)
def api_get_local_logos_by_id(dept_id):
return core_get_logos(dept_id)
def core_get_logo(dept_id, logoname):
2021-12-22 10:23:17 +01:00
logo = find_logo(logoname=logoname, dept_id=dept_id)
if logo is None:
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(),
)
@bp.route("/departement/<string:departement>/logo/<string:logoname>")
@scodoc
@permission_required(Permission.ScoSuperAdmin)
def api_get_local_logo_dept_by_acronym(departement, logoname):
dept_id = Departement.from_acronym(departement).id
return core_get_logo(dept_id, logoname)
@bp.route("/departement/id/<int:dept_id>/logo/<string:logoname>")
@scodoc
@permission_required(Permission.ScoSuperAdmin)
def api_get_local_logo_dept_by_id(dept_id, logoname):
return core_get_logo(dept_id, logoname)