337 lines
13 KiB
Python
337 lines
13 KiB
Python
"""Product app models."""
|
|
from django.contrib.contenttypes import fields as generic
|
|
from django.contrib.gis.db import models as gis_models
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import models
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
|
|
|
from utils.models import (BaseAttributes, ProjectBaseMixin,
|
|
TranslatedFieldsMixin, TJSONField)
|
|
|
|
|
|
class ProductType(TranslatedFieldsMixin, ProjectBaseMixin):
|
|
"""ProductType model."""
|
|
|
|
STR_FIELD_NAME = 'name'
|
|
|
|
# INDEX NAME CHOICES
|
|
FOOD = 'food'
|
|
WINE = 'wine'
|
|
LIQUOR = 'liquor'
|
|
SOUVENIR = 'souvenir'
|
|
BOOK = 'book'
|
|
|
|
INDEX_NAME_TYPES = (
|
|
(FOOD, _('Food')),
|
|
(WINE, _('Wine')),
|
|
(LIQUOR, _('Liquor')),
|
|
(SOUVENIR, _('Souvenir')),
|
|
(BOOK, _('Book')),
|
|
)
|
|
|
|
name = TJSONField(blank=True, null=True, default=None,
|
|
verbose_name=_('Name'), help_text='{"en-GB":"some text"}')
|
|
index_name = models.CharField(max_length=50, choices=INDEX_NAME_TYPES,
|
|
unique=True, db_index=True,
|
|
verbose_name=_('Index name'))
|
|
use_subtypes = models.BooleanField(_('Use subtypes'), default=True)
|
|
tag_categories = models.ManyToManyField('tag.TagCategory',
|
|
related_name='product_types',
|
|
verbose_name=_('Tag'))
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
verbose_name = _('Product type')
|
|
verbose_name_plural = _('Product types')
|
|
|
|
|
|
class ProductSubType(TranslatedFieldsMixin, ProjectBaseMixin):
|
|
"""ProductSubtype model."""
|
|
|
|
STR_FIELD_NAME = 'name'
|
|
|
|
# INDEX NAME CHOICES
|
|
RUM = 'rum'
|
|
PLATE = 'plate'
|
|
OTHER = 'other'
|
|
|
|
INDEX_NAME_TYPES = (
|
|
(RUM, _('Rum')),
|
|
(PLATE, _('Plate')),
|
|
(OTHER, _('Other')),
|
|
)
|
|
|
|
product_type = models.ForeignKey(ProductType, on_delete=models.CASCADE,
|
|
related_name='subtypes',
|
|
verbose_name=_('Product type'))
|
|
name = TJSONField(blank=True, null=True, default=None,
|
|
verbose_name=_('Name'), help_text='{"en-GB":"some text"}')
|
|
index_name = models.CharField(max_length=50, choices=INDEX_NAME_TYPES,
|
|
unique=True, db_index=True,
|
|
verbose_name=_('Index name'))
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
verbose_name = _('Product subtype')
|
|
verbose_name_plural = _('Product subtypes')
|
|
|
|
def clean_fields(self, exclude=None):
|
|
if not self.product_type.use_subtypes:
|
|
raise ValidationError(_('Product type is not use subtypes.'))
|
|
|
|
|
|
class ProductManager(models.Manager):
|
|
"""Extended manager for Product model."""
|
|
|
|
|
|
class ProductQuerySet(models.QuerySet):
|
|
"""Product queryset."""
|
|
|
|
def with_base_related(self):
|
|
return self.select_related('product_type', 'establishment') \
|
|
.prefetch_related('product_type__subtypes', 'country')
|
|
|
|
def common(self):
|
|
return self.filter(category=self.model.COMMON)
|
|
|
|
def online(self):
|
|
return self.filter(category=self.model.ONLINE)
|
|
|
|
def wines(self):
|
|
return self.filter(type__index_name=ProductType.WINE)
|
|
|
|
def by_product_type(self, product_type: str):
|
|
"""Filter by type."""
|
|
return self.filter(product_type__index_name=product_type)
|
|
|
|
def by_product_subtype(self, product_subtype: str):
|
|
"""Filter by subtype."""
|
|
return self.filter(subtypes__index_name=product_subtype)
|
|
|
|
|
|
class Product(TranslatedFieldsMixin, BaseAttributes):
|
|
"""Product models."""
|
|
|
|
EARLIEST_VINTAGE_YEAR = 1700
|
|
LATEST_VINTAGE_YEAR = 2100
|
|
|
|
COMMON = 0
|
|
ONLINE = 1
|
|
|
|
CATEGORY_CHOICES = (
|
|
(COMMON, _('Common')),
|
|
(ONLINE, _('Online')),
|
|
)
|
|
|
|
PUBLISHED = 0
|
|
OUT_OF_PRODUCTION = 1
|
|
WAITING = 2
|
|
|
|
STATE_CHOICES = (
|
|
(PUBLISHED, _('Published')),
|
|
(OUT_OF_PRODUCTION, _('Out_of_production')),
|
|
(WAITING, _('Waiting')),
|
|
)
|
|
|
|
category = models.PositiveIntegerField(choices=CATEGORY_CHOICES,
|
|
default=COMMON)
|
|
name = models.CharField(max_length=255,
|
|
default=None, null=True,
|
|
verbose_name=_('name'))
|
|
description = TJSONField(_('Description'), null=True, blank=True,
|
|
default=None, help_text='{"en-GB":"some text"}')
|
|
country = models.ManyToManyField('location.Country',
|
|
verbose_name=_('Country'))
|
|
available = models.BooleanField(_('Available'), default=True)
|
|
product_type = models.ForeignKey(ProductType, on_delete=models.PROTECT,
|
|
null=True,
|
|
related_name='products', verbose_name=_('Type'))
|
|
subtypes = models.ManyToManyField(ProductSubType, blank=True,
|
|
related_name='products',
|
|
verbose_name=_('Subtypes'))
|
|
establishment = models.ForeignKey('establishment.Establishment', on_delete=models.PROTECT,
|
|
blank=True, null=True,
|
|
related_name='products',
|
|
verbose_name=_('establishment'))
|
|
public_mark = models.PositiveIntegerField(blank=True, null=True, default=None,
|
|
verbose_name=_('public mark'),)
|
|
wine_region = models.ForeignKey('location.WineRegion', on_delete=models.PROTECT,
|
|
related_name='wines',
|
|
blank=True, null=True, default=None,
|
|
verbose_name=_('wine region'))
|
|
wine_sub_region = models.ForeignKey('location.WineSubRegion', on_delete=models.PROTECT,
|
|
related_name='wines',
|
|
blank=True, null=True, default=None,
|
|
verbose_name=_('wine sub region'))
|
|
classifications = models.ManyToManyField('ProductClassification',
|
|
blank=True,
|
|
verbose_name=_('classifications'))
|
|
standards = models.ManyToManyField('ProductStandard',
|
|
blank=True,
|
|
verbose_name=_('standards'),
|
|
help_text=_('attribute from legacy db'))
|
|
wine_village = models.ForeignKey('location.WineVillage', on_delete=models.PROTECT,
|
|
blank=True, null=True,
|
|
verbose_name=_('wine appellation'))
|
|
slug = models.SlugField(unique=True, max_length=255, null=True,
|
|
verbose_name=_('Establishment slug'))
|
|
favorites = generic.GenericRelation(to='favorites.Favorites')
|
|
old_id = models.PositiveIntegerField(_('old id'), default=None,
|
|
blank=True, null=True)
|
|
state = models.PositiveIntegerField(choices=STATE_CHOICES,
|
|
default=WAITING,
|
|
verbose_name=_('state'),
|
|
help_text=_('attribute from legacy db'))
|
|
tags = models.ManyToManyField('tag.Tag', related_name='products',
|
|
verbose_name=_('Tag'))
|
|
old_unique_key = models.CharField(max_length=255, unique=True,
|
|
blank=True, null=True, default=None,
|
|
help_text=_('attribute from legacy db'))
|
|
vintage = models.IntegerField(verbose_name=_('vintage year'),
|
|
null=True, blank=True, default=None,
|
|
validators=[MinValueValidator(EARLIEST_VINTAGE_YEAR),
|
|
MaxValueValidator(LATEST_VINTAGE_YEAR)])
|
|
|
|
objects = ProductManager.from_queryset(ProductQuerySet)()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
verbose_name = _('Product')
|
|
verbose_name_plural = _('Products')
|
|
|
|
def __str__(self):
|
|
"""Override str dunder method."""
|
|
return self.name
|
|
|
|
def clean_fields(self, exclude=None):
|
|
super().clean_fields(exclude=exclude)
|
|
if self.product_type.index_name == ProductType.WINE and not self.wine_region:
|
|
raise ValidationError(_('wine_region field must be specified.'))
|
|
if not self.product_type.index_name == ProductType.WINE and self.wine_region:
|
|
raise ValidationError(_('wine_region field must not be specified.'))
|
|
if (self.wine_region and self.wine_appellation) and \
|
|
self.wine_appellation not in self.wine_region.appellations.all():
|
|
raise ValidationError(_('Wine appellation not exists in wine region.'))
|
|
|
|
|
|
class OnlineProductManager(ProductManager):
|
|
"""Extended manger for OnlineProduct model."""
|
|
|
|
def get_queryset(self):
|
|
"""Overridden get_queryset method."""
|
|
return super().get_queryset().online()
|
|
|
|
|
|
class OnlineProduct(Product):
|
|
"""Online product."""
|
|
|
|
objects = OnlineProductManager.from_queryset(ProductQuerySet)()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
|
|
proxy = True
|
|
verbose_name = _('Online product')
|
|
verbose_name_plural = _('Online products')
|
|
|
|
|
|
class Unit(models.Model):
|
|
"""Product unit model."""
|
|
name = models.CharField(max_length=255,
|
|
verbose_name=_('name'))
|
|
value = models.CharField(max_length=255,
|
|
verbose_name=_('value'))
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
verbose_name = _('unit')
|
|
verbose_name_plural = _('units')
|
|
|
|
def __str__(self):
|
|
"""Overridden dunder method."""
|
|
return self.name
|
|
|
|
|
|
class ProductStandardQuerySet(models.QuerySet):
|
|
"""Product standard queryset."""
|
|
|
|
|
|
class ProductStandard(models.Model):
|
|
"""Product standard model."""
|
|
|
|
APPELLATION = 0
|
|
WINEQUALITY = 1
|
|
YARDCLASSIFICATION = 2
|
|
|
|
STANDARDS = (
|
|
(APPELLATION, _('Appellation')),
|
|
(WINEQUALITY, _('Wine quality')),
|
|
(YARDCLASSIFICATION, _('Yard classification')),
|
|
)
|
|
|
|
name = models.CharField(_('name'), max_length=255)
|
|
standard_type = models.PositiveSmallIntegerField(choices=STANDARDS,
|
|
verbose_name=_('standard type'))
|
|
coordinates = gis_models.PointField(
|
|
_('Coordinates'), blank=True, null=True, default=None)
|
|
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
|
|
|
|
objects = ProductStandardQuerySet.as_manager()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
verbose_name_plural = _('wine standards')
|
|
verbose_name = _('wine standard')
|
|
|
|
|
|
class ProductClassificationType(models.Model):
|
|
"""Product classification type."""
|
|
|
|
name = models.CharField(max_length=255, unique=True,
|
|
verbose_name=_('classification type'))
|
|
product_type = models.ForeignKey(ProductType, on_delete=models.PROTECT,
|
|
null=True, default=None,
|
|
verbose_name=_('product type'))
|
|
product_sub_type = models.ForeignKey(ProductSubType, on_delete=models.PROTECT,
|
|
blank=True, null=True, default=None,
|
|
verbose_name=_('product subtype'),
|
|
help_text=_('Legacy attribute - possible_type (product type).'
|
|
'Product type in our case is product subtype.'))
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
verbose_name = _('wine classification type')
|
|
verbose_name_plural = _('wine classification types')
|
|
|
|
def __str__(self):
|
|
"""Override str dunder."""
|
|
return self.name
|
|
|
|
|
|
class ProductClassificationQuerySet(models.QuerySet):
|
|
"""Product classification QuerySet."""
|
|
|
|
|
|
class ProductClassification(models.Model):
|
|
"""Product classification model."""
|
|
|
|
classification_type = models.ForeignKey(ProductClassificationType, on_delete=models.PROTECT,
|
|
verbose_name=_('classification type'))
|
|
standard = models.ForeignKey(ProductStandard, on_delete=models.PROTECT,
|
|
null=True, blank=True, default=None,
|
|
verbose_name=_('standard'))
|
|
tags = models.ManyToManyField('tag.Tag', related_name='product_classifications',
|
|
verbose_name=_('Tag'))
|
|
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
|
|
|
|
objects = ProductClassificationQuerySet.as_manager()
|
|
|
|
class Meta:
|
|
"""Meta class."""
|
|
verbose_name = _('product classification')
|
|
verbose_name_plural = _('product classifications')
|