2022-10-21 21:39:04 +03:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
2022-10-21 23:34:41 +03:00
|
|
|
class Characteristic(models.Model):
|
2022-10-22 03:09:39 +03:00
|
|
|
name = models.TextField("Имя", blank=False)
|
|
|
|
value = models.TextField("Значение", blank=False)
|
2022-10-21 23:34:41 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.name)
|
|
|
|
|
|
|
|
def serialize_self(self):
|
|
|
|
return {"name": self.name, "value": self.value}
|
|
|
|
|
|
|
|
class Meta:
|
2022-10-22 03:09:39 +03:00
|
|
|
db_table = "characteristic"
|
2022-10-21 23:34:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
class UnitCharacteristic(models.Model):
|
2022-10-22 03:09:39 +03:00
|
|
|
name = models.TextField("Имя", blank=False)
|
|
|
|
value = models.TextField("Значение", blank=False)
|
2022-10-22 16:14:43 +03:00
|
|
|
numeric_value_min = models.IntegerField(default=0)
|
|
|
|
numeric_value_max = models.IntegerField(default=0)
|
2022-10-22 03:09:39 +03:00
|
|
|
unit = models.TextField("Размерность", blank=False)
|
2022-10-21 23:34:41 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.name)
|
|
|
|
|
|
|
|
def serialize_self(self):
|
2022-10-22 16:14:43 +03:00
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"value": self.numeric_value_min
|
|
|
|
if self.numeric_value_min == self.numeric_value_max
|
|
|
|
else f"{self.numeric_value_min}:{self.numeric_value_max}",
|
|
|
|
"unit": self.unit,
|
|
|
|
}
|
2022-10-21 23:34:41 +03:00
|
|
|
|
2022-10-23 18:07:06 +03:00
|
|
|
@property
|
|
|
|
def num_value(self):
|
|
|
|
return (
|
|
|
|
self.numeric_value_min
|
|
|
|
if self.numeric_value_min == self.numeric_value_max
|
|
|
|
else f"{self.numeric_value_min}:{self.numeric_value_max}"
|
|
|
|
)
|
|
|
|
|
2022-10-21 23:34:41 +03:00
|
|
|
class Meta:
|
2022-10-22 03:09:39 +03:00
|
|
|
db_table = "unit_characteristic"
|
2022-10-21 23:34:41 +03:00
|
|
|
|
|
|
|
|
2022-10-21 21:39:04 +03:00
|
|
|
class Category(models.Model):
|
2022-10-22 03:09:39 +03:00
|
|
|
name = models.TextField("Имя", unique=True, blank=False)
|
2022-10-21 21:39:04 +03:00
|
|
|
|
|
|
|
def __str__(self):
|
2022-10-21 22:36:36 +03:00
|
|
|
return str(self.name)
|
2022-10-21 21:39:04 +03:00
|
|
|
|
|
|
|
class Meta:
|
2022-10-22 03:09:39 +03:00
|
|
|
db_table = "category"
|
2022-10-21 21:39:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Product(models.Model):
|
|
|
|
id = models.IntegerField(
|
|
|
|
"ID CTE", primary_key=True, unique=True, blank=False, null=False, db_index=True
|
|
|
|
)
|
2022-10-22 22:29:49 +03:00
|
|
|
name = models.TextField("Название CTE", blank=False)
|
2022-10-21 23:34:41 +03:00
|
|
|
category = models.ForeignKey(
|
|
|
|
Category, related_name="products", on_delete=models.CASCADE
|
|
|
|
)
|
2022-10-21 21:39:04 +03:00
|
|
|
|
2022-10-22 16:14:43 +03:00
|
|
|
score = models.IntegerField(default=0)
|
2022-10-22 05:07:25 +03:00
|
|
|
|
2022-10-21 21:39:04 +03:00
|
|
|
def __str__(self):
|
2022-10-21 22:36:36 +03:00
|
|
|
return str(self.name)
|
|
|
|
|
|
|
|
def serialize_self(self) -> dict:
|
|
|
|
return {
|
2022-10-22 16:14:43 +03:00
|
|
|
"id": self.id,
|
2022-10-21 23:34:41 +03:00
|
|
|
"name": self.name,
|
2022-10-22 16:14:43 +03:00
|
|
|
"score": self.score,
|
2022-10-21 23:34:41 +03:00
|
|
|
"characteristic": [
|
2022-10-22 05:07:25 +03:00
|
|
|
x.characteristic.serialize_self() for x in self.characteristics.all()
|
2022-10-21 23:34:41 +03:00
|
|
|
]
|
2022-10-22 05:07:25 +03:00
|
|
|
+ [
|
|
|
|
x.characteristic.serialize_self()
|
|
|
|
for x in self.unit_characteristics.all()
|
|
|
|
],
|
2022-10-21 22:36:36 +03:00
|
|
|
}
|
2022-10-21 21:39:04 +03:00
|
|
|
|
|
|
|
class Meta:
|
2022-10-22 03:09:39 +03:00
|
|
|
db_table = "product"
|
2022-10-21 22:36:36 +03:00
|
|
|
|
|
|
|
|
2022-10-21 23:34:41 +03:00
|
|
|
class ProductCharacteristic(models.Model):
|
2022-10-21 22:36:36 +03:00
|
|
|
product = models.ForeignKey(
|
2022-10-21 23:34:41 +03:00
|
|
|
Product, related_name="characteristics", on_delete=models.CASCADE
|
2022-10-21 22:36:36 +03:00
|
|
|
)
|
2022-10-21 23:34:41 +03:00
|
|
|
characteristic = models.ForeignKey(
|
|
|
|
Characteristic, related_name="products", on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.product} in {self.characteristic}"
|
|
|
|
|
|
|
|
class Meta:
|
2022-10-22 03:09:39 +03:00
|
|
|
db_table = "product_characteristic"
|
2022-10-21 23:34:41 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ProductUnitCharacteristic(models.Model):
|
|
|
|
product = models.ForeignKey(
|
|
|
|
Product, related_name="unit_characteristics", on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
characteristic = models.ForeignKey(
|
|
|
|
UnitCharacteristic, related_name="products", on_delete=models.CASCADE
|
2022-10-21 22:36:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-10-21 23:34:41 +03:00
|
|
|
return f"{self.product} in {self.characteristic}"
|
|
|
|
|
|
|
|
class Meta:
|
2022-10-22 03:09:39 +03:00
|
|
|
db_table = "product_unit_characteristic"
|