solving conflicts

This commit is contained in:
Max Metz 2024-10-24 14:56:59 +02:00
parent 72d3ad05bf
commit 1f0540f127
3 changed files with 61 additions and 6 deletions

View File

@ -1,6 +1,11 @@
from BreCal.database.sql_handler import execute_sql_query_standalone import pydapper
import datetime import datetime
from BreCal import local_db
from BreCal.schemas import model
from BreCal.database.sql_queries import SQLQuery
from BreCal.database.sql_handler import execute_sql_query_standalone
def get_user_data_for_id(user_id:int, expiration_time:int=90): def get_user_data_for_id(user_id:int, expiration_time:int=90):
"""debugging function, which is useful to pull user_data from the database, which may be used to create stub data and unit tests""" """debugging function, which is useful to pull user_data from the database, which may be used to create stub data and unit tests"""
query = "SELECT * FROM user where id = ?id?" query = "SELECT * FROM user where id = ?id?"
@ -33,4 +38,47 @@ def get_port_ids_for_participant_id(participant_id:int):
"""helper function to load all port ids for a participant""" """helper function to load all port ids for a participant"""
query = "SELECT port_id FROM participant_port_map where participant_id = ?participant_id?" query = "SELECT port_id FROM participant_port_map where participant_id = ?participant_id?"
pdata = execute_sql_query_standalone(query=query, param={"participant_id":participant_id}) pdata = execute_sql_query_standalone(query=query, param={"participant_id":participant_id})
return pdata return pdata
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.
Uses:
shipcall.port_id (the port's int id of each shipcall)
Participant.ports (a list of int ids)
returns: shipcalls (filtered)
"""
shipcalls = [shipcall for shipcall in shipcalls if shipcall.port_id in participant.ports]
return shipcalls
def filter_shipcalls_by_users_assigned_ports(shipcalls:list[model.Shipcall], user_data:dict, commands=None)->list[model.Shipcall]:
"""
this function uses the user_id to filter a list of shipcalls. Only those shipcalls, which belong to the Participant's assigned
port list are relevant.
Uses:
shipcall.port_id (the port's int id of each shipcall)
user_data 'id' to obtain Participant
Participant.ports (a list of int ids)
returns: shipcalls
"""
assert 'id' in list(user_data.keys())
if commands is None:
pooledConnection = local_db.getPoolConnection()
commands = pydapper.using(pooledConnection)
# filter: return only the relevant shipcalls 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')}")
shipcalls = filter_shipcalls_by_port(shipcalls, participant)
return shipcalls

View File

@ -8,11 +8,12 @@ from .. import local_db
from ..services.auth_guard import check_jwt from ..services.auth_guard import check_jwt
from BreCal.database.update_database import evaluate_shipcall_state from BreCal.database.update_database import evaluate_shipcall_state
from BreCal.database.sql_utils import filter_shipcalls_by_users_assigned_ports
from BreCal.database.sql_queries import create_sql_query_shipcall_get, create_sql_query_shipcall_post, create_sql_query_shipcall_put, create_sql_query_history_post, create_sql_query_history_put, SQLQuery from BreCal.database.sql_queries import create_sql_query_shipcall_get, create_sql_query_shipcall_post, create_sql_query_shipcall_put, create_sql_query_history_post, create_sql_query_history_put, SQLQuery
from marshmallow import Schema, fields, ValidationError from marshmallow import Schema, fields, ValidationError
from BreCal.validators.validation_error import create_validation_error_response from BreCal.validators.validation_error import create_validation_error_response
def GetShipcalls(options): def GetShipcalls(user_data, options):
""" """
No parameters, gets all entries No parameters, gets all entries
""" """
@ -24,8 +25,12 @@ def GetShipcalls(options):
# query = SQLQuery.get_shipcalls(options) # query = SQLQuery.get_shipcalls(options)
query = create_sql_query_shipcall_get(options) query = create_sql_query_shipcall_get(options)
data = commands.query(query, model=model.Shipcall.from_query_row, buffered=True) shipcalls = commands.query(query, model=model.Shipcall.from_query_row, buffered=True)
for shipcall in data:
# filter: return only the relevant shipcalls to a user, which belong to his assigned ports
shipcalls = filter_shipcalls_by_users_assigned_ports(shipcalls, user_data, commands=commands)
for shipcall in shipcalls:
# participant_query = SQLQuery.get_participants() # participant_query = SQLQuery.get_participants()
# participants = commands.query(participant_query, model=dict, param={"shipcall_id" : shipcall.id}, buffered=False) # participants = commands.query(participant_query, model=dict, param={"shipcall_id" : shipcall.id}, buffered=False)
# for record in participants: # for record in participants:
@ -35,7 +40,7 @@ def GetShipcalls(options):
pa = model.Participant_Assignment(record["participant_id"], record["type"]) pa = model.Participant_Assignment(record["participant_id"], record["type"])
shipcall.participants.append(pa) shipcall.participants.append(pa)
return json.dumps(data, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'} return json.dumps(shipcalls, default=model.obj_dict), 200, {'Content-Type': 'application/json; charset=utf-8'}
except Exception as ex: except Exception as ex:
logging.error(traceback.format_exc()) logging.error(traceback.format_exc())

View File

@ -14,6 +14,7 @@ def get_shipcall_simple():
shipcall_id = 124 # generate_uuid1_int() shipcall_id = 124 # generate_uuid1_int()
ship_id = 5 # generate_uuid1_int() ship_id = 5 # generate_uuid1_int()
port_id = 12
eta = base_time+datetime.timedelta(hours=3, minutes=12) eta = base_time+datetime.timedelta(hours=3, minutes=12)
role_type = 1 role_type = 1
@ -84,6 +85,7 @@ def get_shipcall_simple():
evaluation_time, evaluation_time,
evaluation_notifications_sent, evaluation_notifications_sent,
time_ref_point, time_ref_point,
port_id,
created, created,
modified, modified,
participants, participants,