From 89603b780d0a56736973e5dec41286c77b63e829 Mon Sep 17 00:00:00 2001 From: Sumandari Date: Fri, 11 Feb 2022 15:41:13 +0800 Subject: [PATCH] add response text field from applicant --- django_project/certification/forms.py | 54 ++++++++++++++++++- ...115_1805.py => 0010_auto_20220204_0935.py} | 25 ++++++--- .../0011_certifyingorganisationchecklist.py | 26 --------- ...rganisationchecklist_applicant_response.py | 18 +++++++ .../models/certificate_checklist.py | 11 ++++ .../certifying_organisation/create.html | 19 ++----- .../includes/checklist.html | 46 ---------------- .../certifying_organisation/pending-list.html | 2 +- .../certifying_organisation/update.html | 1 - .../views/certificate_checklist.py | 2 +- .../views/certifying_organisation.py | 19 ------- django_project/core/base_static/css/form.css | 5 ++ 12 files changed, 113 insertions(+), 115 deletions(-) rename django_project/certification/migrations/{0010_auto_20220115_1805.py => 0010_auto_20220204_0935.py} (58%) delete mode 100644 django_project/certification/migrations/0011_certifyingorganisationchecklist.py create mode 100644 django_project/certification/migrations/0011_certifyingorganisationchecklist_applicant_response.py delete mode 100644 django_project/certification/templates/certifying_organisation/includes/checklist.html 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('Please answer the following questions') + ) + 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 @@

Add Certifying Organisation

-
- {% csrf_token %} - {{ form|crispy }} - {% include 'certifying_organisation/includes/checklist.html' %} -
- -
-
- - - - - - + {% csrf_token %} + {% crispy form %} + {% for form in inlines %} + {% crispy form %} + {% endfor %}
diff --git a/django_project/certification/templates/certifying_organisation/includes/checklist.html b/django_project/certification/templates/certifying_organisation/includes/checklist.html deleted file mode 100644 index e0f11c3b8..000000000 --- a/django_project/certification/templates/certifying_organisation/includes/checklist.html +++ /dev/null @@ -1,46 +0,0 @@ -{% block extra_head %} - -{% endblock %} - -
-
- -
- - - {% for checklist in certification_checklists %} - - - - - - {% endfor %} - -
{{ checklist.question }} - - - -
-
-
- diff --git a/django_project/certification/templates/certifying_organisation/pending-list.html b/django_project/certification/templates/certifying_organisation/pending-list.html index 40d953be4..63c98b5d7 100644 --- a/django_project/certification/templates/certifying_organisation/pending-list.html +++ b/django_project/certification/templates/certifying_organisation/pending-list.html @@ -108,7 +108,7 @@
{{ num_certifyingorganisations }} records found
{{ certifyingorganisation.name }}
{{ certifyingorganisation.submit_date }}
-
{{ certifyingorganisation.remarks }}
+
{{ certifyingorganisation.status }}
diff --git a/django_project/certification/templates/certifying_organisation/update.html b/django_project/certification/templates/certifying_organisation/update.html index c3e935473..d9862bf72 100644 --- a/django_project/certification/templates/certifying_organisation/update.html +++ b/django_project/certification/templates/certifying_organisation/update.html @@ -56,7 +56,6 @@

Update Certifying Organisation

{% csrf_token %} {{ form|crispy }} - {% include 'certifying_organisation/includes/checklist.html' %}
diff --git a/django_project/certification/views/certificate_checklist.py b/django_project/certification/views/certificate_checklist.py index 62b2d52da..cc5ff9623 100644 --- a/django_project/certification/views/certificate_checklist.py +++ b/django_project/certification/views/certificate_checklist.py @@ -23,7 +23,7 @@ class CertificateChecklistCreateView(LoginRequiredMixin, context_object_name = 'question' template_name = 'certificate_checklist/create.html' - fields = ['question'] + fields = ['question', 'is_additional_response_enabled'] def get_success_url(self): project_slug = self.kwargs.get('project_slug', None) diff --git a/django_project/certification/views/certifying_organisation.py b/django_project/certification/views/certifying_organisation.py index cdc6c7f61..81e1245f1 100644 --- a/django_project/certification/views/certifying_organisation.py +++ b/django_project/certification/views/certifying_organisation.py @@ -490,9 +490,6 @@ def get_context_data(self, **kwargs): context['certifyingorganisations'] = \ self.get_queryset().filter(project=self.project) context['the_project'] = self.project - context['certification_checklists'] = ( - self.project.certificatechecklist_set.filter(is_archived=False) - ) return context def form_valid(self, form): @@ -508,10 +505,6 @@ def form_valid(self, form): try: super(CertifyingOrganisationCreateView, self).form_valid(form) - # save checklist - checklists = self.get_checklists_value(self.get_form_kwargs()) - self.create_certifyingorganisation_checklist(checklists) - site = self.request.get_host() recipients = [self.project.owner, ] for manager in self.project.certification_managers.all(): @@ -604,7 +597,6 @@ def get_form_kwargs(self): 'user': self.request.user, 'project': self.project }) - # self.get_checklists_value(kwargs) return kwargs @@ -650,14 +642,6 @@ def get_context_data(self, **kwargs): context['certifyingorganisations'] = self.get_queryset() \ .filter(project=self.project) context['the_project'] = self.project - context['certification_checklists'] = ( - self.project.certificatechecklist_set.filter(is_archived=False) - ) - context['certification_checklists_applied'] = ( - CertifyingOrganisationChecklist.objects.filter( - certifying_organisation=self.object - ) - ) return context def get_queryset(self): @@ -704,9 +688,6 @@ def form_valid(self, form): try: super(CertifyingOrganisationUpdateView, self).form_valid(form) - # save checklist - checklists = self.get_checklists_value(self.get_form_kwargs()) - self.update_certifyingorganisation_checklist(checklists) return HttpResponseRedirect(self.get_success_url()) except IntegrityError: return ValidationError( diff --git a/django_project/core/base_static/css/form.css b/django_project/core/base_static/css/form.css index c6cc3ca29..c7cb7a8e7 100644 --- a/django_project/core/base_static/css/form.css +++ b/django_project/core/base_static/css/form.css @@ -212,4 +212,9 @@ .form-actions { padding-bottom: 50px; +} + +/* CERTIFYING ORGANISATION FORM */ +div#div_id_organisation_owners { + margin-bottom: 350px; } \ No newline at end of file