1919
2020"""Base for Gramps object API resources."""
2121
22- import json
23- from typing import Dict , List , TypeVar
22+ from typing import TypeVar
2423
2524import gramps_ql as gql
2625import object_ql as oql
2726from flask import abort , request
27+ from flask_jwt_extended import get_jwt_identity
2828from gramps .gen .const import GRAMPS_LOCALE as glocale
2929from gramps .gen .db import DbTxn
3030from gramps .gen .db .base import DbReadBase
3737from webargs import fields , validate
3838
3939from gramps_webapi .api .search .indexer import SemanticSearchIndexer
40- from gramps_webapi .types import Handle , ResponseReturnValue
40+ from gramps_webapi .types import ResponseReturnValue
4141
4242from ...auth .const import PERM_ADD_OBJ , PERM_DEL_OBJ , PERM_EDIT_OBJ
4343from ...const import GRAMPS_OBJECT_PLURAL
4444from ..auth import require_permissions
4545from ..search import SearchIndexer , get_search_indexer , get_semantic_search_indexer
46+ from ..tasks import run_task , update_search_indices_from_transaction
4647from ..util import (
4748 check_quota_people ,
4849 get_db_handle ,
@@ -82,7 +83,7 @@ class GrampsObjectResourceHelper(GrampsJSONEncoder):
8283
8384 gramps_class_name : str
8485
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 :
8687 """Get the full object with extended attributes and backlinks."""
8788 if args .get ("backlinks" ):
8889 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:
104105 )
105106 return obj
106107
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 :
108109 """Extend the base object attributes as needed."""
109110 if "extend" in args :
110111 obj .extended = get_extended_attributes (self .db_handle , obj , args )
111112 return obj
112113
113114 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 :
116117 """Sort the list of objects as needed."""
117118 return sort_objects (
118119 self .db_handle , self .gramps_class_name , objects , args , locale = locale
119120 )
120121
121- def match_dates (self , objects : List [GrampsObject ], date : str ) -> List [GrampsObject ]:
122+ def match_dates (self , objects : list [GrampsObject ], date : str ) -> list [GrampsObject ]:
122123 """If supported filter objects using date mask."""
123124 if self .gramps_class_name in ["Event" , "Media" , "Citation" ]:
124125 return match_dates (objects , date )
@@ -232,7 +233,7 @@ class GrampsObjectResource(GrampsObjectResourceHelper, Resource):
232233 },
233234 location = "query" ,
234235 )
235- def get (self , args : Dict , handle : str ) -> ResponseReturnValue :
236+ def get (self , args : dict , handle : str ) -> ResponseReturnValue :
236237 """Get the object."""
237238 try :
238239 obj = self .get_object_from_handle (handle )
@@ -290,17 +291,13 @@ def put(self, handle: str) -> ResponseReturnValue:
290291 trans_dict = transaction_to_json (trans )
291292 # update search index
292293 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+ )
304301 return self .response (200 , trans_dict , total_items = len (trans_dict ))
305302
306303
@@ -384,7 +381,7 @@ class GrampsObjectsResource(GrampsObjectResourceHelper, Resource):
384381 },
385382 location = "query" ,
386383 )
387- def get (self , args : Dict ) -> ResponseReturnValue :
384+ def get (self , args : dict ) -> ResponseReturnValue :
388385 """Get all objects."""
389386 locale = get_locale_for_language (args ["locale" ], default = True )
390387 if "gramps_id" in args :
@@ -486,17 +483,13 @@ def post(self) -> ResponseReturnValue:
486483 update_usage_people ()
487484 # update search index
488485 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+ )
500493 return self .response (201 , trans_dict , total_items = len (trans_dict ))
501494
502495
0 commit comments