[DEV] try add a basic checker of format for BDD

This commit is contained in:
Edouard DUPIN 2019-12-17 23:55:01 +01:00
parent 9866963aca
commit 49dcbd099d
9 changed files with 109 additions and 10 deletions

View File

@ -20,6 +20,7 @@ from sanic import Sanic
from sanic import response
from sanic import views
from sanic import Blueprint
from sanic.exceptions import ServerError
from sanic_simple_swagger import swagger_blueprint, openapi_blueprint
from sanic_simple_swagger import doc
@ -28,6 +29,9 @@ import tools
import data_interface
import data_global_elements
import hashlib
import shutil
tmp_value = 0
#curl -F 'file=@Totally_Spies.mp4;type=application/octet-stream' -H 'transfer-encoding:chunked' 127.0.0.1:15080/data -X POST -O; echo ;
@ -55,11 +59,11 @@ def add(_app, _name_api):
async def streaming(_response):
debug.info("streaming " + str(_response));
total_size = 0
temporary_file = os.path.join(app.config['REST_TMP_DATA'], str(tmp_value) + ".tmp")
if not os.path.exists(app.config['REST_TMP_DATA']):
os.makedirs(app.config['REST_TMP_DATA'])
if not os.path.exists(app.config['REST_MEDIA_DATA']):
os.makedirs(app.config['REST_MEDIA_DATA'])
temporary_file = os.path.join(_app.config['REST_TMP_DATA'], str(tmp_value) + ".tmp")
if not os.path.exists(_app.config['REST_TMP_DATA']):
os.makedirs(_app.config['REST_TMP_DATA'])
if not os.path.exists(_app.config['REST_MEDIA_DATA']):
os.makedirs(_app.config['REST_MEDIA_DATA'])
file_stream = open(temporary_file,"wb")
sha1 = hashlib.sha512()
while True:
@ -73,7 +77,7 @@ def add(_app, _name_api):
sha1.update(body)
file_stream.close()
print("SHA512: " + str(sha1.hexdigest()))
destination_filename = os.path.join(app.config['REST_MEDIA_DATA'], str(sha1.hexdigest()))
destination_filename = os.path.join(_app.config['REST_MEDIA_DATA'], str(sha1.hexdigest()))
if os.path.isfile(destination_filename) == True:
answer_data = {
"size": total_size,

View File

@ -20,6 +20,7 @@ from sanic import Sanic
from sanic import response
from sanic import views
from sanic import Blueprint
from sanic.exceptions import ServerError
from sanic_simple_swagger import swagger_blueprint, openapi_blueprint
from sanic_simple_swagger import doc
@ -31,6 +32,12 @@ import data_global_elements
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)
class DataModelBdd:
id = int
name = str
data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd)
class DataModel:
name = str

View File

@ -20,6 +20,7 @@ from sanic import Sanic
from sanic import response
from sanic import views
from sanic import Blueprint
from sanic.exceptions import ServerError
from sanic_simple_swagger import swagger_blueprint, openapi_blueprint
from sanic_simple_swagger import doc

View File

@ -20,6 +20,7 @@ from sanic import Sanic
from sanic import response
from sanic import views
from sanic import Blueprint
from sanic.exceptions import ServerError
from sanic_simple_swagger import swagger_blueprint, openapi_blueprint
from sanic_simple_swagger import doc
@ -31,6 +32,13 @@ import data_global_elements
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)
class DataModelBdd:
id = int
number = int
group_id = int
data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd)
class DataModel:
number = int
group_id = int

View File

@ -20,6 +20,7 @@ from sanic import Sanic
from sanic import response
from sanic import views
from sanic import Blueprint
from sanic.exceptions import ServerError
from sanic_simple_swagger import swagger_blueprint, openapi_blueprint
from sanic_simple_swagger import doc
@ -31,6 +32,13 @@ 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)
class DataModel:
name = str
description = str

View File

