Téléverser les fichiers vers "routes"
This commit is contained in:
BIN
routes/__init__.py
Normal file
BIN
routes/__init__.py
Normal file
Binary file not shown.
34
routes/auth.py
Normal file
34
routes/auth.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
from flask_jwt_extended import create_access_token
|
||||||
|
import pymysql
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
auth_bp = Blueprint('auth', __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
|
||||||
|
)
|
||||||
|
|
||||||
|
#POST — Login et génération du token JWT
|
||||||
|
@auth_bp.route('/login', methods=['POST'])
|
||||||
|
def login():
|
||||||
|
body = request.get_json()
|
||||||
|
username = body.get('username')
|
||||||
|
password = body.get('password')
|
||||||
|
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password))
|
||||||
|
user = cursor.fetchone()
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
if user:
|
||||||
|
token = create_access_token(identity=username)
|
||||||
|
return jsonify({'token': token}), 200
|
||||||
|
else:
|
||||||
|
return jsonify({'message': 'Identifiants incorrects'}), 401
|
||||||
41
routes/climate.py
Normal file
41
routes/climate.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
import pymysql
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
climate_bp = Blueprint('climate', __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 toutes les données climatiques
|
||||||
|
@climate_bp.route('/', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
def get_climate():
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT * FROM climate_data ORDER BY timestamp DESC LIMIT 50")
|
||||||
|
data = cursor.fetchall()
|
||||||
|
db.close()
|
||||||
|
return jsonify(data), 200
|
||||||
|
|
||||||
|
#POST — Ajouter une donnée climatique
|
||||||
|
@climate_bp.route('/', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
|
def add_climate():
|
||||||
|
body = request.get_json()
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO climate_data (temperature, humidity, pressure, luminosity) VALUES (%s, %s, %s, %s)",
|
||||||
|
(body.get('temperature'), body.get('humidity'), body.get('pressure'), body.get('luminosity'))
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
return jsonify({'message': 'Donnée ajoutée avec succès'}), 201
|
||||||
41
routes/security.py
Normal file
41
routes/security.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
import pymysql
|
||||||
|
from config import Config
|
||||||
|
|
||||||
|
security_bp = Blueprint('security', __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 événements de sécurité
|
||||||
|
@security_bp.route('/', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
def get_security():
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("SELECT * FROM security_events ORDER BY timestamp DESC LIMIT 50")
|
||||||
|
data = cursor.fetchall()
|
||||||
|
db.close()
|
||||||
|
return jsonify(data), 200
|
||||||
|
|
||||||
|
# POST — Ajouter un événement de sécurité
|
||||||
|
@security_bp.route('/', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
|
def add_security():
|
||||||
|
body = request.get_json()
|
||||||
|
db = get_db()
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO security_events (event_type, location) VALUES (%s, %s)",
|
||||||
|
(body.get('event_type'), body.get('location'))
|
||||||
|
)
|
||||||
|
db.commit()
|
||||||
|
db.close()
|
||||||
|
return jsonify({'message': 'Événement ajouté avec succès'}), 201
|
||||||
Reference in New Issue
Block a user