preparing novel endpoints for getlatest. Initially tried with berths, which does not have PUT/POST/DEL endpoints.
This commit is contained in:
parent
50e9261267
commit
8b43416e4c
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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):
|
||||
|
||||
|
||||
@ -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):
|
||||
|
||||
|
||||
@ -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):
|
||||
"""
|
||||
|
||||
@ -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):
|
||||
"""
|
||||
|
||||
@ -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):
|
||||
"""
|
||||
|
||||
@ -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'}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user