diff --git a/assets/admin.py b/assets/admin.py index 3e6c9d58..ca39ca3b 100644 --- a/assets/admin.py +++ b/assets/admin.py @@ -23,10 +23,15 @@ class SupplierAdmin(admin.ModelAdmin): @admin.register(assets.Asset) class AssetAdmin(admin.ModelAdmin): list_display = ['id', 'asset_id', 'description', 'category', 'status'] - list_filter = ['is_cable', 'category'] + list_filter = ['is_cable', 'category', 'status'] search_fields = ['id', 'asset_id', 'description'] +@admin.register(assets.CableType) +class CableTypeAdmin(admin.ModelAdmin): + list_display = ['id', '__str__', 'plug', 'socket', 'cores', 'circuits'] + + @admin.register(assets.Connector) class ConnectorAdmin(admin.ModelAdmin): list_display = ['id', '__str__', 'current_rating', 'voltage_rating', 'num_pins'] diff --git a/assets/forms.py b/assets/forms.py index ea05efd3..5afc82a3 100644 --- a/assets/forms.py +++ b/assets/forms.py @@ -34,3 +34,9 @@ class SupplierForm(forms.ModelForm): class SupplierSearchForm(forms.Form): query = forms.CharField(required=False) + + +class CableTypeForm(forms.ModelForm): + class Meta: + model = models.CableType + fields = '__all__' diff --git a/assets/models.py b/assets/models.py index b1a106fc..fc822137 100644 --- a/assets/models.py +++ b/assets/models.py @@ -3,6 +3,7 @@ from django.core.exceptions import ValidationError from django.db import models, connection from django.urls import reverse +from django.db.models import F from django.db.models.signals import pre_save from django.dispatch.dispatcher import receiver @@ -67,6 +68,9 @@ class Connector(models.Model): class CableType(models.Model): + class Meta: + ordering = ['plug', 'socket', '-circuits'] + circuits = models.IntegerField(blank=True, null=True) cores = models.IntegerField(blank=True, null=True) plug = models.ForeignKey(Connector, on_delete=models.SET_NULL, @@ -75,7 +79,7 @@ class CableType(models.Model): related_name='socket', blank=True, null=True) def __str__(self): - return self.plug.description + "--->" + self.socket.description + return "%s → %s" % (self.plug.description, self.socket.description) @reversion.register @@ -139,7 +143,7 @@ class Asset(models.Model, RevisionMixin): def __str__(self): out = str(self.asset_id) + ' - ' + self.description if self.is_cable: - out += '{} - {}m - {}'.format(self.plug, self.length, self.socket) + out += '{} - {}m - {}'.format(self.cable_type.plug, self.length, self.cable_type.socket) return out def clean(self): @@ -164,14 +168,16 @@ class Asset(models.Model, RevisionMixin): errdict["length"] = ["The length of a cable must be more than 0"] if not self.csa or self.csa <= 0: errdict["csa"] = ["The CSA of a cable must be more than 0"] - if not self.circuits or self.circuits <= 0: - errdict["circuits"] = ["There must be at least one circuit in a cable"] - if not self.cores or self.cores <= 0: - errdict["cores"] = ["There must be at least one core in a cable"] - if self.socket is None: - errdict["socket"] = ["A cable must have a socket"] - if self.plug is None: - errdict["plug"] = ["A cable must have a plug"] + if not self.cable_type: + errdict["cable_type"] = ["A cable must have a type"] + # if not self.circuits or self.circuits <= 0: + # errdict["circuits"] = ["There must be at least one circuit in a cable"] + # if not self.cores or self.cores <= 0: + # errdict["cores"] = ["There must be at least one core in a cable"] + # if self.socket is None: + # errdict["socket"] = ["A cable must have a socket"] + # if self.plug is None: + # errdict["plug"] = ["A cable must have a plug"] if errdict != {}: # If there was an error when validation raise ValidationError(errdict) diff --git a/assets/templates/cable_type_form.html b/assets/templates/cable_type_form.html index c72019e9..c8b792ad 100644 --- a/assets/templates/cable_type_form.html +++ b/assets/templates/cable_type_form.html @@ -1,12 +1,11 @@ {% extends 'base_assets.html' %} {% load widget_tweaks %} -{% block title %}Asset {{ object.asset_id }}{% endblock %} +{% block title %}Cable Type{% endblock %} {% block content %} -
| Cable Type | +Circuits | +Cores | +Quick Links | +
|---|---|---|---|
| {{ item }} | +{{ item.circuits }} | +{{ item.cores }} | ++ View + Edit + | +