Add package parameter model.

Restructure Parameter models to use a common abstract base class.
This commit is contained in:
Mario Hüttel 2024-11-19 21:33:12 +01:00
parent 841be4f1bb
commit 39e40d62f4
3 changed files with 75 additions and 6 deletions

View File

@ -9,6 +9,7 @@ admin.site.register(parts_models.Manufacturer)
admin.site.register(parts_models.Storage) admin.site.register(parts_models.Storage)
admin.site.register(parts_models.Stock) admin.site.register(parts_models.Stock)
admin.site.register(parts_models.ComponentParameter) admin.site.register(parts_models.ComponentParameter)
admin.site.register(parts_models.PackageParameter)
admin.site.register(parts_models.ComponentParameterType) admin.site.register(parts_models.ComponentParameterType)
admin.site.register(parts_models.ComponentType) admin.site.register(parts_models.ComponentType)
admin.site.register(parts_models.Distributor) admin.site.register(parts_models.Distributor)

View File

@ -0,0 +1,49 @@
# Generated by Django 5.1.3 on 2024-11-19 20:26
import django.db.models.deletion
import uuid
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("parts", "0012_storage_verbose_name"),
]
operations = [
migrations.CreateModel(
name="PackageParameter",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
("value", models.FloatField(default=0)),
("text_value", models.TextField(blank=True)),
(
"package",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="parts.package"
),
),
(
"parameter_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="parts.componentparametertype",
),
),
],
options={
"ordering": ["id"],
"unique_together": {("package", "parameter_type")},
},
),
]

View File

@ -238,24 +238,25 @@ class Component(models.Model):
sum = 0 sum = 0
return sum return sum
class AbstractParameter(models.Model):
class ComponentParameter(models.Model):
class Meta: class Meta:
unique_together = ('component', 'parameter_type') abstract = True
ordering = ['id']
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
component = models.ForeignKey(Component, on_delete=models.CASCADE) # A target component is required!
parameter_type = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE) parameter_type = models.ForeignKey(ComponentParameterType, on_delete=models.CASCADE)
value = models.FloatField(default=0) value = models.FloatField(default=0)
text_value = models.TextField(null=False, blank=True) text_value = models.TextField(null=False, blank=True)
def _get_object_of_param(self):
return None
def __str__(self): def __str__(self):
if self.parameter_type.parameter_type == 'F': if self.parameter_type.parameter_type == 'F':
value = self.text_value value = self.text_value
else: else:
value = str(self.value) value = str(self.value)
return str(self.component)+ ': '+ str(self.parameter_type) + ': ' + value return str(self._get_object_of_param())+ ': '+ str(self.parameter_type) + ': ' + value
def resolved_value_as_string(self): def resolved_value_as_string(self):
my_type = self.parameter_type.parameter_type my_type = self.parameter_type.parameter_type
@ -270,6 +271,24 @@ class ComponentParameter(models.Model):
elif my_type == 'F': elif my_type == 'F':
return self.text_value return self.text_value
class ComponentParameter(AbstractParameter):
class Meta:
unique_together = ('component', 'parameter_type')
ordering = ['id']
component = models.ForeignKey(Component, on_delete=models.CASCADE) # A target component is required!
def _get_object_of_param(self):
return self.component
class PackageParameter(AbstractParameter):
class Meta:
unique_together = ('package', 'parameter_type')
ordering = ['id']
package = models.ForeignKey(Package, on_delete=models.CASCADE)
def _get_object_of_param(self):
return self.package
class Stock(models.Model): class Stock(models.Model):
class Meta: class Meta:
unique_together = ('component', 'storage') unique_together = ('component', 'storage')