refactor(people): Move relationship answers to answer set

Answers to the relationship questions have been moved from the relationship
to another model RelationshipAnswerSet.  A new answer set is created each
time a user answers the relationship questions.

Resolves #16
Resolves #17
Resolves #18
This commit is contained in:
James Graham
2020-03-05 15:22:28 +00:00
parent 1a9ba731cf
commit 9b3b759254
10 changed files with 375 additions and 148 deletions

View File

@@ -25,22 +25,17 @@ class PersonForm(forms.ModelForm):
}
class RelationshipForm(forms.ModelForm):
class RelationshipAnswerSetForm(forms.ModelForm):
"""
Form to allow users to describe a relationship - includes :class:`RelationshipQuestion`s.
Form to allow users to describe a relationship.
Dynamic fields inspired by https://jacobian.org/2010/feb/28/dynamic-form-generation/
"""
class Meta:
model = models.Relationship
model = models.RelationshipAnswerSet
fields = [
'source',
'target',
'relationship',
]
widgets = {
'source': Select2Widget(),
'target': Select2Widget(),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -53,7 +48,7 @@ class RelationshipForm(forms.ModelForm):
choices=choices)
self.fields['question_{}'.format(question.pk)] = field
def save(self, commit=True) -> models.Relationship:
def save(self, commit=True) -> models.RelationshipAnswerSet:
# Save Relationship model
self.instance = super().save(commit=commit)
@@ -61,7 +56,9 @@ class RelationshipForm(forms.ModelForm):
# Save answers to relationship questions
for key, value in self.cleaned_data.items():
if key.startswith('question_'):
answer = models.RelationshipQuestionChoice.objects.get(pk=value)
question_id = key.replace('question_', '', 1)
answer = models.RelationshipQuestionChoice.objects.get(pk=value,
question__pk=question_id)
self.instance.question_answers.add(answer)
return self.instance