added transfer for product notes

This commit is contained in:
Anatoly 2019-11-12 12:41:27 +03:00
parent 22521f76fc
commit 7064790606
6 changed files with 99 additions and 20 deletions

View File

@ -0,0 +1,36 @@
# Generated by Django 2.2.7 on 2019-11-12 09:37
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('product', '0011_product_win_import_id'),
]
operations = [
migrations.RemoveField(
model_name='product',
name='win_import_id',
),
migrations.CreateModel(
name='ProductNote',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(default=django.utils.timezone.now, editable=False, verbose_name='Date created')),
('modified', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
('text', models.TextField(verbose_name='text')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='notes', to='product.Product', verbose_name='product')),
('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='notes', to=settings.AUTH_USER_MODEL, verbose_name='author')),
],
options={
'verbose_name': 'product notes',
'verbose_name_plural': 'product note',
},
),
]

View File

@ -193,9 +193,6 @@ class Product(TranslatedFieldsMixin, BaseAttributes):
validators=[MinValueValidator(EARLIEST_VINTAGE_YEAR), validators=[MinValueValidator(EARLIEST_VINTAGE_YEAR),
MaxValueValidator(LATEST_VINTAGE_YEAR)]) MaxValueValidator(LATEST_VINTAGE_YEAR)])
gallery = models.ManyToManyField('gallery.Image', through='ProductGallery') 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') reviews = generic.GenericRelation(to='review.Review')
comments = generic.GenericRelation(to='comment.Comment') comments = generic.GenericRelation(to='comment.Comment')
awards = generic.GenericRelation(to='main.Award', related_query_name='product') awards = generic.GenericRelation(to='main.Award', related_query_name='product')
@ -409,3 +406,26 @@ class ProductClassification(models.Model):
"""Meta class.""" """Meta class."""
verbose_name = _('product classification') verbose_name = _('product classification')
verbose_name_plural = _('product classifications') verbose_name_plural = _('product classifications')
class ProductNoteQuerySet(models.QuerySet):
"""QuerySet for model ProductNote."""
class ProductNote(ProjectBaseMixin):
"""Note model for Product entity."""
text = models.TextField(verbose_name=_('text'))
product = models.ForeignKey(Product, on_delete=models.PROTECT,
related_name='notes',
verbose_name=_('product'))
user = models.ForeignKey('account.User', on_delete=models.PROTECT,
null=True,
related_name='notes',
verbose_name=_('author'))
objects = ProductNoteQuerySet.as_manager()
class Meta:
"""Meta class."""
verbose_name_plural = _('product note')
verbose_name = _('product notes')

View File

@ -113,18 +113,17 @@ def transfer_product():
pprint(f"transfer_product errors: {errors}") pprint(f"transfer_product errors: {errors}")
def transfer_product_description(): def transfer_product_note():
pass queryset = transfer_models.ProductNotes.objects.exclude(text='')
# queryset = transfer_models.Products.objects.all() serialized_data = product_serializers.ProductNoteSerializer(
# serialized_data = product_serializers.ProductSerializer( data=list(queryset.values()),
# data=list(queryset.values()), many=True)
# many=True) if serialized_data.is_valid():
# if serialized_data.is_valid(): serialized_data.save()
# serialized_data.save() else:
# else: errors = []
# errors = [] for d in serialized_data.errors: errors.append(d) if d else None
# for d in serialized_data.errors: errors.append(d) if d else None pprint(f"transfer_product_note errors: {errors}")
# pprint(f"transfer_product errors: {errors}")
def transfer_plate(): def transfer_plate():
@ -166,9 +165,9 @@ data_types = {
"product": [ "product": [
transfer_product, transfer_product,
], ],
# "product_description": [ "product_note": [
# transfer_product_description, transfer_product_note,
# ], ],
"souvenir": [ "souvenir": [
transfer_plate, transfer_plate,
transfer_plate_image, transfer_plate_image,

View File

@ -32,7 +32,7 @@ class Command(BaseCommand):
'inquiries', # №6 - перенос запросов оценок 'inquiries', # №6 - перенос запросов оценок
'wine_characteristics', 'wine_characteristics',
'product', 'product',
'product_description', 'product_note',
'souvenir', 'souvenir',
] ]

View File

@ -973,7 +973,7 @@ class Products(MigrateMixin):
class ProductNotes(MigrateMixin): class ProductNotes(MigrateMixin):
using = 'legacy' using = 'legacy'
product_id = models.ForeignKey(Products, on_delete=models.DO_NOTHING) product = models.ForeignKey(Products, on_delete=models.DO_NOTHING)
text = models.CharField(max_length=255) text = models.CharField(max_length=255)
win_import_id = models.CharField(max_length=255) win_import_id = models.CharField(max_length=255)

View File

@ -541,3 +541,27 @@ class PlateImageSerializer(serializers.ModelSerializer):
product_qs = models.Product.objects.filter(old_id=product_id) product_qs = models.Product.objects.filter(old_id=product_id)
if product_qs.exists(): if product_qs.exists():
return product_qs.first() return product_qs.first()
class ProductNoteSerializer(TransferSerializerMixin):
product_id = serializers.PrimaryKeyRelatedField(
queryset=transfer_models.Products.objects.all())
text = serializers.CharField(allow_blank=True)
class Meta:
model = models.ProductNote
fields = (
'product_id',
'text',
)
def validate(self, attrs):
attrs['product'] = self.get_product(attrs.pop('product_id'))
return attrs
def get_product(self, product):
if product:
old_id = product.id if hasattr(product, 'id') else product
qs = models.Product.objects.filter(old_id=old_id)
if qs.exists():
return qs.first()