Files
consumocuidado-server/core/management/commands/addtaxonomy.py
2021-02-12 12:34:08 +00:00

35 lines
1.2 KiB
Python

import logging
from django.core.management.base import BaseCommand
from django.conf import settings
from core.models import TreeTag
from products.models import Product
class Command(BaseCommand):
help = 'Load taxonomy terms into Product.tags'
def handle(self, *args, **kwargs):
print(self.help)
print("Deleting existing instances")
TreeTag.objects.all().delete()
file_path = settings.BASE_DIR + '/../datasets/' + settings.TAXONOMY_FILE
counter = 0
with open(file_path, 'rt') as data_file:
print(f"Reading from {settings.TAXONOMY_FILE}")
for line in data_file.readlines():
try:
# tag = TreeTag.objects.create(name=line)
tag = Product.tags.tag_model.objects.create(name=line)
counter += 1
print('.', end='')
logging.debug(f"{tag} created from {line}")
except Exception as e:
logging.error(f"{type(e)} while creating tags from {settings.TAXONOMY_FILE}")
print(f"\nAdded {counter} Tag objects to Product.tags")
print('Shutting down\n')