2012-04-14 103 views
0

在Satchmo商店中,我需要将一个小的.png(条形码)附加到django完成订单时发送的电子邮件中。该电子邮件使用send_order_confirmation()调用send_order_confirmation()来格式化,send_store_mail()(都是satchmo的一部分)。这些函数都不能提供附加文件的功能(我认为),所以我应该重写它们吗?我想知道是否可以/更好地使用信号做到这一点。也许rendering_store_mail()?将.png文件附加到由satchmo发送的电子邮件

顺便说一句,条码会动态生成,所以没有办法链接到某个服务器上的某个文件。

非常感谢, 托马斯

回答

0

好,我也曾经有过额外的相关信息添加到确认电子邮件,只有文字虽然。所以这将是使用信号为电子邮件添加额外内容的简单方法,恕我直言,这是最好的方法。如果您可以避免覆盖satchmo-core,请始终使用信号;-)

  1. 定义您的侦听器为渲染添加一些上下文。在这种情况下,我将一个额外的注释字段的内容添加到该上下文中,并假定该命令的条形码(假定有一个名为get_barcode_img(<order>)的函数)。我假设在这里,get_barcode_img函数不仅会返回一个PNG,而且会像MIMEImage(如from email.MIMEImage import MIMEImage)那样能够直接包含它。另外,可能还需要更多信息,例如img的MIME头。

    # localsite/payment/listeners.py 
    
    def add_extra_context_to_store_mail(sender, 
         send_mail_args={}, context={}, **kwargs): 
        if not 'order' in context: 
         return 
        context['barcode_header'] = get_barcode_header(context['order']) 
        context['barcode_img'] = get_barcode_img(context['order']) 
        context['notes'] = context['order'].notes 
    
  2. 监听器连接到某个地方的代码将被“发现”可以肯定的信号,如models.py

    # localsite/payment/models.py 
    
    from satchmo_store.shop.signals import rendering_store_mail, order_notice_sender 
    
    rendering_store_mail.connect(payment_listeners.add_extra_context_to_store_mail, sender=order_notice_sender) 
    
  3. 覆盖模板局部(如order_placed_notice.html)添加新的上下文。请注意放置模板的位置,因为路径对于django采用新模板而不是satchmo的模板非常重要。在这种情况下,从项目的根路径开始,可能有一个模板文件夹,并且在其中,必须有与satchmo文件夹中完全相同的路径。例如。 /templates/shop/email/order_placed_notice.html ...这可以应用于应用程序内的任何“有效”模板文件夹。由您来决定,应该在何处/如何组织模板。

    <!-- templates/shop/email/order_placed_notice.html --> 
    <!DOCTYPE ...><html> 
    <head> 
        <!-- include the image-header here somewhere??? --> 
        <title></title> 
    </head> 
    <body>   
    ... 
    Your comments: 
    {{ notes }} 
    
    Barcode: 
    {{ barcode_img }}"