diff --git a/engine/core/management/commands/demo_data.py b/engine/core/management/commands/demo_data.py index d9b2b75c..7ebffbe2 100644 --- a/engine/core/management/commands/demo_data.py +++ b/engine/core/management/commands/demo_data.py @@ -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,16 +518,18 @@ 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, + }, ) - user.set_password(password) - user.save() + if created: + user.set_password(password) + user.save() Balance.objects.get_or_create( user=user, @@ -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,20 +704,23 @@ 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 "title_ru" in post_data: - post.title_ru_ru = post_data["title_ru"] # ty:ignore[unresolved-attribute] - if content_ru: - post.content_ru_ru = content_ru # ty:ignore[unresolved-attribute] - if "meta_description_ru" in post_data: - post.meta_description_ru_ru = post_data["meta_description_ru"] # ty:ignore[unresolved-attribute] - post.save() + if created: + if "title_ru" in post_data: + post.title_ru_ru = post_data["title_ru"] # ty:ignore[unresolved-attribute] + if content_ru: + post.content_ru_ru = content_ru # ty:ignore[unresolved-attribute] + if "meta_description_ru" in post_data: + post.meta_description_ru_ru = post_data["meta_description_ru"] # ty:ignore[unresolved-attribute] + post.save() for tag_name in post_data.get("tags", []): try: