From 12f646547e6009585bdbb1fd1d6dfbec816424be Mon Sep 17 00:00:00 2001 From: Jean-Marie PLACE Date: Tue, 21 Dec 2021 18:49:33 +0100 Subject: [PATCH 1/2] api list logos --- app/api/sco_api.py | 81 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/app/api/sco_api.py b/app/api/sco_api.py index 969753d1..dd08c744 100644 --- a/app/api/sco_api.py +++ b/app/api/sco_api.py @@ -38,18 +38,18 @@ # Scolarite/Notes/groups_view # Scolarite/Notes/moduleimpl_status # Scolarite/setGroups +from datetime import datetime -from flask import jsonify, request, url_for, abort, g -from flask_login import current_user +from flask import jsonify, request, g, send_file from sqlalchemy.sql import func from app import db, log from app.api import bp from app.api.auth import token_auth -from app.api.errors import bad_request, error_response -from app.decorators import permission_required +from app.api.errors import error_response from app import models from app.models import FormSemestre, FormSemestreInscription, Identite +from app.scodoc.sco_logos import list_logos, find_logo from app.scodoc.sco_permissions import Permission @@ -79,3 +79,76 @@ def etudiants(): FormSemestre.date_fin >= func.now(), ) return jsonify([e.to_dict_bul(include_urls=False) for e in query]) + + +def format_required(default_format="json", allowed_formats=None): + """Extract required format from a request. + * default value is json. a list of allowed formats may be provided + (['json'] considered if not provided). + * if the required format is not in allowed list, returns None. + NB: if json in not in allowed_formats, format specification is mandatory.""" + format_type = request.args.get("format", default_format) + if format_type in (allowed_formats or ["json"]): + return format_type + return None + + +def get_dept_id(dept_name=None): + dept = models.Departement.query.filter_by(acronym=dept_name).first_or_404() + return dept.id + + +@bp.route("/logos", methods=["GET"]) +@token_auth.login_required +def api_get_glob_logos(): + if not g.current_user.has_permission(Permission.ScoSuperAdmin, None): + return error_response(401, message="accès interdit") + required_format = format_required() # json only + if required_format is None: + return error_response(400, "Illegal format") + logos = list_logos()[None] + return jsonify(list(logos.keys())) + + +@bp.route("/logos/", methods=["GET"]) +@token_auth.login_required +def api_get_glob_logo(logoname): + if not g.current_user.has_permission(Permission.ScoSuperAdmin, None): + return error_response(401, message="accès interdit") + logo = find_logo(logoname=logoname) + if logo is None: + return error_response(404, message="logo not found") + logo.select() + return send_file( + logo.filepath, + mimetype=f"image/{logo.suffix}", + last_modified=datetime.now(), + ) + + +@bp.route("/departements//logos", methods=["GET"]) +@token_auth.login_required +def api_get_local_logos(departement): + dept_id = get_dept_id(departement) + if not g.current_user.has_permission(Permission.ScoChangePreferences, departement): + return error_response(401, message="accès interdit") + logos = list_logos().get(dept_id, dict()) + return jsonify(list(logos.keys())) + + +@bp.route("/departements//logos/", methods=["GET"]) +@token_auth.login_required +def api_get_local_logo(departement, logoname): + # format = format_required("jpg", ['png', 'jpg']) + dept_id = get_dept_id(departement) + if not g.current_user.has_permission(Permission.ScoChangePreferences, departement): + return error_response(401, message="accès interdit") + logo = find_logo(logoname=logoname, dept_id=dept_id) + if logo is None: + return error_response(404, message="logo not found") + logo.select() + return send_file( + logo.filepath, + mimetype=f"image/{logo.suffix}", + last_modified=datetime.now(), + ) From 8db9a027cbdfcc60cd2dc5799e3ce0e2d70ae85c Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 22 Dec 2021 00:35:58 +0100 Subject: [PATCH 2/2] API logos / reorganise code --- app/api/__init__.py | 19 +++++++++- app/api/sco_api.py | 76 +------------------------------------- app/models/departements.py | 5 +++ 3 files changed, 24 insertions(+), 76 deletions(-) diff --git a/app/api/__init__.py b/app/api/__init__.py index 956c1b46..d397a93d 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -2,8 +2,25 @@ """ from flask import Blueprint +from flask import request bp = Blueprint("api", __name__) -from app.api import sco_api + +def requested_format(default_format="json", allowed_formats=None): + """Extract required format from query string. + * default value is json. A list of allowed formats may be provided + (['json'] considered if not provided). + * if the required format is not in allowed list, returns None. + + NB: if json in not in allowed_formats, format specification is mandatory. + """ + format_type = request.args.get("format", default_format) + if format_type in (allowed_formats or ["json"]): + return format_type + return None + + from app.api import tokens +from app.api import sco_api +from app.api import logos diff --git a/app/api/sco_api.py b/app/api/sco_api.py index dd08c744..6aa488c2 100644 --- a/app/api/sco_api.py +++ b/app/api/sco_api.py @@ -44,12 +44,11 @@ from flask import jsonify, request, g, send_file from sqlalchemy.sql import func from app import db, log -from app.api import bp +from app.api import bp, requested_format from app.api.auth import token_auth from app.api.errors import error_response from app import models from app.models import FormSemestre, FormSemestreInscription, Identite -from app.scodoc.sco_logos import list_logos, find_logo from app.scodoc.sco_permissions import Permission @@ -79,76 +78,3 @@ def etudiants(): FormSemestre.date_fin >= func.now(), ) return jsonify([e.to_dict_bul(include_urls=False) for e in query]) - - -def format_required(default_format="json", allowed_formats=None): - """Extract required format from a request. - * default value is json. a list of allowed formats may be provided - (['json'] considered if not provided). - * if the required format is not in allowed list, returns None. - NB: if json in not in allowed_formats, format specification is mandatory.""" - format_type = request.args.get("format", default_format) - if format_type in (allowed_formats or ["json"]): - return format_type - return None - - -def get_dept_id(dept_name=None): - dept = models.Departement.query.filter_by(acronym=dept_name).first_or_404() - return dept.id - - -@bp.route("/logos", methods=["GET"]) -@token_auth.login_required -def api_get_glob_logos(): - if not g.current_user.has_permission(Permission.ScoSuperAdmin, None): - return error_response(401, message="accès interdit") - required_format = format_required() # json only - if required_format is None: - return error_response(400, "Illegal format") - logos = list_logos()[None] - return jsonify(list(logos.keys())) - - -@bp.route("/logos/", methods=["GET"]) -@token_auth.login_required -def api_get_glob_logo(logoname): - if not g.current_user.has_permission(Permission.ScoSuperAdmin, None): - return error_response(401, message="accès interdit") - logo = find_logo(logoname=logoname) - if logo is None: - return error_response(404, message="logo not found") - logo.select() - return send_file( - logo.filepath, - mimetype=f"image/{logo.suffix}", - last_modified=datetime.now(), - ) - - -@bp.route("/departements//logos", methods=["GET"]) -@token_auth.login_required -def api_get_local_logos(departement): - dept_id = get_dept_id(departement) - if not g.current_user.has_permission(Permission.ScoChangePreferences, departement): - return error_response(401, message="accès interdit") - logos = list_logos().get(dept_id, dict()) - return jsonify(list(logos.keys())) - - -@bp.route("/departements//logos/", methods=["GET"]) -@token_auth.login_required -def api_get_local_logo(departement, logoname): - # format = format_required("jpg", ['png', 'jpg']) - dept_id = get_dept_id(departement) - if not g.current_user.has_permission(Permission.ScoChangePreferences, departement): - return error_response(401, message="accès interdit") - logo = find_logo(logoname=logoname, dept_id=dept_id) - if logo is None: - return error_response(404, message="logo not found") - logo.select() - return send_file( - logo.filepath, - mimetype=f"image/{logo.suffix}", - last_modified=datetime.now(), - ) diff --git a/app/models/departements.py b/app/models/departements.py index aa9c1006..95167383 100644 --- a/app/models/departements.py +++ b/app/models/departements.py @@ -42,3 +42,8 @@ class Departement(db.Model): "date_creation": self.date_creation, } return data + + @classmethod + def from_acronym(cls, acronym): + dept = cls.query.filter_by(acronym=acronym).first_or_404() + return dept