Release pool connection handle und all circumstances especially also when a query fails before the call is finished. This should avoid connection starvation. fix prod. link production fix Fixed application path
68 lines
3.4 KiB
Python
68 lines
3.4 KiB
Python
import json
|
|
import logging
|
|
import pydapper
|
|
|
|
from ..schemas import model
|
|
from .. import local_db
|
|
from BreCal.database.sql_queries import SQLQuery
|
|
|
|
def GetParticipant(options):
|
|
"""
|
|
:param options: A dictionary containing all the paramters for the Operations
|
|
options["user_id"]: **Id of user**. *Example: 2*. User id returned by login call.
|
|
"""
|
|
|
|
pooledConnection = None
|
|
try:
|
|
pooledConnection = local_db.getPoolConnection()
|
|
commands = pydapper.using(pooledConnection)
|
|
if "user_id" in options and options["user_id"]:
|
|
# query = SQLQuery.get_participant_by_user_id()
|
|
query = ("SELECT p.id as id, p.name as name, p.street as street, p.postal_code as postal_code, p.city as city, p.type as type, p.flags as flags, " +
|
|
"p.created as created, p.modified as modified, p.deleted as deleted FROM participant p " +
|
|
"INNER JOIN user u WHERE u.participant_id = p.id and u.id = %s") % options["user_id"]
|
|
data = commands.query(query, model=model.Participant)
|
|
for participant in data:
|
|
port_query = "SELECT port_id FROM participant_port_map WHERE participant_id=?id?"
|
|
for record in commands.query(port_query, model=model.Port_Assignment, param={"id" : participant.id}, buffered=False):
|
|
pa = model.Port_Assignment(record.port_id)
|
|
participant.ports.append(pa.port_id)
|
|
else:
|
|
# query = SQLQuery.get_participants()
|
|
if "participant_id" in options:
|
|
# list only participants that are assigned to the same ports than participant of caller
|
|
query = ("SELECT p.id as id, name, street, postal_code, city, type, flags, p.created, p.modified, p.deleted " +
|
|
"FROM participant p " +
|
|
"JOIN participant_port_map ON p.id = participant_port_map.participant_id " +
|
|
"WHERE participant_port_map.port_id IN " +
|
|
"(SELECT port_id FROM participant_port_map where participant_id = %s) " +
|
|
"GROUP BY id " +
|
|
"ORDER BY p.name") % options["participant_id"]
|
|
else:
|
|
query = ("SELECT p.id as id, name, street, postal_code, city, type, flags, p.created, p.modified, p.deleted " +
|
|
"FROM participant p " +
|
|
"JOIN participant_port_map ON p.id = participant_port_map.participant_id " +
|
|
"GROUP BY id " +
|
|
"ORDER BY p.name")
|
|
|
|
data = commands.query(query, model=model.Participant)
|
|
for participant in data:
|
|
port_query = "SELECT port_id FROM participant_port_map WHERE participant_id=?id?"
|
|
for record in commands.query(port_query, model=model.Port_Assignment, param={"id" : participant.id}, buffered=False):
|
|
pa = model.Port_Assignment(record.port_id)
|
|
participant.ports.append(pa.port_id)
|
|
|
|
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("call failed"), 500
|
|
|
|
finally:
|
|
if pooledConnection is not None:
|
|
pooledConnection.close()
|
|
|