feat: configurable order for static questions

This commit is contained in:
James Graham
2021-03-08 18:20:34 +00:00
parent ba4f8d8ffd
commit 7e9a6e992a
4 changed files with 66 additions and 4 deletions

View File

@@ -46,11 +46,17 @@ class DynamicAnswerSetBase(forms.Form):
super().__init__(*args, **kwargs)
initial = kwargs.get('initial', {})
field_order = []
for question in self.question_model.objects.all():
if as_filters and not question.answer_is_public:
continue
# Placeholder question for sorting hardcoded questions
if question.is_hardcoded and (question.text in self.Meta.fields):
field_order.append(question.text)
continue
field_class = self.field_class
field_widget = self.field_widget
@@ -72,11 +78,15 @@ class DynamicAnswerSetBase(forms.Form):
and not question.allow_free_text),
initial=initial.get(field_name, None))
self.fields[field_name] = field
field_order.append(field_name)
if question.allow_free_text:
free_field = forms.CharField(label=f'{question} free text',
required=False)
self.fields[f'{field_name}_free'] = free_field
field_order.append(f'{field_name}_free')
self.order_fields(field_order)
class OrganisationAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
@@ -161,12 +171,15 @@ class PersonAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
'latitude': forms.HiddenInput,
'longitude': forms.HiddenInput,
}
labels = {
'project_started_date':
f'Date started on the {settings.PARENT_PROJECT_NAME} project',
}
help_texts = {
'organisation_started_date':
'If you don\'t know the exact date, an approximate date is okay.',
'project_started_date':
(f'Date you started on the {settings.PARENT_PROJECT_NAME} project. '
'If you don\'t know the exact date, an approximate date is okay.'),
'If you don\'t know the exact date, an approximate date is okay.',
}
question_model = models.PersonQuestion
@@ -245,7 +258,8 @@ class RelationshipAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
return self.instance
class OrganisationRelationshipAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
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/

View File

@@ -0,0 +1,38 @@
# Generated by Django 2.2.10 on 2021-03-08 17:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('people', '0041_add_static_org_questions'),
]
operations = [
migrations.AddField(
model_name='organisationquestion',
name='is_hardcoded',
field=models.BooleanField(default=False, help_text='Only the order field has any effect for a hardcoded question.'),
),
migrations.AddField(
model_name='organisationrelationshipquestion',
name='is_hardcoded',
field=models.BooleanField(default=False, help_text='Only the order field has any effect for a hardcoded question.'),
),
migrations.AddField(
model_name='personquestion',
name='is_hardcoded',
field=models.BooleanField(default=False, help_text='Only the order field has any effect for a hardcoded question.'),
),
migrations.AddField(
model_name='relationshipquestion',
name='is_hardcoded',
field=models.BooleanField(default=False, help_text='Only the order field has any effect for a hardcoded question.'),
),
migrations.AlterField(
model_name='personanswerset',
name='job_title',
field=models.CharField(blank=True, help_text='Contractual job title', max_length=255),
),
]

View File

@@ -175,7 +175,10 @@ class PersonAnswerSet(AnswerSet):
project_started_date = models.DateField(blank=False, null=True)
#: Job title this person holds within their organisation
job_title = models.CharField(max_length=255, blank=True, null=False)
job_title = models.CharField(help_text='Contractual job title',
max_length=255,
blank=True,
null=False)
#: Project themes within this person works
themes = models.ManyToManyField(Theme, related_name='people', blank=True)

View File

@@ -46,6 +46,13 @@ class Question(models.Model):
blank=False,
null=False)
#: Is this question hardcoded in an AnswerSet?
is_hardcoded = models.BooleanField(
help_text='Only the order field has any effect for a hardcoded question.',
default=False,
blank=False,
null=False)
#: Should people be able to add their own answers?
allow_free_text = models.BooleanField(default=False,
blank=False,