feat(demo_data): enhance data generation with get_or_create
- Replaced `create` operations with `get_or_create` to ensure idempotency during data generation. - Avoided redundant user, product image, and post creation when duplicates exist. - Updated user and stock handling to leverage defaults for improved clarity. - Prevented overwriting existing blog post and product image content.
This commit is contained in:
parent
df0d503c13
commit
09610d98a2
1 changed files with 48 additions and 37 deletions
|
|
@ -257,8 +257,8 @@ class Command(BaseCommand):
|
|||
Attribute.objects.filter(group__name__in=group_names).delete()
|
||||
AttributeGroup.objects.filter(name__in=group_names).delete()
|
||||
|
||||
self.staff_user.delete()
|
||||
self.super_user.delete()
|
||||
User.objects.filter(email=f"staff@{DEMO_EMAIL_DOMAIN}").delete()
|
||||
User.objects.filter(email=f"super@{DEMO_EMAIL_DOMAIN}").delete()
|
||||
|
||||
self.stdout.write("")
|
||||
self.stdout.write(self.style.SUCCESS("=" * 50))
|
||||
|
|
@ -409,13 +409,15 @@ class Command(BaseCommand):
|
|||
product.description_ru_ru = prod_data["description_ru"] # ty: ignore[invalid-assignment]
|
||||
product.save()
|
||||
|
||||
Stock.objects.create(
|
||||
Stock.objects.get_or_create(
|
||||
vendor=vendor,
|
||||
product=product,
|
||||
sku=f"GS-{prod_data['partnumber']}",
|
||||
price=prod_data["price"],
|
||||
purchase_price=prod_data["purchase_price"],
|
||||
quantity=prod_data["quantity"],
|
||||
defaults={
|
||||
"sku": f"GS-{prod_data['partnumber']}",
|
||||
"price": prod_data["price"],
|
||||
"purchase_price": prod_data["purchase_price"],
|
||||
"quantity": prod_data["quantity"],
|
||||
},
|
||||
)
|
||||
|
||||
# Add product image
|
||||
|
|
@ -475,6 +477,9 @@ class Command(BaseCommand):
|
|||
def _save_product_image(
|
||||
self, product: Product, image_path: Path, priority: int
|
||||
) -> None:
|
||||
if product.images.filter(priority=priority).exists():
|
||||
return
|
||||
|
||||
with open(image_path, "rb") as f:
|
||||
image_content = f.read()
|
||||
|
||||
|
|
@ -513,14 +518,16 @@ class Command(BaseCommand):
|
|||
|
||||
existing_emails.add(email)
|
||||
|
||||
# Create user
|
||||
user = User(
|
||||
user, created = User.objects.get_or_create(
|
||||
email=email,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
defaults={
|
||||
"first_name": first_name,
|
||||
"last_name": last_name,
|
||||
"is_active": True,
|
||||
"is_verified": True,
|
||||
},
|
||||
)
|
||||
if created:
|
||||
user.set_password(password)
|
||||
user.save()
|
||||
|
||||
|
|
@ -594,12 +601,14 @@ class Command(BaseCommand):
|
|||
|
||||
address = Address.objects.filter(user=user).first()
|
||||
|
||||
order = Order.objects.create(
|
||||
order, _ = Order.objects.get_or_create(
|
||||
user=user,
|
||||
status=status,
|
||||
buy_time=order_date,
|
||||
billing_address=address,
|
||||
shipping_address=address,
|
||||
defaults={
|
||||
"billing_address": address,
|
||||
"shipping_address": address,
|
||||
},
|
||||
)
|
||||
|
||||
Order.objects.filter(pk=order.pk).update(created=order_date)
|
||||
|
|
@ -620,12 +629,14 @@ class Command(BaseCommand):
|
|||
else:
|
||||
op_status = random.choice(["ACCEPTED", "PENDING"])
|
||||
|
||||
OrderProduct.objects.create(
|
||||
OrderProduct.objects.get_or_create(
|
||||
order=order,
|
||||
product=product,
|
||||
quantity=quantity,
|
||||
buy_price=round(price, 2),
|
||||
status=op_status,
|
||||
defaults={
|
||||
"quantity": quantity,
|
||||
"buy_price": round(price, 2),
|
||||
"status": op_status,
|
||||
},
|
||||
)
|
||||
|
||||
orders.append(order)
|
||||
|
|
@ -682,9 +693,6 @@ class Command(BaseCommand):
|
|||
tag.save()
|
||||
|
||||
for post_data in data.get("blog_posts", []):
|
||||
if Post.objects.filter(title=post_data["title"]).exists():
|
||||
continue
|
||||
|
||||
content_en = self._load_blog_content(post_data["content_file"], "en")
|
||||
content_ru = self._load_blog_content(post_data["content_file"], "ru")
|
||||
|
||||
|
|
@ -696,13 +704,16 @@ class Command(BaseCommand):
|
|||
)
|
||||
continue
|
||||
|
||||
post = Post(
|
||||
author=author,
|
||||
post, created = Post.objects.get_or_create(
|
||||
title=post_data["title"],
|
||||
content=content_en,
|
||||
meta_description=post_data.get("meta_description", ""),
|
||||
is_static_page=post_data.get("is_static_page", False),
|
||||
defaults={
|
||||
"author": author,
|
||||
"content": content_en,
|
||||
"meta_description": post_data.get("meta_description", ""),
|
||||
"is_static_page": post_data.get("is_static_page", False),
|
||||
},
|
||||
)
|
||||
if created:
|
||||
if "title_ru" in post_data:
|
||||
post.title_ru_ru = post_data["title_ru"] # ty:ignore[unresolved-attribute]
|
||||
if content_ru:
|
||||
|
|
|
|||
Loading…
Reference in a new issue