TP_JO2024/medaille/app.py

122 lines
4.3 KiB
Python
Raw Normal View History

2024-03-27 14:17:05 +01:00
from flask import Flask, jsonify, request
from pathlib import Path
import json
2024-03-27 15:20:10 +01:00
from models import ListeMedaille, Medaille
2024-03-27 14:17:05 +01:00
from flask_swagger_ui import get_swaggerui_blueprint
import os
app = Flask(__name__)
app.config['MEDAILLE_FILE'] = os.getenv('MEDAILLE_FILE', Path(__file__).parent.parent / 'data' / 'medailles.json')
@app.route('/ping', methods=["GET"])
def ping():
return jsonify({"message": "pong"}), 200
@app.route('/', methods=["GET"])
2024-03-27 15:20:10 +01:00
def listeMedaille():
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
Renvoie la liste des médilles
2024-03-27 14:17:05 +01:00
"""
# Offset / Limit
offset = request.args.get('offset', 0)
limit = request.args.get('limit', 10)
2024-03-27 15:20:10 +01:00
listeMedailles = ListeMedaille()
listeMedailles.loadFromJson(app.config['MEDAILLE_FILE'])
2024-03-27 14:17:05 +01:00
if limit != 0:
2024-03-27 15:20:10 +01:00
listeMedailles.root = listeMedailles.root[int(offset):int(offset)+int(limit)]
2024-03-27 14:17:05 +01:00
else:
2024-03-27 15:20:10 +01:00
listeMedailles.root = listeMedailles.root[int(offset):]
2024-03-27 14:17:05 +01:00
2024-03-27 15:20:10 +01:00
return jsonify(listeMedailles.model_dump()), 200
2024-03-27 14:17:05 +01:00
@app.route('/<int:id>', methods=["GET"])
2024-03-27 15:20:10 +01:00
def getMedaille(id: int):
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
Renvoie un médille par son id
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
listeMedailles = ListeMedaille()
listeMedailles.loadFromJson(app.config['MEDAILLE_FILE'])
for medaille in listeMedailles.root:
if medaille.id == id:
return jsonify(medaille.model_dump()), 200
return jsonify({"message": "Medaille introuvable"}), 404
2024-03-27 14:17:05 +01:00
@app.route('/<int:id>', methods=["DELETE"])
2024-03-27 15:20:10 +01:00
def deleteMedaille(id: int):
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
Supprime un médille par son id
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
listeMedailles = ListeMedaille()
listeMedailles.loadFromJson(app.config['MEDAILLE_FILE'])
for medaille in listeMedailles.root:
if medaille.id == id:
listeMedailles.root.remove(medaille)
2024-03-27 14:17:05 +01:00
with open(app.config['MEDAILLE_FILE'], 'w') as f:
2024-03-27 15:20:10 +01:00
json.dump(listeMedailles.model_dump(), f, indent=4)
return jsonify({"message": "Medaille supprimé"}), 200
return jsonify({"message": "Medaille introuvable"}), 404
2024-03-27 14:17:05 +01:00
@app.route('/<int:id>', methods=["PUT"])
2024-03-27 15:20:10 +01:00
def updateMedaille(id: int):
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
Met à jour un médille par son id
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
listeMedailles = ListeMedaille()
listeMedailles.loadFromJson(app.config['MEDAILLE_FILE'])
for medaille in listeMedailles.root:
if medaille.id == id:
2024-03-27 14:17:05 +01:00
data = json.loads(request.data)
for key, value in data.items():
2024-03-27 15:20:10 +01:00
setattr(medaille, key, value)
2024-03-27 14:17:05 +01:00
with open(app.config['MEDAILLE_FILE'], 'w') as f:
2024-03-27 15:20:10 +01:00
json.dump(listeMedailles.model_dump(), f, indent=4)
return jsonify({"message": "Medaille mis à jour"}), 200
return jsonify({"message": "Medaille introuvable"}), 404
2024-03-27 14:17:05 +01:00
@app.route('/<int:id>', methods=["PATCH"])
2024-03-27 15:20:10 +01:00
def patchMedaille(id: int):
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
Met à jour un médille par son id
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
listeMedailles = ListeMedaille()
listeMedailles.loadFromJson(app.config['MEDAILLE_FILE'])
for medaille in listeMedailles.root:
if medaille.id == id:
2024-03-27 14:17:05 +01:00
data = json.loads(request.data)
2024-03-27 15:20:10 +01:00
data["id"] = medaille.id # On ne peut pas changer l'id
2024-03-27 14:17:05 +01:00
for key, value in data.items():
2024-03-27 15:20:10 +01:00
if hasattr(medaille, key):
setattr(medaille, key, value)
if not medaille.validate():
return jsonify({"message": "Données invalides"}), 400
2024-03-27 14:17:05 +01:00
with open(app.config['MEDAILLE_FILE'], 'w') as f:
2024-03-27 15:20:10 +01:00
json.dump(listeMedailles.model_dump(), f, indent=4)
return jsonify({"message": "Medaille mis à jour"}), 200
return jsonify({"message": "Medaille introuvable"}), 404
2024-03-27 14:17:05 +01:00
@app.route('/', methods=["POST"])
2024-03-27 15:20:10 +01:00
def addMedaille():
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
Ajoute un médille
2024-03-27 14:17:05 +01:00
"""
2024-03-27 15:20:10 +01:00
listeMedailles = ListeMedaille()
listeMedailles.loadFromJson(app.config['MEDAILLE_FILE'])
medaille = Medaille(**json.loads(request.data))
if not medaille.validate():
return jsonify({"message": "Données invalides"}), 400
medaille.id = max([medaille.id for medaille in listeMedailles.root]) + 1
listeMedailles.root.append(medaille)
2024-03-27 14:17:05 +01:00
with open(app.config['MEDAILLE_FILE'], 'w') as f:
2024-03-27 15:20:10 +01:00
json.dump(listeMedailles.model_dump(), f, indent=4)
return jsonify(medaille.model_dump()), 200
2024-03-27 14:17:05 +01:00
swaggerui_blueprint = get_swaggerui_blueprint(
"/swagger",
"/static/swagger.yaml"
)
app.register_blueprint(swaggerui_blueprint)
def create_app():
return app
if __name__ == '__main__':
app.run()