Commits

Bruce Kroeze  committed 6735e8e

Order emails are now handled by a common module

  • Participants
  • Parent commits e48cf15

Comments (0)

Files changed (3)

File satchmo/payment/common/pay_ship.py

-import sys
+from django.template import loader, Context
+from django.utils.translation import ugettext as _
 from decimal import Decimal
 from django.conf import settings
+from django.core.mail import send_mail
 from satchmo.configuration import config_value
 from satchmo.contact.models import OrderItem
+from satchmo.shop.models import Config
 from satchmo.shop.utils import load_module
+from socket import error as SocketError
 import logging
 
 log = logging.getLogger('pay_ship')
     
     new_order.recalculate_total()
 
+def send_order_confirmation(newOrder, template='email/order_complete.txt'):
+    """Send an order confirmation mail to the customer."""
+    shop_config = Config.get_shop_config()
+    shop_email = shop_config.store_email
+    shop_name = shop_config.store_name
+    t = loader.get_template(template)
+    c = Context({'order': newOrder,
+                  'shop_name': shop_name})
+    subject = _("Thank you for your order from %(shop_name)s") % {'shop_name' : shop_name}
+
+    try:
+        email = newOrder.contact.email
+        body = t.render(c)
+        send_mail(subject, body, shop_email,
+                  [email], fail_silently=False)
+    except SocketError, e:
+        if settings.DEBUG:
+            log.error('Error sending mail: %s' % e)
+            log.warn('Ignoring email error, since you are running in DEBUG mode.  Email was:\nTo:%s\nSubject: %s\n---\n%s', email, subject, body)
+        else:
+            log.fatal('Error sending mail: %s' % e)
+            raise IOError('Could not send email, please check to make sure your email settings are correct, and that you are not being blocked by your ISP.')    
+
 
    
 

File satchmo/payment/common/views/confirm.py

 # Last step in the order process - confirm the info and process it
 #####################################################################
 
-from django.conf import settings
 from django.core import urlresolvers
-from django.core.mail import send_mail
 from django.http import HttpResponseRedirect
 from django.shortcuts import render_to_response
-from django.template import loader, RequestContext, Context
+from django.template import RequestContext
 from django.utils.translation import ugettext as _
-from satchmo.contact.models import Order, OrderStatus
+from satchmo.contact.models import Order
+from satchmo.payment.common.pay_ship import send_order_confirmation
 from satchmo.payment.urls import lookup_url, lookup_template
-from satchmo.shop.models import Cart, Config
-from satchmo.shop.utils import load_module
-from socket import error as SocketError
-import datetime
+from satchmo.shop.models import Cart
 import logging
 
 log = logging.getLogger('satchmo.payment.common.views')
             
             orderToProcess.add_status(status='Pending', notes = "Order successfully submitted")
 
-            #Now, send a confirmation email
-            shop_config = Config.get_shop_config()
-            shop_email = shop_config.store_email
-            shop_name = shop_config.store_name
-            t = loader.get_template('email/order_complete.txt')
-            c = Context({'order': orderToProcess,
-                          'shop_name': shop_name})
-            subject = "Thank you for your order from %s" % shop_name
-                     
-            try:
-                email = orderToProcess.contact.email
-                body = t.render(c)
-                send_mail(subject, body, shop_email,
-                          [email], fail_silently=False)
-            except SocketError, e:
-                if settings.DEBUG:
-                    log.error('Error sending mail: %s' % e)
-                    log.warn('Ignoring email error, since you are running in DEBUG mode.  Email was:\nTo:%s\nSubject: %s\n---\n%s', email, subject, body)
-                else:
-                    log.fatal('Error sending mail: %s' % e)
-                    raise IOError('Could not send email, please check to make sure your email settings are correct, and that you are not being blocked by your ISP.')    
+            send_order_confirmation(orderToProcess)
             
             #Redirect to the success page
             url = lookup_url(payment_module, 'satchmo_checkout-success')
         'errors': errors,
         'checkout_step2': lookup_url(payment_module, 'satchmo_checkout-step2')})
     return render_to_response(template, context)
-
-

File satchmo/payment/modules/autosuccess/views.py

-from django.conf import settings
-from django.core import urlresolvers
-from django.core.mail import send_mail
 from django.http import HttpResponseRedirect
 from django.shortcuts import render_to_response
-from django.template import loader, RequestContext, Context
-from django.utils.translation import ugettext as _
-from satchmo.configuration import config_value, config_get_group
+from django.template import RequestContext
+from satchmo.configuration import config_get_group
 from satchmo.contact.models import Order, Contact, OrderPayment
-from satchmo.payment.common.pay_ship import pay_ship_save
-from satchmo.payment.common.views import payship
-from satchmo.payment.config import payment_live
+from satchmo.payment.common.pay_ship import pay_ship_save, send_order_confirmation
 from satchmo.payment.urls import lookup_url, lookup_template
-from satchmo.shop.models import Cart, Config
-from socket import error as SocketError
+from satchmo.shop.models import Cart
 
 import logging
 
     contact = Contact.from_request(request, create=False)
     if contact is None:
         url = lookup_url(payment_module, 'satchmo_checkout-step1')
-        return http.HttpResponseRedirect(url)
+        return HttpResponseRedirect(url)
     #Verify we still have items in the cart
     if request.session.get('cart', False):
         tempCart = Cart.objects.get(id=request.session['cart'])
 
     #Now, send a confirmation email
     if payment_module['EMAIL'].value:
-        shop_config = Config.get_shop_config()
-        shop_email = shop_config.store_email
-        shop_name = shop_config.store_name
-        t = loader.get_template('email/order_complete.txt')
-        c = Context({'order': newOrder,
-                      'shop_name': shop_name})
-        subject = "Thank you for your order from %s" % shop_name
-             
-        try:
-            email = newOrder.contact.email
-            body = t.render(c)
-            send_mail(subject, body, shop_email,
-                      [email], fail_silently=False)
-        except SocketError, e:
-            if settings.DEBUG:
-                log.error('Error sending mail: %s' % e)
-                log.warn('Ignoring email error, since you are running in DEBUG mode.  Email was:\nTo:%s\nSubject: %s\n---\n%s', email, subject, body)
-            else:
-                log.fatal('Error sending mail: %s' % e)
-                raise IOError('Could not send email, please check to make sure your email settings are correct, and that you are not being blocked by your ISP.')    
-    
-    
+        send_order_confirmation(newOrder)    
+
     tempCart.empty()
     success = lookup_url(payment_module, 'satchmo_checkout-success')
     return HttpResponseRedirect(success)