From 8b43416e4c893b256bc6fc8f1e07f7c00f8209e9 Mon Sep 17 00:00:00 2001 From: Max Metz Date: Mon, 10 Jun 2024 11:53:17 +0200 Subject: [PATCH] preparing novel endpoints for getlatest. Initially tried with berths, which does not have PUT/POST/DEL endpoints. --- src/server/BreCal/api/__init__.py | 34 +++++++++++++++++++++++++ src/server/BreCal/api/berths.py | 10 ++++++++ src/server/BreCal/impl/__init__.py | 2 +- src/server/BreCal/impl/berths.py | 28 ++++++++++++++++++++ src/server/BreCal/impl/history.py | 1 + src/server/BreCal/impl/login.py | 1 + src/server/BreCal/impl/notifications.py | 1 + src/server/BreCal/impl/participant.py | 1 + src/server/BreCal/impl/shipcalls.py | 1 + src/server/BreCal/impl/ships.py | 4 ++- src/server/BreCal/impl/times.py | 1 + src/server/BreCal/impl/user.py | 1 + 12 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/server/BreCal/api/__init__.py b/src/server/BreCal/api/__init__.py index e69de29..1803976 100644 --- a/src/server/BreCal/api/__init__.py +++ b/src/server/BreCal/api/__init__.py @@ -0,0 +1,34 @@ +import typing +import datetime + +# global dictionary, which informs about the latest change of a given database (e.g., 'berths') +latest_get_request_dict = {database:None for database in ["berths", "history", "notifications", "participant", "shipcalls", "ships", "times", "user"]} + +def update_latest_modification_time(key:str, modification_time:datetime.datetime)->None: + """ + This function updates the {latest_get_request_dict} inplace at the specified {key} with the defined {modification_time}, + *if* the time is more recent than the currently stored value + """ + global latest_get_request_dict + print(f"(update_latest_modification_time INFO): before executing the funtion", latest_get_request_dict, key, modification_time) + value = latest_get_request_dict.get(key,None) + if value is None: + # when there is no value stored for the key, update the value + latest_get_request_dict[key] = modification_time + else: + # when the modification date is more recent than the stored value, update it + if modification_time > value: + latest_get_request_dict[key] = modification_time + print(f"(update_latest_modification_time INFO): after executing the funtion", latest_get_request_dict, key, modification_time) + return + +def get_latest_modification_time(key:str)->typing.Optional[str]: + """ + This function returns the latest modification time in .isoformat, if a datetime is stored for the respective key in {latest_get_request_dict}. + When there has not yet been an update, this function returns None + """ + value = latest_get_request_dict.get(key,None) + if isinstance(value,datetime.datetime): + return value.isoformat() + return value # None + \ No newline at end of file diff --git a/src/server/BreCal/api/berths.py b/src/server/BreCal/api/berths.py index ba8f4ff..1c8a33a 100644 --- a/src/server/BreCal/api/berths.py +++ b/src/server/BreCal/api/berths.py @@ -16,3 +16,13 @@ def GetBerths(): return impl.berths.GetBerths(token) else: return json.dumps("not authenticated"), 403 + +@bp.route('/getlatestberths', methods=['get']) +@auth_guard() # no restriction by role +def GetLatestBerths(): + + if 'Authorization' in request.headers: + token = request.headers.get('Authorization') + return impl.berths.GetLatestBerths(token) + else: + return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/impl/__init__.py b/src/server/BreCal/impl/__init__.py index 93ec9fc..4577c7f 100644 --- a/src/server/BreCal/impl/__init__.py +++ b/src/server/BreCal/impl/__init__.py @@ -6,4 +6,4 @@ from . import times from . import ships from . import login from . import user -from . import history \ No newline at end of file +from . import history diff --git a/src/server/BreCal/impl/berths.py b/src/server/BreCal/impl/berths.py index 9087856..cc6baf3 100644 --- a/src/server/BreCal/impl/berths.py +++ b/src/server/BreCal/impl/berths.py @@ -1,9 +1,11 @@ import json import logging import pydapper +import datetime from ..schemas import model from .. import local_db +from BreCal.api import latest_get_request_dict, update_latest_modification_time, get_latest_modification_time def GetBerths(token): """ @@ -14,6 +16,8 @@ def GetBerths(token): pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) data = commands.query("SELECT id, name, `lock`, owner_id, authority_id, created, modified, deleted FROM berth WHERE deleted = 0 ORDER BY name", model=model.Berth) + # update_latest_modification_time(key="berths", modification_time=datetime.datetime.now()) + return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: @@ -28,5 +32,29 @@ def GetBerths(token): pooledConnection.close() +def GetLatestBerths(token): + """ + Returns a datetime of the latest modification within the berths database. When there has not yet been a modification, this method returns null. + + # #TODO: should this become a data model? + """ + + try: + modification_time = get_latest_modification_time(key="berths") + data = {"key":"berths", "value":modification_time} + + return json.dumps(data), 200, {'Content-Type': 'application/json; charset=utf-8'} + + except Exception as ex: + logging.error(ex) + print(ex) + result = {} + result["message"] = "call failed" + return json.dumps(result), 500 + + + + + diff --git a/src/server/BreCal/impl/history.py b/src/server/BreCal/impl/history.py index f408001..2fa7e1c 100644 --- a/src/server/BreCal/impl/history.py +++ b/src/server/BreCal/impl/history.py @@ -7,6 +7,7 @@ from ..schemas import model from ..schemas.model import History from .. import local_db +from BreCal.api import latest_get_request_dict, update_latest_modification_time def GetHistory(options): diff --git a/src/server/BreCal/impl/login.py b/src/server/BreCal/impl/login.py index 21b46a6..e208620 100644 --- a/src/server/BreCal/impl/login.py +++ b/src/server/BreCal/impl/login.py @@ -6,6 +6,7 @@ import bcrypt from ..schemas import model from .. import local_db from ..services import jwt_handler +from BreCal.api import latest_get_request_dict, update_latest_modification_time def GetUser(options): diff --git a/src/server/BreCal/impl/notifications.py b/src/server/BreCal/impl/notifications.py index 67619ab..793f4c7 100644 --- a/src/server/BreCal/impl/notifications.py +++ b/src/server/BreCal/impl/notifications.py @@ -4,6 +4,7 @@ import pydapper from ..schemas import model from .. import local_db +from BreCal.api import latest_get_request_dict, update_latest_modification_time def GetNotifications(options): """ diff --git a/src/server/BreCal/impl/participant.py b/src/server/BreCal/impl/participant.py index aad0def..174a3f5 100644 --- a/src/server/BreCal/impl/participant.py +++ b/src/server/BreCal/impl/participant.py @@ -4,6 +4,7 @@ import pydapper from ..schemas import model from .. import local_db +from BreCal.api import latest_get_request_dict, update_latest_modification_time def GetParticipant(options): """ diff --git a/src/server/BreCal/impl/shipcalls.py b/src/server/BreCal/impl/shipcalls.py index 59f9311..c02c7c4 100644 --- a/src/server/BreCal/impl/shipcalls.py +++ b/src/server/BreCal/impl/shipcalls.py @@ -8,6 +8,7 @@ from .. import local_db from ..services.auth_guard import check_jwt from BreCal.database.update_database import evaluate_shipcall_state +from BreCal.api import latest_get_request_dict, update_latest_modification_time def GetShipcalls(options): """ diff --git a/src/server/BreCal/impl/ships.py b/src/server/BreCal/impl/ships.py index b0b0b94..69964d8 100644 --- a/src/server/BreCal/impl/ships.py +++ b/src/server/BreCal/impl/ships.py @@ -1,9 +1,11 @@ import json import logging import pydapper +import datetime from ..schemas import model from .. import local_db +from BreCal.api import latest_get_request_dict, update_latest_modification_time def GetShips(token): """ @@ -11,10 +13,10 @@ def GetShips(token): """ try: - pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) data = commands.query("SELECT id, name, imo, callsign, participant_id, length, width, is_tug, bollard_pull, eni, created, modified, deleted FROM ship ORDER BY name", model=model.Ship) + update_latest_modification_time(key="ships", modification_time=datetime.datetime.now()) return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} diff --git a/src/server/BreCal/impl/times.py b/src/server/BreCal/impl/times.py index 1f16574..966599f 100644 --- a/src/server/BreCal/impl/times.py +++ b/src/server/BreCal/impl/times.py @@ -6,6 +6,7 @@ import pydapper from ..schemas import model from .. import local_db from ..services.auth_guard import check_jwt +from BreCal.api import latest_get_request_dict, update_latest_modification_time from BreCal.database.update_database import evaluate_shipcall_state diff --git a/src/server/BreCal/impl/user.py b/src/server/BreCal/impl/user.py index cd79f89..2855645 100644 --- a/src/server/BreCal/impl/user.py +++ b/src/server/BreCal/impl/user.py @@ -5,6 +5,7 @@ import bcrypt from ..schemas import model from .. import local_db +from BreCal.api import latest_get_request_dict, update_latest_modification_time def PutUser(schemaModel): """