diff --git a/django_project/certification/forms.py b/django_project/certification/forms.py index 43780daef..d3b4d9e3c 100644 --- a/django_project/certification/forms.py +++ b/django_project/certification/forms.py @@ -18,10 +18,13 @@ Fieldset, Submit, Field, + HTML ) from .models import ( CertifyingOrganisation, + CertifyingOrganisationChecklist, CertificateType, + CertificateChecklist, CourseConvener, CourseType, TrainingCenter, @@ -69,6 +72,27 @@ def __init__(self, *args, **kwargs): form_title = 'New Certifying Organisation for %s' % self.project.name self.helper = FormHelper() self.helper.include_media = False + self.RESPONSE_NAME_PREFIX = 'checklist-response' + + # additional layout for checklist questions + self.certificate_checklists = CertificateChecklist.objects.filter( + is_archived=False, is_additional_response_enabled=True).all() + checklist_questions = [] + if CertificateChecklist.objects.filter( + is_archived=False, + is_additional_response_enabled=True).exists(): + checklist_questions.append( + HTML('') + ) + for _ in self.certificate_checklists: + checklist_questions.append( + Field( + f'{self.RESPONSE_NAME_PREFIX}_{_.id}', + css_class='form-control', + label=f'{_.question}' + ) + ) + layout = Layout( Fieldset( form_title, @@ -80,8 +104,9 @@ def __init__(self, *args, **kwargs): Field('organisation_phone', css_class='form-control'), Field('logo', css_class='form-control'), Field('organisation_owners', css_class='form-control'), + *checklist_questions, Field('project', css_class='form-control'), - css_id='project-form') + css_id='project-form'), ) self.helper.layout = layout self.helper.html5_required = False @@ -91,12 +116,39 @@ def __init__(self, *args, **kwargs): self.fields['organisation_owners'].initial = [self.user] self.fields['project'].initial = self.project self.fields['project'].widget = forms.HiddenInput() + # additional fields for checklist questions + for cl in self.certificate_checklists: + field_name = f'{self.RESPONSE_NAME_PREFIX}_{cl.id}' + self.fields[field_name] = forms.CharField( + widget=forms.Textarea(), + label=f'{cl.question}' + ) + if (self.instance and cl.certifyingorganisationchecklist_set.filter( + certifying_organisation=self.instance).exists()): + applicant_resp = cl.certifyingorganisationchecklist_set.filter( + certifying_organisation=self.instance + ).last().applicant_response + self.fields[field_name].initial = applicant_resp self.helper.add_input(Submit('submit', 'Submit')) def save(self, commit=True): instance = super(CertifyingOrganisationForm, self).save(commit=False) instance.save() self.save_m2m() + # save the checklist_response + for cl in self.certificate_checklists: + cert_org_cl, _ = ( + CertifyingOrganisationChecklist.objects.get_or_create( + question=cl, certifying_organisation=instance + ) + ) + if cl.is_additional_response_enabled: + field_name = f'{self.RESPONSE_NAME_PREFIX}_{cl.id}' + response_text = None + if self.cleaned_data[field_name]: + response_text = self.cleaned_data[field_name] + cert_org_cl.applicant_response = response_text + cert_org_cl.save() return instance diff --git a/django_project/certification/migrations/0010_auto_20220115_1805.py b/django_project/certification/migrations/0010_auto_20220204_0935.py similarity index 58% rename from django_project/certification/migrations/0010_auto_20220115_1805.py rename to django_project/certification/migrations/0010_auto_20220204_0935.py index 8d098eb4f..cb5fc3079 100644 --- a/django_project/certification/migrations/0010_auto_20220115_1805.py +++ b/django_project/certification/migrations/0010_auto_20220204_0935.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.18 on 2022-01-15 16:05 +# Generated by Django 2.2.18 on 2022-02-04 07:35 from django.db import migrations, models import django.db.models.deletion @@ -12,11 +12,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.AlterField( - model_name='course', - name='certificate_type', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='certification.CertificateType'), - ), migrations.CreateModel( name='CertificateChecklist', fields=[ @@ -24,10 +19,28 @@ class Migration(migrations.Migration): ('question', models.CharField(help_text='Question for certifying organisation applicant.', max_length=1000, unique=True)), ('sort_number', models.SmallIntegerField(blank=True, help_text='The order in which this category is listed within a project', null=True)), ('is_archived', models.BooleanField(default=False, help_text='Is this question archived?')), + ('is_additional_response_enabled', models.BooleanField(default=False, help_text='Let the applicant add a response when making their application.')), ('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='base.Project')), ], options={ 'unique_together': {('question', 'sort_number')}, }, ), + migrations.AlterField( + model_name='course', + name='certificate_type', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='certification.CertificateType'), + ), + migrations.CreateModel( + name='CertifyingOrganisationChecklist', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('is_checked', models.BooleanField(help_text='Is the answer is yes or no', null=True)), + ('certifying_organisation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='certification.CertifyingOrganisation')), + ('question', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='certification.CertificateChecklist')), + ], + options={ + 'unique_together': {('question', 'certifying_organisation')}, + }, + ), ] diff --git a/django_project/certification/migrations/0011_certifyingorganisationchecklist.py b/django_project/certification/migrations/0011_certifyingorganisationchecklist.py deleted file mode 100644 index 845804905..000000000 --- a/django_project/certification/migrations/0011_certifyingorganisationchecklist.py +++ /dev/null @@ -1,26 +0,0 @@ -# Generated by Django 2.2.18 on 2022-01-27 16:56 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('certification', '0010_auto_20220115_1805'), - ] - - operations = [ - migrations.CreateModel( - name='CertifyingOrganisationChecklist', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('is_checked', models.BooleanField(help_text='Is the answer is yes or no', null=True)), - ('certifying_organisation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='certification.CertifyingOrganisation')), - ('question', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='certification.CertificateChecklist')), - ], - options={ - 'unique_together': {('question', 'certifying_organisation')}, - }, - ), - ] diff --git a/django_project/certification/migrations/0011_certifyingorganisationchecklist_applicant_response.py b/django_project/certification/migrations/0011_certifyingorganisationchecklist_applicant_response.py new file mode 100644 index 000000000..cf9dccf8a --- /dev/null +++ b/django_project/certification/migrations/0011_certifyingorganisationchecklist_applicant_response.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.18 on 2022-02-11 03:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('certification', '0010_auto_20220204_0935'), + ] + + operations = [ + migrations.AddField( + model_name='certifyingorganisationchecklist', + name='applicant_response', + field=models.CharField(blank=True, help_text='Response from applicant.', max_length=1000, null=True), + ), + ] diff --git a/django_project/certification/models/certificate_checklist.py b/django_project/certification/models/certificate_checklist.py index b5159640a..b87689191 100644 --- a/django_project/certification/models/certificate_checklist.py +++ b/django_project/certification/models/certificate_checklist.py @@ -23,6 +23,11 @@ class CertificateChecklist(models.Model): help_text=_('Is this question archived?'), default=False ) + is_additional_response_enabled = models.BooleanField( + help_text=_('Let the applicant add a response when making their ' + 'application.'), + default=False + ) project = models.ForeignKey('base.Project', on_delete=models.CASCADE) class Meta: @@ -47,6 +52,12 @@ class CertifyingOrganisationChecklist(models.Model): help_text=_('Is the answer is yes or no'), null=True ) + applicant_response = models.CharField( + help_text=_('Response from applicant.'), + max_length=1000, + blank=True, + null=True + ) class Meta: unique_together = ( diff --git a/django_project/certification/templates/certifying_organisation/create.html b/django_project/certification/templates/certifying_organisation/create.html index 9cd47d391..55c4186f0 100644 --- a/django_project/certification/templates/certifying_organisation/create.html +++ b/django_project/certification/templates/certifying_organisation/create.html @@ -33,20 +33,11 @@
{{ checklist.question }} | -- - | -- - | -