I am making a shopping cart using mezzanine and cartridge.
In my order model there is a field called status which has choices of unprocessed and processed. After an order is placed an admin can change their status. I want to send an email to the user whenever an admin updates the status.
I have done some R&D on it but I didn't find anything which tells me how to send an email on change of status.
My model:
class Order(models.Model):
...
....
........
status = models.IntegerField(_("Status"),
choices=settings.SHOP_ORDER_STATUS_CHOICES,
default=settings.SHOP_ORDER_STATUS_CHOICES[0][0])
objects = managers.OrderManager()
# These are fields that are stored in the session. They're copied to
# the order in setup() and removed from the session in complete().
session_fields = ("shipping_type", "shipping_total", "discount_total")
class Meta:
verbose_name = _("Order")
verbose_name_plural = _("Orders")
ordering = ("-id",)
def __unicode__(self):
return "#%s %s %s" % (self.id, self.billing_name(), self.time)
def billing_name(self):
return "%s %s" % (self.billing_detail_first_name,
self.billing_detail_last_name)
def setup(self, request):
self.key = request.session.session_key
self.user_id = request.user.id
for field in self.session_fields:
if field in request.session:
setattr(self, field, request.session[field])
self.total = self.item_total = request.cart.total_price()
if self.shipping_total is not None:
self.shipping_total = Decimal(str(self.shipping_total))
self.total += self.shipping_total
if self.discount_total is not None:
self.total -= self.discount_total
self.save() # We need an ID before we can add related items.
for item in request.cart:
product_fields = [f.name for f in SelectedProduct._meta.fields]
item = dict([(f, getattr(item, f)) for f in product_fields])
self.items.create(**item)
def complete(self, request):
self.save() # Save the transaction ID.
for field in self.session_fields:
if field in request.session:
del request.session[field]
del request.session["order"]
for item in request.cart:
try:
variation = ProductVariation.objects.get(sku=item.sku)
except ProductVariation.DoesNotExist:
pass
else:
variation.update_stock(item.quantity * -1)
variation.product.actions.purchased()
code = request.session.get('discount_code')
if code:
DiscountCode.objects.active().filter(code=code).update(
uses_remaining=F('uses_remaining') - 1)
request.cart.delete()
def details_as_dict(self):
context = {}
for fieldset in ("billing_detail", "shipping_detail"):
fields = [(f.verbose_name, getattr(self, f.name)) for f in
self._meta.fields if f.name.startswith(fieldset)]
context["order_%s_fields" % fieldset] = fields
return context
Admin:
class OrderAdmin(admin.ModelAdmin):
ordering = ("status", "-id")
list_display = ("id", "billing_name", "total", "time", "status",
"transaction_id", "invoice")
list_editable = ("status",)
list_filter = ("status", "time")
list_display_links = ("id", "billing_name",)
search_fields = (["id", "status", "transaction_id"] +
billing_fields + shipping_fields)
date_hierarchy = "time"
radio_fields = {"status": admin.HORIZONTAL}
inlines = (OrderItemInline,)
formfield_overrides = {MoneyField: {"widget": MoneyWidget}}
fieldsets = (
(_("Billing details"), {"fields": (tuple(billing_fields),)}),
(_("Shipping details"), {"fields": (tuple(shipping_fields),)}),
(None, {"fields": ("additional_instructions", ("shipping_total",
"shipping_type"), ("discount_total", "discount_code"),
"item_total", ("total", "status"), "transaction_id")}),
)
admin.site.register(Order, OrderAdmin)
Can anybody please tell me how I can send this email?
You need to add a listener on the
post_save
signal for the model. Your listener (which is a standard Python method) will check what field has changed and then dispatch the email to the user.