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 %} + + +
+{% include 'form_errors.html' %} +{% csrf_token %} + +
+
+
+ + {% render_field form.plug|add_class:'form-control'%} +
+
+ + {% render_field form.socket|add_class:'form-control'%} +
+
+ + {% render_field form.circuits|add_class:'form-control' value=object.circuits %} +
+
+ + {% render_field form.cores|add_class:'form-control' value=object.cores %} +
+
+
+
+{% endblock %} diff --git a/assets/templates/partials/asset_form.html b/assets/templates/partials/asset_form.html index 45424992..a1fd4c88 100644 --- a/assets/templates/partials/asset_form.html +++ b/assets/templates/partials/asset_form.html @@ -23,7 +23,6 @@ {% render_field form.category|add_class:'form-control'%} - {% render_field form.is_cable|attr:'onchange=checkIfCableHidden()' %}
{% render_field form.status|add_class:'form-control'%} @@ -32,6 +31,10 @@ {% render_field form.serial_number|add_class:'form-control' value=object.serial_number %}
+
+ + {% render_field form.is_cable|attr:'onchange=checkIfCableHidden()' %} +
diff --git a/assets/templates/partials/cable_form.html b/assets/templates/partials/cable_form.html index e5deb006..1a7001c2 100644 --- a/assets/templates/partials/cable_form.html +++ b/assets/templates/partials/cable_form.html @@ -6,12 +6,11 @@
{% if create or edit or duplicate %}
- - {% render_field form.plug|add_class:'form-control'%} -
-
- - {% render_field form.socket|add_class:'form-control'%} + +
+ {% render_field form.cable_type|add_class:'form-control' %} + {{ form.length.help_text }} +
@@ -27,14 +26,6 @@ {{ form.csa.help_text }}
-
- - {% render_field form.circuits|add_class:'form-control' value=object.circuits %} -
-
- - {% render_field form.cores|add_class:'form-control' value=object.cores %} -
{% else %}
Socket