import yangilandi
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user