Skip to content

Commit 73ee0fe

Browse files
committed
Added: journaling subfields and onCreate (#133)
1 parent f95acab commit 73ee0fe

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

libreforms_fastapi/utils/document_database.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,19 @@ def create_document(
352352
self.approved_field: metadata.get(self.approved_field, None),
353353
self.approved_by_field: metadata.get(self.approved_by_field, None),
354354
self.approval_signature_field: metadata.get(self.approval_signature_field, None),
355-
self.journal_field: []
356355
}
357356
}
358357

358+
# data_dict['metadata'][self.journal_field] = []
359+
journal = []
360+
journal.append (
361+
{
362+
"data": convert_data_to_dict.copy(),
363+
"metadata": data_dict['metadata'].copy(),
364+
}
365+
)
366+
data_dict['metadata'][self.journal_field] = journal
367+
359368
# document_id = self.databases[form_name].insert(data_dict)
360369
_ = self.databases[form_name].insert(data_dict, document_id=document_id)
361370

@@ -448,10 +457,14 @@ def update_document(
448457
journal = document['metadata'].get(self.journal_field)
449458
journal.append (
450459
{
451-
self.last_modified_field: current_timestamp.isoformat(),
452-
self.last_editor_field: metadata.get(self.last_editor_field, None),
453-
self.ip_address_field: metadata.get(self.ip_address_field, None),
454-
**dropping_unchanged_data,
460+
"data": {
461+
**dropping_unchanged_data,
462+
},
463+
"metadata": {
464+
self.last_modified_field: current_timestamp.isoformat(),
465+
self.last_editor_field: metadata.get(self.last_editor_field, None),
466+
self.ip_address_field: metadata.get(self.ip_address_field, None),
467+
},
455468
}
456469
)
457470

@@ -551,14 +564,17 @@ def sign_document(
551564
journal = document['metadata'].get(self.journal_field)
552565
journal.append (
553566
{
554-
self.signature_field: signature,
555-
self.last_modified_field: current_timestamp.isoformat(),
556-
self.last_editor_field: metadata.get(self.last_editor_field, None),
557-
self.ip_address_field: metadata.get(self.ip_address_field, None),
567+
"metadata": {
568+
self.signature_field: signature,
569+
self.last_modified_field: current_timestamp.isoformat(),
570+
self.last_editor_field: metadata.get(self.last_editor_field, None),
571+
self.ip_address_field: metadata.get(self.ip_address_field, None),
572+
},
558573
}
559574
)
560575

561576

577+
562578
# Here we update only a few metadata fields ... fields like approval and signature should be
563579
# handled through separate API calls.
564580
document['metadata'][self.last_modified_field] = current_timestamp.isoformat()
@@ -669,13 +685,18 @@ def delete_document(self, form_name:str, document_id:str, limit_users:Union[bool
669685
journal = document['metadata'].get(self.journal_field)
670686
journal.append (
671687
{
672-
self.last_modified_field: current_timestamp.isoformat(),
673-
self.last_editor_field: metadata.get(self.last_editor_field, None),
674-
self.ip_address_field: metadata.get(self.ip_address_field, None),
675-
self.is_deleted_field: restore==False, # Here we base the value for _is_deleted based on the `restore` param
688+
"metadata": {
689+
self.last_modified_field: current_timestamp.isoformat(),
690+
self.last_editor_field: metadata.get(self.last_editor_field, None),
691+
self.ip_address_field: metadata.get(self.ip_address_field, None),
692+
self.is_deleted_field: restore==False, # Here we base the value for _is_deleted based on the `restore` param
693+
},
676694
}
677695
)
678696

697+
698+
699+
679700
# Here we update only a few metadata fields ... fields like approval and signature should be
680701
# handled through separate API calls. The most important here are _is_deleted and _journal.
681702
document['metadata'][self.last_modified_field] = current_timestamp.isoformat()

0 commit comments

Comments
 (0)