From 7e9a6e992a25a11c87bb6d8d928698f8d96a5cb1 Mon Sep 17 00:00:00 2001 From: James Graham Date: Mon, 8 Mar 2021 18:20:34 +0000 Subject: [PATCH] feat: configurable order for static questions --- people/forms.py | 20 ++++++++-- .../migrations/0042_is_hardcoded_questions.py | 38 +++++++++++++++++++ people/models/person.py | 5 ++- people/models/question.py | 7 ++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 people/migrations/0042_is_hardcoded_questions.py diff --git a/people/forms.py b/people/forms.py index c2f4447..282a3cb 100644 --- a/people/forms.py +++ b/people/forms.py @@ -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/ diff --git a/people/migrations/0042_is_hardcoded_questions.py b/people/migrations/0042_is_hardcoded_questions.py new file mode 100644 index 0000000..867062b --- /dev/null +++ b/people/migrations/0042_is_hardcoded_questions.py @@ -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), + ), + ] diff --git a/people/models/person.py b/people/models/person.py index 8dfad5c..4d1489b 100644 --- a/people/models/person.py +++ b/people/models/person.py @@ -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) diff --git a/people/models/question.py b/people/models/question.py index b5ccadf..8bdb245 100644 --- a/people/models/question.py +++ b/people/models/question.py @@ -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,