Skip to content

Commit

Permalink
Merge pull request #1383 from sumandari/1371_certification_type
Browse files Browse the repository at this point in the history
Add Certificate Type
  • Loading branch information
sumandari authored Feb 14, 2022
2 parents b2895c7 + f2254ea commit bdb45db
Show file tree
Hide file tree
Showing 22 changed files with 627 additions and 16 deletions.
11 changes: 11 additions & 0 deletions django_project/certification/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.gis import admin
from simple_history.admin import SimpleHistoryAdmin
from certification.models.certificate import Certificate
from certification.models.certificate_type import CertificateType
from certification.models.course import Course
from certification.models.training_center import TrainingCenter
from certification.models.course_convener import CourseConvener
Expand Down Expand Up @@ -34,6 +35,15 @@ def queryset(self, request):
return query_set


class CertificateTypeAdmin(admin.ModelAdmin):
"""CertificateType admin model."""

list_display = ('name', 'wording', 'order')
list_editable = ('order', )
search_fields = ('name', 'wording')
ordering = ('order', )


class AttendeeAdmin(admin.ModelAdmin):
"""Attendee admin model."""
list_display = ('firstname', 'surname', 'email', 'certifying_organisation')
Expand Down Expand Up @@ -163,6 +173,7 @@ class StatusAdmin(admin.ModelAdmin):


admin.site.register(Certificate, CertificateAdmin)
admin.site.register(CertificateType, CertificateTypeAdmin)
admin.site.register(Attendee, AttendeeAdmin)
admin.site.register(Course, CourseAdmin)
admin.site.register(CourseType, CourseTypeAdmin)
Expand Down
7 changes: 7 additions & 0 deletions django_project/certification/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)
from .models import (
CertifyingOrganisation,
CertificateType,
CourseConvener,
CourseType,
TrainingCenter,
Expand Down Expand Up @@ -305,6 +306,7 @@ class Meta:
'end_date',
'template_certificate',
'certifying_organisation',
'certificate_type',
)

def __init__(self, *args, **kwargs):
Expand All @@ -324,6 +326,7 @@ def __init__(self, *args, **kwargs):
Field('start_date', css_class='form-control'),
Field('end_date', css_class='form-control'),
Field('template_certificate', css_class='form-control'),
Field('certificate_type', css_class='form-control'),
)
)
self.helper.layout = layout
Expand All @@ -345,6 +348,10 @@ def __init__(self, *args, **kwargs):
self.certifying_organisation
self.fields['certifying_organisation'].widget = forms.HiddenInput()
self.helper.add_input(Submit('submit', 'Submit'))
self.fields['certificate_type'].queryset = \
CertificateType.objects.filter(
projectcertificatetype__project=
self.certifying_organisation.project)

def save(self, commit=True):
instance = super(CourseForm, self).save(commit=False)
Expand Down
34 changes: 34 additions & 0 deletions django_project/certification/migrations/0007_certificatetype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 2.2.18 on 2021-12-03 00:56

from django.db import migrations, models


def add_certification_type_as_existing_value(apps, schema_editor):
CertificateType = apps.get_model('certification', 'CertificateType')
CertificateType.objects.create(
name='attendance and completion',
wording='Has attended and completed the course:',
order=0
)


class Migration(migrations.Migration):

dependencies = [
('certification', '0006_auto_20210730_0615'),
]

operations = [
migrations.CreateModel(
name='CertificateType',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(help_text='Certificate type.', max_length=200, unique=True)),
('description', models.TextField(blank=True, help_text='Certificate type description - 1000 characters limit.', max_length=1000, null=True)),
('wording', models.CharField(help_text='Wording that will be placed on certificate. e.g. "Has attended and completed".', max_length=500)),
('order', models.IntegerField(blank=True, null=True, unique=True)),
],
),

