added command fix_sugar_content_tag in products
This commit is contained in:
parent
0b157244e0
commit
d07fd9282e
21
apps/product/management/commands/fix_sugar_content_tag.py
Normal file
21
apps/product/management/commands/fix_sugar_content_tag.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
|
||||
from transfer import models as transfer_models
|
||||
from transfer.serializers.product import ProductTagSerializer
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Fix sugar content tag'
|
||||
|
||||
def handle(self, *args, **kwarg):
|
||||
errors = []
|
||||
legacy_products = transfer_models.Products.objects.filter(wine_color__isnull=False)
|
||||
serialized_data = ProductTagSerializer(
|
||||
data=list(legacy_products.values()),
|
||||
many=True)
|
||||
if serialized_data.is_valid():
|
||||
serialized_data.save()
|
||||
else:
|
||||
for d in serialized_data.errors: errors.append(d) if d else None
|
||||
|
||||
self.stdout.write(self.style.WARNING(f'Error count: {len(errors)}\nErrors: {errors}'))
|
||||
18
apps/product/migrations/0011_product_win_import_id.py
Normal file
18
apps/product/migrations/0011_product_win_import_id.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.7 on 2019-11-12 08:03
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('product', '0010_auto_20191111_1227'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='win_import_id',
|
||||
field=models.CharField(blank=True, default=None, help_text='attribute from legacy db', max_length=255, null=True),
|
||||
),
|
||||
]
|
||||
|
|
@ -193,6 +193,9 @@ class Product(TranslatedFieldsMixin, BaseAttributes):
|
|||
validators=[MinValueValidator(EARLIEST_VINTAGE_YEAR),
|
||||
MaxValueValidator(LATEST_VINTAGE_YEAR)])
|
||||
gallery = models.ManyToManyField('gallery.Image', through='ProductGallery')
|
||||
win_import_id = models.CharField(max_length=255,
|
||||
blank=True, null=True, default=None,
|
||||
help_text=_('attribute from legacy db'))
|
||||
reviews = generic.GenericRelation(to='review.Review')
|
||||
comments = generic.GenericRelation(to='comment.Comment')
|
||||
awards = generic.GenericRelation(to='main.Award', related_query_name='product')
|
||||
|
|
|
|||
|
|
@ -113,6 +113,20 @@ def transfer_product():
|
|||
pprint(f"transfer_product errors: {errors}")
|
||||
|
||||
|
||||
def transfer_product_description():
|
||||
pass
|
||||
# queryset = transfer_models.Products.objects.all()
|
||||
# serialized_data = product_serializers.ProductSerializer(
|
||||
# data=list(queryset.values()),
|
||||
# many=True)
|
||||
# if serialized_data.is_valid():
|
||||
# serialized_data.save()
|
||||
# else:
|
||||
# errors = []
|
||||
# for d in serialized_data.errors: errors.append(d) if d else None
|
||||
# pprint(f"transfer_product errors: {errors}")
|
||||
|
||||
|
||||
def transfer_plate():
|
||||
queryset = transfer_models.Merchandise.objects.all()
|
||||
serialized_data = product_serializers.PlateSerializer(
|
||||
|
|
@ -151,7 +165,12 @@ data_types = {
|
|||
],
|
||||
"product": [
|
||||
transfer_product,
|
||||
# transfer_plate,
|
||||
# transfer_plate_image,
|
||||
],
|
||||
# "product_description": [
|
||||
# transfer_product_description,
|
||||
# ],
|
||||
"souvenir": [
|
||||
transfer_plate,
|
||||
transfer_plate_image,
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,4 +93,13 @@ class TransferSerializerMixin(serializers.ModelSerializer):
|
|||
return qs.first()
|
||||
|
||||
def get_slug(self, name, old_id):
|
||||
return slugify(f'{name}-{old_id}')
|
||||
return slugify(f'{name}-{old_id}')
|
||||
|
||||
def get_tag(self, tag, category_index_name: str):
|
||||
tag = tag.name if hasattr(tag, 'name') else tag
|
||||
if tag:
|
||||
qs = tag_models.Tag.objects.filter(
|
||||
value=slugify(tag),
|
||||
category__index_name=category_index_name)
|
||||
if qs.exists():
|
||||
return qs.first()
|
||||
|
|
|
|||
|
|
@ -970,6 +970,18 @@ class Products(MigrateMixin):
|
|||
db_table = 'products'
|
||||
|
||||
|
||||
class ProductNotes(MigrateMixin):
|
||||
using = 'legacy'
|
||||
|
||||
product_id = models.ForeignKey(Products, on_delete=models.DO_NOTHING)
|
||||
text = models.CharField(max_length=255)
|
||||
win_import_id = models.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = 'product_notes'
|
||||
|
||||
|
||||
class HomePages(models.Model):
|
||||
using = 'legacy'
|
||||
|
||||
|
|
|
|||
|
|
@ -407,15 +407,6 @@ class ProductSerializer(TransferSerializerMixin):
|
|||
if village_qs.exists():
|
||||
return village_qs.first()
|
||||
|
||||
def get_tag(self, tag, category_index_name: str):
|
||||
tag = tag.name if hasattr(tag, 'name') else tag
|
||||
if tag:
|
||||
qs = tag_models.Tag.objects.filter(
|
||||
value=tag,
|
||||
category__index_name=category_index_name)
|
||||
if qs.exists():
|
||||
return qs.first()
|
||||
|
||||
def get_wine_standard(self, standard, standard_type):
|
||||
if standard:
|
||||
if isinstance(standard, transfer_models.ProductClassification):
|
||||
|
|
@ -432,6 +423,41 @@ class ProductSerializer(TransferSerializerMixin):
|
|||
return False
|
||||
|
||||
|
||||
class ProductTagSerializer(TransferSerializerMixin):
|
||||
"""Serializer for fixing existing products with missing tags."""
|
||||
|
||||
id = serializers.IntegerField()
|
||||
wine_color_id = serializers.PrimaryKeyRelatedField(
|
||||
queryset=transfer_models.WineColor.objects.all(),
|
||||
allow_null=True)
|
||||
|
||||
class Meta:
|
||||
model = models.Product
|
||||
fields = (
|
||||
'id',
|
||||
'wine_color_id',
|
||||
)
|
||||
|
||||
def validate(self, attrs):
|
||||
attrs['old_id'] = attrs.pop('id')
|
||||
attrs['wine_color_tag'] = self.get_tag(attrs.pop('wine_color_id', None),
|
||||
WineColorSerializer.CATEGORY_INDEX_NAME)
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
qs = self.Meta.model.objects.filter(old_id=validated_data.get('old_id'))
|
||||
|
||||
# tags
|
||||
wine_color_tag = validated_data.pop('wine_color_tag', None)
|
||||
|
||||
if qs.exists():
|
||||
obj = qs.first()
|
||||
|
||||
# adding missing tag
|
||||
obj.tags.add(wine_color_tag)
|
||||
return obj
|
||||
|
||||
|
||||
class PlateSerializer(TransferSerializerMixin):
|
||||
|
||||
PRODUCT_TYPE_INDEX_NAME = 'souvenir'
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user