Skip to content

Commit

Permalink
add response text field from applicant
Browse files Browse the repository at this point in the history
  • Loading branch information
sumandari committed Feb 11, 2022
1 parent 06ce99e commit 228563a
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 115 deletions.
55 changes: 54 additions & 1 deletion django_project/certification/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
Fieldset,
Submit,
Field,
HTML
)
from .models import (
CertifyingOrganisation,
CertifyingOrganisationChecklist,
CertificateType,
CertificateChecklist,
CourseConvener,
CourseType,
TrainingCenter,
Expand Down Expand Up @@ -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('<legend>Please answer the following questions</legend>')
)
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,
Expand All @@ -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
Expand All @@ -91,12 +116,40 @@ 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


Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,22 +12,35 @@ 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=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('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')},
},
),
]

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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),
),
]
11 changes: 11 additions & 0 deletions django_project/certification/models/certificate_checklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,11 @@ <h1>Add Certifying Organisation</h1>

<section id="forms">
<div class='container'>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
{% include 'certifying_organisation/includes/checklist.html' %}
<div class="form-actions">
<input type="submit" name="submit" value="Submit" class="btn btn-primary" id="submit-id-submit">
</div>
</form>
<!-- {% csrf_token %}-->
<!-- {% crispy form %}-->
<!-- {% for form in inlines %}-->
<!-- {% crispy form %}-->
<!-- {% endfor %}-->
<!-- -->
{% csrf_token %}
{% crispy form %}
{% for form in inlines %}
{% crispy form %}
{% endfor %}
</div>
</section>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h5 class="text-muted">{{ num_certifyingorganisations }} records found</h5>
<a href="{% url 'certifyingorganisation-review' project_slug=certifyingorganisation.project.slug slug=certifyingorganisation.slug %}">
<div class="col-xs-6">{{ certifyingorganisation.name }}</div>
<div class="col-xs-3">{{ certifyingorganisation.submit_date }} </div>
<div class="col-xs-3">{{ certifyingorganisation.remarks }}</div>
<div class="col-xs-3">{{ certifyingorganisation.status }}</div>
</a>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ <h1>Update Certifying Organisation</h1>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
{% include 'certifying_organisation/includes/checklist.html' %}
<div class="form-actions">
<input type="submit" name="submit" value="Submit" class="btn btn-primary" id="submit-id-submit">
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions django_project/certification/views/certifying_organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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():
Expand Down Expand Up @@ -604,7 +597,6 @@ def get_form_kwargs(self):
'user': self.request.user,
'project': self.project
})
# self.get_checklists_value(kwargs)
return kwargs


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions django_project/core/base_static/css/form.css
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,9 @@

.form-actions {
padding-bottom: 50px;
}

/* CERTIFYING ORGANISATION FORM */
div#div_id_organisation_owners {
margin-bottom: 350px;
}

0 comments on commit 228563a

Please sign in to comment.