migrations.RunPython(add_certification_type_as_existing_value, reverse_code=migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 2.2.18 on 2021-12-10 02:23

from django.db import migrations, models
import django.db.models.deletion

def create_existing_project_certificate_type(apps, schema_editor):
CertificateType = apps.get_model('certification', 'CertificateType')
ProjectCertificateType = apps.get_model('certification', 'ProjectCertificateType')
Project = apps.get_model('base', 'Project')
certificate_type = CertificateType.objects.filter(
name='attendance and completion').first()
projects = Project.objects.all()

for project in projects:
ProjectCertificateType.objects.create(
project=project,
certificate_type=certificate_type
)

class Migration(migrations.Migration):

dependencies = [
('base', '0006_auto_20210308_0244'),
('certification', '0007_certificatetype'),
]

operations = [
migrations.CreateModel(
name='ProjectCertificateType',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('certificate_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='certification.CertificateType')),
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='base.Project')),
],
),

migrations.RunPython(create_existing_project_certificate_type, reverse_code=migrations.RunPython.noop),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.2.18 on 2021-12-10 08:31

from django.db import migrations, models
import django.db.models.deletion

def set_existing_certificate_type_value(apps, shcema_editor):
CertificateType = apps.get_model('certification', 'CertificateType')
Course = apps.get_model('certification', 'Course')
certificate_type = CertificateType.objects.filter(
name='attendance and completion').first()
courses = Course.objects.all()

for course in courses:
course.certificate_type = certificate_type
course.save(update_fields=['certificate_type'])


class Migration(migrations.Migration):

dependencies = [
('certification', '0008_projectcertificatetype'),
]

operations = [
migrations.AddField(
model_name='course',
name='certificate_type',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='certification.CertificateType'),
),

migrations.RunPython(set_existing_certificate_type_value, reverse_code=migrations.RunPython.noop),

migrations.AlterField(
model_name='course',
name='certificate_type',
field=models.ForeignKey(null=False, on_delete=django.db.models.deletion.PROTECT,
to='certification.CertificateType'),
preserve_default=False,
),

]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.2.18 on 2022-02-12 02:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('certification', '0007_courseconvener_is_active'),
('certification', '0009_course_certificate_type'),
]

