6
6
"""
7
7
8
8
import logging
9
+ import textwrap
9
10
import uuid as uuidlib
10
11
from collections import deque
11
12
from datetime import datetime
@@ -170,7 +171,7 @@ async def add_project(
170
171
force_project_uuid = False ,
171
172
force_as_template = False ,
172
173
) -> Dict :
173
- """ Inserts a new project in the database and, if a user is specified, it assigns ownership
174
+ """Inserts a new project in the database and, if a user is specified, it assigns ownership
174
175
175
176
- A valid uuid is automaticaly assigned to the project except if force_project_uuid=False. In the latter case,
176
177
invalid uuid will raise an exception.
@@ -193,7 +194,10 @@ async def add_project(
193
194
# TODO: check best rollback design. see transaction.begin...
194
195
# TODO: check if template, otherwise standard (e.g. template- prefix in uuid)
195
196
prj .update (
196
- {"creationDate" : now_str (), "lastChangeDate" : now_str (),}
197
+ {
198
+ "creationDate" : now_str (),
199
+ "lastChangeDate" : now_str (),
200
+ }
197
201
)
198
202
kargs = _convert_to_db_names (prj )
199
203
kargs .update (
@@ -249,13 +253,15 @@ async def load_user_projects(self, user_id: int) -> List[Dict]:
249
253
log .info ("Loading projects for user %s" , user_id )
250
254
async with self .engine .acquire () as conn :
251
255
user_groups : List [RowProxy ] = await self .__load_user_groups (conn , user_id )
252
- query = f"""
253
- SELECT *
254
- FROM projects
255
- WHERE projects.type != 'TEMPLATE'
256
- AND (jsonb_exists_any(projects.access_rights, array[{ ', ' .join (f"'{ group .gid } '" for group in user_groups )} ])
257
- OR prj_owner = { user_id } )
258
- """
256
+ query = textwrap .dedent (
257
+ f"""\
258
+ SELECT *
259
+ FROM projects
260
+ WHERE projects.type != 'TEMPLATE'
261
+ AND (jsonb_exists_any(projects.access_rights, array[{ ', ' .join (f"'{ group .gid } '" for group in user_groups )} ])
262
+ OR prj_owner = { user_id } )
263
+ """
264
+ )
259
265
projects_list = await self .__load_projects (
260
266
conn , query , user_id , user_groups
261
267
)
@@ -272,16 +278,20 @@ async def load_template_projects(
272
278
273
279
async with self .engine .acquire () as conn :
274
280
user_groups : List [RowProxy ] = await self .__load_user_groups (conn , user_id )
281
+
275
282
# NOTE: in order to use specific postgresql function jsonb_exists_any we use raw call here
276
- query = f"""
277
- SELECT *
278
- FROM projects
279
- WHERE projects.type = 'TEMPLATE'
280
- { 'AND projects.published ' if only_published else '' }
281
- AND (jsonb_exists_any(projects.access_rights, array[{ ', ' .join (f"'{ group .gid } '" for group in user_groups )} ])
282
- OR prj_owner = { user_id } )
283
- """
283
+ query = textwrap .dedent (
284
+ f"""\
285
+ SELECT *
286
+ FROM projects
287
+ WHERE projects.type = 'TEMPLATE'
288
+ { 'AND projects.published ' if only_published else '' }
289
+ AND (jsonb_exists_any(projects.access_rights, array[{ ', ' .join (f"'{ group .gid } '" for group in user_groups )} ])
290
+ OR prj_owner = { user_id } )
291
+ """
292
+ )
284
293
db_projects = await self .__load_projects (conn , query , user_id , user_groups )
294
+
285
295
projects_list .extend (db_projects )
286
296
287
297
return projects_list
@@ -337,15 +347,17 @@ async def _get_project(
337
347
user_groups : List [RowProxy ] = await self .__load_user_groups (conn , user_id )
338
348
339
349
# NOTE: in order to use specific postgresql function jsonb_exists_any we use raw call here
340
- query = f"""
341
- SELECT *
342
- FROM projects
343
- WHERE
344
- { "" if include_templates else "projects.type != 'TEMPLATE' AND" }
345
- uuid = '{ project_uuid } '
346
- AND (jsonb_exists_any(projects.access_rights, array[{ ', ' .join (f"'{ group .gid } '" for group in user_groups )} ])
347
- OR prj_owner = { user_id } )
348
- """
350
+ query = textwrap .dedent (
351
+ f"""\
352
+ SELECT *
353
+ FROM projects
354
+ WHERE
355
+ { "" if include_templates else "projects.type != 'TEMPLATE' AND" }
356
+ uuid = '{ project_uuid } '
357
+ AND (jsonb_exists_any(projects.access_rights, array[{ ', ' .join (f"'{ group .gid } '" for group in user_groups )} ])
358
+ OR prj_owner = { user_id } )
359
+ """
360
+ )
349
361
result = await conn .execute (query )
350
362
project_row = await result .first ()
351
363
@@ -392,7 +404,7 @@ async def remove_tag(self, user_id: int, project_uuid: str, tag_id: int) -> Dict
392
404
return _convert_to_schema_names (project , user_email )
393
405
394
406
async def get_user_project (self , user_id : int , project_uuid : str ) -> Dict :
395
- """ Returns all projects *owned* by the user
407
+ """Returns all projects *owned* by the user
396
408
397
409
- prj_owner
398
410
- Notice that a user can have access to a template but he might not onw it
@@ -448,16 +460,21 @@ async def get_template_project(
448
460
return template_prj
449
461
450
462
async def update_user_project (
451
- self , project_data : Dict , user_id : int , project_uuid : str , include_templates : Optional [bool ] = False
463
+ self ,
464
+ project_data : Dict ,
465
+ user_id : int ,
466
+ project_uuid : str ,
467
+ include_templates : Optional [bool ] = False ,
452
468
):
453
- """ updates a project from a user
454
-
455
- """
469
+ """updates a project from a user"""
456
470
log .info ("Updating project %s for user %s" , project_uuid , user_id )
457
471
458
472
async with self .engine .acquire () as conn :
459
473
row = await self ._get_project (
460
- user_id , project_uuid , exclude_foreign = ["tags" ], include_templates = include_templates
474
+ user_id ,
475
+ project_uuid ,
476
+ exclude_foreign = ["tags" ],
477
+ include_templates = include_templates ,
461
478
)
462
479
user_groups : List [RowProxy ] = await self .__load_user_groups (conn , user_id )
463
480
_check_project_permissions (row , user_id , user_groups , "write" )
@@ -505,7 +522,7 @@ async def delete_user_project(self, user_id: int, project_uuid: str):
505
522
)
506
523
507
524
async def make_unique_project_uuid (self ) -> str :
508
- """ Generates a project identifier still not used in database
525
+ """Generates a project identifier still not used in database
509
526
510
527
WARNING: this method does not guarantee always unique id due to possible race condition
511
528
(i.e. while client gets this uuid and uses it, another client might have used the same id already)
0 commit comments