@@ -207,11 +207,14 @@ def dj_query_route(self):
207207class InsertComponent (Component ):
208208 rest_verb = ["POST" , "GET" ]
209209 fields_route_format = "{route}/fields"
210+ presets_route_format = "{route}/presets"
210211
211212 def __init__ (self , * args , ** kwargs ):
212213 super ().__init__ (* args , ** kwargs )
213- component_config = kwargs .get ("component_config" , args [1 ] if args else None )
214- self .fields_map = component_config .get ("map" )
214+ self .component_config = kwargs .get (
215+ "component_config" , args [1 ] if args else None
216+ )
217+ self .fields_map = self .component_config .get ("map" )
215218 self .tables = [
216219 getattr (
217220 dj .VirtualModule (
@@ -222,7 +225,8 @@ def __init__(self, *args, **kwargs):
222225 t ,
223226 )
224227 for s , t in (
225- _ .format (** request .args ).split ("." ) for _ in component_config ["tables" ]
228+ _ .format (** request .args ).split ("." )
229+ for _ in self .component_config ["tables" ]
226230 )
227231 ]
228232 self .parents = sorted (
@@ -243,6 +247,24 @@ def __init__(self, *args, **kwargs):
243247 }
244248 self .input_lookup = {v : k for k , v in self .destination_lookup .items ()}
245249
250+ if "presets" in self .component_config :
251+ lcls = locals ()
252+ exec (self .component_config ["presets" ], globals (), lcls )
253+ self .presets = lcls ["presets" ]
254+
255+ self .preset_vm_list = [
256+ dj .VirtualModule (
257+ s ,
258+ s .replace ("__" , "-" ),
259+ connection = self .connection ,
260+ )
261+ for s in inspect .getfullargspec (self .presets ).args
262+ ]
263+
264+ @property
265+ def presets_dict (self ):
266+ return self .presets (* self .preset_vm_list )
267+
246268 def dj_query_route (self ):
247269 with self .connection .transaction :
248270 for t in self .tables :
@@ -315,6 +337,45 @@ def fields_route(self):
315337 + list (source_fields .values ())
316338 )
317339
340+ def presets_route (self ):
341+ # Table content for presets should follow the following format:
342+ #
343+ # preset_names: string
344+ # ---
345+ # presets: blob or json
346+ #
347+ # Example result from query:
348+ # [['preset_name', {"b_id": 1, "b_number": 2345}],
349+ # ['preset2_name', {"b_id": 13, "b_number": 225}]]
350+ #
351+ # If you have a name mapping it will be applied to each preset
352+ # Route will 404 if no preset query is defined and 500 if there is an Exception
353+
354+ # Helper function to filter out fields not in the insert,
355+ # as well as apply the fields_map
356+ def filterPreset (preset : dict ):
357+ return {
358+ (self .input_lookup [k ] if k in self .input_lookup else k ): v
359+ for k , v in preset .items ()
360+ }
361+
362+ if "presets" not in self .component_config :
363+ return (
364+ "No Preset query found" ,
365+ 404 ,
366+ {"Content-Type" : "text/plain" },
367+ )
368+
369+ filtered_preset_dictionary = {
370+ k : filterPreset (v ) for k , v in self .presets_dict .items ()
371+ }
372+
373+ return (
374+ NumpyEncoder .dumps (filtered_preset_dictionary ),
375+ 200 ,
376+ {"Content-Type" : "application/json" },
377+ )
378+
318379
319380class TableComponent (FetchComponent ):
320381 attributes_route_format = "{route}/attributes"
0 commit comments