operations = [
]
1 change: 1 addition & 0 deletions django_project/certification/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
from certification.models.course_attendee import *
from certification.models.course_type import *
from certification.models.course_convener import *
from certification.models.certificate_type import *
from certification.models.certificate import *
from certification.models.organisation_certificate import *
55 changes: 55 additions & 0 deletions django_project/certification/models/certificate_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Certificate type model for certification app"""

from django.db import models
from django.utils.translation import ugettext_lazy as _

from base.models.project import Project


class CertificateType(models.Model):
name = models.CharField(
help_text=_('Certificate type.'),
max_length=200,
null=False,
blank=False,
unique=True,
)

description = models.TextField(
help_text=_('Certificate type description - 1000 characters limit.'),
max_length=1000,
null=True,
blank=True,
)

wording = models.CharField(
help_text=_(
'Wording that will be placed on certificate. '
'e.g. "Has attended and completed".'
),
max_length=500,
null=False,
blank=False
)

order = models.IntegerField(
blank=True,
null=True,
unique=True
)

def __str__(self):
return self.name


class ProjectCertificateType(models.Model):
"""A model to store a certificate type linked to a project"""

project = models.ForeignKey(
Project,
on_delete=models.CASCADE
)
certificate_type = models.ForeignKey(
CertificateType,
on_delete=models.CASCADE
)
3 changes: 3 additions & 0 deletions django_project/certification/models/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .course_type import CourseType
from certification.utilities import check_slug
from .training_center import TrainingCenter
from certification.models.certificate_type import CertificateType

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -86,6 +87,8 @@ class Course(models.Model):
on_delete=models.CASCADE)
certifying_organisation = models.ForeignKey(CertifyingOrganisation,
on_delete=models.CASCADE)
certificate_type = models.ForeignKey(
CertificateType, on_delete=models.PROTECT, null=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
objects = models.Manager()

Expand Down
43 changes: 43 additions & 0 deletions django_project/certification/templates/certificate_type/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% extends "project_base.html" %}

{% block extra_js %}
{% endblock %}

{% block content %}
<style>
.table tbody:first-of-type tr {
background-color: transparent;
}
</style>

<div class="page-header">
<h1 class="text-muted">Certificate Types</h1>
</div>

<table class="table">
<thead>
<tr>
<th scope="col">Certificate Type</th>
<th scope="col">Wording</th>
<th scope="col">Apply</th>
</tr>
</thead>
<tbody>
<form action="{% url 'certificate-type-update' project_slug %}" method="post">
{% csrf_token %}
{% for cer_type in certificate_types %}
<tr {% if cer_type.id in certificate_types_applied %}style="background-color: #d7e5f0" {% endif %}>
<td>{{ cer_type.name }}</td>
<td>{{ cer_type.wording }}</td>
<td>
<input type="checkbox" name="certificate_types" value="{{ cer_type.name }}"
{% if cer_type.id in certificate_types_applied %}checked{% endif %}
>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<input class="btn btn-primary" type="submit" value="Save">
</form>
{% endblock %}
5 changes: 5 additions & 0 deletions django_project/certification/templates/course/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h3>New Course for {{ organisation.name }}</h3>
<input type="hidden" name="end_date" value="">
<input type="hidden" name="template_certificate" value="">
<input type="hidden" name="trained_competence" value="">
<input type="hidden" name="certificate_type" value="">
<input id="preview-certificate" type="submit" class="btn btn-default" value="Preview Certificate" form="preview-form">
</form>
<p id="error-submit" style="color: darkred; font-weight: bold"></p>
Expand Down Expand Up @@ -130,6 +131,9 @@ <h3>New Course for {{ organisation.name }}</h3>
}else if($('input[id=id_end_date]').val() === ''){
$('#error-submit').html('Please choose end date.');
return false
}else if($('select[id=id_certificate_type]').val() === ''){
$('#error-submit').html('Please choose certificate type.');
return false
}

$('#preview-form input[name=course_convener]').val($('select[name=course_convener]').val());
Expand All @@ -138,6 +142,7 @@ <h3>New Course for {{ organisation.name }}</h3>
$('#preview-form input[name=start_date]').val($('input[id=id_start_date]').val());
$('#preview-form input[name=end_date]').val($('input[id=id_end_date]').val());
$('#preview-form input[name=trained_competence]').val($('input[id=id_trained_competence]').val());
$('#preview-form input[name=certificate_type]').val($('select[id=id_certificate_type]').val());
}

//check if browser supports file api and filereader features
Expand Down
5 changes: 5 additions & 0 deletions django_project/certification/templates/course/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ <h3>Update Course for {{ organisation.name }}</h3>
<input type="hidden" name="end_date" value="">
<input type="hidden" name="template_certificate" value="">
<input type="hidden" name="trained_competence" value="">
<input type="hidden" name="certificate_type" value="">
<input id="preview-certificate" type="submit" class="btn btn-default" value="Preview Certificate" form="preview-form">
</form>
<p id="error-submit" style="color: darkred; font-weight: bold"></p>
Expand Down Expand Up @@ -138,6 +139,9 @@ <h3>Update Course for {{ organisation.name }}</h3>
}else if($('input[id=id_end_date]').val() === ''){
$('#error-submit').html('Please choose end date.');
return false
}else if($('select[id=id_certificate_type]').val() === ''){
$('#error-submit').html('Please choose certificate type.');
return false
}

$('#preview-form input[name=course_convener]').val($('select[name=course_convener]').val());
Expand All @@ -146,6 +150,7 @@ <h3>Update Course for {{ organisation.name }}</h3>
$('#preview-form input[name=start_date]').val($('input[id=id_start_date]').val());
$('#preview-form input[name=end_date]').val($('input[id=id_end_date]').val());
$('#preview-form input[name=trained_competence]').val($('input[id=id_trained_competence]').val());
$('#preview-form input[name=certificate_type]').val($('select[id=id_certificate_type]').val());
}

//check if browser supports file api and filereader features
Expand Down
Loading

0 comments on commit bdb45db

Please sign in to comment.