import json import logging import pydapper from ..schemas import model from .. import local_db def GetBerths(options): """ No parameters, gets all entries """ pooledConnection = None try: pooledConnection = local_db.getPoolConnection() commands = pydapper.using(pooledConnection) # only load berths to ports that the participant is assigned to if "participant_id" in options: query = ("SELECT id, name, `lock`, owner_id, port_id, authority_id, created, modified, deleted FROM berth WHERE " + "deleted = 0 AND + " "port_id IN (SELECT port_id FROM participant_port_map WHERE participant_id = %d) " + "ORDER BY name") % (options["participant_id"]) else: query = ("SELECT id, name, `lock`, owner_id, port_id, authority_id, created, modified, deleted FROM berth WHERE " + "deleted = 0 ORDER BY name") data = commands.query(query, model=model.Berth) return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} except Exception as ex: logging.error(ex) print(ex) result = {} result["error_field"] = "call failed" return json.dumps(result), 500 finally: if pooledConnection is not None: pooledConnection.close()