简体   繁体   English

使用 Django 奥斯卡信号

[英]Use Django Oscar Signals

I want to send an email to admin if an Order is placed (currently only user who have placed an order received the email).如果下订单,我想向管理员发送电子邮件(目前只有下订单的用户收到了电子邮件)。 order_paced Oscar Signal can work for me here. order_paced Oscar Signal 可以在这里为我工作。

For this I have already forked order app and inside this app order_placed function is created in signals.py .为此,我已经分叉的order应用程序,并在此应用中order_placed中创建功能signals.py I have also imported signals in config.py but still this order_placed not getting fired when I am placing an order from site.我还在config.py导入了信号,但是当我从站点下订单时,这个order_placed仍然没有被触发。

Can anyone share any example of oscar signal usage ?任何人都可以分享任何奥斯卡信号使用的例子吗?

Code :代码 :

config.py

from oscar.apps.order import config


class OrderConfig(config.OrderConfig):
    name = 'catalogue.order'
    
    def ready(self):
        from oscar.apps.order import signals

signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver

from oscar.apps.order.models import Order

@receiver(post_save, sender=Order)
def order_placed(*args, **kwargs):
    """
    :param args:
    :param kwargs:
    :return:
    """

    print("i ma here ----------------------")

You don't need signals for this, as part of the payment flow (framework) oscar provides the view: PaymentDetailsView which in time implements the mixin OrderPlacementMixin .您不需要为此提供信号,作为支付流程(框架)的一部分,oscar 提供了视图: PaymentDetailsView ,它及时实现了 mixin OrderPlacementMixin

In such mixin you'll find the method: handle_successful_order which is the correct place to send the messages, and do other things being sure the order was placed.在这样的 mixin 中,您会找到方法: handle_successful_order ,这是发送消息的正确位置,并执行其他操作以确保已下订单。

So, do not fork order app, fork checkout app and override this method in order to do something like this:因此,不要分叉order应用程序,分叉checkout应用程序并覆盖此方法以执行以下操作:

from django.conf import settings

class PaymentDetailView:
    # ...
    def handle_successful_order(order):
        send_mail_to_admin(settings.ADMIN_EMAIL_ADDRESS)
        super(PaymentDetailView, self).handle_successful_order(order)

If you read the code of this method in oscar you'll see that this is indeed where oscar notify the user about the order that has been just placed.如果您在 oscar 中阅读此方法的代码,您会发现这确实是 oscar 通知用户刚刚下的订单的地方。

An of course, we can not ignore the docstring which states:当然,我们不能忽略说明的文档字符串:

Override this view if you want to perform custom actions when an order is submitted.如果您想在提交订单时执行自定义操作,请覆盖此视图。

def handle_successful_order(self, order):
    """
    Handle the various steps required after an order has been successfully
    placed.

    Override this view if you want to perform custom actions when an
    order is submitted.
    """
    # Send confirmation message (normally an email)
    self.send_confirmation_message(order, self.communication_type_code)

    # Flush all session data
    self.checkout_session.flush()

    # Save order id in session so thank-you page can load it
    self.request.session['checkout_order_id'] = order.id

    response = HttpResponseRedirect(self.get_success_url())
    self.send_signal(self.request, response, order)
    return response

Like @raydel-miranda pointed out you don't need signal to send email to the admin when an order is placed by the customer.就像@raydel-miranda 指出的那样,当客户下订单时,您不需要信号来向管理员发送电子邮件。

just fork the checkout app using ./python manage.py oscar_fork_app checkout apps只需使用./python manage.py oscar_fork_app checkout apps

Inside the above forked checkout app create views.py file and override the default oscar checkout/views.py file with this code.在上面的分叉结帐应用程序中创建views.py文件并使用此代码覆盖默认的 oscar checkout/views.py 文件。

yourproject_name/apps/checkout/views.py yourproject_name/apps/checkout/views.py

from django.conf import settings
from django.views.generic import FormView
from django.contrib import messages


from django.core.mail import EmailMessage,send_mail

OscarPaymentDetailsView = get_class("checkout.views", "PaymentDetailsView")


class PaymentDetailsView(CheckCountryPreCondition, OscarPaymentDetailsView):
    
    def handle_successful_order(self, order):
        print('order creted') 
        send_mail(
        subject="New Order Needs attention",
        message='Please login the dashboard and complete the order',
        from_email=(settings.EMAIL_HOST_USER),
        recipient_list=['admin_email_address_1', 'admin_email_address_2'], #x.email for x in partner_id.users.all()
        fail_silently=False,
        )
        ctx=super(PaymentDetailsView, self).handle_successful_order(order)
        #send_mail_to_admin(settings.ADMIN_EMAIL_ADDRESS)
        

This may save someone else quality time trying to create signals for sending admin email when an order is placed.这可以节省其他人在下订单时尝试创建用于发送管理员电子邮件的信号的质量时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM