Téléverser les fichiers vers "/"
This commit is contained in:
BIN
__init__.py
Normal file
BIN
__init__.py
Normal file
Binary file not shown.
67
admin_routes.py
Normal file
67
admin_routes.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
import pymysql
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
admin_bp = Blueprint('admin', __name__)
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
return pymysql.connect(
|
||||||
|
host=Config.MYSQL_HOST,
|
||||||
|
user=Config.MYSQL_USER,
|
||||||
|
password=Config.MYSQL_PASSWORD,
|
||||||
|
database=Config.MYSQL_DB,
|
||||||
|
cursorclass=pymysql.cursors.DictCursor
|
||||||
|
)
|
||||||
|
|
||||||
|
#GET — Récupérer tous les utilisateurs
|
||||||
|
@admin_bp.route('/users', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
def get_users():
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT * FROM users")
|
||||||
|
data = cursor.fetchall()
|
||||||
|
db.close()
|
||||||
|
return jsonify(data), 200
|
||||||
|
|
||||||
|
# POST — Ajouter un utilisateur
|
||||||
|
@admin_bp.route('/users', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
|
def add_user():
|
||||||
|
body = request.get_json()
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO users (username, password) VALUES (%s, %s)",
|
||||||
|
(body.get('username'), body.get('password'))
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
return jsonify({'message': 'Utilisateur ajouté avec succès'}), 201
|
||||||
|
|
||||||
|
#GET — Récupérer les seuils
|
||||||
|
@admin_bp.route('/thresholds', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
def get_thresholds():
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT * FROM thresholds")
|
||||||
|
data = cursor.fetchall()
|
||||||
|
db.close()
|
||||||
|
return jsonify(data), 200
|
||||||
|
|
||||||
|
#POST — Modifier un seuil
|
||||||
|
@admin_bp.route('/thresholds', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
|
def set_threshold():
|
||||||
|
body = request.get_json()
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO thresholds (sensor, min_value, max_value) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE min_value=%s, max_value=%s",
|
||||||
|
(body.get('sensor'), body.get('min_value'), body.get('max_value'), body.get('min_value'), body.get('max_value'))
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
return jsonify({'message': 'Seuil mis à jour avec succès'}), 200
|
||||||
Reference in New Issue
Block a user