Files
breccia-mapper/people/forms.py
James Graham 6d2737b1a6 feat(people): Add first user data fields
To support choices in CharFields, Django choices enums have been
backported by bringing in the source file from Django 3.0.3

See #1
2020-02-21 17:10:26 +00:00

58 lines
1.7 KiB
Python

"""
Forms for creating / updating models belonging to the 'people' app.
"""
from django import forms
from . import models
class PersonForm(forms.ModelForm):
"""
Form for creating / updating an instance of :class:`Person`.
"""
class Meta:
model = models.Person
exclude = [
'user',
'relationship_targets',
]
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