11import os
22
3- import psycopg2
43import yaml
54from flask import Flask , jsonify , request
65from flask_sqlalchemy import SQLAlchemy
76from flask_swagger_ui import get_swaggerui_blueprint
87from sqlalchemy import create_engine
9- from sqlalchemy .exc import SQLAlchemyError
108from sqlalchemy .orm import sessionmaker
119
1210from api .enums import is_valid_action
2018 extract_delegates_mandates , extract_mandate_data ,
2119 extract_mandate_subdelegate_data ,
2220 extract_representee_mandates , get_mandates ,
23- get_roles_pg , subdelegate_mandate_pg , delete_subdelegated_mandates_pg , get_person_pg ,
24- get_deleted_mandates )
21+ get_roles_pg , subdelegate_mandate_pg , delete_subdelegated_mandates_pg )
2522from api .validators import (validate_add_mandate_payload ,
2623 validate_add_mandate_subdelegate_payload ,
2724 validate_person_company_code )
@@ -84,27 +81,37 @@ def handle_unhandled_error(e):
8481 return jsonify (base .to_dict ()), 500
8582
8683 @app .route ('/v1/delegates/<string:delegate_identifier>/representees/mandates' , methods = ['GET' ])
87- def get_delegates_representees_mandates (delegate_identifier ):
84+ def get_mandates_by_delegate (delegate_identifier ):
8885 xroad_user_id = request .headers .get ('X-Road-UserId' )
8986 app .logger .info (f'X-Road-UserId: { xroad_user_id } Getting delegate mandates' )
9087 error_config = app .config ['SETTINGS' ]['errors' ]['legal_person_format_validation_failed' ]
9188 validate_person_company_code (delegate_identifier , error_config )
9289
93- data_rows = get_mandates (db , delegate_identifier = delegate_identifier )
90+ args = request .args
91+ role_ns = args .get ('ns' )
92+ subdelegated_by_identifier = args .get ('subDelegatedBy' )
93+
94+ data_rows = get_mandates (
95+ db ,
96+ delegate_identifier = delegate_identifier ,
97+ role_ns = role_ns ,
98+ subdelegated_by_identifier = subdelegated_by_identifier
99+ )
94100 if not data_rows :
95101 return make_success_response (data_rows , 200 )
96102 delegate , representees = extract_delegates_mandates (data_rows )
97103 response_data = serialize_delegate_mandates (delegate , representees , app .config ['SETTINGS' ])
98104 return make_success_response (response_data , 200 )
99105
100106 @app .route ('/v1/representees/<string:representee_identifier>/delegates/mandates' , methods = ['GET' ])
101- def get_representees_delegates_mandates (representee_identifier ):
107+ def get_mandates_by_representee (representee_identifier ):
102108 xroad_user_id = request .headers .get ('X-Road-UserId' )
103109 app .logger .info (f'X-Road-UserId: { xroad_user_id } Getting representee mandates' )
104110
105111 args = request .args
106- subdelegated_by_identifier = args .get ('subDelegatedBy' )
107112 delegate_identifier = args .get ('delegate' )
113+ role_ns = args .get ('ns' )
114+ subdelegated_by_identifier = args .get ('subDelegatedBy' )
108115
109116 error_config = app .config ['SETTINGS' ]['errors' ]['legal_person_format_validation_failed' ]
110117 [
@@ -115,6 +122,7 @@ def get_representees_delegates_mandates(representee_identifier):
115122 db ,
116123 representee_identifier = representee_identifier ,
117124 delegate_identifier = delegate_identifier ,
125+ role_ns = role_ns ,
118126 subdelegated_by_identifier = subdelegated_by_identifier
119127 )
120128 if not data_rows :
@@ -144,13 +152,10 @@ def post_representee_delegate_mandate(representee_identifier, delegate_identifie
144152 db_uri = app .config ['SQLALCHEMY_DATABASE_URI' ]
145153 try :
146154 create_mandate_pg (db_uri , data_to_insert )
147- except psycopg2 . errors . RaiseException as e :
148- app .logger .exception (str (e ))
155+ except Exception as custom_exception :
156+ app .logger .exception (str (custom_exception ))
149157 error_config = app .config ['SETTINGS' ]['errors' ]['unprocessable_request' ]
150- raise UnprocessableRequestError (
151- 'Unprocessable request while creating mandate. Something went wrong.' ,
152- error_config
153- )
158+ raise UnprocessableRequestError (str (custom_exception ), error_config )
154159 return make_success_response ([], 201 )
155160
156161 @app .route (
@@ -206,7 +211,6 @@ def delete_mandate(representee_id, delegate_id, mandate_id):
206211 )
207212 def post_subdelegate_mandate (representee_id , delegate_id , mandate_id ):
208213 xroad_user_id = request .headers .get ('X-Road-UserId' )
209- xroad_represented_party = request .headers .get ('X-Road-Represented-Party' )
210214 app .logger .info (f'X-Road-UserId: { xroad_user_id } Creating subdelegate' )
211215
212216 data = request .json
@@ -224,13 +228,10 @@ def post_subdelegate_mandate(representee_id, delegate_id, mandate_id):
224228 data_to_insert ['data_created_by' ] = xroad_user_id
225229 try :
226230 result = subdelegate_mandate_pg (app .config ['SQLALCHEMY_DATABASE_URI' ], data_to_insert )
227- except psycopg2 . errors . RaiseException as e :
228- app .logger .exception (str (e ))
231+ except Exception as custom_exception :
232+ app .logger .exception (str (custom_exception ))
229233 error_config = app .config ['SETTINGS' ]['errors' ]['unprocessable_request' ]
230- raise UnprocessableRequestError (
231- 'Unprocessable request while subdelegating mandate. Something went wrong.' ,
232- error_config
233- )
234+ raise UnprocessableRequestError (str (custom_exception ), error_config )
234235 if not result :
235236 error_config = app .config ['SETTINGS' ]['errors' ]['mandate_not_found' ]
236237 raise MandateNotFound ('Mandate to delete was not found' , error_config )
@@ -242,24 +243,26 @@ def get_roles():
242243 roles_data = get_roles_pg (db )
243244 mapped = {
244245 'code' : 'code' ,
245- 'delegate_can_equal_to_representee' : 'delegateCanEqualToRepresentee' ,
246- 'modified' : 'modified' ,
247- 'validity_period_from_not_in_future' : 'validityPeriodFromNotInFuture' ,
248- 'validity_period_through_must_be_undefined' : 'validityPeriodThroughMustBeUndefined' ,
249- 'assignable_only_if_representee_has_role_in' : 'assignableOnlyIfRepresenteeHasRoleIn' ,
246+ 'delegate_must_equal_to_representee' : 'delegateMustEqualToRepresenteeOnAdd' ,
247+ 'addable_only_if_representee_has_role_in' : 'addableOnlyIfRepresenteeHasRoleIn' ,
250248 'delegate_type' : 'delegateType' ,
251- 'can_sub_delegate' : 'canSubDelegate' ,
249+ 'sub_delegable' : 'subDelegable' ,
250+ 'sub_delegate_type' : 'subDelegateType' ,
251+ 'sub_delegable_by' : 'subDelegableBy' ,
252+ 'sub_delegating_must_be_signed' : 'subDelegatingMustBeSigned' ,
252253 'addable_by' : 'addableBy' ,
253254 'adding_must_be_signed' : 'addingMustBeSigned' ,
254- 'assignable_by' : 'assignableBy' ,
255255 'waivable_by' : 'waivableBy' ,
256256 'waiving_must_be_signed' : 'waivingMustBeSigned' ,
257257 'withdrawable_by' : 'withdrawableBy' ,
258258 'withdrawal_must_be_signed' : 'withdrawalMustBeSigned' ,
259259 'deletable_by' : 'deletableBy' ,
260260 'deletable_by_delegate' : 'deletableByDelegate' ,
261261 'representee_type' : 'representeeType' ,
262- 'visible' : 'visible' ,
262+ 'hidden' : 'hidden' ,
263+ 'representee_identifier_in' : 'representeeIdentifierIn' ,
264+ 'validity_period_from_not_in_future' : 'validityPeriodFromNotInFuture' ,
265+ 'validity_period_through_must_be_undefined' : 'validityPeriodThroughMustBeUndefined'
263266 }
264267 for role in roles_data :
265268 role_item = {
0 commit comments