providing an alternative python-filter for the berths by port id. This may be useful for future development or make MySQL-queries easier

This commit is contained in:
Max Metz 2024-10-24 15:54:51 +02:00
parent 5c1fcec490
commit 4a8ade1eef
2 changed files with 48 additions and 0 deletions

View File

@ -40,6 +40,53 @@ def get_port_ids_for_participant_id(participant_id:int):
pdata = execute_sql_query_standalone(query=query, param={"participant_id":participant_id})
return pdata
def filter_berths_by_port(berths:list[model.Berth], participant:model.Participant)->list[model.Berth]:
"""
this function filters a list of berths by the participant's assigned ports.
Uses:
berth.port_id (the port's int id of each berth)
Participant.ports (a list of int ids)
returns: berths (filtered)
"""
berths = [berth for berth in berths if berth.port_id in participant.ports]
return berths
def filter_berths_by_users_assigned_ports(berths:list[model.Berth], user_data:dict, commands=None)->list[model.Berth]:
"""
this function uses the user_id to filter a list of berths. Only those berths, which belong to the Participant's assigned
port list are relevant.
Uses:
berth.port_id (the port's int id of each berth)
user_data 'id' to obtain Participant
Participant.ports (a list of int ids)
returns: berths
"""
assert 'id' in list(user_data.keys())
if commands is None:
pooledConnection = local_db.getPoolConnection()
commands = pydapper.using(pooledConnection)
# filter: return only the relevant berths to a user, which belong to his assigned ports
user_participant_query = SQLQuery.get_participant_by_user_id()
# use the user's id to obtain the matching participant
sentinel = object()
participant = commands.query_single_or_default(
user_participant_query, default=sentinel, model=model.Participant,
param={"userid":user_data.get("id")})
if participant is sentinel:
raise Exception(f"could not find a user with id {user_data.get('id')}")
berths = filter_berths_by_port(berths, participant)
return berths
def filter_shipcalls_by_port(shipcalls:list[model.Shipcall], participant:model.Participant)->list[model.Shipcall]:
"""
this function filters a list of shipcalls by the participant's assigned ports.

View File

@ -4,6 +4,7 @@ import pydapper
from ..schemas import model
from .. import local_db
from BreCal.database.sql_utils import filter_berths_by_users_assigned_ports
def GetBerths(options):
"""