From 142c94750561853b210d3adbf4063619d80e4f9d Mon Sep 17 00:00:00 2001 From: Simon Briere Date: Wed, 6 Apr 2022 14:55:38 -0400 Subject: [PATCH] Refs #79. Added features in ServiceQuerySessions & ServiceQueryTests API --- .../API/service/ServiceQuerySessions.py | 3 +- .../API/service/ServiceQueryTests.py | 29 +++++++++++++++++-- .../python/opentera/db/models/TeraTest.py | 15 ++++++++++ .../python/opentera/db/models/TeraTestType.py | 4 +++ 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/teraserver/python/modules/FlaskModule/API/service/ServiceQuerySessions.py b/teraserver/python/modules/FlaskModule/API/service/ServiceQuerySessions.py index f1e131413..7d1012d60 100644 --- a/teraserver/python/modules/FlaskModule/API/service/ServiceQuerySessions.py +++ b/teraserver/python/modules/FlaskModule/API/service/ServiceQuerySessions.py @@ -196,8 +196,7 @@ def post(self): session_name = TeraSessionType.get_session_type_by_id(json_session['id_session_type']).session_type_name session_date = json_session['session_start_datetime'] if not isinstance(session_date, datetime): - import dateutil.parser as parser - session_date = parser.parse(json_session['session_start_datetime']) + session_date = datetime.fromisoformat(json_session['session_start_datetime']) # session_name += ' [' + session_date.strftime('%d-%m-%Y %H:%M') + ']' session_name += ' [' + str(session_date.year) + '.' + str(TeraSession.get_count()) + ']' json_session['session_name'] = session_name diff --git a/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTests.py b/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTests.py index 29f1a83a8..720977159 100644 --- a/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTests.py +++ b/teraserver/python/modules/FlaskModule/API/service/ServiceQueryTests.py @@ -5,9 +5,11 @@ from modules.FlaskModule.FlaskModule import service_api_ns as api from opentera.db.models.TeraTest import TeraTest from opentera.db.models.TeraService import TeraService +from opentera.db.models.TeraTestType import TeraTestType from opentera.redis.RedisVars import RedisVars from modules.DatabaseModule.DBManager import DBManager from sqlalchemy import exc +from datetime import datetime # Parser definition(s) @@ -137,11 +139,32 @@ def post(self): if 'id_session' not in test_info: return gettext('Unknown session'), 400 - if 'id_test_type' not in test_info: + if 'id_test_type' not in test_info and 'test_type_uuid' not in test_info: return gettext('Missing id_test_type field'), 400 - if 'test_name' not in test_info and not test_info['test_name']: - return gettext('Invalid test name'), 400 + if 'test_type_uuid' in test_info: + test_type = TeraTestType.get_test_type_by_uuid(test_info['test_type_uuid']) + if not test_type: + return gettext('Invalid test type'), 400 + test_info.pop('test_type_uuid') + test_info['id_test_type'] = test_type.id_test_type + else: + test_type = TeraTestType.get_test_type_by_id(test_info['id_test_type']) + + if 'test_datetime' not in test_info: + test_info['test_datetime'] = datetime.now() + + if 'test_name' not in test_info: + test_date = test_info['test_datetime'] + if not isinstance(test_date, datetime): + test_date = datetime.fromisoformat(test_info['test_datetime']) + if test_type.test_type_key: + test_name = test_type.test_type_key + else: + test_name = test_type.test_name + test_name += ' [' + str(test_date.year) + '.' + \ + str(TeraTest.count_with_filters({'id_session': test_info['id_session']})) + ']' + test_info['test_name'] = test_name # Check if the service can create/update that test if test_info['id_test'] != 0 and 'id_session' not in test_info: diff --git a/teraserver/python/opentera/db/models/TeraTest.py b/teraserver/python/opentera/db/models/TeraTest.py index 7c2187f86..14ac00900 100644 --- a/teraserver/python/opentera/db/models/TeraTest.py +++ b/teraserver/python/opentera/db/models/TeraTest.py @@ -1,6 +1,7 @@ from opentera.db.Base import db, BaseModel from enum import Enum import uuid +import json class TeraTestStatus(Enum): @@ -182,4 +183,18 @@ def insert(cls, test): # Generate UUID test.test_uuid = str(uuid.uuid4()) + # Check if summary is in json + if isinstance(test.test_summary, dict): + test.test_summary = json.dumps(test.test_summary) + super().insert(test) + + @classmethod + def update(cls, update_id: int, values: dict): + + # Check if summary is in json + if 'test_summary' in values: + if isinstance(values['test_summary'], dict): + values['test_summary'] = json.dumps(values['test_summary']) + + super().update(update_id=update_id, values=values) diff --git a/teraserver/python/opentera/db/models/TeraTestType.py b/teraserver/python/opentera/db/models/TeraTestType.py index e38eec5b5..3d3f544be 100644 --- a/teraserver/python/opentera/db/models/TeraTestType.py +++ b/teraserver/python/opentera/db/models/TeraTestType.py @@ -82,6 +82,10 @@ def get_test_type_by_id(test_type_id: int): def get_test_type_by_key(tt_key: int): return TeraTestType.query.filter_by(test_type_key=tt_key).first() + @staticmethod + def get_test_type_by_uuid(tt_uuid: int): + return TeraTestType.query.filter_by(test_type_uuid=tt_uuid).first() + @staticmethod def get_test_types_for_service(id_service: int): return TeraTestType.query.filter_by(id_service=id_service).all()