diff --git a/assets/management/commands/generateSampleAssetsData.py b/assets/management/commands/generateSampleAssetsData.py index 258551c2..5748a086 100644 --- a/assets/management/commands/generateSampleAssetsData.py +++ b/assets/management/commands/generateSampleAssetsData.py @@ -92,7 +92,7 @@ class Command(BaseCommand): csa=random.choice(csas), length=random.choice(lengths), circuits=random.choice(circuits), - cores=random.choice(circuits) + cores=random.choice(cores) ) if i % 5 == 0: diff --git a/assets/migrations/0011_auto_20200218_1617.py b/assets/migrations/0011_auto_20200218_1617.py new file mode 100644 index 00000000..92696bc8 --- /dev/null +++ b/assets/migrations/0011_auto_20200218_1617.py @@ -0,0 +1,29 @@ +# Generated by Django 2.0.13 on 2020-02-18 16:17 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0010_auto_20200207_1737'), + ] + + operations = [ + migrations.CreateModel( + name='CableType', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('circuits', models.IntegerField(blank=True, null=True)), + ('cores', models.IntegerField(blank=True, null=True)), + ('plug', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='plug', to='assets.Connector')), + ('socket', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='socket', to='assets.Connector')), + ], + ), + migrations.AddField( + model_name='asset', + name='cable_type', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='assets.CableType'), + ), + ] diff --git a/assets/migrations/0012_auto_20200218_1627.py b/assets/migrations/0012_auto_20200218_1627.py new file mode 100644 index 00000000..bf61b845 --- /dev/null +++ b/assets/migrations/0012_auto_20200218_1627.py @@ -0,0 +1,26 @@ +# Generated by Django 2.0.13 on 2020-02-18 16:27 + +from django.db import migrations +from django.db.models import Q + + +def move_cable_type_data(apps, schema_editor): + Asset = apps.get_model('assets', 'Asset') + CableType = apps.get_model('assets', 'CableType') + for asset in Asset.objects.filter(is_cable=True): + # Only create one type per...well...type + if(not CableType.objects.filter(Q(plug=asset.plug) & Q(socket=asset.socket))): + cabletype = CableType.objects.create(plug=asset.plug, socket=asset.socket, circuits=asset.circuits, cores=asset.cores) + asset.save() + cabletype.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0011_auto_20200218_1617'), + ] + + operations = [ + migrations.RunPython(move_cable_type_data) + ] diff --git a/assets/migrations/0013_auto_20200218_1639.py b/assets/migrations/0013_auto_20200218_1639.py new file mode 100644 index 00000000..c7cebec1 --- /dev/null +++ b/assets/migrations/0013_auto_20200218_1639.py @@ -0,0 +1,29 @@ +# Generated by Django 2.0.13 on 2020-02-18 16:39 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0012_auto_20200218_1627'), + ] + + operations = [ + migrations.RemoveField( + model_name='asset', + name='circuits', + ), + migrations.RemoveField( + model_name='asset', + name='cores', + ), + migrations.RemoveField( + model_name='asset', + name='plug', + ), + migrations.RemoveField( + model_name='asset', + name='socket', + ), + ] diff --git a/assets/models.py b/assets/models.py index 5bf830bc..b1a106fc 100644 --- a/assets/models.py +++ b/assets/models.py @@ -66,6 +66,18 @@ class Connector(models.Model): return self.description +class CableType(models.Model): + circuits = models.IntegerField(blank=True, null=True) + cores = models.IntegerField(blank=True, null=True) + plug = models.ForeignKey(Connector, on_delete=models.SET_NULL, + related_name='plug', blank=True, null=True) + socket = models.ForeignKey(Connector, on_delete=models.SET_NULL, + related_name='socket', blank=True, null=True) + + def __str__(self): + return self.plug.description + "--->" + self.socket.description + + @reversion.register class Asset(models.Model, RevisionMixin): class Meta: @@ -92,16 +104,12 @@ class Asset(models.Model, RevisionMixin): # Cable assets is_cable = models.BooleanField(default=False) - plug = models.ForeignKey(Connector, on_delete=models.SET_NULL, - related_name='plug', blank=True, null=True) - socket = models.ForeignKey(Connector, on_delete=models.SET_NULL, - related_name='socket', blank=True, null=True) + cable_type = models.ForeignKey(to=CableType, blank=True, null=True, on_delete=models.SET_NULL) length = models.DecimalField(decimal_places=1, max_digits=10, blank=True, null=True, help_text='m') csa = models.DecimalField(decimal_places=2, max_digits=10, blank=True, null=True, help_text='mm^2') - circuits = models.IntegerField(blank=True, null=True) - cores = models.IntegerField(blank=True, null=True) + # Hidden asset_id components # For example, if asset_id was "C1001" then asset_id_prefix would be "C" and number "1001" diff --git a/assets/templates/cable_type_form.html b/assets/templates/cable_type_form.html new file mode 100644 index 00000000..c72019e9 --- /dev/null +++ b/assets/templates/cable_type_form.html @@ -0,0 +1,37 @@ +{% extends 'base_assets.html' %} +{% load widget_tweaks %} +{% block title %}Asset {{ object.asset_id }}{% endblock %} + +{% block content %} + +