Features: 1) None;
Fixes: 1) Add missing `created` parameter handling in signal receivers for `Product` and `Category`; 2) Ensure proper formatting for long method calls and imports; 3) Adjust inline object creation to enhance readability; Extra: Update import style and consistent formatting for readability improvements throughout `signals.py`;
This commit is contained in:
parent
8c906a2880
commit
d4ff637169
1 changed files with 39 additions and 11 deletions
|
|
@ -11,7 +11,10 @@ from django.utils.translation import gettext_lazy as _
|
|||
from sentry_sdk import capture_exception
|
||||
|
||||
from core.models import Category, Order, Product, PromoCode, Wishlist
|
||||
from core.utils import generate_human_readable_id, resolve_translations_for_elasticsearch
|
||||
from core.utils import (
|
||||
generate_human_readable_id,
|
||||
resolve_translations_for_elasticsearch,
|
||||
)
|
||||
from core.utils.emailing import send_order_created_email, send_order_finished_email
|
||||
from evibes.utils.misc import create_object
|
||||
from vibes_auth.models import User
|
||||
|
|
@ -30,7 +33,9 @@ def create_order_on_user_creation_signal(instance, created, **_kwargs):
|
|||
if Order.objects.filter(human_readable_id=human_readable_id).exists():
|
||||
human_readable_id = generate_human_readable_id()
|
||||
continue
|
||||
Order.objects.create(user=instance, status="PENDING", human_readable_id=human_readable_id)
|
||||
Order.objects.create(
|
||||
user=instance, status="PENDING", human_readable_id=human_readable_id
|
||||
)
|
||||
break
|
||||
|
||||
|
||||
|
|
@ -44,7 +49,9 @@ def create_wishlist_on_user_creation_signal(instance, created, **_kwargs):
|
|||
def create_promocode_on_user_referring(instance, created, **_kwargs):
|
||||
try:
|
||||
if created and instance.attributes.get("referrer", ""):
|
||||
referrer_uuid = urlsafe_base64_decode(instance.attributes.get("referrer", ""))
|
||||
referrer_uuid = urlsafe_base64_decode(
|
||||
instance.attributes.get("referrer", "")
|
||||
)
|
||||
referrer = User.objects.get(uuid=referrer_uuid)
|
||||
code = f"WELCOME-{get_random_string(6)}"
|
||||
PromoCode.objects.create(
|
||||
|
|
@ -71,10 +78,16 @@ def process_order_changes(instance, created, **_kwargs):
|
|||
except IntegrityError:
|
||||
human_readable_id = generate_human_readable_id()
|
||||
while True:
|
||||
if Order.objects.filter(human_readable_id=human_readable_id).exists():
|
||||
if Order.objects.filter(
|
||||
human_readable_id=human_readable_id
|
||||
).exists():
|
||||
human_readable_id = generate_human_readable_id()
|
||||
continue
|
||||
Order.objects.create(user=instance, status="PENDING", human_readable_id=human_readable_id)
|
||||
Order.objects.create(
|
||||
user=instance,
|
||||
status="PENDING",
|
||||
human_readable_id=human_readable_id,
|
||||
)
|
||||
break
|
||||
|
||||
if instance.status in ["CREATED", "PAYMENT"]:
|
||||
|
|
@ -87,20 +100,31 @@ def process_order_changes(instance, created, **_kwargs):
|
|||
|
||||
try:
|
||||
vendor_name = (
|
||||
order_product.product.stocks.filter(price=order_product.buy_price).first().vendor.name.lower()
|
||||
order_product.product.stocks.filter(
|
||||
price=order_product.buy_price
|
||||
)
|
||||
.first()
|
||||
.vendor.name.lower()
|
||||
)
|
||||
|
||||
vendor = create_object(f"core.vendors.{vendor_name}", f"{vendor_name.title()}Vendor")
|
||||
vendor = create_object(
|
||||
f"core.vendors.{vendor_name}", f"{vendor_name.title()}Vendor"
|
||||
)
|
||||
|
||||
vendor.buy_order_product(order_product)
|
||||
|
||||
except Exception as e:
|
||||
order_product.add_error(f"Failed to buy {order_product.uuid}. Reason: {e}...")
|
||||
order_product.add_error(
|
||||
f"Failed to buy {order_product.uuid}. Reason: {e}..."
|
||||
)
|
||||
|
||||
else:
|
||||
instance.finalize()
|
||||
|
||||
if instance.order_products.filter(status="FAILED").count() == instance.order_products.count():
|
||||
if (
|
||||
instance.order_products.filter(status="FAILED").count()
|
||||
== instance.order_products.count()
|
||||
):
|
||||
instance.status = "FAILED"
|
||||
instance.save()
|
||||
|
||||
|
|
@ -109,12 +133,16 @@ def process_order_changes(instance, created, **_kwargs):
|
|||
|
||||
|
||||
@receiver(post_save, sender=Product)
|
||||
def update_product_name_lang(instance, _created, **_kwargs):
|
||||
def update_product_name_lang(instance, created, **_kwargs):
|
||||
if created:
|
||||
pass
|
||||
resolve_translations_for_elasticsearch(instance, "name")
|
||||
resolve_translations_for_elasticsearch(instance, "description")
|
||||
|
||||
|
||||
@receiver(post_save, sender=Category)
|
||||
def update_category_name_lang(instance, _created, **_kwargs):
|
||||
def update_category_name_lang(instance, created, **_kwargs):
|
||||
if created:
|
||||
pass
|
||||
resolve_translations_for_elasticsearch(instance, "name")
|
||||
resolve_translations_for_elasticsearch(instance, "description")
|
||||
|
|
|
|||
Loading…
Reference in a new issue