Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please consider this change since the old code didn't work. #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions subscription/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
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

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,
Expand Down Expand Up @@ -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()."""
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)