From 7d9b6d1f30ce2d694385447381a307a384dad2da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 12 Nov 2019 15:01:12 +0300 Subject: [PATCH] Add category tag for products --- .../account/management/commands/add_social.py | 1 - .../management/commands/add_product_tag.py | 38 +++++++++++++++++++ apps/tag/models.py | 2 + requirements/base.txt | 3 ++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 apps/product/management/commands/add_product_tag.py 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/product/management/commands/add_product_tag.py b/apps/product/management/commands/add_product_tag.py new file mode 100644 index 00000000..9e63d213 --- /dev/null +++ b/apps/product/management/commands/add_product_tag.py @@ -0,0 +1,38 @@ +from django.core.management.base import BaseCommand +from django.db import connections +from social_django.models import UserSocialAuth +from establishment.management.commands.add_position import namedtuplefetchall +from account.models import User +from tag.models import Tag, TagCategory +from tqdm import tqdm + +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 + id, + key_name as category, + value_type as value_type + from product_key_value_metadata + ''') + return namedtuplefetchall(cursor) + + def add_category_tag(self): + objects = [] + for c in tqdm(self.category_sql()): + cat = TagCategory.objects.filter(index_name=c.category, + value_type=c.value_type, + label={'en-GB':c.category}) + if not cat.exists(): + objects.append(TagCategory(index_name=c.category, + value_type=c.value_type, + label={'en-GB': c.category})) + TagCategory.objects.bulk_create(objects) + self.stdout.write(self.style.WARNING(f'Created tag category.')) + + def handle(self, *args, **kwargs): + self.add_category_tag() \ No newline at end of file diff --git a/apps/tag/models.py b/apps/tag/models.py index 5965b4fb..59e2339c 100644 --- a/apps/tag/models.py +++ b/apps/tag/models.py @@ -107,11 +107,13 @@ class TagCategory(TranslatedFieldsMixin, models.Model): STRING = 'string' LIST = 'list' INTEGER = 'integer' + FLOAT = 'float' VALUE_TYPE_CHOICES = ( (STRING, _('string')), (LIST, _('list')), (INTEGER, _('integer')), + (FLOAT, _('float')) ) label = TJSONField(blank=True, null=True, default=None, 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