44import json
55import sqlalchemy
66from docassemble .base .logger import logmessage
7- from docassemble .base .functions import server , this_thread
7+ import docassemble .base .functions
8+ from docassemble .base .functions import server
89from docassemble .base .util import DAList , DAObjectPlusParameters
910from docassemble .base .error import DAAttributeError , DAException
1011from alembic .config import Config
@@ -72,17 +73,17 @@ def __setstate__(self, pickle_dict):
7273
7374 @classmethod
7475 def filter (cls , instance_name , ** kwargs ):
75- if 'dbcache' not in this_thread .misc :
76- this_thread .misc ['dbcache' ] = {}
76+ if 'dbcache' not in docassemble . base . functions . this_thread .misc :
77+ docassemble . base . functions . this_thread .misc ['dbcache' ] = {}
7778 listobj = DAList (instance_name , object_type = cls , auto_gather = False )
7879 filters = []
7980 for key , val in kwargs .items ():
8081 if not hasattr (cls ._model , key ):
8182 raise DAException ("filter: class " + cls .__name__ + " does not have column " + key )
8283 filters .append (getattr (cls ._model , key ) == val )
8384 for db_entry in list (cls ._session .query (cls ._model ).filter (* filters ).order_by (cls ._model .id ).all ()):
84- if cls ._model .__name__ in this_thread .misc ['dbcache' ] and db_entry .id in this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
85- listobj .append (this_thread .misc ['dbcache' ][cls ._model .__name__ ][db_entry .id ])
85+ if cls ._model .__name__ in docassemble . base . functions . this_thread .misc ['dbcache' ] and db_entry .id in docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
86+ listobj .append (docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ][db_entry .id ])
8687 else :
8788 obj = listobj .appendObject ()
8889 obj .id = db_entry .id
@@ -107,16 +108,16 @@ def any(cls):
107108
108109 @classmethod
109110 def all (cls , instance_name = None ):
110- if 'dbcache' not in this_thread .misc :
111- this_thread .misc ['dbcache' ] = {}
111+ if 'dbcache' not in docassemble . base . functions . this_thread .misc :
112+ docassemble . base . functions . this_thread .misc ['dbcache' ] = {}
112113 if instance_name :
113114 listobj = DAList (instance_name , object_type = cls )
114115 else :
115116 listobj = DAList (object_type = cls )
116117 listobj .set_random_instance_name ()
117118 for db_entry in list (cls ._session .query (cls ._model ).order_by (cls ._model .id ).all ()):
118- if cls ._model .__name__ in this_thread .misc ['dbcache' ] and db_entry .id in this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
119- listobj .append (this_thread .misc ['dbcache' ][cls ._model .__name__ ][db_entry .id ])
119+ if cls ._model .__name__ in docassemble . base . functions . this_thread .misc ['dbcache' ] and db_entry .id in docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
120+ listobj .append (docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ][db_entry .id ])
120121 else :
121122 obj = listobj .appendObject ()
122123 obj .id = db_entry .id
@@ -134,12 +135,12 @@ def all(cls, instance_name=None):
134135
135136 @classmethod
136137 def by_id (cls , the_id , instance_name = None ):
137- if 'dbcache' not in this_thread .misc :
138- this_thread .misc ['dbcache' ] = {}
139- if cls ._model .__name__ in this_thread .misc ['dbcache' ] and the_id in this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
138+ if 'dbcache' not in docassemble . base . functions . this_thread .misc :
139+ docassemble . base . functions . this_thread .misc ['dbcache' ] = {}
140+ if cls ._model .__name__ in docassemble . base . functions . this_thread .misc ['dbcache' ] and the_id in docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
140141 if instance_name is None :
141- return this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]
142- obj = this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]
142+ return docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]
143+ obj = docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]
143144 obj .fix_instance_name (obj .instanceName , instance_name )
144145 if instance_name is None :
145146 obj = cls (id = the_id )
@@ -169,8 +170,8 @@ def by_uid(cls, uid, instance_name=None):
169170 def delete_by_id (cls , the_id ):
170171 cls ._session .query (cls ._model ).filter (cls ._model .id == the_id ).delete ()
171172 cls ._session .commit ()
172- if 'dbcache' in this_thread .misc and cls ._model .__name__ in this_thread .misc ['dbcache' ] and the_id in this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
173- this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]._zombie = True
173+ if 'dbcache' in docassemble . base . functions . this_thread .misc and cls ._model .__name__ in docassemble . base . functions . this_thread .misc ['dbcache' ] and the_id in docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
174+ docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]._zombie = True
174175
175176 @classmethod
176177 def delete_by_uid (cls , uid ):
@@ -181,8 +182,8 @@ def delete_by_uid(cls, uid):
181182 the_id = db_entry .id
182183 cls ._session .query (cls ._model ).filter (getattr (cls ._model , cls ._uid ) == uid ).delete ()
183184 cls ._session .commit ()
184- if 'dbcache' in this_thread .misc and cls ._model .__name__ in this_thread .misc ['dbcache' ] and the_id in this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
185- this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]._zombie = True
185+ if 'dbcache' in docassemble . base . functions . this_thread .misc and cls ._model .__name__ in docassemble . base . functions . this_thread .misc ['dbcache' ] and the_id in docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ]:
186+ docassemble . base . functions . this_thread .misc ['dbcache' ][cls ._model .__name__ ][the_id ]._zombie = True
186187
187188 @classmethod
188189 def id_exists (cls , the_id ):
@@ -201,25 +202,25 @@ def uid_exists(cls, uid):
201202 return True
202203
203204 def db_from_cache (self , the_id ):
204- if 'dbcache' not in this_thread .misc :
205- this_thread .misc ['dbcache' ] = {}
206- if self ._model .__name__ not in this_thread .misc ['dbcache' ]:
207- this_thread .misc ['dbcache' ][self ._model .__name__ ] = {}
208- if the_id in this_thread .misc ['dbcache' ][self ._model .__name__ ]:
209- return this_thread .misc ['dbcache' ][self ._model .__name__ ][the_id ]
205+ if 'dbcache' not in docassemble . base . functions . this_thread .misc :
206+ docassemble . base . functions . this_thread .misc ['dbcache' ] = {}
207+ if self ._model .__name__ not in docassemble . base . functions . this_thread .misc ['dbcache' ]:
208+ docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ] = {}
209+ if the_id in docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ]:
210+ return docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ][the_id ]
210211 return None
211212
212213 def db_cache (self ):
213- if 'dbcache' not in this_thread .misc :
214- this_thread .misc ['dbcache' ] = {}
215- if self ._model .__name__ not in this_thread .misc ['dbcache' ]:
216- this_thread .misc ['dbcache' ][self ._model .__name__ ] = {}
214+ if 'dbcache' not in docassemble . base . functions . this_thread .misc :
215+ docassemble . base . functions . this_thread .misc ['dbcache' ] = {}
216+ if self ._model .__name__ not in docassemble . base . functions . this_thread .misc ['dbcache' ]:
217+ docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ] = {}
217218 if hasattr (self , 'id' ):
218- this_thread .misc ['dbcache' ][self ._model .__name__ ][self .id ] = self
219+ docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ][self .id ] = self
219220
220221 def __del__ (self ):
221- if hasattr (self , 'id' ) and 'dbcache' in this_thread .misc and self ._model .__name__ in this_thread .misc ['dbcache' ] and self .id in this_thread .misc ['dbcache' ][self ._model .__name__ ]:
222- del this_thread .misc ['dbcache' ][self ._model .__name__ ][self .id ]
222+ if hasattr (self , 'id' ) and 'dbcache' in docassemble . base . functions . this_thread .misc and self ._model .__name__ in docassemble . base . functions . this_thread .misc ['dbcache' ] and self .id in docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ]:
223+ del docassemble . base . functions . this_thread .misc ['dbcache' ][self ._model .__name__ ][self .id ]
223224
224225 def db_delete (self ):
225226 self .db_read ()
0 commit comments