Add Certification Version model, moving existing data to it

This commit is contained in:
Adam Goldsmith 2022-02-04 16:30:52 -05:00
parent da8417c12b
commit a586688653
3 changed files with 89 additions and 3 deletions

View File

@ -2,12 +2,24 @@ from django.contrib import admin
from .models import (CmsRedRiverVeteransScholarship, from .models import (CmsRedRiverVeteransScholarship,
CertificationDefinition, Certification, CertificationDefinition, Certification,
CertificationVersion,
InstructorOrVendor, SpecialProgram, Waiver) 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) @admin.register(CertificationDefinition)
class CertificationDefinitionAdmin(admin.ModelAdmin): class CertificationDefinitionAdmin(admin.ModelAdmin):
search_fields = ['certification_name', 'department'] search_fields = ['certification_name', 'department']
inlines = [CertificationVersionInline]
@admin.register(Certification) @admin.register(Certification)

View File

@ -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',
),
]

View File

@ -35,18 +35,32 @@ class CertificationDefinition(models.Model):
ordering = ('certification_name', 'department') 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): class Certification(models.Model):
number = models.AutoField(db_column='Number', primary_key=True) 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) 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) 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) 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) 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) shop_lead_notified = models.DateTimeField(db_column='Shop Lead Notified', blank=True, null=True)
def __str__(self): def __str__(self):
return f"{self.name} - {self.certification} [{self.version}]" return f"{self.name} - {self.certification_version}"
class Meta: class Meta:
db_table = 'Certifications' db_table = 'Certifications'