diff --git a/subscription/models.py b/subscription/models.py index add0522..684bc0f 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -3,9 +3,11 @@ 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 -from paypal.standard import ipn +import paypal.standard.ipn.models as ipn_models +import paypal.standard.ipn.signals as ipn_signals import signals, utils @@ -13,9 +15,9 @@ 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, + 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, @@ -164,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().""" @@ -239,7 +245,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 @@ -265,7 +271,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 +316,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 +325,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 +364,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) @@ -371,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, @@ -385,8 +392,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 +425,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)