]> git.parisson.com Git - diggersdigest.git/commitdiff
add utils
authorGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Mon, 28 Sep 2015 10:29:09 +0000 (12:29 +0200)
committerGuillaume Pellerin <guillaume.pellerin@ircam.fr>
Mon, 28 Sep 2015 10:29:09 +0000 (12:29 +0200)
app/records/utils.py [new file with mode: 0644]

diff --git a/app/records/utils.py b/app/records/utils.py
new file mode 100644 (file)
index 0000000..6373355
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import unicode_literals
+
+import datetime
+import os
+import fnmatch
+from importlib import import_module
+from mezzanine.conf import settings
+
+from cartridge.shop.models import Product, Category, Cart, Order, ProductVariation, DiscountCode
+from paypal.standard.ipn.signals import valid_ipn_received
+
+
+def payment_complete(sender, **kwargs):
+    """Performs the same logic as the code in
+    cartridge.shop.models.Order.complete(), but fetches the session,
+    order, and cart objects from storage, rather than relying on the
+    request object being passed in (which it isn't, since this is
+    triggered on PayPal IPN callback)."""
+
+    ipn_obj = sender
+    print sender
+    if ipn_obj.custom and ipn_obj.invoice:
+        s_key, cart_pk = ipn_obj.custom.split(',')
+        SessionStore = import_module(settings.SESSION_ENGINE) \
+                           .SessionStore
+        session = SessionStore(s_key)
+
+        try:
+            cart = Cart.objects.get(id=cart_pk)
+            try:
+                order = Order.objects.get(
+                    transaction_id=ipn_obj.invoice)
+                print order
+                # order = Order.objects.get(key=s_key)
+                for field in order.session_fields:
+                    if field in session:
+                        del session[field]
+                try:
+                    del session["order"]
+                except KeyError:
+                    pass
+
+                # Since we're manually changing session data outside of
+                # a normal request, need to force the session object to
+                # save after modifying its data.
+                session.save()
+
+                for item in 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 = session.get('discount_code')
+                if code:
+                    DiscountCode.objects.active().filter(code=code) \
+                        .update(uses_remaining=F('uses_remaining') - 1)
+                cart.delete()
+            except Order.DoesNotExist:
+                pass
+        except Cart.DoesNotExist:
+            pass
+
+valid_ipn_received.connect(payment_complete)