ajout des routes pour la page horaires
This commit is contained in:
1 parent
abccf46f59
commit
eded87131e
4 files changed
+501
-2
No files matched your search
+2
-1
@@ -21,6 +21,7 @@ from src.config import DefaultConfig
|
||||
from src.auth import auth
|
||||
from src.account import account
|
||||
from src.params import params
|
||||
from src.schedule import schedule
|
||||
from src.log import log as logs
|
||||
|
||||
#########################################################
|
||||
@@ -132,5 +133,5 @@ def configure_blueprints(app=None):
|
||||
|
||||
:return None:
|
||||
'''
|
||||
for bp in [auth, logs, account, params]:
|
||||
for bp in [auth, logs, account, params, schedule]:
|
||||
app.register_blueprint(bp)
|
||||
@@ -0,0 +1,6 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
# @author : vincent.benoit@benserv.fr
|
||||
# @brief : schedule views and models
|
||||
|
||||
from .views import schedule
|
||||
@@ -0,0 +1,129 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
# @author : vincent.benoit@benserv.fr
|
||||
# @brief : Scheduler routes
|
||||
|
||||
#########################################################
|
||||
# Importation de modules externes #
|
||||
|
||||
import sys, re, os
|
||||
import logging as log
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from flask import Flask, Blueprint, request, abort, jsonify, current_app
|
||||
from flask_api import status
|
||||
from flask_jwt_extended import create_access_token
|
||||
from flask_jwt_extended import get_jwt
|
||||
from flask_jwt_extended import set_access_cookies
|
||||
from flask_jwt_extended import unset_jwt_cookies
|
||||
from flask_jwt_extended import get_jwt_identity
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
import json
|
||||
import shutil
|
||||
import hashlib
|
||||
from werkzeug.exceptions import HTTPException
|
||||
|
||||
#########################################################
|
||||
# Class et Methods #
|
||||
|
||||
schedule = Blueprint('schedule', __name__, url_prefix='/api/configurateur')
|
||||
|
||||
@schedule.errorhandler(HTTPException)
|
||||
def handle_exception(e):
|
||||
''' return JSON instead of HTML for HTTP errors '''
|
||||
response = e.get_response()
|
||||
# replace the body with JSON
|
||||
response.data = json.dumps({
|
||||
'code': e.code,
|
||||
'name': e.name,
|
||||
'description': e.description,
|
||||
})
|
||||
response.content_type = "application/json"
|
||||
return response
|
||||
|
||||
@schedule.after_request
|
||||
def refresh_expiring_tokens(response):
|
||||
''' Using an 'after_request' callback, we refresh any token that is within
|
||||
30 minutes of expiring.'''
|
||||
try:
|
||||
exp_timestamp = get_jwt()['exp']
|
||||
now = datetime.now(timezone.utc)
|
||||
target_timestamp = datetime.timestamp(now + current_app.config['DELTA'])
|
||||
if target_timestamp > exp_timestamp:
|
||||
current_app.logger.warning("On doit recréer un token JWT ....")
|
||||
access_token = create_access_token(identity=get_jwt_identity())
|
||||
# refresh token in storage place
|
||||
if os.path.exists(os.path.join("/tmp", current_app.config['PROJECT'])):
|
||||
with open(os.path.join("/tmp", current_app.config['PROJECT'], get_jwt_identity()['id']), 'w') as f:
|
||||
f.write(access_token)
|
||||
# Modifiy a Flask Response to set a cookie containing the access JWT.
|
||||
set_access_cookies(response, access_token)
|
||||
return response
|
||||
except (RuntimeError, KeyError):
|
||||
return response
|
||||
|
||||
@schedule.route('/scheduler', methods=['GET'])
|
||||
@jwt_required()
|
||||
def retreive_scheduler():
|
||||
''' Récupération des paramètres de l'intercom
|
||||
'''
|
||||
current_app.logger.info("Récupération des paramètres de l'intercom")
|
||||
current_user = get_jwt_identity()
|
||||
# load data from JSON database
|
||||
with open(current_app.config['DB_PATH'], 'r') as f:
|
||||
data = json.load(f)
|
||||
content = []
|
||||
if 'HORAIRES' in data:
|
||||
for day in data['HORAIRES']:
|
||||
content.append({'name':day, 'horaires':data['HORAIRES'][day]})
|
||||
current_app.logger.debug("{}".format(content))
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="horaire manquant en base de données")
|
||||
|
||||
return content, status.HTTP_200_OK
|
||||
|
||||
@schedule.route('/update_scheduler', methods=['POST'])
|
||||
@jwt_required()
|
||||
def update_scheduler():
|
||||
''' Mise à jour des paramètres de l'intercom
|
||||
'''
|
||||
current_app.logger.info("Mise à jour des paramètres de l'intercom")
|
||||
current_user = get_jwt_identity()
|
||||
# recuperation des attributs JSON de la requete
|
||||
data_req = request.get_json()
|
||||
current_app.logger.debug("request: {}".format(data_req))
|
||||
# load data from JSON database
|
||||
with open(current_app.config['DB_PATH'], 'r') as f:
|
||||
data = json.load(f)
|
||||
|
||||
if 'pin_actif' in data_req:
|
||||
data['PIN_ACTIF'] = data_req['pin_actif']
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant")
|
||||
if 'code_pin' in data_req:
|
||||
data['CODE_PIN'] = data_req['code_pin']
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant")
|
||||
if 'num_autorized' in data_req:
|
||||
data['NUM_AUTORISE'] = data_req['num_autorized']
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant")
|
||||
if 'tone_duration' in data_req:
|
||||
data['TONE_DURATION'] = int(data_req['tone_duration'])
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant")
|
||||
if 'dtmf_code' in data_req:
|
||||
data['DTMF_CODE'] = data_req['dtmf_code']
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant")
|
||||
if 'dtmf_duration' in data_req:
|
||||
data['DTMF_DURATION'] = int(data_req['dtmf_duration'])
|
||||
else:
|
||||
abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètre manquant")
|
||||
|
||||
with open(current_app.config['DB_PATH'], 'w') as f:
|
||||
json.dump(data, f)
|
||||
content = {'message':'maj parameters successful!'}
|
||||
return content, status.HTTP_200_OK
|
||||
|
||||
Reference in new issue
Block a user