added WineAppellation model

This commit is contained in:
Anatoly 2019-10-28 12:33:48 +03:00
parent e41b539ed7
commit d12df5c8e1
5 changed files with 35 additions and 19 deletions

View File

@ -19,9 +19,15 @@ class CityAdmin(admin.ModelAdmin):
"""City admin.""" """City admin."""
class WineAppellationInline(admin.TabularInline):
model = models.WineAppellation
extra = 0
@admin.register(models.WineRegion) @admin.register(models.WineRegion)
class WineRegionAdmin(admin.ModelAdmin): class WineRegionAdmin(admin.ModelAdmin):
"""WineRegion admin.""" """WineRegion admin."""
inlines = [WineAppellationInline, ]
@admin.register(models.WineAppellation) @admin.register(models.WineAppellation)

View File

@ -167,7 +167,6 @@ class WineRegionBaseSerializer(serializers.ModelSerializer):
"""Wine region serializer.""" """Wine region serializer."""
name_translated = TranslatedField() name_translated = TranslatedField()
country = CountrySerializer() country = CountrySerializer()
appellations = WineAppellationBaseSerializer(many=True)
class Meta: class Meta:
"""Meta class.""" """Meta class."""
@ -176,5 +175,4 @@ class WineRegionBaseSerializer(serializers.ModelSerializer):
'id', 'id',
'name_translated', 'name_translated',
'country', 'country',
'appellations',
] ]

View File

@ -79,8 +79,8 @@ class ProductQuerySet(models.QuerySet):
"""Product queryset.""" """Product queryset."""
def with_base_related(self): def with_base_related(self):
return self.select_related('country', 'product_type', 'establishment') \ return self.select_related('product_type', 'establishment') \
.prefetch_related('product_type__subtypes') .prefetch_related('product_type__subtypes', 'country')
def common(self): def common(self):
return self.filter(category=self.model.COMMON) return self.filter(category=self.model.COMMON)
@ -120,7 +120,7 @@ class Product(TranslatedFieldsMixin, BaseAttributes):
description = TJSONField(_('Description'), null=True, blank=True, description = TJSONField(_('Description'), null=True, blank=True,
default=None, help_text='{"en-GB":"some text"}') default=None, help_text='{"en-GB":"some text"}')
characteristics = JSONField(_('Characteristics')) characteristics = JSONField(_('Characteristics'))
country = models.ForeignKey('location.Country', on_delete=models.PROTECT, country = models.ManyToManyField('location.Country',
verbose_name=_('Country')) verbose_name=_('Country'))
available = models.BooleanField(_('Available'), default=True) available = models.BooleanField(_('Available'), default=True)
product_type = models.ForeignKey(ProductType, on_delete=models.PROTECT, product_type = models.ForeignKey(ProductType, on_delete=models.PROTECT,
@ -138,6 +138,9 @@ class Product(TranslatedFieldsMixin, BaseAttributes):
related_name='wines', related_name='wines',
blank=True, null=True, blank=True, null=True,
verbose_name=_('wine region')) verbose_name=_('wine region'))
wine_appellation = models.ForeignKey('location.WineAppellation', on_delete=models.PROTECT,
blank=True, null=True,
verbose_name=_('wine appellation'))
objects = ProductManager.from_queryset(ProductQuerySet)() objects = ProductManager.from_queryset(ProductQuerySet)()
@ -153,6 +156,9 @@ class Product(TranslatedFieldsMixin, BaseAttributes):
raise ValidationError(_('wine_region field must be specified.')) raise ValidationError(_('wine_region field must be specified.'))
if not self.product_type.index_name == ProductType.WINE and self.wine_region: if not self.product_type.index_name == ProductType.WINE and self.wine_region:
raise ValidationError(_('wine_region field must not be specified.')) 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): class OnlineProductManager(ProductManager):

View File

@ -2,7 +2,8 @@
from rest_framework import serializers from rest_framework import serializers
from utils.serializers import TranslatedField from utils.serializers import TranslatedField
from product.models import Product, ProductSubType, ProductType from product.models import Product, ProductSubType, ProductType
from location.serializers import WineRegionBaseSerializer from location.serializers import (WineRegionBaseSerializer, WineAppellationBaseSerializer,
CountrySimpleSerializer)
class ProductSubTypeBaseSerializer(serializers.ModelSerializer): class ProductSubTypeBaseSerializer(serializers.ModelSerializer):
@ -41,6 +42,8 @@ class ProductBaseSerializer(serializers.ModelSerializer):
product_type = ProductTypeBaseSerializer() product_type = ProductTypeBaseSerializer()
subtypes = ProductSubTypeBaseSerializer(many=True) subtypes = ProductSubTypeBaseSerializer(many=True)
wine_region = WineRegionBaseSerializer(allow_null=True) wine_region = WineRegionBaseSerializer(allow_null=True)
wine_appellation = WineAppellationBaseSerializer(allow_null=True)
available_countries = CountrySimpleSerializer(source='country', many=True)
class Meta: class Meta:
"""Meta class.""" """Meta class."""
@ -55,4 +58,6 @@ class ProductBaseSerializer(serializers.ModelSerializer):
'subtypes', 'subtypes',
'public_mark', 'public_mark',
'wine_region', 'wine_region',
'wine_appellation',
'available_countries',
] ]

View File

@ -44,6 +44,7 @@ class TJSONField(JSONField):
def to_locale(language): def to_locale(language):
"""Turn a language name (en-us) into a locale name (en_US).""" """Turn a language name (en-us) into a locale name (en_US)."""
if language:
language, _, country = language.lower().partition('-') language, _, country = language.lower().partition('-')
if not country: if not country:
return language return language