@ -20,6 +20,7 @@ from sanic import Sanic
from sanic import response
from sanic import views
from sanic import Blueprint
from sanic.exceptions import ServerError
from sanic_simple_swagger import swagger_blueprint, openapi_blueprint
from sanic_simple_swagger import doc
@ -31,6 +32,22 @@ import data_global_elements
def add(_app, _name_api):
elem_blueprint = Blueprint(_name_api)
class DataModelBdd:
id = int
sha512 = str
type_id = int
saison_id = int
episode = [int, type(None)]
group_id = int
name = str
description = str
# creating time
date = str
# number of second
time = [int, type(None)]
data_global_elements.get_interface(_name_api).set_data_model(DataModelBdd)
class DataModel:
type_id = int
saison_id = int
@ -60,6 +77,10 @@ def add(_app, _name_api):
if "group_name" in request.json.keys():
id_group = data_global_elements.get_interface(API_GROUP).find_or_create_name(request.json["group_name"])
"""
if "episode" not in request.json.keys():
request.json["episode"] = None
else:
request.json["episode"] = int(request.json["episode"])
return response.json(data_global_elements.get_interface(_name_api).post(request.json))
@elem_blueprint.get('/' + _name_api + '/<id:int>', strict_slashes=True)

View File

@ -32,6 +32,8 @@ import datetime
import time, threading
import realog.debug as debug
debug.enable_color()
import tools
import data_interface
import data_global_elements
@ -91,8 +93,6 @@ api_video.add(app, API_VIDEO)
import api.data as api_data
api_data.add(app, API_DATA)
import hashlib
import shutil

View File

@ -12,8 +12,11 @@ import tools
import json
from realog import debug
from sanic.exceptions import ServerError
class DataInterface():
def __init__(self, _name, _file):
self.model = None
self.name = _name
self.file = _file
self.bdd = []
@ -26,6 +29,51 @@ class DataInterface():
self.bdd = json.loads(data)
self.upgrade_global_bdd_id();
def set_data_model(self, _data_model):
self.model = _data_model
def check_with_model(self, _data):
if self.model == None:
return True
values = []
for elem in dir(self.model):
if elem[:2] == "__":
continue
values.append(elem)
have_error = False
for key in _data.keys():
if key not in values:
have_error = True
# TODO: ...
debug.warning("Add element that is not allowed " + key + " not in " + str(values))
for elem in values:
if key not in _data.keys():
have_error = True
# TODO: ...
debug.warning("Missing key " + elem + " not in " + str(_data.keys()))
if have_error == True:
return False
for key in _data.keys():
elem = getattr(self.model, key)
if type(elem) == list:
find_error = True
for my_type in elem:
if type(_data[key]) == my_type:
find_error = False
break
if find_error == True:
debug.warning("data : " + str(_data))
tmp_list = []
for my_type in elem:
tmp_list.append(my_type.__name__)
debug.warning("[key='" + key + "'] try to add wrong type in BDD " + type(_data[key]).__name__ + " is not: " + str(my_type))
else:
if type(_data[key]) != getattr(self.model, key):
debug.warning("data : " + str(_data))
debug.warning("[key='" + key + "'] try to add wrong type in BDD " + type(_data[key]).__name__ + " is not: " + getattr(self.model, key).__name__)
return False
return True
def upgrade_global_bdd_id(self):
for elem in self.bdd:
if 'id' not in elem.keys():
@ -85,6 +133,8 @@ class DataInterface():
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.need_save = True
return value

View File

@ -375,10 +375,10 @@ def push_video_file(_path, _basic_key={}):
group_id = result_group_data.json()["id"]
data_model["group_id"] = group_id
if "saison" in _basic_key.keys():
result_saison_data = requests.post("http://127.0.0.1:15080/saison/find", data=json.dumps({"number":_basic_key["saison"], "group_id":group_id}, sort_keys=True, indent=4))
result_saison_data = requests.post("http://127.0.0.1:15080/saison/find", data=json.dumps({"number":int(_basic_key["saison"]), "group_id":group_id}, sort_keys=True, indent=4))
print("Create saison ??? *********** : " + str(result_saison_data) + " " + result_saison_data.text)
if result_saison_data.status_code == 404:
result_saison_data = requests.post("http://127.0.0.1:15080/saison", data=json.dumps({"number":_basic_key["saison"], "group_id":group_id}, sort_keys=True, indent=4))
result_saison_data = requests.post("http://127.0.0.1:15080/saison", data=json.dumps({"number":int(_basic_key["saison"]), "group_id":group_id}, sort_keys=True, indent=4))
print("yes we create new saison *********** : " + str(result_saison_data) + " " + result_saison_data.text)
saison_id = result_saison_data.json()["id"]
data_model["saison_id"] = saison_id