added field - average price to model Product and transfer command
This commit is contained in:
parent
b6292261e4
commit
484a9a3250
|
|
@ -181,6 +181,7 @@ class EstablishmentShortSerializer(serializers.ModelSerializer):
|
|||
city = CitySerializer(source='address.city', allow_null=True)
|
||||
establishment_type = EstablishmentTypeGeoSerializer()
|
||||
establishment_subtypes = EstablishmentSubTypeBaseSerializer(many=True)
|
||||
currency = CurrencySerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
|
@ -193,6 +194,7 @@ class EstablishmentShortSerializer(serializers.ModelSerializer):
|
|||
'city',
|
||||
'establishment_type',
|
||||
'establishment_subtypes',
|
||||
'currency',
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -202,6 +204,7 @@ class EstablishmentProductShortSerializer(serializers.ModelSerializer):
|
|||
establishment_subtypes = EstablishmentSubTypeBaseSerializer(many=True)
|
||||
address = AddressBaseSerializer()
|
||||
city = CityShortSerializer(source='address.city', allow_null=True)
|
||||
currency_detail = CurrencySerializer(source='currency', read_only=True)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
|
@ -215,6 +218,7 @@ class EstablishmentProductShortSerializer(serializers.ModelSerializer):
|
|||
'establishment_type',
|
||||
'establishment_subtypes',
|
||||
'address',
|
||||
'currency_detail',
|
||||
]
|
||||
|
||||
|
||||
|
|
|
|||
19
apps/product/management/commands/add_average_price.py
Normal file
19
apps/product/management/commands/add_average_price.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
|
||||
from product.models import Product
|
||||
from transfer.models import Products
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = """Add average price to product from legacy table products."""
|
||||
|
||||
def handle(self, *args, **kwarg):
|
||||
update_products = []
|
||||
old_products = Products.objects.values_list('id', 'price')
|
||||
for old_id, price in old_products:
|
||||
product = Product.objects.get(old_id=old_id)
|
||||
product.average_price = price
|
||||
update_products.append(product)
|
||||
|
||||
Product.objects.bulk_update(update_products, ['average_price', ])
|
||||
self.stdout.write(self.style.WARNING(f'Updated products: {len(update_products)}'))
|
||||
18
apps/product/migrations/0016_product_average_price.py
Normal file
18
apps/product/migrations/0016_product_average_price.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.7 on 2019-11-19 13:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('product', '0015_auto_20191117_1954'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='average_price',
|
||||
field=models.DecimalField(blank=True, decimal_places=2, default=None, max_digits=14, null=True, verbose_name='average price'),
|
||||
),
|
||||
]
|
||||
|
|
@ -211,6 +211,9 @@ class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes, HasTagsM
|
|||
null=True, blank=True, default=None,
|
||||
validators=[MinValueValidator(EARLIEST_VINTAGE_YEAR),
|
||||
MaxValueValidator(LATEST_VINTAGE_YEAR)])
|
||||
average_price = models.DecimalField(max_digits=14, decimal_places=2,
|
||||
blank=True, null=True, default=None,
|
||||
verbose_name=_('average price'))
|
||||
gallery = models.ManyToManyField('gallery.Image', through='ProductGallery')
|
||||
reviews = generic.GenericRelation(to='review.Review')
|
||||
comments = generic.GenericRelation(to='comment.Comment')
|
||||
|
|
|
|||
|
|
@ -130,7 +130,6 @@ class ProductDetailSerializer(ProductBaseSerializer):
|
|||
grape_variety = TagBaseSerializer(many=True, read_only=True)
|
||||
image_url = serializers.URLField(allow_null=True,
|
||||
read_only=True)
|
||||
|
||||
new_image = ImageBaseSerializer(source='crop_main_image', allow_null=True, read_only=True)
|
||||
|
||||
class Meta(ProductBaseSerializer.Meta):
|
||||
|
|
@ -146,6 +145,7 @@ class ProductDetailSerializer(ProductBaseSerializer):
|
|||
'image_url',
|
||||
'new_image',
|
||||
'grape_variety',
|
||||
'average_price',
|
||||
]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -131,6 +131,7 @@ class ProductDocument(Document):
|
|||
'state',
|
||||
'old_unique_key',
|
||||
'vintage',
|
||||
'average_price',
|
||||
)
|
||||
related_models = [models.ProductType]
|
||||
|
||||
|
|
|
|||
|
|
@ -245,4 +245,5 @@ class ProductDocumentSerializer(DocumentSerializer):
|
|||
'wine_colors',
|
||||
'grape_variety',
|
||||
'establishment_detail',
|
||||
'average_price',
|
||||
)
|
||||
|
|
|
|||
|
|
@ -265,6 +265,7 @@ class ProductSerializer(TransferSerializerMixin):
|
|||
state = serializers.CharField()
|
||||
bottles_produced = serializers.CharField(allow_null=True, allow_blank=True)
|
||||
unique_key = serializers.CharField(allow_null=True)
|
||||
price = serializers.DecimalField(max_digits=14, decimal_places=2)
|
||||
|
||||
class Meta:
|
||||
model = models.Product
|
||||
|
|
@ -287,6 +288,7 @@ class ProductSerializer(TransferSerializerMixin):
|
|||
'state', # done
|
||||
'bottles_produced', # done
|
||||
'unique_key', # done
|
||||
'price',
|
||||
)
|
||||
|
||||
def validate(self, attrs):
|
||||
|
|
@ -308,6 +310,7 @@ class ProductSerializer(TransferSerializerMixin):
|
|||
old_id = attrs.pop('id')
|
||||
state = self.get_state(attrs.pop('state', None))
|
||||
|
||||
|
||||
attrs['old_id'] = old_id
|
||||
attrs['name'] = name
|
||||
attrs['old_unique_key'] = attrs.pop('unique_key')
|
||||
|
|
@ -332,6 +335,7 @@ class ProductSerializer(TransferSerializerMixin):
|
|||
attrs['wine_village'] = self.get_wine_village(village)
|
||||
attrs['available'] = self.get_availability(state)
|
||||
attrs['slug'] = self.get_slug(name, old_id)
|
||||
attrs['average_price'] = attrs.pop('price')
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user