category type boyciah filter to'g'rilandi

This commit is contained in:
Husanjonazamov
2026-03-28 19:34:40 +05:00
parent 2648849764
commit 9a16c4ae17

View File

@@ -14,6 +14,10 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
base_dir = Path(settings.BASE_DIR) base_dir = Path(settings.BASE_DIR)
full_json_path = base_dir / 'assets' / 'menu_full.json' full_json_path = base_dir / 'assets' / 'menu_full.json'
# If not in assets, check root (user's active doc was in root)
if not full_json_path.exists():
full_json_path = base_dir / 'menu_full.json'
images_dir = base_dir / 'assets' / 'images' images_dir = base_dir / 'assets' / 'images'
if not full_json_path.exists(): if not full_json_path.exists():
@@ -38,13 +42,17 @@ class Command(BaseCommand):
if section_slug.lower() == 'bar': if section_slug.lower() == 'bar':
cat_type = CategoryModel.CategoryType.BAR cat_type = CategoryModel.CategoryType.BAR
# We need to distinguish sections in Category names if they share typeNames but are different entities
# However, the type field on Category should handle separation.
for group in groups: for group in groups:
type_name = group.get('typeName', 'General') type_name = group.get('typeName', 'General')
# USE ALL FIELDS IN TO BE UNIQUE: filial, name, type
category, _ = CategoryModel.objects.get_or_create( category, _ = CategoryModel.objects.get_or_create(
filial=filial, filial=filial,
name=type_name, name=type_name,
defaults={'type': cat_type} type=cat_type
) )
self.stdout.write(f" Category({cat_type}): {type_name}") self.stdout.write(f" Category({cat_type}): {type_name}")
@@ -64,6 +72,9 @@ class Command(BaseCommand):
price = prod_data.get('price', 0) price = prod_data.get('price', 0)
image_name = prod_data.get('image') image_name = prod_data.get('image')
# ID from JSON can help with uniqueness
json_id = prod_data.get('id')
product, _ = ProductsModel.objects.get_or_create( product, _ = ProductsModel.objects.get_or_create(
subcategory=subcategory, subcategory=subcategory,
name=prod_name, name=prod_name,
@@ -71,22 +82,22 @@ class Command(BaseCommand):
) )
if image_name: if image_name:
self.attach_image(product, image_name, images_dir) self.attach_image(product, image_name, images_dir)
# Attach first images if not set
if i == 0: if i == 0:
if not subcategory.image: if not subcategory.image:
self.attach_image(subcategory, image_name, images_dir) self.attach_image(subcategory, image_name, images_dir)
if not category.image: if not category.image:
self.attach_image(category, image_name, images_dir) self.attach_image(category, image_name, images_dir)
# Subproducts (Variants)
for var_data in prod_data.get('variants', []): for var_data in prod_data.get('variants', []):
var_name = var_data.get('name', 'Unnamed Variant') var_name = var_data.get('name', 'Unnamed Variant')
var_price = var_data.get('price', 0) var_price = var_data.get('price', 0)
if var_name and var_price:
SubProductModel.objects.get_or_create( SubProductModel.objects.get_or_create(
product=product, product=product,
name=var_name, name=var_name,
defaults={'price': var_price if var_price > 0 else 0} defaults={'price': var_price if var_price > 0 else 0}
) )
def attach_image(self, instance, image_name, images_dir): def attach_image(self, instance, image_name, images_dir):
if not image_name: if not image_name: