Skip to content

Commit d623b14

Browse files
authored
Merge pull request #7 from e-gov/PH-1067_update-requests
PH-1067 Pythoni näidisrakenduse up-to-date viimine vahepeal lisandunud funktsionaalsuse osas
2 parents 0635848 + e5712b2 commit d623b14

11 files changed

Lines changed: 366 additions & 232 deletions

api/app.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import os
22

3-
import psycopg2
43
import yaml
54
from flask import Flask, jsonify, request
65
from flask_sqlalchemy import SQLAlchemy
76
from flask_swagger_ui import get_swaggerui_blueprint
87
from sqlalchemy import create_engine
9-
from sqlalchemy.exc import SQLAlchemyError
108
from sqlalchemy.orm import sessionmaker
119

1210
from api.enums import is_valid_action
@@ -20,8 +18,7 @@
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)
2522
from 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 = {

api/enums.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
class Action(Enum):
55
DELETE_WITHDRAW = 'DELETE_WITHDRAW'
66
DELETE_WAIVE = 'DELETE_WAIVE'
7-
DELETE = 'DELETE'
87

98

109
def is_valid_action(value):

api/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def serialize_mandate(representee, delegate, mandate, settings):
8181
mandate_data = {
8282
'links': links,
8383
'role': mandate['role'],
84+
'subDelegable': mandate['can_sub_delegate'],
8485
**({'subDelegatorIdentifier': mandate['subdelegated_by_identifier']}
8586
if mandate['subdelegated_by_identifier'] else {}),
8687
**({'validityPeriod': validity_period}

api/services.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
from sqlalchemy import text
66

77

8-
def get_mandates(db, representee_identifier=None, delegate_identifier=None, subdelegated_by_identifier=None):
8+
def get_mandates(
9+
db,
10+
representee_identifier=None,
11+
delegate_identifier=None,
12+
role_ns=None,
13+
subdelegated_by_identifier=None
14+
):
915
params = {
1016
'date_now': datetime.today()
1117
}
@@ -20,6 +26,10 @@ def get_mandates(db, representee_identifier=None, delegate_identifier=None, subd
2026
where_conditions.append('delegate_identifier=:delegate_identifier')
2127
params['delegate_identifier'] = delegate_identifier
2228

29+
if role_ns:
30+
where_conditions.append("role_ns=:role_ns")
31+
params['role_ns'] = role_ns
32+
2333
if subdelegated_by_identifier:
2434
where_conditions.append('subdelegated_by_identifier=:subdelegated_by_identifier')
2535
params['subdelegated_by_identifier'] = subdelegated_by_identifier

0 commit comments

Comments
 (0)