From 02ab9aa099b41e5d6a8df7453b2e2de8871788d8 Mon Sep 17 00:00:00 2001 From: Vincent BENOIT Date: Mon, 10 Oct 2022 17:36:08 +0200 Subject: [PATCH] ajout des routes pour le parametrage --- db.json | 78 ++++++++++++++++++++++++++++- db.json.origin | 69 ++++++++++++++++++++++++++ src/app.py | 3 +- src/params/__init__.py | 6 +++ src/params/views.py | 110 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 src/params/__init__.py create mode 100644 src/params/views.py diff --git a/db.json b/db.json index 0c0db17..34252d8 100644 --- a/db.json +++ b/db.json @@ -1 +1,77 @@ -{"utilisateur": {"id": "admin", "password": "9ac564e9a29c952a3ddeffd8635adb0c252dbd1d58f3efde265e028f7f808aa9"}} \ No newline at end of file +{ + "utilisateur": { + "id": "admin", + "password": "9ac564e9a29c952a3ddeffd8635adb0c252dbd1d58f3efde265e028f7f808aa9" + }, + "PIN_ACTIF": true, + "CODE_PIN": "1234", + "NUM_AUTORISE": "607297154", + "TONE_DURATION": 3, + "DTMF_CODE": "*", + "DTMF_DURATION": 1, + "HORAIRES": { + "Monday":{ + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Tuesday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Wednesday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Thursday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Friday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Saturday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":0,"06h30":0, + "07h00":0,"07h30":0,"08h00":0,"08h30":0,"09h00":0,"09h30":0,"10h00":0, + "10h30":0,"11h00":0,"11h30":0,"12h00":0,"12h30":0,"13h00":0,"13h30":0, + "14h00":0,"14h30":0,"15h00":0,"15h30":0,"16h00":0,"16h30":0,"17h00":0, + "17h30":0,"18h00":0,"18h30":0,"19h00":0,"19h30":0,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Sunday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":0,"06h30":0, + "07h00":0,"07h30":0,"08h00":0,"08h30":0,"09h00":0,"09h30":0,"10h00":0, + "10h30":0,"11h00":0,"11h30":0,"12h00":0,"12h30":0,"13h00":0,"13h30":0, + "14h00":0,"14h30":0,"15h00":0,"15h30":0,"16h00":0,"16h30":0,"17h00":0, + "17h30":0,"18h00":0,"18h30":0,"19h00":0,"19h30":0,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + } + } +} diff --git a/db.json.origin b/db.json.origin index 7bb9d60..39c9fe9 100644 --- a/db.json.origin +++ b/db.json.origin @@ -2,5 +2,74 @@ "utilisateur":{ "id" : "admin", "password" : "28e55d1d25cd6e50cf4933c85636299367813225cecd7fd73c88282ed9e2c0d8" + }, + "CODE_PIN": 1234, + "NUM_AUTORISE": "0607297154", + "DTMF_CODE": "*", + "DTMF_DURATION": 1, + "HORAIRES": { + "Monday":{ + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Tuesday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Wednesday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Thursday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Friday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":1,"06h30":1, + "07h00":1,"07h30":1,"08h00":1,"08h30":1,"09h00":1,"09h30":1,"10h00":1, + "10h30":1,"11h00":1,"11h30":1,"12h00":1,"12h30":1,"13h00":1,"13h30":1, + "14h00":1,"14h30":1,"15h00":1,"15h30":1,"16h00":1,"16h30":1,"17h00":1, + "17h30":1,"18h00":1,"18h30":1,"19h00":1,"19h30":1,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Saturday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":0,"06h30":0, + "07h00":0,"07h30":0,"08h00":0,"08h30":0,"09h00":0,"09h30":0,"10h00":0, + "10h30":0,"11h00":0,"11h30":0,"12h00":0,"12h30":0,"13h00":0,"13h30":0, + "14h00":0,"14h30":0,"15h00":0,"15h30":0,"16h00":0,"16h30":0,"17h00":0, + "17h30":0,"18h00":0,"18h30":0,"19h00":0,"19h30":0,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + }, + "Sunday": { + "00h00":0,"00h30":0,"01h00":0,"01h30":0,"02h00":0,"02h30":0,"03h00":0, + "03h30":0,"04h00":0,"04h30":0,"05h00":0,"05h30":0,"06h00":0,"06h30":0, + "07h00":0,"07h30":0,"08h00":0,"08h30":0,"09h00":0,"09h30":0,"10h00":0, + "10h30":0,"11h00":0,"11h30":0,"12h00":0,"12h30":0,"13h00":0,"13h30":0, + "14h00":0,"14h30":0,"15h00":0,"15h30":0,"16h00":0,"16h30":0,"17h00":0, + "17h30":0,"18h00":0,"18h30":0,"19h00":0,"19h30":0,"20h00":0,"20h30":0, + "21h00":0,"21h30":0,"22h00":0,"22h30":0,"23h00":0,"23h30":0 + } } } diff --git a/src/app.py b/src/app.py index ea808a3..e4e196c 100644 --- a/src/app.py +++ b/src/app.py @@ -20,6 +20,7 @@ import jwt from src.config import DefaultConfig from src.auth import auth from src.account import account +from src.params import params from src.log import log as logs ######################################################### @@ -131,5 +132,5 @@ def configure_blueprints(app=None): :return None: ''' - for bp in [auth, logs, account]: + for bp in [auth, logs, account, params]: app.register_blueprint(bp) diff --git a/src/params/__init__.py b/src/params/__init__.py new file mode 100644 index 0000000..f1bdbbe --- /dev/null +++ b/src/params/__init__.py @@ -0,0 +1,6 @@ +# -*- encoding: utf-8 -*- + +# @author : vincent.benoit@benserv.fr +# @brief : params views and models + +from .views import params diff --git a/src/params/views.py b/src/params/views.py new file mode 100644 index 0000000..299bf5d --- /dev/null +++ b/src/params/views.py @@ -0,0 +1,110 @@ +# -*- encoding: utf-8 -*- + +# @author : vincent.benoit@benserv.fr +# @brief : Params 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 # + +params = Blueprint('params', __name__, url_prefix='/api/configurateur') + +@params.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 + +@params.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 + +@params.route('/params', methods=['GET']) +@jwt_required() +def retreive_params(): + ''' 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) + if 'PIN_ACTIF' and 'CODE_PIN' and 'NUM_AUTORISE' and 'TONE_DURATION' and 'DTMF_CODE' and 'DTMF_DURATION' in data: + content = {'pin_actif': data['PIN_ACTIF'], 'code_pin': data['CODE_PIN'], 'num_autorized': data['NUM_AUTORISE'], 'tone_duration': data['TONE_DURATION'], 'dtmf_code': data['DTMF_CODE'], 'dtmf_duration': data['DTMF_DURATION']} + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="paramètres manquant en base de données") + + return content, status.HTTP_200_OK + +@params.route('/update_params', methods=['POST']) +@jwt_required() +def update_params(): + ''' 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 'old' in data_req: + if data_req['old'] != data['utilisateur']['password']: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="Mauvais ancien mot de passe") + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="Ancien mot de passe invalide") + if 'new' in data_req: + data['utilisateur']['password'] = data_req['new'] + with open(current_app.config['DB_PATH'], 'w') as f: + json.dump(data, f) + else: + abort(status.HTTP_406_NOT_ACCEPTABLE, description="Nouveau mot de passe invalide") + + content = {'message':'maj password successful!'} + return content, status.HTTP_200_OK +