22
22
import fastapi
23
23
import requests
24
24
25
- from . import config , costing , exceptions
25
+ from . import config , costing , exceptions , models
26
26
27
27
VERIFICATION_ENDPOINT = {
28
28
"PRIVATE-TOKEN" : "/account/verification/pat" ,
@@ -183,17 +183,25 @@ def get_accepted_licences(auth_header: tuple[str, str]) -> set[tuple[str, int]]:
183
183
return accepted_licences
184
184
185
185
186
- def check_licences (
187
- required_licences : set [tuple [str , int ]], accepted_licences : set [tuple [str , int ]]
186
+ def verify_licences (
187
+ accepted_licences : set [tuple [str , int ]] | list [tuple [str , int ]],
188
+ required_licences : set [tuple [str , int ]] | list [tuple [str , int ]],
189
+ api_request_url : str ,
190
+ process_id : str ,
188
191
) -> set [tuple [str , int ]]:
189
- """Check if accepted licences satisfy required ones.
192
+ """
193
+ Verify if all the licences required for the process submission have been accepted.
190
194
191
195
Parameters
192
196
----------
193
- required_licences : set[tuple[str, int]]
194
- Required licences.
195
- accepted_licences : set[tuple[str, int]]
196
- Accepted licences.
197
+ accepted_licences : set[tuple[str, int]] | list[tuple[str, int]],
198
+ Licences accepted by a user stored in the Extended Profiles database.
199
+ required_licences : set[tuple[str, int]] | list[tuple[str, int]],
200
+ Licences bound to the required process/dataset.
201
+ api_request_url : str
202
+ API request URL, required to generate the URL to the dataset licences page.
203
+ process_id : str
204
+ Process identifier, required to generate the URL to the dataset licences page.
197
205
198
206
Returns
199
207
-------
@@ -205,39 +213,30 @@ def check_licences(
205
213
exceptions.PermissionDenied
206
214
Raised if not all required licences have been accepted.
207
215
"""
216
+ if not isinstance (accepted_licences , set ):
217
+ accepted_licences = set (accepted_licences )
218
+ if not isinstance (required_licences , set ):
219
+ required_licences = set (required_licences )
208
220
missing_licences = required_licences - accepted_licences
209
221
if not len (missing_licences ) == 0 :
210
- missing_licences_detail = [
211
- {"id" : licence [0 ], "revision" : licence [1 ]} for licence in missing_licences
212
- ]
222
+ missing_licences_message_template = (
223
+ config .ensure_settings ().missing_licences_message
224
+ )
225
+ dataset_licences_url_template = config .ensure_settings ().dataset_licences_url
226
+ parsed_api_request_url = urllib .parse .urlparse (api_request_url )
227
+ base_url = f"{ parsed_api_request_url .scheme } ://{ parsed_api_request_url .netloc } "
228
+ dataset_licences_url = dataset_licences_url_template .format (
229
+ base_url = base_url , process_id = process_id
230
+ )
231
+ missing_licences_message = missing_licences_message_template .format (
232
+ dataset_licences_url = dataset_licences_url
233
+ )
213
234
raise exceptions .PermissionDenied (
214
- title = "required licences not accepted" ,
215
- detail = (
216
- "required licences not accepted; "
217
- "please accept the following licences to proceed: "
218
- f"{ missing_licences_detail } "
219
- ),
235
+ title = "required licences not accepted" , detail = missing_licences_message
220
236
)
221
237
return missing_licences
222
238
223
239
224
- def validate_licences (
225
- accepted_licences : set [tuple [str , str ]],
226
- licences : list [tuple [str , int ]],
227
- ) -> None :
228
- """Validate process execution request's payload in terms of required licences.
229
-
230
- Parameters
231
- ----------
232
- stored_accepted_licences : set[tuple[str, str]]
233
- Licences accepted by a user stored in the Extended Profiles database.
234
- licences : list[tuple[str, int]]
235
- Licences bound to the required process/dataset.
236
- """
237
- required_licences = set (licences )
238
- check_licences (required_licences , accepted_licences ) # type: ignore
239
-
240
-
241
240
def verify_if_disabled (disabled_reason : str | None , user_role : str | None ) -> None :
242
241
"""Verify if a dataset's disabling reason grant access to the dataset for a specific user role.
243
242
@@ -262,7 +261,7 @@ def verify_if_disabled(disabled_reason: str | None, user_role: str | None) -> No
262
261
263
262
264
263
def verify_cost (
265
- request : dict [str , Any ], adaptor_properties : dict [str , Any ]
264
+ request : dict [str , Any ], adaptor_properties : dict [str , Any ], request_origin : str
266
265
) -> dict [str , float ] | None :
267
266
"""Verify if the cost of a process execution request is within the allowed limits.
268
267
@@ -272,6 +271,8 @@ def verify_cost(
272
271
Process execution request.
273
272
adaptor_properties : dict[str, Any]
274
273
Adaptor properties.
274
+ request_origin : str
275
+ Origin of the request. Can be either "api" or "ui".
275
276
276
277
Raises
277
278
------
@@ -283,9 +284,13 @@ def verify_cost(
283
284
dict[str, float] | None
284
285
Request costs.
285
286
"""
286
- costing_info = costing .compute_costing (request , adaptor_properties )
287
- max_costs_exceeded = costing_info .max_costs_exceeded
288
- if max_costs_exceeded :
287
+ costing_info : models .CostingInfo = costing .compute_costing (
288
+ request , adaptor_properties , request_origin
289
+ )
290
+ highest_cost : models .RequestCost = costing .compute_highest_cost_limit_ratio (
291
+ costing_info
292
+ )
293
+ if highest_cost .cost > highest_cost .limit :
289
294
raise exceptions .PermissionDenied (
290
295
title = "cost limits exceeded" ,
291
296
detail = "Your request is too large, please reduce your selection." ,
0 commit comments