From ab31104056e4450621ae889423f16e22b6190b2f Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 21 Feb 2020 22:54:58 +0100 Subject: [PATCH] [DEV] update checker --- back/src/api/data.py | 43 +++++++++++++---- back/src/api/group.py | 34 +++++++++++--- back/src/api/saison.py | 41 ++++++++++++---- back/src/api/type.py | 33 ++++++++++--- back/src/api/univers.py | 34 +++++++++++--- back/src/api/video.py | 95 ++++++++++++++++++++++++++++++-------- back/src/app_video.py | 2 +- back/src/data_interface.py | 94 ++++++++++++++++++++++++++++--------- 8 files changed, 295 insertions(+), 81 deletions(-) diff --git a/back/src/api/data.py b/back/src/api/data.py index 51e939a..2bcb2c6 100644 --- a/back/src/api/data.py +++ b/back/src/api/data.py @@ -53,16 +53,39 @@ def add(_app, _name_api): return response.json(data_global_elements.get_interface(_name_api).gets()) """ - class DataModelBdd: - id = int - size = int - sha512 = str - mime_type = str - original_name = [str, type(None)] - # creating time - create_date = str - - data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + dataModelBdd = [ + { + "name": "id", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "size", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "sha512", + "type": "str", + "modifiable": False, + "can_be_null": False + }, + { + "name": "mime_type", + "type": "str", + "modifiable": False, + "can_be_null": False + }, + { + "name": "original_name", + "type": "str", + "modifiable": False, + "can_be_null": True + }, + ] + data_global_elements.get_interface(_name_api).set_data_model(dataModelBdd) @elem_blueprint.get('/' + _name_api + '/exist/', strict_slashes=True) diff --git a/back/src/api/group.py b/back/src/api/group.py index e5efa68..e4f62b8 100644 --- a/back/src/api/group.py +++ b/back/src/api/group.py @@ -34,13 +34,33 @@ import data_global_elements def add(_app, _name_api): elem_blueprint = Blueprint(_name_api) - class DataModelBdd: - id = int - name = str - description = [str, type(None)] - covers = [[], type(None)] - - data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + dataModelBdd = [ + { + "name": "id", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "name", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "description", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "cover", + "type": "list", + "modifiable": False, + "can_be_null": False + }, + ] + data_global_elements.get_interface(_name_api).set_data_model(dataModelBdd) class DataModel: name = str diff --git a/back/src/api/saison.py b/back/src/api/saison.py index 440b14e..e9b443e 100644 --- a/back/src/api/saison.py +++ b/back/src/api/saison.py @@ -33,14 +33,39 @@ import data_global_elements def add(_app, _name_api): elem_blueprint = Blueprint(_name_api) - class DataModelBdd: - id = int - number = int - description = [str, type(None)] - group_id = int - covers = [[], type(None)] - - data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + dataModelBdd = [ + { + "name": "id", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "number", + "type": "int", + "modifiable": True, + "can_be_null": False + }, + { + "name": "description", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "group_id", + "type": "int", + "modifiable": True, + "can_be_null": False + }, + { + "name": "cover", + "type": "list", + "modifiable": False, + "can_be_null": False + }, + ] + data_global_elements.get_interface(_name_api).set_data_model(dataModelBdd) class DataModel: number = int diff --git a/back/src/api/type.py b/back/src/api/type.py index 79c22cf..e2dbe00 100644 --- a/back/src/api/type.py +++ b/back/src/api/type.py @@ -34,12 +34,33 @@ import data_global_elements def add(_app, _name_api): elem_blueprint = Blueprint(_name_api) - class DataModelBdd: - id = int - name = str - description = str - - data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + dataModelBdd = [ + { + "name": "id", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "name", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "description", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "cover", + "type": "list", + "modifiable": False, + "can_be_null": False + }, + ] + data_global_elements.get_interface(_name_api).set_data_model(dataModelBdd) class DataModel: name = str diff --git a/back/src/api/univers.py b/back/src/api/univers.py index b4ef4f7..f7a4cf1 100644 --- a/back/src/api/univers.py +++ b/back/src/api/univers.py @@ -32,13 +32,33 @@ import data_global_elements def add(_app, _name_api): elem_blueprint = Blueprint(_name_api) - class DataModelBdd: - id = int - name = str - description = str - covers = [[], type(None)] - - data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + dataModelBdd = [ + { + "name": "id", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "name", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "description", + "type": "str", + "modifiable": True, + "can_be_null": False + }, + { + "name": "cover", + "type": "list", + "modifiable": False, + "can_be_null": False + }, + ] + data_global_elements.get_interface(_name_api).set_data_model(dataModelBdd) class DataModel: name = str diff --git a/back/src/api/video.py b/back/src/api/video.py index c8e344e..055e510 100644 --- a/back/src/api/video.py +++ b/back/src/api/video.py @@ -73,26 +73,81 @@ def generate_name(_value): def add(_app, _name_api): elem_blueprint = Blueprint(_name_api) - class DataModelBdd: - id = int - data_id = int - type_id = int - saison_id = [int, type(None)] - episode = [int, type(None)] - univers_id = [int, type(None)] - group_id = [int, type(None)] - name = str - description = [str, type(None)] - # creating time - create_date = str - # date of the video - date = [int, type(None)] - # number of second - time = [int, type(None)] - # number of second - covers = [[], type(None)] - - data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd) + dataModelBdd = [ + { + "name": "id", + "type": "int", + "modifiable": False, + "can_be_null": False + }, + { + "name": "data_id", + "type": "int", + "modifiable": True, + "can_be_null": False + }, + { + "name": "type_id", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "saison_id", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "episode", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "univers_id", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "group_id", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "name", + "type": "str", + "modifiable": True, + "can_be_null": True + }, + { + "name": "description", + "type": "str", + "modifiable": True, + "can_be_null": True + }, + { + "name": "date", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "time", + "type": "int", + "modifiable": True, + "can_be_null": True + }, + { + "name": "cover", + "type": "list", + "modifiable": False, + "can_be_null": True + }, + ] + data_global_elements.get_interface(_name_api).set_data_model(dataModelBdd) class DataModel: type_id = int diff --git a/back/src/app_video.py b/back/src/app_video.py index 18cd8c7..053f518 100755 --- a/back/src/app_video.py +++ b/back/src/app_video.py @@ -45,7 +45,7 @@ app = Sanic(__name__) spf = SanicPluginsFramework(app) spf.register_plugin(cors, automatic_options=True) -app.config['API_VERSION'] = '1.0.0' +app.config['API_VERSION'] = '2.0.0' app.config['API_TITLE'] = 'Rest personal video API' app.config['API_DESCRIPTION'] = 'Simple API for the Video broker.' app.config['API_CONTACT_EMAIL'] = "yui.heero@gmail.com" diff --git a/back/src/data_interface.py b/back/src/data_interface.py index 1f81c98..eb32ff7 100644 --- a/back/src/data_interface.py +++ b/back/src/data_interface.py @@ -18,6 +18,47 @@ from psycopg2.extras import RealDictCursor import db +def is_str(s, authorise): + if s == None: + if authorise == True: + return True + return False; + if type(s) == str: + return True + return False + +def is_boolean(s, authorise): + if s == None: + if authorise == True: + return True + return False; + if s == True or s == False: + return True + return False + +def is_int(s, authorise): + if s == None: + if authorise == True: + return True + return False; + try: + int(s) + return True + except ValueError: + return False + return False + +def is_float(s, authorise): + if s == None: + if authorise == True: + return True + return False; + try: + float(s) + return True + except ValueError: + return False + return False ## ## @breif Generic interface to access to the BDD (no BDD, direct file IO) ## @@ -189,6 +230,34 @@ class DataInterface(): self.mark_to_store(); return True + def is_value_modifiable_and_good_type(self, _key, _value): + if self.model == None: + return True + for elem in self.model: + if _key == elem["name"]: + if elem["modifiable"] == False: + debug.warning("Try to set an input '" + str(_key) + "' but the element is not modifiable ... "); + raise ServerError("FORBIDDEN Try to set an input '" + str(_key) + "' but the element is not modifiable", status_code=403) + if elem["type"] == "str": + if is_str(_value, elem["can_be_null"]) == True: + return True + elif if elem["type"] == "int": + if is_int(_value, elem["can_be_null"]) == True: + return True + elif if elem["type"] == "float": + if is_float(_value, elem["can_be_null"]) == True: + return True + elif if elem["type"] == "boolean": + if is_boolean(_value, elem["can_be_null"]) == True: + return True + else: + return True; + debug.warning("get element type == '" + str(type(_value)) + "' but request " + str(elem["type"])); + raise ServerError("FORBIDDEN get element type == '" + str(type(_value)) + "' but request " + str(elem["type"]), status_code=403) + # The key does not exist ... + debug.warning("The KEY: '" + str(_key) + "' Is not in the list of availlable keys"); + raise ServerError("FORBIDDEN The KEY: '" + str(_key) + "' Is not in the list of availlable keys", status_code=403) + def put(self, _id, _value): debug.info("put in " + self.name + ": " + str(_id)) cursor = self.connection.cursor() @@ -198,6 +267,8 @@ class DataInterface(): for elem in _value.keys(): if elem == "id": continue + if self.is_value_modifiable_and_good_type(elem, _value[elem]) == False: + return; if first == True: first = False else: @@ -209,35 +280,14 @@ class DataInterface(): debug.info("Request executed : '" + request + "'") cursor.execute(request, list_data) self.mark_to_store(); - - """ - debug.info("put " + self.name + ": " + str(_id)) - id_in_bdd = self.get_table_index(_id) - if id_in_bdd == None: - return False - # todo: check the model before update ... - debug.warning("update element: " + str(_id)) - value_bdd = copy.deepcopy(self.bdd[id_in_bdd]); - for elem in _value.keys(): - debug.warning(" [" + elem + "] " + str(value_bdd[elem]) + " ==> " + str(_value[elem])) - value_bdd[elem] = _value[elem] - if self.check_with_model(value_bdd) == False: - raise ServerError("FORBIDDEN Corelation with BDD error", status_code=403) - self.bdd[id_in_bdd] = value_bdd - debug.warning(" ==> " + str(self.bdd[id_in_bdd])) - self.mark_to_store() - """ return True def post(self, _value): - """ debug.info("post " + self.name) - _value["id"] = self.last_id - self.last_id += 1 + """ if self.check_with_model(_value) == False: raise ServerError("Corelation with BDD error", status_code=404) self.bdd.append(_value) - self.mark_to_store() """ self.mark_to_store(); return _value