From a586688653150520f3c6997cd9f1a240b215e1ae Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 4 Feb 2022 16:30:52 -0500 Subject: [PATCH] Add Certification Version model, moving existing data to it --- paperwork/admin.py | 12 ++++ .../0002_add_certification_version_model.py | 60 +++++++++++++++++++ paperwork/models.py | 20 ++++++- 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 paperwork/migrations/0002_add_certification_version_model.py diff --git a/paperwork/admin.py b/paperwork/admin.py index a2bdbb7..1b9495a 100644 --- a/paperwork/admin.py +++ b/paperwork/admin.py @@ -2,12 +2,24 @@ from django.contrib import admin from .models import (CmsRedRiverVeteransScholarship, CertificationDefinition, Certification, + CertificationVersion, InstructorOrVendor, SpecialProgram, Waiver) +class CertificationVersionInline(admin.TabularInline): + model = CertificationVersion + extra = 1 + + +@admin.register(CertificationVersion) +class CertificationVersionAdmin(admin.ModelAdmin): + search_fields = ['definition__certification_name', 'version'] + + @admin.register(CertificationDefinition) class CertificationDefinitionAdmin(admin.ModelAdmin): search_fields = ['certification_name', 'department'] + inlines = [CertificationVersionInline] @admin.register(Certification) diff --git a/paperwork/migrations/0002_add_certification_version_model.py b/paperwork/migrations/0002_add_certification_version_model.py new file mode 100644 index 0000000..da8d8d5 --- /dev/null +++ b/paperwork/migrations/0002_add_certification_version_model.py @@ -0,0 +1,60 @@ +# (Partially) Generated by Django 4.0.2 on 2022-02-04 18:01 + +from django.db import migrations, models +import django.db.models.deletion + + +def migrate_certification_version_forward(apps, schema_editor): + db_alias = schema_editor.connection.alias + + Certification = apps.get_model("paperwork", "Certification") + CertificationVersion = apps.get_model("paperwork", "CertificationVersion") + + for certification in Certification.objects.using(db_alias).all(): + (version, _) = CertificationVersion.objects.using(db_alias).get_or_create( + definition=certification.certification, version=certification.version + ) + certification.certification_version = version + certification.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('membershipworks', '0001_initial'), + ('paperwork', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='CertificationVersion', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('version', models.CharField(blank=True, db_column='Version', max_length=255, null=True)), + ('definition', models.ForeignKey(db_column='Certification', on_delete=django.db.models.deletion.PROTECT, to='paperwork.certificationdefinition')), + ], + ), + migrations.AddConstraint( + model_name='certificationversion', + constraint=models.UniqueConstraint(fields=('definition', 'version'), name='unique_certification_version'), + ), + migrations.AddField( + model_name='certification', + name='certification_version', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='paperwork.certificationversion', null=True), + ), + migrations.RunPython(migrate_certification_version_forward), + migrations.AlterField( + model_name='certification', + name='certification_version', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='paperwork.certificationversion'), + ), + migrations.RemoveField( + model_name='certification', + name='certification', + ), + migrations.RemoveField( + model_name='certification', + name='version', + ), + ] diff --git a/paperwork/models.py b/paperwork/models.py index 60add71..1c25e8b 100644 --- a/paperwork/models.py +++ b/paperwork/models.py @@ -35,18 +35,32 @@ class CertificationDefinition(models.Model): ordering = ('certification_name', 'department') +class CertificationVersion(models.Model): + definition = models.ForeignKey(CertificationDefinition, on_delete=models.PROTECT, db_column='Certification') + version = models.CharField(db_column='Version', max_length=255, blank=True, null=True) + + def __str__(self): + return f"{self.definition} [{self.version}]" + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["definition", "version"], name="unique_certification_version" + ) + ] + + class Certification(models.Model): number = models.AutoField(db_column='Number', primary_key=True) - certification = models.ForeignKey(CertificationDefinition, on_delete=models.PROTECT, db_column='Certification') + certification_version = models.ForeignKey(CertificationVersion, on_delete=models.PROTECT) name = models.CharField(db_column='Name', max_length=255) member = models.ForeignKey(Member, on_delete=models.PROTECT, to_field='uid', db_column='uid', blank=True, null=True, db_constraint=False) certified_by = models.CharField(db_column='Certified_By', max_length=255, blank=True, null=True) date = models.DateField(db_column='Date', blank=True, null=True) - version = models.CharField(db_column='Version', max_length=255, blank=True, null=True) shop_lead_notified = models.DateTimeField(db_column='Shop Lead Notified', blank=True, null=True) def __str__(self): - return f"{self.name} - {self.certification} [{self.version}]" + return f"{self.name} - {self.certification_version}" class Meta: db_table = 'Certifications'