Add category tag for products

This commit is contained in:
Виктор Гладких 2019-11-12 15:01:12 +03:00
parent 9d208610df
commit 7d9b6d1f30
4 changed files with 43 additions and 1 deletions

View File

@ -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

View File

@ -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()

View File

@ -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,

View File

@ -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