19
19
20
20
"""Base for Gramps object API resources."""
21
21
22
- import json
23
- from typing import Dict , List , TypeVar
22
+ from typing import TypeVar
24
23
25
24
import gramps_ql as gql
26
25
import object_ql as oql
27
26
from flask import abort , request
27
+ from flask_jwt_extended import get_jwt_identity
28
28
from gramps .gen .const import GRAMPS_LOCALE as glocale
29
29
from gramps .gen .db import DbTxn
30
30
from gramps .gen .db .base import DbReadBase
37
37
from webargs import fields , validate
38
38
39
39
from gramps_webapi .api .search .indexer import SemanticSearchIndexer
40
- from gramps_webapi .types import Handle , ResponseReturnValue
40
+ from gramps_webapi .types import ResponseReturnValue
41
41
42
42
from ...auth .const import PERM_ADD_OBJ , PERM_DEL_OBJ , PERM_EDIT_OBJ
43
43
from ...const import GRAMPS_OBJECT_PLURAL
44
44
from ..auth import require_permissions
45
45
from ..search import SearchIndexer , get_search_indexer , get_semantic_search_indexer
46
+ from ..tasks import run_task , update_search_indices_from_transaction
46
47
from ..util import (
47
48
check_quota_people ,
48
49
get_db_handle ,
@@ -82,7 +83,7 @@ class GrampsObjectResourceHelper(GrampsJSONEncoder):
82
83
83
84
gramps_class_name : str
84
85
85
- def full_object (self , obj : T , args : Dict , locale : GrampsLocale = glocale ) -> T :
86
+ def full_object (self , obj : T , args : dict , locale : GrampsLocale = glocale ) -> T :
86
87
"""Get the full object with extended attributes and backlinks."""
87
88
if args .get ("backlinks" ):
88
89
obj .backlinks = get_backlinks (self .db_handle , obj .handle )
@@ -104,21 +105,21 @@ def full_object(self, obj: T, args: Dict, locale: GrampsLocale = glocale) -> T:
104
105
)
105
106
return obj
106
107
107
- def object_extend (self , obj : T , args : Dict , locale : GrampsLocale = glocale ) -> T :
108
+ def object_extend (self , obj : T , args : dict , locale : GrampsLocale = glocale ) -> T :
108
109
"""Extend the base object attributes as needed."""
109
110
if "extend" in args :
110
111
obj .extended = get_extended_attributes (self .db_handle , obj , args )
111
112
return obj
112
113
113
114
def sort_objects (
114
- self , objects : List [GrampsObject ], args : Dict , locale : GrampsLocale = glocale
115
- ) -> List :
115
+ self , objects : list [GrampsObject ], args : dict , locale : GrampsLocale = glocale
116
+ ) -> list :
116
117
"""Sort the list of objects as needed."""
117
118
return sort_objects (
118
119
self .db_handle , self .gramps_class_name , objects , args , locale = locale
119
120
)
120
121
121
- def match_dates (self , objects : List [GrampsObject ], date : str ) -> List [GrampsObject ]:
122
+ def match_dates (self , objects : list [GrampsObject ], date : str ) -> list [GrampsObject ]:
122
123
"""If supported filter objects using date mask."""
123
124
if self .gramps_class_name in ["Event" , "Media" , "Citation" ]:
124
125
return match_dates (objects , date )
@@ -232,7 +233,7 @@ class GrampsObjectResource(GrampsObjectResourceHelper, Resource):
232
233
},
233
234
location = "query" ,
234
235
)
235
- def get (self , args : Dict , handle : str ) -> ResponseReturnValue :
236
+ def get (self , args : dict , handle : str ) -> ResponseReturnValue :
236
237
"""Get the object."""
237
238
try :
238
239
obj = self .get_object_from_handle (handle )
@@ -290,17 +291,13 @@ def put(self, handle: str) -> ResponseReturnValue:
290
291
trans_dict = transaction_to_json (trans )
291
292
# update search index
292
293
tree = get_tree_from_jwt_or_fail ()
293
- indexer : SearchIndexer = get_search_indexer (tree )
294
- for _trans_dict in trans_dict :
295
- handle = _trans_dict ["handle" ]
296
- class_name = _trans_dict ["_class" ]
297
- indexer .add_or_update_object (handle , db_handle , class_name )
298
- if app_has_semantic_search ():
299
- semantic_indexer : SemanticSearchIndexer = get_semantic_search_indexer (tree )
300
- for _trans_dict in trans_dict :
301
- handle = _trans_dict ["handle" ]
302
- class_name = _trans_dict ["_class" ]
303
- semantic_indexer .add_or_update_object (handle , db_handle , class_name )
294
+ user_id = get_jwt_identity ()
295
+ run_task (
296
+ update_search_indices_from_transaction ,
297
+ trans_dict = trans_dict ,
298
+ tree = tree ,
299
+ user_id = user_id ,
300
+ )
304
301
return self .response (200 , trans_dict , total_items = len (trans_dict ))
305
302
306
303
@@ -384,7 +381,7 @@ class GrampsObjectsResource(GrampsObjectResourceHelper, Resource):
384
381
},
385
382
location = "query" ,
386
383
)
387
- def get (self , args : Dict ) -> ResponseReturnValue :
384
+ def get (self , args : dict ) -> ResponseReturnValue :
388
385
"""Get all objects."""
389
386
locale = get_locale_for_language (args ["locale" ], default = True )
390
387
if "gramps_id" in args :
@@ -486,17 +483,13 @@ def post(self) -> ResponseReturnValue:
486
483
update_usage_people ()
487
484
# update search index
488
485
tree = get_tree_from_jwt_or_fail ()
489
- indexer : SearchIndexer = get_search_indexer (tree )
490
- for _trans_dict in trans_dict :
491
- handle = _trans_dict ["handle" ]
492
- class_name = _trans_dict ["_class" ]
493
- indexer .add_or_update_object (handle , db_handle , class_name )
494
- if app_has_semantic_search ():
495
- semantic_indexer : SemanticSearchIndexer = get_semantic_search_indexer (tree )
496
- for _trans_dict in trans_dict :
497
- handle = _trans_dict ["handle" ]
498
- class_name = _trans_dict ["_class" ]
499
- semantic_indexer .add_or_update_object (handle , db_handle , class_name )
486
+ user_id = get_jwt_identity ()
487
+ run_task (
488
+ update_search_indices_from_transaction ,
489
+ trans_dict = trans_dict ,
490
+ tree = tree ,
491
+ user_id = user_id ,
492
+ )
500
493
return self .response (201 , trans_dict , total_items = len (trans_dict ))
501
494
502
495
0 commit comments