44from flask import Flask , jsonify , request
55from flask_sqlalchemy import SQLAlchemy
66
7- from api .exceptions import (CompanyCodeInvalid , DelegateNotFound ,
7+ from api .exceptions import (CompanyCodeInvalid ,
88 ErrorConfigBase , MandateDataInvalid ,
99 MandateNotFound , MandateSubdelegateDataInvalid ,
10- RepresenteeNotFound , UnprocessableRequestError )
10+ UnprocessableRequestError , ActionInvalid )
1111from api .serializers import (serialize_delegate_mandates ,
1212 serialize_representee_mandates )
1313from api .services import (create_mandate_pg , delete_mandate_pg ,
@@ -46,11 +46,10 @@ def create_app():
4646 db .init_app (app )
4747 app .config ['SETTINGS' ] = parse_settings (app .config ['SETTINGS_PATH' ])
4848
49+ app .errorhandler (ActionInvalid )(create_error_handler (501 ))
4950 app .errorhandler (CompanyCodeInvalid )(create_error_handler (400 ))
5051 app .errorhandler (MandateDataInvalid )(create_error_handler (400 ))
5152 app .errorhandler (MandateSubdelegateDataInvalid )(create_error_handler (400 ))
52- app .errorhandler (RepresenteeNotFound )(create_error_handler (404 ))
53- app .errorhandler (DelegateNotFound )(create_error_handler (404 ))
5453 app .errorhandler (MandateNotFound )(create_error_handler (404 ))
5554 app .errorhandler (UnprocessableRequestError )(create_error_handler (422 ))
5655
@@ -99,10 +98,8 @@ def get_representees_delegates_mandates(representee_id):
9998 delegate_identifier = delegate_identifier ,
10099 subdelegated_by_identifier = subdelegated_by_identifier
101100 )
102- # does representee really uknown ?
103101 if not data_rows :
104- error_config = app .config ['SETTINGS' ]['errors' ]['representee_not_found' ]
105- raise RepresenteeNotFound ('Representee not found' , error_config )
102+ return make_success_response (data_rows , 200 )
106103
107104 representee , delegates = extract_representee_mandates (data_rows )
108105 response_data = serialize_representee_mandates (representee , delegates , app .config ['SETTINGS' ])
@@ -139,16 +136,20 @@ def post_representee_delegate_mandate(representee_id, delegate_id):
139136 return make_success_response ([], 201 )
140137
141138 @app .route (
142- '/nss/<string:ns>/ representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>' ,
143- methods = ['DELETE ' ]
139+ '/representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>' ,
140+ methods = ['PUT ' ]
144141 )
145- def delete_mandate (ns , representee_id , delegate_id , mandate_id ):
142+ def delete_mandate (representee_id , delegate_id , mandate_id ):
146143 xroad_user_id = request .headers .get ('X-Road-UserId' )
147144 app .logger .info (f'X-Road-UserId: { xroad_user_id } Deleting mandate' )
145+ data = request .json
146+ if data ['action' ] != 'DELETE' :
147+ error_config = app .config ['SETTINGS' ]['errors' ]['action_invalid' ]
148+ raise ActionInvalid ("Action invalid" , error_config )
148149
149150 db_uri = app .config ['SQLALCHEMY_DATABASE_URI' ]
150151 try :
151- deleted = delete_mandate_pg (db_uri , ns , representee_id , delegate_id , mandate_id )
152+ deleted = delete_mandate_pg (db_uri , representee_id , delegate_id , mandate_id )
152153 except psycopg2 .errors .RaiseException as e :
153154 app .logger .exception (str (e ))
154155 error_config = app .config ['SETTINGS' ]['errors' ]['unprocessable_request' ]
@@ -160,10 +161,10 @@ def delete_mandate(ns, representee_id, delegate_id, mandate_id):
160161 raise MandateNotFound ('Mandate to delete was not found' , error_config )
161162
162163 @app .route (
163- '/nss/<string:ns>/ representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>/subdelegates' ,
164+ '/representees/<string:representee_id>/delegates/<string:delegate_id>/mandates/<string:mandate_id>/subdelegates' ,
164165 methods = ['POST' ]
165166 )
166- def post_subdelegate_mandate (ns , representee_id , delegate_id , mandate_id ):
167+ def post_subdelegate_mandate (representee_id , delegate_id , mandate_id ):
167168 xroad_user_id = request .headers .get ('X-Road-UserId' )
168169 xroad_represented_party = request .headers .get ('X-Road-Represented-Party' )
169170 app .logger .info (f'X-Road-UserId: { xroad_user_id } Creating subdelegate' )
0 commit comments