2023-04-24 20:11:27 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
from modelcluster.models import ClusterableModel
|
|
|
|
from modelcluster.fields import ParentalKey
|
|
|
|
from wagtail.admin.panels import (
|
|
|
|
FieldPanel,
|
|
|
|
InlinePanel
|
|
|
|
)
|
|
|
|
from wagtail.models import Orderable
|
|
|
|
|
2023-04-24 20:11:27 +00:00
|
|
|
|
2023-04-27 21:06:28 +00:00
|
|
|
class ProductAuthor(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2023-04-24 20:11:27 +00:00
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class ProductCategory(ClusterableModel):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
panels = [
|
|
|
|
FieldPanel("name"),
|
|
|
|
InlinePanel("category_params")
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class CategoryParamTypeChoices(models.TextChoices):
|
|
|
|
INT = "int"
|
|
|
|
STRING = "str"
|
|
|
|
FLOAT = "float"
|
|
|
|
|
|
|
|
|
|
|
|
class ProductCategoryParam(ClusterableModel):
|
|
|
|
category = ParentalKey(ProductCategory, on_delete=models.CASCADE, related_name="category_params")
|
|
|
|
key = models.CharField(max_length=200)
|
|
|
|
param_type = models.CharField(max_length=200, choices=CategoryParamTypeChoices.choices)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.key
|
|
|
|
|
|
|
|
|
|
|
|
class ProductTemplate(ClusterableModel):
|
|
|
|
category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE)
|
|
|
|
author = models.ForeignKey(ProductAuthor, on_delete=models.CASCADE)
|
2023-04-27 21:06:28 +00:00
|
|
|
title = models.CharField(max_length=255)
|
|
|
|
code = models.CharField(max_length=255)
|
|
|
|
description = models.TextField()
|
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2023-04-27 21:06:28 +00:00
|
|
|
def get_images(self):
|
|
|
|
return self.images.objects.all().values_list("image")
|
2023-04-24 20:11:27 +00:00
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
panels = [
|
|
|
|
FieldPanel("category"),
|
|
|
|
FieldPanel("author"),
|
|
|
|
FieldPanel('title'),
|
|
|
|
FieldPanel('code'),
|
|
|
|
FieldPanel('description'),
|
|
|
|
InlinePanel("images")
|
|
|
|
]
|
|
|
|
|
2023-04-27 21:06:28 +00:00
|
|
|
|
|
|
|
class ProductImage(models.Model):
|
2023-05-10 19:47:03 +00:00
|
|
|
template = ParentalKey(
|
2023-04-27 21:06:28 +00:00
|
|
|
ProductTemplate, on_delete=models.CASCADE, related_name="images"
|
|
|
|
)
|
|
|
|
image = models.ImageField()
|
2023-04-24 20:11:27 +00:00
|
|
|
|
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
class Product(ClusterableModel):
|
|
|
|
template = models.ForeignKey(ProductTemplate, on_delete=models.CASCADE)
|
2023-04-24 20:11:27 +00:00
|
|
|
price = models.FloatField()
|
2023-04-27 21:06:28 +00:00
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
panels = [
|
|
|
|
FieldPanel("template"),
|
|
|
|
FieldPanel("price"),
|
|
|
|
InlinePanel("param_values")
|
|
|
|
]
|
2023-04-27 21:06:28 +00:00
|
|
|
|
2023-05-10 19:47:03 +00:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
return f"{self.template.title} - {self.price}"
|
|
|
|
|
|
|
|
|
|
|
|
class TemplateParamValue(models.Model):
|
|
|
|
param = models.ForeignKey(ProductCategoryParam, on_delete=models.CASCADE)
|
|
|
|
product = ParentalKey(Product, on_delete=models.CASCADE, related_name="param_values")
|
|
|
|
value = models.CharField(max_length=255)
|