diff --git a/apps/product/migrations/0012_auto_20191112_0937.py b/apps/product/migrations/0012_auto_20191112_0937.py new file mode 100644 index 00000000..9df6a9bc --- /dev/null +++ b/apps/product/migrations/0012_auto_20191112_0937.py @@ -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', + }, + ), + ] diff --git a/apps/product/models.py b/apps/product/models.py index 6c037219..79cf5ded 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -193,9 +193,6 @@ 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') @@ -409,3 +406,26 @@ class ProductClassification(models.Model): """Meta class.""" verbose_name = _('product classification') 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') diff --git a/apps/product/transfer_data.py b/apps/product/transfer_data.py index 24d5c236..77e7cd89 100644 --- a/apps/product/transfer_data.py +++ b/apps/product/transfer_data.py @@ -113,18 +113,17 @@ 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_product_note(): + queryset = transfer_models.ProductNotes.objects.exclude(text='') + serialized_data = product_serializers.ProductNoteSerializer( + 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_note errors: {errors}") def transfer_plate(): @@ -166,9 +165,9 @@ data_types = { "product": [ transfer_product, ], - # "product_description": [ - # transfer_product_description, - # ], + "product_note": [ + transfer_product_note, + ], "souvenir": [ transfer_plate, transfer_plate_image, diff --git a/apps/transfer/management/commands/transfer.py b/apps/transfer/management/commands/transfer.py index 58bf7807..d2cfaf41 100644 --- a/apps/transfer/management/commands/transfer.py +++ b/apps/transfer/management/commands/transfer.py @@ -32,7 +32,7 @@ class Command(BaseCommand): 'inquiries', # №6 - перенос запросов оценок 'wine_characteristics', 'product', - 'product_description', + 'product_note', 'souvenir', ] diff --git a/apps/transfer/models.py b/apps/transfer/models.py index 7f7dcc75..8b5ba081 100644 --- a/apps/transfer/models.py +++ b/apps/transfer/models.py @@ -973,7 +973,7 @@ class Products(MigrateMixin): class ProductNotes(MigrateMixin): 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) win_import_id = models.CharField(max_length=255) diff --git a/apps/transfer/serializers/product.py b/apps/transfer/serializers/product.py index a9dab39a..0d6e938b 100644 --- a/apps/transfer/serializers/product.py +++ b/apps/transfer/serializers/product.py @@ -541,3 +541,27 @@ class PlateImageSerializer(serializers.ModelSerializer): product_qs = models.Product.objects.filter(old_id=product_id) if product_qs.exists(): 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()