feat: add relationships from person to org

Resolves #77
See #78
This commit is contained in:
James Graham
2021-03-02 09:03:10 +00:00
parent 6d5188af72
commit 936a375992
11 changed files with 454 additions and 30 deletions

View File

@@ -239,6 +239,48 @@ class RelationshipAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
return self.instance
class OrganisationRelationshipAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
"""Form to allow users to describe a relationship with an organisation.
Dynamic fields inspired by https://jacobian.org/2010/feb/28/dynamic-form-generation/
"""
class Meta:
model = models.OrganisationRelationshipAnswerSet
fields = [
'relationship',
]
widgets = {
'relationship': forms.HiddenInput,
}
question_model = models.OrganisationRelationshipQuestion
answer_model = models.OrganisationRelationshipQuestionChoice
def save(self, commit=True) -> models.OrganisationRelationshipAnswerSet:
# Save model
self.instance = super().save(commit=commit)
if commit:
# Save answers to questions
for key, value in self.cleaned_data.items():
if key.startswith('question_') and value:
if key.endswith('_free'):
# Create new answer from free text
value, _ = self.answer_model.objects.get_or_create(
text=value,
question=self.question_model.objects.get(
pk=key.split('_')[1]))
try:
self.instance.question_answers.add(value)
except TypeError:
# Value is a QuerySet - multiple choice question
self.instance.question_answers.add(*value.all())
return self.instance
class NetworkFilterForm(DynamicAnswerSetBase):
"""
Form to provide filtering on the network view.