diff --git a/src/server/BreCal/__init__.py b/src/server/BreCal/__init__.py index eb832f5..90869b3 100644 --- a/src/server/BreCal/__init__.py +++ b/src/server/BreCal/__init__.py @@ -13,6 +13,7 @@ from .api import ships from .api import login from .api import user from .api import history +from .api import latest from BreCal.brecal_utils.file_handling import get_project_root, ensure_path from BreCal.brecal_utils.test_handling import execute_test_with_pytest, execute_coverage_test @@ -61,6 +62,7 @@ def create_app(test_config=None): app.register_blueprint(login.bp) app.register_blueprint(user.bp) app.register_blueprint(history.bp) + app.register_blueprint(latest.bp) logging.basicConfig(filename='brecaldevel.log', level=logging.DEBUG, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s') local_db.initPool(os.path.dirname(app.instance_path)) diff --git a/src/server/BreCal/api/__init__.py b/src/server/BreCal/api/__init__.py index a8b173a..7b19b24 100644 --- a/src/server/BreCal/api/__init__.py +++ b/src/server/BreCal/api/__init__.py @@ -3,7 +3,7 @@ import datetime # global dictionary, which informs about the latest change of a given database (e.g., 'berths') # initialize all values as null (None) -latest_get_request_dict = {database:None for database in ["berths", "history", "notifications", "participants", "shipcalls", "ships", "times"]} +latest_get_request_dict = {database:None for database in ["shipcalls", "ships", "times"]} def update_latest_modification_time(key:str, modification_time:datetime.datetime)->None: """ @@ -23,15 +23,13 @@ def update_latest_modification_time(key:str, modification_time:datetime.datetime return -def get_latest_modification_time(key:str)->typing.Optional[str]: +def get_latest_modification_times()->dict[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 + This function returns a dictionary, where each key determines the database name, and each value is either an isoformatted datetime + of the latest modification time, or None. """ global latest_get_request_dict - value = latest_get_request_dict.get(key,None) - if isinstance(value,datetime.datetime): - return value.isoformat() - return value # None + latest_dict = {k:v.isoformat() if isinstance(v,datetime.datetime) else v for k,v in latest_get_request_dict.items()} + return latest_dict diff --git a/src/server/BreCal/api/berths.py b/src/server/BreCal/api/berths.py index 1c8a33a..0209851 100644 --- a/src/server/BreCal/api/berths.py +++ b/src/server/BreCal/api/berths.py @@ -17,12 +17,3 @@ def GetBerths(): 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/api/history.py b/src/server/BreCal/api/history.py index 4d087a0..33652a8 100644 --- a/src/server/BreCal/api/history.py +++ b/src/server/BreCal/api/history.py @@ -19,13 +19,3 @@ def GetParticipant(): # #TODO: rename? Naming might be a copy-paste typo else: return json.dumps("not authenticated"), 403 - -@bp.route('/getlatesthistory', methods=['get']) -@auth_guard() # no restriction by role -def GetLatestHistory(): - - if 'Authorization' in request.headers: - token = request.headers.get('Authorization') - return impl.history.GetLatestHistory(token) - else: - return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/api/latest.py b/src/server/BreCal/api/latest.py new file mode 100644 index 0000000..9d34ad5 --- /dev/null +++ b/src/server/BreCal/api/latest.py @@ -0,0 +1,18 @@ +from flask import Blueprint, request +from webargs.flaskparser import parser +from .. import impl +from ..services.auth_guard import auth_guard +import json + +bp = Blueprint('getlatest', __name__) + + +@bp.route('/getlatest', methods=['get']) +@auth_guard() # no restriction by role +def GetLatest(): + """This endpoint verifies the user and executes impl.latest.GetLatest""" + if 'Authorization' in request.headers: + token = request.headers.get('Authorization') + return impl.latest.GetLatest(token) + else: + return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/api/notifications.py b/src/server/BreCal/api/notifications.py index 5a7e434..c8225b5 100644 --- a/src/server/BreCal/api/notifications.py +++ b/src/server/BreCal/api/notifications.py @@ -18,13 +18,4 @@ def GetNotifications(): logging.warning("attempt to load notifications without shipcall id") return json.dumps("missing argument"), 400 -@bp.route('/getlatestnotifications', methods=['get']) -@auth_guard() # no restriction by role -def GetLatestNotifications(): - - if 'Authorization' in request.headers: - token = request.headers.get('Authorization') - return impl.notifications.GetLatestNotifications(token) - else: - return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/api/participant.py b/src/server/BreCal/api/participant.py index 60d7743..65887f9 100644 --- a/src/server/BreCal/api/participant.py +++ b/src/server/BreCal/api/participant.py @@ -17,12 +17,3 @@ def GetParticipant(): else: return json.dumps("not authenticated"), 403 -@bp.route('/getlatestparticipants', methods=['get']) -@auth_guard() # no restriction by role -def GetLatestParticipants(): - - if 'Authorization' in request.headers: - token = request.headers.get('Authorization') - return impl.participant.GetLatestParticipants(token) - else: - return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/api/shipcalls.py b/src/server/BreCal/api/shipcalls.py index 1b22dff..7d20949 100644 --- a/src/server/BreCal/api/shipcalls.py +++ b/src/server/BreCal/api/shipcalls.py @@ -54,11 +54,3 @@ def PutShipcalls(): return impl.shipcalls.PutShipcalls(loadedModel) -@bp.route('/getlatestshipcalls', methods=['get']) -@auth_guard() # no restriction by role -def GetLatestShipcalls(): - if 'Authorization' in request.headers: - token = request.headers.get('Authorization') - return impl.shipcalls.GetLatestShipcalls(token) - else: - return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/api/ships.py b/src/server/BreCal/api/ships.py index 6720a4a..e1f36b8 100644 --- a/src/server/BreCal/api/ships.py +++ b/src/server/BreCal/api/ships.py @@ -67,12 +67,4 @@ def DeleteShip(): return impl.ships.DeleteShip(options) -@bp.route('/getlatestships', methods=['get']) -@auth_guard() # no restriction by role -def GetLatestShips(): - if 'Authorization' in request.headers: - token = request.headers.get('Authorization') - return impl.ships.GetLatestShips(token) - else: - return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/api/times.py b/src/server/BreCal/api/times.py index 9e8f30d..9e94a40 100644 --- a/src/server/BreCal/api/times.py +++ b/src/server/BreCal/api/times.py @@ -68,12 +68,3 @@ def DeleteTimes(): logging.warning("Times delete missing id argument") return json.dumps("missing argument"), 400 -@bp.route('/getlatesttimes', methods=['get']) -@auth_guard() # no restriction by role -def GetLatestTimes(): - - if 'Authorization' in request.headers: - token = request.headers.get('Authorization') - return impl.times.GetLatestTimes(token) - else: - return json.dumps("not authenticated"), 403 diff --git a/src/server/BreCal/impl/berths.py b/src/server/BreCal/impl/berths.py index 2abe922..d020b3f 100644 --- a/src/server/BreCal/impl/berths.py +++ b/src/server/BreCal/impl/berths.py @@ -5,7 +5,6 @@ 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): """ @@ -31,29 +30,6 @@ def GetBerths(token): pooledConnection.close() -def GetLatestBerths(token): - """ - Returns a datetime of the latest modification within the 'berth' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #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 f6b99df..a592f57 100644 --- a/src/server/BreCal/impl/history.py +++ b/src/server/BreCal/impl/history.py @@ -7,7 +7,6 @@ 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, get_latest_modification_time def GetHistory(options): @@ -39,24 +38,4 @@ def GetHistory(options): return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} -def GetLatestHistory(token): - """ - Returns a datetime of the latest modification within the 'history' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #TODO: should this become a data model? - """ - - try: - modification_time = get_latest_modification_time(key="history") - data = {"key":"history", "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/latest.py b/src/server/BreCal/impl/latest.py new file mode 100644 index 0000000..7bfa4b9 --- /dev/null +++ b/src/server/BreCal/impl/latest.py @@ -0,0 +1,32 @@ +import json +import logging +import pydapper +import datetime + +from ..schemas import model +from .. import local_db +from BreCal.api import get_latest_modification_times + +def GetLatest(token): + """ + Returns a dictionary of the latest modification dates within the databases 'ships', 'shipcalls' and 'times'. When there has not yet been a modification, this method returns null. + Creates a dictionary of type dict[str, typing.Optional[datetime.datetime]] + """ + + try: + data = get_latest_modification_times() + 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/login.py b/src/server/BreCal/impl/login.py index e208620..21b46a6 100644 --- a/src/server/BreCal/impl/login.py +++ b/src/server/BreCal/impl/login.py @@ -6,7 +6,6 @@ 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 9ca02ca..697b761 100644 --- a/src/server/BreCal/impl/notifications.py +++ b/src/server/BreCal/impl/notifications.py @@ -4,7 +4,6 @@ import pydapper 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 GetNotifications(options): """ @@ -31,25 +30,4 @@ def GetNotifications(options): return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} -def GetLatestNotifications(token): - """ - Returns a datetime of the latest modification within the 'notification' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #TODO: should this become a data model? - """ - - try: - modification_time = get_latest_modification_time(key="notifications") - data = {"key":"notifications", "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/participant.py b/src/server/BreCal/impl/participant.py index 2f334a9..aad0def 100644 --- a/src/server/BreCal/impl/participant.py +++ b/src/server/BreCal/impl/participant.py @@ -4,7 +4,6 @@ import pydapper 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 GetParticipant(options): """ @@ -34,25 +33,3 @@ def GetParticipant(options): if pooledConnection is not None: pooledConnection.close() - - -def GetLatestParticipants(token): - """ - Returns a datetime of the latest modification within the 'participant' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #TODO: should this become a data model? - """ - - try: - modification_time = get_latest_modification_time(key="participants") - data = {"key":"participants", "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/shipcalls.py b/src/server/BreCal/impl/shipcalls.py index 1577605..3b72c3c 100644 --- a/src/server/BreCal/impl/shipcalls.py +++ b/src/server/BreCal/impl/shipcalls.py @@ -9,7 +9,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, get_latest_modification_time +from BreCal.api import update_latest_modification_time def GetShipcalls(options): """ @@ -264,25 +264,3 @@ def PutShipcalls(schemaModel): finally: if pooledConnection is not None: pooledConnection.close() - - -def GetLatestShipcalls(token): - """ - Returns a datetime of the latest modification within the 'shipcall' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #TODO: should this become a data model? - """ - - try: - modification_time = get_latest_modification_time(key="shipcalls") - data = {"key":"shipcalls", "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/ships.py b/src/server/BreCal/impl/ships.py index 9b2107c..857a096 100644 --- a/src/server/BreCal/impl/ships.py +++ b/src/server/BreCal/impl/ships.py @@ -5,7 +5,7 @@ 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 +from BreCal.api import update_latest_modification_time def GetShips(token): """ @@ -159,24 +159,4 @@ def DeleteShip(options): result = {} result["message"] = "call failed" return json.dumps(result), 500, {'Content-Type': 'application/json; charset=utf-8'} - -def GetLatestShips(token): - """ - Returns a datetime of the latest modification within the 'ship' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #TODO: should this become a data model? - """ - - try: - modification_time = get_latest_modification_time(key="ships") - data = {"key":"ships", "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 + \ No newline at end of file diff --git a/src/server/BreCal/impl/times.py b/src/server/BreCal/impl/times.py index 6161313..fa9cb79 100644 --- a/src/server/BreCal/impl/times.py +++ b/src/server/BreCal/impl/times.py @@ -7,8 +7,8 @@ import datetime 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, get_latest_modification_time +from BreCal.api import update_latest_modification_time from BreCal.database.update_database import evaluate_shipcall_state def GetTimes(options): @@ -215,24 +215,3 @@ def DeleteTimes(options): finally: if pooledConnection is not None: pooledConnection.close() - -def GetLatestTimes(token): - """ - Returns a datetime of the latest modification within the 'times' database. When there has not yet been a modification, this method returns null. - Always creates an output dictionary with the format {key:string, value:str (datetime isoformat)} - - # #TODO: should this become a data model? - """ - - try: - modification_time = get_latest_modification_time(key="times") - data = {"key":"times", "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