mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
feat(people): Allow people to describe relationships
Creating a relationship requires answering each of the relationship questions resolve #2 partial #5
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""
|
||||
Forms for creating / updating models belonging to the 'people' app.
|
||||
"""
|
||||
|
||||
from django import forms
|
||||
|
||||
from . import models
|
||||
@@ -17,3 +16,42 @@ class PersonForm(forms.ModelForm):
|
||||
'name',
|
||||
'core_member',
|
||||
]
|
||||
|
||||
|
||||
class RelationshipForm(forms.ModelForm):
|
||||
"""
|
||||
Form to allow users to describe a relationship - includes :class:`RelationshipQuestion`s.
|
||||
|
||||
Dynamic fields inspired by https://jacobian.org/2010/feb/28/dynamic-form-generation/
|
||||
"""
|
||||
class Meta:
|
||||
model = models.Relationship
|
||||
fields = [
|
||||
'source',
|
||||
'target',
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
for question in models.RelationshipQuestion.objects.all():
|
||||
# Get choices from model and add default 'not selected' option
|
||||
choices = question.choices + [['', '---------']]
|
||||
|
||||
field = forms.ChoiceField(label=question,
|
||||
choices=choices)
|
||||
self.fields['question_{}'.format(question.pk)] = field
|
||||
|
||||
def save(self, commit=True) -> models.Relationship:
|
||||
# Save Relationship model
|
||||
self.instance = super().save(commit=commit)
|
||||
|
||||
if commit:
|
||||
# Save answers to relationship questions
|
||||
for key, value in self.cleaned_data.items():
|
||||
if key.startswith('question_'):
|
||||
question_pk = key.split('_')[-1]
|
||||
answer = models.RelationshipQuestionChoice.objects.get(pk=value)
|
||||
self.instance.question_answers.add(answer)
|
||||
|
||||
return self.instance
|
||||
|
||||
Reference in New Issue
Block a user