diff --git a/apps/account/management/commands/add_social.py b/apps/account/management/commands/add_social.py index ecf86d60..c9b056f5 100644 --- a/apps/account/management/commands/add_social.py +++ b/apps/account/management/commands/add_social.py @@ -1,6 +1,5 @@ from django.core.management.base import BaseCommand from django.db import connections -from django.db.models import Q from social_django.models import UserSocialAuth from establishment.management.commands.add_position import namedtuplefetchall from account.models import User diff --git a/apps/establishment/management/commands/update_employee.py b/apps/establishment/management/commands/update_employee.py index dfe8b5a0..41831f0d 100644 --- a/apps/establishment/management/commands/update_employee.py +++ b/apps/establishment/management/commands/update_employee.py @@ -2,8 +2,7 @@ from django.core.management.base import BaseCommand from django.db import connections from establishment.management.commands.add_position import namedtuplefetchall from establishment.models import Employee -from django.db.models import Q - +from tqdm import tqdm class Command(BaseCommand): help = 'Add employee from old db to new db.' @@ -28,10 +27,7 @@ class Command(BaseCommand): def handle(self, *args, **options): objects = [] - for e in self.employees_sql(): - empl = Employee.objects.filter(old_id=e.profile_id) - if empl.exists(): - empl.name = e.name - empl.save() + for e in tqdm(self.employees_sql()): + empl = Employee.objects.filter(old_id=e.profile_id).update(name=e.name) self.stdout.write(self.style.WARNING(f'Update employee name objects.')) diff --git a/apps/product/management/commands/add_product_tag.py b/apps/product/management/commands/add_product_tag.py new file mode 100644 index 00000000..5633230c --- /dev/null +++ b/apps/product/management/commands/add_product_tag.py @@ -0,0 +1,116 @@ +from django.core.management.base import BaseCommand +from django.db import connections +from establishment.management.commands.add_position import namedtuplefetchall +from tag.models import Tag, TagCategory +from product.models import Product +from tqdm import tqdm +from django.db.models.functions import Lower + + +class Command(BaseCommand): + help = '''Add add product tags networks from old db to new db. + Run after add_product!!!''' + + def category_sql(self): + with connections['legacy'].cursor() as cursor: + cursor.execute(''' + select + DISTINCT + trim(CONVERT(v.key_name USING utf8)) as category, + trim(CONVERT(v.value_type USING utf8)) as value_type + FROM product_metadata m + JOIN product_key_value_metadata v on v.id = m.product_key_value_metadatum_id + + ''') + return namedtuplefetchall(cursor) + + def add_category_tag(self): + objects = [] + for c in tqdm(self.category_sql(), desc='Add category tags'): + categories = TagCategory.objects.filter(index_name=c.category, + value_type=c.value_type + ) + if not categories.exists(): + objects.append( + TagCategory(label={"en-GB": c.category}, + value_type=c.value_type, + index_name=c.category + ) + ) + TagCategory.objects.bulk_create(objects) + self.stdout.write(self.style.WARNING(f'Add or get tag category objects.')) + + def tag_sql(self): + with connections['legacy'].cursor() as cursor: + cursor.execute(''' + select + DISTINCT + trim(CONVERT(m.value USING utf8)) as tag_value, + trim(CONVERT(v.key_name USING utf8)) as tag_category + FROM product_metadata m + join product_key_value_metadata v on v.id = m.product_key_value_metadatum_id + ''') + return namedtuplefetchall(cursor) + + def add_tag(self): + objects = [] + for t in tqdm(self.tag_sql(), desc='Add tags'): + category = TagCategory.objects.get(index_name=t.tag_category) + + tag = Tag.objects.filter( + category=category, + value=t.tag_value + ) + + if not tag.exists(): + objects.append(Tag(label={"en-GB": t.tag_value}, + category=category, + value=t.tag_value + )) + Tag.objects.bulk_create(objects) + self.stdout.write(self.style.WARNING(f'Add or get tag objects.')) + + def product_sql(self): + with connections['legacy'].cursor() as cursor: + cursor.execute(''' + select + DISTINCT + m.product_id, + lower(trim(CONVERT(m.value USING utf8))) as tag_value, + trim(CONVERT(v.key_name USING utf8)) as tag_category + FROM product_metadata m + JOIN product_key_value_metadata v on v.id = m.product_key_value_metadatum_id + ''') + return namedtuplefetchall(cursor) + + def add_product_tag(self): + objects = [] + for t in tqdm(self.product_sql(), desc='Add product tag'): + category = TagCategory.objects.get(index_name=t.tag_category) + tag = Tag.objects.annotate(lower_value=Lower('value')) + tag.filter(lower_value=t.tag_value, category=category) + products = Product.objects.filter(old_id=t.product_id) + if products.exists(): + products.tags.add(tag) + products.save() + + self.stdout.write(self.style.WARNING(f'Add or get tag objects.')) + + def check_tag(self): + tags = Tag.objects.filter(value__contains='_').all() + for tag in tqdm(tags, desc='Check label for tag'): + new_label = {} + for k, v in tag.label.items(): + if isinstance(v, str) and '_' in v: + sp = v.split('_') + v = ' '.join([sp[0].capitalize()] + sp[1:]) + new_label[k] = v + tag.label = new_label + tag.save() + + + def handle(self, *args, **kwargs): + self.add_category_tag() + self.add_tag() + self.add_product_tag() + self.check_tag() \ No newline at end of file diff --git a/apps/tag/migrations/0011_auto_20191112_1203.py b/apps/tag/migrations/0011_auto_20191112_1203.py new file mode 100644 index 00000000..498eccda --- /dev/null +++ b/apps/tag/migrations/0011_auto_20191112_1203.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.7 on 2019-11-12 12:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tag', '0010_auto_20191112_0104'), + ] + + operations = [ + migrations.AddField( + model_name='tagcategory', + name='old_id', + field=models.IntegerField(blank=True, null=True), + ), + migrations.AlterField( + model_name='tagcategory', + name='value_type', + field=models.CharField(choices=[('string', 'string'), ('list', 'list'), ('integer', 'integer'), ('float', 'float')], default='list', max_length=255, verbose_name='value type'), + ), + ] diff --git a/apps/tag/models.py b/apps/tag/models.py index 45200290..e5e1fdbe 100644 --- a/apps/tag/models.py +++ b/apps/tag/models.py @@ -36,6 +36,7 @@ class Tag(TranslatedFieldsMixin, models.Model): verbose_name=_('Category')) chosen_tag_settings = models.ManyToManyField(Country, through='ChosenTagSettings') priority = models.PositiveIntegerField(null=True, default=0) + old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None) objects = TagQuerySet.as_manager() @@ -107,12 +108,14 @@ class TagCategory(TranslatedFieldsMixin, models.Model): STRING = 'string' LIST = 'list' INTEGER = 'integer' + FLOAT = 'float' PERCENTAGE = 'percentage' VALUE_TYPE_CHOICES = ( (STRING, _('string')), (LIST, _('list')), (INTEGER, _('integer')), + (FLOAT, _('float')), (PERCENTAGE, _('percentage')), ) @@ -128,6 +131,7 @@ class TagCategory(TranslatedFieldsMixin, models.Model): value_type = models.CharField(_('value type'), max_length=255, choices=VALUE_TYPE_CHOICES, default=LIST, ) + old_id = models.IntegerField(blank=True, null=True) objects = TagCategoryQuerySet.as_manager() diff --git a/project/settings/local.py b/project/settings/local.py index 62298faa..6745d272 100644 --- a/project/settings/local.py +++ b/project/settings/local.py @@ -36,7 +36,7 @@ THUMBNAIL_DEBUG = True # ADDED TRANSFER APP -INSTALLED_APPS.append('transfer.apps.TransferConfig') +# INSTALLED_APPS.append('transfer.apps.TransferConfig') # DATABASES diff --git a/requirements/base.txt b/requirements/base.txt index a1408fc5..d7050a29 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -21,6 +21,9 @@ pycountry==19.8.18 django-phonenumber-field[phonenumbers]==2.1.0 django-timezone-field==3.1 +# custom +tqdm==4.38.0 + # auth socials django-rest-framework-social-oauth2==1.1.0