From 02ede72ce9ddeb4d7d7241503585bb07fe3e2c50 Mon Sep 17 00:00:00 2001 From: Kyle McFarland Date: Sat, 20 Oct 2018 21:51:14 -0600 Subject: Allow suppliers to have multiple representatives This adds a basic Representative model and allows for editing them in the admin interface both in the Suppliers admin panel for each supplier and in a new Representatives admin panel which allows bulk editing of all representatives. Currently multiple representatives are just listed in the component view as extra rows below the company row but it would probably make sense to add a view for viewing suppliers directly. The REST API has also been slightly modified to return a list of representatives for each supplier in the components endpoint, adding a suppliers endpoint would probably also be a good idea. Requires running: $ python manage.py migrate procurement 0002_add_representative To update the database for the new model, both forward and lossy reverse data migration is implemented in the migration. --- procurement/migrations/0002_add_representative.py | 80 +++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 procurement/migrations/0002_add_representative.py (limited to 'procurement/migrations') diff --git a/procurement/migrations/0002_add_representative.py b/procurement/migrations/0002_add_representative.py new file mode 100644 index 0000000..86a2f38 --- /dev/null +++ b/procurement/migrations/0002_add_representative.py @@ -0,0 +1,80 @@ +# Generated by Django 2.1.2 on 2018-10-18 02:18 + +from django.db import migrations, models +import django.db.models.deletion +import warnings + +def copy_reps_forward(apps, editor): + Supplier = apps.get_model("procurement", "Supplier") + Representative = apps.get_model("procurement", "Representative") + suppliers = Supplier.objects.all() + for sup in suppliers: + name = sup.representative_name + email = sup.representative_email + rep = Representative(name=name, email=email, supplier=sup) + rep.save() + +class ReverseMigrationDataLossWarning(UserWarning): + def __init__(self, supplier, kept_rep, lost_reps): + self.supplier = supplier + self.kept_rep = kept_rep + self.lost_reps = lost_reps + + @staticmethod + def format_rep(rep): + return '{} <{}>'.format(rep.name, rep.email) + + def __str__(self): + return "Supplier {} has multiple representatives, only keeping {}. {} will be lost".format(self.supplier.name, self.format_rep(self.kept_rep), [self.format_rep(rep) for rep in self.lost_reps]) + +def copy_reps_rev(apps, editor): + warnings.filterwarnings("always", category=ReverseMigrationDataLossWarning) + Supplier = apps.get_model("procurement", "Supplier") + Representative = apps.get_model("procurement", "Representative") + suppliers = Supplier.objects.all() + for sup in suppliers: + reps = sup.representatives.all() + if reps: + keep = reps[0] + if len(reps) > 1: + lost = reps[1:] + warnings.warn(ReverseMigrationDataLossWarning(sup, keep, lost)) + sup.representative_name = keep.name + sup.representative_email = keep.email + sup.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('procurement', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Representative', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created', models.DateTimeField(auto_now_add=True)), + ('updated', models.DateTimeField(auto_now=True)), + ('name', models.CharField(max_length=255)), + ('email', models.CharField(max_length=255)), + ], + options={ + 'abstract': False, + }, + ), + migrations.AddField( + model_name='representative', + name='supplier', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='representatives', to='procurement.Supplier'), + ), + migrations.RunPython(copy_reps_forward, copy_reps_rev), + migrations.RemoveField( + model_name='supplier', + name='representative_email', + ), + migrations.RemoveField( + model_name='supplier', + name='representative_name', + ), + ] -- cgit v1.1