From 1e3c4ae3fa53bd648542e2ff6091a4cdb74e039e Mon Sep 17 00:00:00 2001 From: Abd Allah Diab Date: Wed, 25 Jul 2012 10:29:05 +0400 Subject: [PATCH 1/4] Properly imported ipn signals and models. --- subscription/models.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/subscription/models.py b/subscription/models.py index add0522..37c951c 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -5,7 +5,8 @@ from django.contrib import auth from django.utils.translation import ugettext as _, ungettext, ugettext_lazy -from paypal.standard import ipn +import paypal.standard.ipn.models as ipn_models +import paypal.standard.ipn.signals as ipn_signals import signals, utils @@ -15,7 +16,7 @@ class Transaction(models.Model): null=True, blank=True, editable=False) user = models.ForeignKey(auth.models.User, null=True, blank=True, editable=False) - ipn = models.ForeignKey(ipn.models.PayPalIPN, + ipn = models.ForeignKey(ipn_models.PayPalIPN, null=True, blank=True, editable=False) event = models.CharField(max_length=100, editable=False) amount = models.DecimalField(max_digits=64, decimal_places=2, @@ -265,7 +266,7 @@ def __init__(self, user, subscription): Transaction(user=u, subscription=s, ipn=payment, event='new usersubscription', amount=payment.mc_gross ).save() - else: us = PseudoUS(user=u,subscription=s) + else: us = PseudoUS(user=u,subscription=s) return us @@ -310,7 +311,7 @@ def handle_payment_was_successful(sender, **kwargs): event='unexpected payment', amount=sender.mc_gross ).save() signals.event.send(s, ipn=sender, subscription=s, user=u, event='unexpected_payment') -ipn.signals.payment_was_successful.connect(handle_payment_was_successful) +ipn_signals.payment_was_successful.connect(handle_payment_was_successful) def handle_payment_was_flagged(sender, **kwargs): us = _ipn_usersubscription(sender) @@ -319,7 +320,7 @@ def handle_payment_was_flagged(sender, **kwargs): event='payment flagged', amount=sender.mc_gross ).save() signals.event.send(s, ipn=sender, subscription=s, user=u, event='flagged') -ipn.signals.payment_was_flagged.connect(handle_payment_was_flagged) +ipn_signals.payment_was_flagged.connect(handle_payment_was_flagged) def handle_subscription_signup(sender, **kwargs): us = _ipn_usersubscription(sender) @@ -358,7 +359,7 @@ def handle_subscription_signup(sender, **kwargs): ).save() signals.event.send(s, ipn=sender, subscription=s, user=u, event='unexpected_subscription') -ipn.signals.subscription_signup.connect(handle_subscription_signup) +ipn_signals.subscription_signup.connect(handle_subscription_signup) def handle_subscription_cancel(sender, **kwargs): us = _ipn_usersubscription(sender) @@ -385,8 +386,8 @@ def handle_subscription_cancel(sender, **kwargs): event='unexpected cancel', amount=sender.mc_gross ).save() signals.event.send(s, ipn=sender, subscription=s, user=u, event='unexpected_cancel') -ipn.signals.subscription_cancel.connect(handle_subscription_cancel) -ipn.signals.subscription_eot.connect(handle_subscription_cancel) +ipn_signals.subscription_cancel.connect(handle_subscription_cancel) +ipn_signals.subscription_eot.connect(handle_subscription_cancel) def handle_subscription_modify(sender, **kwargs): us = _ipn_usersubscription(sender) @@ -418,4 +419,4 @@ def handle_subscription_modify(sender, **kwargs): ).save() signals.event.send(s, ipn=sender, subscription=s, user=u, event='unexpected_subscription_modify') -ipn.signals.subscription_modify.connect(handle_subscription_modify) +ipn_signals.subscription_modify.connect(handle_subscription_modify) From 34f770e3a0b59023951852acf6500106a7326b54 Mon Sep 17 00:00:00 2001 From: Abd Allah Diab Date: Thu, 26 Jul 2012 13:49:04 +0400 Subject: [PATCH 2/4] bugfix importing `auth` and filtering expired --- subscription/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/subscription/models.py b/subscription/models.py index 37c951c..677047b 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -3,6 +3,7 @@ from django.conf import settings from django.db import models from django.contrib import auth +import django.contrib.auth.models from django.utils.translation import ugettext as _, ungettext, ugettext_lazy import paypal.standard.ipn.models as ipn_models @@ -14,7 +15,7 @@ class Transaction(models.Model): timestamp = models.DateTimeField(auto_now_add=True, editable=False) subscription = models.ForeignKey('subscription.Subscription', null=True, blank=True, editable=False) - user = models.ForeignKey(auth.models.User, + user = models.ForeignKey(django.contrib.auth.models.User, null=True, blank=True, editable=False) ipn = models.ForeignKey(ipn_models.PayPalIPN, null=True, blank=True, editable=False) @@ -240,7 +241,7 @@ def unsubscribe_expired(): Loops through all UserSubscription objects with `expires' field earlier than datetime.date.today() and forces correct group membership.""" - for us in UserSubscription.objects.get(expires__lt=datetime.date.today()): + for us in UserSubscription.objects.filter(expires__lt=datetime.date.today()): us.fix() #### Handle PayPal signals From ef5d2b510f0282356bcb36534b94a8834ca46bf9 Mon Sep 17 00:00:00 2001 From: Abd Allah Diab Date: Thu, 26 Jul 2012 13:56:14 +0400 Subject: [PATCH 3/4] subscribe/unsubscribe modifies active state --- subscription/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/subscription/models.py b/subscription/models.py index 677047b..d541d85 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -166,12 +166,16 @@ def valid(self): def unsubscribe(self): """Unsubscribe user.""" self.user.groups.remove(self.subscription.group) + self.active = False self.user.save() + self.save() def subscribe(self): """Subscribe user.""" self.user.groups.add(self.subscription.group) + self.active = True self.user.save() + self.save() def fix(self): """Fix group membership if not valid().""" From c1cf7a4063d5b26023501d045bfc5e24ddb4ec66 Mon Sep 17 00:00:00 2001 From: Abd Allah Diab Date: Thu, 26 Jul 2012 15:07:12 +0400 Subject: [PATCH 4/4] fix subscription cancel unsubscribe --- subscription/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/subscription/models.py b/subscription/models.py index d541d85..684bc0f 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -377,6 +377,7 @@ def handle_subscription_cancel(sender, **kwargs): event='remove subscription (cancelled)', amount=sender.mc_gross ).save() else: + us.unsubscribe() us.cancelled = True us.save() Transaction(user=u, subscription=s, ipn=sender,