From 1ee818e6259f4cdfc9e8d64582310e6ae585c181 Mon Sep 17 00:00:00 2001 From: Husanjonazamov Date: Fri, 27 Mar 2026 20:32:39 +0500 Subject: [PATCH] import yangilandi --- .../api/management/commands/import_assets.py | 74 ++++++++----------- .../migrations/0004_subcategorymodel_image.py | 18 +++++ core/apps/api/models/category.py | 3 +- .../api/serializers/category/subcategory.py | 3 + 4 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 core/apps/api/migrations/0004_subcategorymodel_image.py diff --git a/core/apps/api/management/commands/import_assets.py b/core/apps/api/management/commands/import_assets.py index d3f3cbb..8376730 100644 --- a/core/apps/api/management/commands/import_assets.py +++ b/core/apps/api/management/commands/import_assets.py @@ -19,7 +19,6 @@ class Command(BaseCommand): # 0. Clear Database self.stdout.write(self.style.WARNING("Clearing existing data...")) FilialModel.objects.all().delete() - # Cascading deletes should clear Categories, Subcategories, Products, and SubProducts product_map = {} # Maps (filial_name, item_id) to ProductsModel instance @@ -28,25 +27,38 @@ class Command(BaseCommand): for file_path in food_container_files: file_name = file_path.name - # Extract Filial name: FoodContainerKaromat20.json -> Karomat - match = re.search(r'FoodContainer([A-Za-z]+)(\d+)?\.json', file_name) + # Extract Filial name and Type ID: FoodContainerKaromat15.json -> Karomat, 15 + match = re.search(r'FoodContainer([A-Za-z]+)(\d+)\.json', file_name) if not match: self.stdout.write(self.style.WARNING(f"Skipping {file_name}, regex mismatch")) continue - filial_name = match.group(1) - if not filial_name: - filial_name = "Default" - - self.stdout.write(self.style.SUCCESS(f"Processing filial: {filial_name} from {file_name}")) + filial_name = match.group(1) or "Default" + type_id = match.group(2) + + # Determine Category by Filename ID (15=Restaurant, 20=Bar) + if type_id == '15': + cat_name = "Restaurant" + cat_type = CategoryModel.CategoryType.RESTAURANT + elif type_id == '20': + cat_name = "Bar" + cat_type = CategoryModel.CategoryType.BAR + else: + cat_name = f"Category {type_id}" + cat_type = CategoryModel.CategoryType.RESTAURANT + + self.stdout.write(self.style.SUCCESS(f"Processing Filial: {filial_name}, Category: {cat_name} from {file_name}")) - # Filial can be shared across multiple JSON files filial, _ = FilialModel.objects.get_or_create(name=filial_name) + category, _ = CategoryModel.objects.get_or_create( + filial=filial, + name=cat_name, + defaults={'type': cat_type} + ) with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) - current_category = None current_subcategory = None with transaction.atomic(): @@ -56,49 +68,26 @@ class Command(BaseCommand): price = item.get('price', 0) image_name = item.get('image') json_id = item.get('id') - item_type = item.get('type', '') if is_header: - # Determine if this category is Bar or Restaurant - # Usually anything with "BAR" in type string is Bar - cat_type = CategoryModel.CategoryType.RESTAURANT - if "BAR" in item_type.upper(): - cat_type = CategoryModel.CategoryType.BAR - - # Create Category - current_category, created = CategoryModel.objects.get_or_create( - filial=filial, - name=name, - defaults={'type': cat_type} - ) - if created and image_name: - self.attach_image(current_category, image_name, images_dir) - elif not created and current_category.type != cat_type: - # Update type if it was created differently before - current_category.type = cat_type - current_category.save() - - # Link Subcategory (using the same name for now as the JSON doesn't define a sub-level) + # Headers are Subcategories current_subcategory, created = SubcategoryModel.objects.get_or_create( - category=current_category, + category=category, name=name ) - self.stdout.write(f" Category({cat_type}): {name}") + if created and image_name: + self.attach_image(current_subcategory, image_name, images_dir) + self.stdout.write(f" Subcategory: {name}") else: + # Items are Products if not current_subcategory: # Fallback if no header found yet - cat_name = "General" - current_category, _ = CategoryModel.objects.get_or_create( - filial=filial, - name=cat_name - ) + fb_name = "General" current_subcategory, _ = SubcategoryModel.objects.get_or_create( - category=current_category, - name=cat_name + category=category, + name=fb_name ) - # Create Product - # We use get_or_create to avoid duplicates within the same subcategory product, created = ProductsModel.objects.get_or_create( name=name, subcategory=current_subcategory, @@ -115,6 +104,7 @@ class Command(BaseCommand): food_detail_files = sorted(json_dir.glob('FoodDetailContainer*.json')) for file_path in food_detail_files: file_name = file_path.name + # Handle detail containers: FoodDetailContainerKaromat15.json -> Karomat match = re.search(r'FoodDetailContainer(.*?)(?:\d+)?\.json', file_name) if not match: continue diff --git a/core/apps/api/migrations/0004_subcategorymodel_image.py b/core/apps/api/migrations/0004_subcategorymodel_image.py new file mode 100644 index 0000000..e38c5c3 --- /dev/null +++ b/core/apps/api/migrations/0004_subcategorymodel_image.py @@ -0,0 +1,18 @@ +# Generated by Django 5.2.7 on 2026-03-27 15:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0003_categorymodel_type'), + ] + + operations = [ + migrations.AddField( + model_name='subcategorymodel', + name='image', + field=models.ImageField(blank=True, null=True, upload_to='subcategories/', verbose_name='image'), + ), + ] diff --git a/core/apps/api/models/category.py b/core/apps/api/models/category.py index 27b6437..7a2cace 100644 --- a/core/apps/api/models/category.py +++ b/core/apps/api/models/category.py @@ -63,9 +63,10 @@ class SubcategoryModel(AbstractBaseModel): on_delete=models.CASCADE, ) name = models.CharField(verbose_name=_("name"), max_length=255) + image = models.ImageField(verbose_name=_("image"), upload_to="subcategories/", null=True, blank=True) def __str__(self): - return self.name + return f"{self.category.name} -> {self.name}" @classmethod def _baker(cls): diff --git a/core/apps/api/serializers/category/subcategory.py b/core/apps/api/serializers/category/subcategory.py index 1802491..5fa9aa4 100644 --- a/core/apps/api/serializers/category/subcategory.py +++ b/core/apps/api/serializers/category/subcategory.py @@ -13,6 +13,7 @@ class BaseSubcategorySerializer(serializers.ModelSerializer): "id", "category", "name", + "image", "products", ] @@ -23,6 +24,7 @@ class ListSubcategorySerializer(BaseSubcategorySerializer): "id", "category", "name", + "image", ] @@ -36,4 +38,5 @@ class CreateSubcategorySerializer(BaseSubcategorySerializer): "id", "category", "name", + "image", ]