individual models for each tagged field in Product

This commit is contained in:
Sam
2021-02-25 11:17:35 +00:00
parent 8ea0f74b8c
commit db55de0cf5
6 changed files with 41 additions and 24 deletions

View File

@@ -3,7 +3,7 @@ import logging
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.conf import settings from django.conf import settings
from core.models import TreeTag from products.models import CategoryTag
from products.models import Product from products.models import Product
@@ -15,7 +15,7 @@ class Command(BaseCommand):
print(self.help) print(self.help)
print("Deleting existing instances") print("Deleting existing instances")
TreeTag.objects.all().delete() CategoryTag.objects.all().delete()
file_path = settings.BASE_DIR + '/../datasets/' + settings.TAXONOMY_FILE file_path = settings.BASE_DIR + '/../datasets/' + settings.TAXONOMY_FILE
counter = 0 counter = 0
@@ -23,11 +23,11 @@ class Command(BaseCommand):
print(f"Reading from {settings.TAXONOMY_FILE}") print(f"Reading from {settings.TAXONOMY_FILE}")
for line in data_file.readlines(): for line in data_file.readlines():
try: try:
tag = Product.tags.tag_model.objects.create(name=line) tag = Product.category.tag_model.objects.create(name=line)
counter += 1 counter += 1
print('.', end='') print('.', end='')
logging.debug(f"{tag} created from {line}") logging.debug(f"{tag} created from {line}")
except Exception as e: except Exception as e:
logging.error(f"{type(e)} while creating tags from {settings.TAXONOMY_FILE}") logging.error(f"{type(e)} while creating tags from {settings.TAXONOMY_FILE}")
print(f"\nAdded {counter} Tag objects to Product.tags") print(f"\nAdded {counter} Tag objects to Product.category")
print('Shutting down\n') print('Shutting down\n')

View File

@@ -3,19 +3,19 @@ import logging
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.conf import settings from django.conf import settings
from core.models import TreeTag from products.models import CategoryTag
class Command(BaseCommand): class Command(BaseCommand):
help = 'Extract top level tags' help = 'Extract top level catefory tags'
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
# get all instances # get all instances
tags = TreeTag.objects.all() tags = CategoryTag.objects.all()
top_tags = [] top_tags = []
print("Extracting top-level tags from TreeTag instances") print("Extracting top-level tags from CategoryTag instances")
# extract tags with no ancestor # extract tags with no ancestor
for tag in tags: for tag in tags:
if not tag.get_ancestors(): if not tag.get_ancestors():

View File

@@ -73,10 +73,3 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
verbose_name = 'Usuario' verbose_name = 'Usuario'
verbose_name_plural = 'Usuarios' verbose_name_plural = 'Usuarios'
class TreeTag(TagTreeModel):
class TagMeta:
initial = ""
force_lowercase = True
max_count=20
# autocomplete_view = 'myapp.views.hobbies_autocomplete'

View File

@@ -2,11 +2,34 @@ from django.contrib.gis.db import models
from tagulous.models import SingleTagField, TagField, TagTreeModel from tagulous.models import SingleTagField, TagField, TagTreeModel
from core.models import TreeTag
from companies.models import Company from companies.models import Company
# Create your models here. # Create your models here.
class TreeTag(TagTreeModel):
class TagMeta:
initial = ""
force_lowercase = True
max_count=20
# autocomplete_view = 'myapp.views.hobbies_autocomplete'
class CategoryTag(TagTreeModel):
class TagMeta:
initial = ""
force_lowercase = True
max_count=20
# autocomplete_view = 'myapp.views.hobbies_autocomplete'
class AttributeTag(TagTreeModel):
class TagMeta:
initial = ""
force_lowercase = True
max_count=20
# autocomplete_view = 'myapp.views.hobbies_autocomplete'
class Product(models.Model): class Product(models.Model):
@@ -35,8 +58,8 @@ class Product(models.Model):
discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True) discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True)
stock = models.PositiveIntegerField('Stock', null=True, blank=True) stock = models.PositiveIntegerField('Stock', null=True, blank=True)
tags = TagField(to=TreeTag) tags = TagField(to=TreeTag)
category = SingleTagField(null=True, blank=True) # main tag category category = SingleTagField(to=CategoryTag, null=True, blank=True) # main tag category
attributes = TagField(to=TreeTag, related_name='product_attributes') attributes = TagField(to=AttributeTag, related_name='product_attributes')
identifiers = models.TextField('Identificador único de producto', null=True, blank=True) identifiers = models.TextField('Identificador único de producto', null=True, blank=True)
# internal # internal

View File

@@ -318,7 +318,7 @@ class ProductViewSetTest(APITestCase):
'discount': '0.05', 'discount': '0.05',
'stock': 22, 'stock': 22,
'tags': ['tag1x, tag2x'], 'tags': ['tag1x, tag2x'],
'category': 'MayorTagCategory2', 'category': 'mayortagcategory2',
'attributes': ['color/blue', 'size/m'], 'attributes': ['color/blue', 'size/m'],
'identifiers': '34rf34f43c43', 'identifiers': '34rf34f43c43',
} }

View File

@@ -89,7 +89,7 @@ def create_imported_product(info, company, history, user):
return new return new
else: else:
logging.error(f"{serializer.errors}") logging.error(f"{serializer.errors}")
return [] return None
def migrate_shop_products(url, key, secret, user=None, version="wc/v3"): def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
@@ -145,6 +145,7 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
counter = 0 counter = 0
for product in products: for product in products:
new = create_imported_product(product, company, history, user) new = create_imported_product(product, company, history, user)
if new is not None:
new_products.append(new) new_products.append(new)
counter += 1 counter += 1
logging.info(f"Product '{new.name}' created") logging.info(f"Product '{new.name}' created")