]> git.parisson.com Git - diggersdigest.git/commitdiff
Move back payment_complete in records.models.py
authorThomas Fillon <thomas@parisson.com>
Tue, 29 Sep 2015 23:03:42 +0000 (01:03 +0200)
committerThomas Fillon <thomas@parisson.com>
Tue, 29 Sep 2015 23:03:42 +0000 (01:03 +0200)
app/records/models.py
app/records/utils.py

index fec84e9b0cf330e95f62bedfef90ae6b645b7e49..30811590ef7e286705853a95db409759c9afc2c5 100644 (file)
@@ -18,8 +18,11 @@ from mezzanine.blog.models import BlogPost
 from mezzanine.utils.models import upload_to
 
 from cartridge.shop.models import Product
+from cartridge.shop.models import Category, Cart, Order, ProductVariation, DiscountCode
+
+from paypal.standard.ipn.signals import valid_ipn_received
+
 
-#from utils import *
 
 # Auto-generated Django models with manage.py inspectdb on the old database
 # You'll have to do the following manually to clean this up:
@@ -295,3 +298,63 @@ class Podcast(BlogPost):
     #visu1 = models.IntegerField()
     # ordre : on laisse tombé ?
     # published --> status / 0 --> CONTENT_STATUS_DRAFT = 1 / 1 CONTENT_STATUS_PUBLISHED = 2
+
+
+# Signals
+    
+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)
index 758b504c8f5c054c34a6b242734a151d5910fdf8..c440396934674e3d152d5b16b32bcbec6573c6de 100644 (file)
@@ -1,22 +1,13 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-from __future__ import unicode_literals
-
-from django.db import models
-from django.utils.translation import ugettext_lazy as _
 from django import forms
 
-import datetime
-import os
-import fnmatch
-from importlib import import_module
-from mezzanine.conf import settings
-
 from cartridge.shop import checkout
-from cartridge.shop.models import Product, Category, Cart, Order, ProductVariation, DiscountCode
 from cartridge.shop.utils import set_shipping
 
+from mezzanine.conf import settings
+
 from django_countries.data import COUNTRIES
 COUNTRY_CHOICES = tuple((k,v) for k,v in COUNTRIES.items())
 
@@ -26,67 +17,6 @@ COUNTRY_CHOICES = tuple(sorted(COUNTRY_CHOICES, key=itemgetter(1)))
     
 from payments.multipayments.forms.base import CallbackUUIDOrderForm
 
-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)
-
 # See https://github.com/SmileyChris/django-countries/blob/master/django_countries/data.py#L46
 EURO_ZONE = [
     ## Zone 1