Skip to content

Commit e6c5c30

Browse files
committed
Added: form_stage metadata field and get_all_documents_by_stage method to doc_dc (#62)
1 parent 0874833 commit e6c5c30

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

libreforms_fastapi/utils/document_database.py

+35
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def _initialize_metadata_fields(self):
203203
self.ip_address_field = "ip_address"
204204
self.created_by_field = "created_by"
205205
self.signature_field = "signatures"
206+
self.form_stage_field = "form_stage"
206207
self.last_editor_field = "last_editor"
207208
# self.approved_field = "approved"
208209
# self.approved_by_field = "approved_by"
@@ -223,6 +224,7 @@ def _initialize_metadata_fields(self):
223224
self.ip_address_field,
224225
self.created_by_field,
225226
self.signature_field,
227+
self.form_stage_field,
226228
self.last_editor_field,
227229
# self.approved_field,
228230
# self.approved_by_field,
@@ -296,6 +298,12 @@ def get_all_documents(self, form_name:str, exclude_deleted:bool=True):
296298
"""Retrieves all entries from the specified form's database."""
297299
pass
298300

301+
302+
@abstractmethod
303+
def get_all_documents_by_stage(self, form_name:str, stage_name:str, exclude_deleted:bool=True):
304+
"""Retrieves all entries from the specified form's database when they reside in a specified stage."""
305+
pass
306+
299307
@abstractmethod
300308
def get_one_document(self, form_name:str, search_query, exclude_deleted:bool=True):
301309
"""Retrieves a single entry that matches the search query."""
@@ -1090,6 +1098,33 @@ def get_all_documents(
10901098

10911099
return documents
10921100

1101+
def get_all_documents_by_stage(
1102+
self,
1103+
form_name:str,
1104+
stage_name:str,
1105+
exclude_deleted:bool=True
1106+
) -> list:
1107+
"""Retrieves all entries from the specified form's database where metadata['form_stage'] equals stage_name."""
1108+
1109+
# Ensure the form exists in the configuration
1110+
self._check_form_exists(form_name)
1111+
1112+
# Query the database for documents with the specified stage_name. I'm using Query here,
1113+
# which I don't do very often. Considering pivoting to just using straight python logic instead...
1114+
query = (self.Form.metadata['form_stage'] == stage_name)
1115+
1116+
if exclude_deleted:
1117+
query &= (self.Form.metadata[self.is_deleted_field] == False)
1118+
1119+
documents = self.databases[form_name].search(query)
1120+
1121+
# Construct a list of Document IDs ... this is better for caching because
1122+
# other form data might change without needing to trigger cache invalidation...
1123+
# So, storing the full document is alluring, but will lead to unwise assumptions.
1124+
document_ids = [x.doc_id for x in documents]
1125+
1126+
return document_ids
1127+
10931128
def get_one_document(
10941129
self,
10951130
form_name:str,

0 commit comments

Comments
 (0)