-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement flat_answer_map (analog to ember-caluma)
- Loading branch information
1 parent
70e875c
commit 8d96071
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -334,6 +334,39 @@ class Document(core_models.UUIDModel): | |
) | ||
meta = models.JSONField(default=dict) | ||
|
||
def flat_answer_map(self): | ||
""" | ||
Returns a dictionary with the flattened answer map for this document. | ||
The keys are the question IDs, and the values are either the answer value | ||
or date for non-table questions, or a list of flattened answer maps for | ||
table questions. | ||
Example usage: | ||
>>> doc = Document.objects.get(pk=1) | ||
>>> flat_map = doc.flat_answer_map() | ||
>>> print(flat_map) | ||
{ | ||
"first-name": 'John', | ||
"second-name": 'Doe', | ||
"email-in-subform": '[email protected]', | ||
"projects": [ | ||
{"title": 'Project A', "date": '2022-01-01'}, | ||
{"title": 'Project B', "date": '2022-02-01'}, | ||
{"title": 'Project C', "date": '2022-03-01'} | ||
] | ||
} | ||
""" | ||
answers = {} | ||
for answer in self.answers.all(): | ||
if answer.question.type == Question.TYPE_TABLE: | ||
answers[answer.question_id] = [ | ||
answer.flat_answer_map() for answer in answer.documents.all() | ||
] | ||
else: | ||
answers[answer.question_id] = answer.value or answer.date | ||
|
||
return answers | ||
|
||
def set_family(self, root_doc): | ||
"""Set the family to the given root_doc. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters