diff --git a/misc/update_1.1_to_1.2.sql b/misc/update_1.1_to_1.2.sql index 198b144..75179b6 100644 --- a/misc/update_1.1_to_1.2.sql +++ b/misc/update_1.1_to_1.2.sql @@ -54,7 +54,7 @@ ADD CONSTRAINT `FK_USER_PART` CREATE TABLE `bremen_calling_devel`.`history` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `participant_id` INT UNSIGNED NOT NULL, - `ship_id` INT UNSIGNED NOT NULL, + `shipcall_id` INT UNSIGNED NOT NULL, `timestamp` DATETIME NOT NULL COMMENT 'Time of saving', `eta` DATETIME NOT NULL COMMENT 'Current ETA / ETD value (depends if shipcall or times were saved)', `type` INT NOT NULL COMMENT 'shipcall or times', @@ -66,7 +66,7 @@ COMMENT = 'This table stores a history of changes made to shipcalls so that ever ALTER TABLE `bremen_calling_devel`.`history` ADD INDEX `FK_HISTORY_PARTICIPANT_idx` (`participant_id` ASC) VISIBLE, -ADD INDEX `FK_HISTORY_SHIP_idx` (`ship_id` ASC) VISIBLE; +ADD INDEX `FK_HISTORY_SHIPCALL_idx` (`shipcall_id` ASC) VISIBLE; ; ALTER TABLE `bremen_calling_devel`.`history` ADD CONSTRAINT `FK_HISTORY_PARTICIPANT` @@ -74,8 +74,8 @@ ADD CONSTRAINT `FK_HISTORY_PARTICIPANT` REFERENCES `bremen_calling_devel`.`participant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, -ADD CONSTRAINT `FK_HISTORY_SHIP` - FOREIGN KEY (`ship_id`) - REFERENCES `bremen_calling_devel`.`ship` (`id`) +ADD CONSTRAINT `FK_HISTORY_SHIPCALL` + FOREIGN KEY (`shipcall_id`) + REFERENCES `bremen_calling_devel`.`shipcall` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION; diff --git a/src/server/BreCal/__init__.py b/src/server/BreCal/__init__.py index 4ee4bbf..eb832f5 100644 --- a/src/server/BreCal/__init__.py +++ b/src/server/BreCal/__init__.py @@ -12,6 +12,7 @@ from .api import berths from .api import ships from .api import login from .api import user +from .api import history 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 @@ -59,7 +60,7 @@ def create_app(test_config=None): app.register_blueprint(ships.bp) app.register_blueprint(login.bp) app.register_blueprint(user.bp) - + app.register_blueprint(history.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)) @@ -73,8 +74,8 @@ def create_app(test_config=None): return app __all__ = [ - "get_project_root", - "ensure_path", + "get_project_root", + "ensure_path", "execute_test_with_pytest", "execute_coverage_test", "difference_to_then", diff --git a/src/server/BreCal/api/history.py b/src/server/BreCal/api/history.py new file mode 100644 index 0000000..c1fe5ad --- /dev/null +++ b/src/server/BreCal/api/history.py @@ -0,0 +1,21 @@ +from flask import Blueprint, request +from .. import impl +from ..services.auth_guard import auth_guard +import json + +bp = Blueprint('history', __name__) + +@bp.route('/history', methods=['get']) +@auth_guard() # no restriction by role +def GetParticipant(): + + if 'Authorization' in request.headers: + token = request.headers.get('Authorization') + options = {} + if not 'shipcall_id' in request.args: + return json.dumps("missing parameter"), 400 + options["shipcall_id"] = request.args.get("shipcall_id") + return impl.history.GetHistory(options) + else: + return json.dumps("not authenticated"), 403 + diff --git a/src/server/BreCal/impl/__init__.py b/src/server/BreCal/impl/__init__.py index 4e1e218..93ec9fc 100644 --- a/src/server/BreCal/impl/__init__.py +++ b/src/server/BreCal/impl/__init__.py @@ -5,4 +5,5 @@ from . import shipcalls from . import times from . import ships from . import login -from . import user \ No newline at end of file +from . import user +from . import history \ No newline at end of file diff --git a/src/server/BreCal/impl/berths.py b/src/server/BreCal/impl/berths.py index d41025c..e6b8d32 100644 --- a/src/server/BreCal/impl/berths.py +++ b/src/server/BreCal/impl/berths.py @@ -10,8 +10,6 @@ def GetBerths(token): No parameters, gets all entries """ - # TODO: validate token - try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) diff --git a/src/server/BreCal/impl/history.py b/src/server/BreCal/impl/history.py new file mode 100644 index 0000000..f3a10ff --- /dev/null +++ b/src/server/BreCal/impl/history.py @@ -0,0 +1,31 @@ +import json +import logging +import pydapper + +from ..schemas import model +from .. import local_db + +def GetHistory(options): + + """ + :param options: A dictionary containing all the paramters for the Operations + options["shipcall_id"]: **Id of shipcall**. + """ + + try: + pooledConnection = local_db.getPoolConnection() + commands = pydapper.using(pooledConnection) + if "shipcall_id" in options and options["shipcall_id"]: + data = commands.query("SELECT id, participant_id, shipcall_id, timestamp, eta, type, operation FROM history WHERE shipcall_id = ?shipcallid?", model=model.History, param={"shipcallid" : options["shipcall_id"]}) + + pooledConnection.close() + + except Exception as ex: + logging.error(ex) + print(ex) + result = {} + result["message"] = "call failed" + return json.dumps("call failed"), 500 + + return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} + diff --git a/src/server/BreCal/impl/participant.py b/src/server/BreCal/impl/participant.py index be1d7ec..05937d4 100644 --- a/src/server/BreCal/impl/participant.py +++ b/src/server/BreCal/impl/participant.py @@ -11,7 +11,6 @@ def GetParticipant(options): options["user_id"]: **Id of user**. *Example: 2*. User id returned by login call. """ - # TODO: validate token try: pooledConnection = local_db.getPoolConnection() diff --git a/src/server/BreCal/impl/shipcalls.py b/src/server/BreCal/impl/shipcalls.py index 452a8be..30f1c39 100644 --- a/src/server/BreCal/impl/shipcalls.py +++ b/src/server/BreCal/impl/shipcalls.py @@ -13,8 +13,6 @@ def GetShipcalls(options): No parameters, gets all entries """ - # TODO: validate token - try: pooledConnection = local_db.getPoolConnection() diff --git a/src/server/BreCal/impl/ships.py b/src/server/BreCal/impl/ships.py index 71858e5..66c46ec 100644 --- a/src/server/BreCal/impl/ships.py +++ b/src/server/BreCal/impl/ships.py @@ -10,8 +10,6 @@ def GetShips(token): No parameters, gets all entries """ - # TODO: validate token - try: pooledConnection = local_db.getPoolConnection() diff --git a/src/server/BreCal/impl/times.py b/src/server/BreCal/impl/times.py index 56e4c3f..718a522 100644 --- a/src/server/BreCal/impl/times.py +++ b/src/server/BreCal/impl/times.py @@ -14,8 +14,6 @@ def GetTimes(options): """ - # TODO: validate token - try: pooledConnection = local_db.getPoolConnection() diff --git a/src/server/BreCal/schemas/model.py b/src/server/BreCal/schemas/model.py index 20033f6..faaaac7 100644 --- a/src/server/BreCal/schemas/model.py +++ b/src/server/BreCal/schemas/model.py @@ -257,6 +257,18 @@ class ShipSchema(Schema): modified = fields.DateTime(allow_none=True, metadata={'Required':False}) deleted = fields.Int(allow_none=True, metadata={'Required':False}) + +@dataclass +class History(Schema): + id: int + participant_id: int + ship_id: int + timestamp: datetime + eta: datetime + type: int + operation: int + + class TimesId(Schema): pass