Adding models for payment history.

This commit is contained in:
Samuel Clay 2012-12-03 14:35:21 -08:00
parent 8dcc19cbbe
commit b87bb34073
5 changed files with 167 additions and 19 deletions

View file

@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'PaymentHistory'
db.create_table('profile_paymenthistory', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
('payment_date', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
('payment_amount', self.gf('django.db.models.fields.IntegerField')()),
('payment_provider', self.gf('django.db.models.fields.CharField')(max_length=20)),
))
db.send_create_signal('profile', ['PaymentHistory'])
# Adding field 'Profile.premium_expire'
db.add_column('profile_profile', 'premium_expire',
self.gf('django.db.models.fields.DateTimeField')(null=True, blank=True),
keep_default=False)
def backwards(self, orm):
# Deleting model 'PaymentHistory'
db.delete_table('profile_paymenthistory')
# Deleting field 'Profile.premium_expire'
db.delete_column('profile_profile', 'premium_expire')
models = {
'auth.group': {
'Meta': {'object_name': 'Group'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
'auth.permission': {
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'profile.paymenthistory': {
'Meta': {'object_name': 'PaymentHistory'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'payment_amount': ('django.db.models.fields.IntegerField', [], {}),
'payment_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'payment_provider': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
},
'profile.profile': {
'Meta': {'object_name': 'Profile'},
'collapsed_folders': ('django.db.models.fields.TextField', [], {'default': "'[]'"}),
'dashboard_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'feed_pane_size': ('django.db.models.fields.IntegerField', [], {'default': '240'}),
'has_found_friends': ('django.db.models.fields.NullBooleanField', [], {'default': 'False', 'null': 'True', 'blank': 'True'}),
'has_setup_feeds': ('django.db.models.fields.NullBooleanField', [], {'default': 'False', 'null': 'True', 'blank': 'True'}),
'has_trained_intelligence': ('django.db.models.fields.NullBooleanField', [], {'default': 'False', 'null': 'True', 'blank': 'True'}),
'hide_getting_started': ('django.db.models.fields.NullBooleanField', [], {'default': 'False', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_premium': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_seen_ip': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}),
'last_seen_on': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'preferences': ('django.db.models.fields.TextField', [], {'default': "'{}'"}),
'premium_expire': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'secret_token': ('django.db.models.fields.CharField', [], {'max_length': '12', 'null': 'True', 'blank': 'True'}),
'send_emails': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'stripe_4_digits': ('django.db.models.fields.CharField', [], {'max_length': '4', 'null': 'True', 'blank': 'True'}),
'stripe_id': ('django.db.models.fields.CharField', [], {'max_length': '24', 'null': 'True', 'blank': 'True'}),
'timezone': ('vendor.timezones.fields.TimeZoneField', [], {'default': "'America/New_York'", 'max_length': '100'}),
'tutorial_finished': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'user': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'profile'", 'unique': 'True', 'to': "orm['auth.User']"}),
'view_settings': ('django.db.models.fields.TextField', [], {'default': "'{}'"})
}
}
complete_apps = ['profile']

View file

@ -1,4 +1,5 @@
import datetime
import stripe
import mongoengine as mongo
from django.db import models
from django.db import IntegrityError
@ -20,11 +21,13 @@ from utils import json_functions as json
from utils.user_functions import generate_secret_token
from vendor.timezones.fields import TimeZoneField
from vendor.paypal.standard.ipn.signals import subscription_signup
from vendor.paypal.standard.ipn.models import PayPalIPN
from zebra.signals import zebra_webhook_customer_subscription_created
class Profile(models.Model):
user = models.OneToOneField(User, unique=True, related_name="profile")
is_premium = models.BooleanField(default=False)
premium_expire = models.DateTimeField(blank=True, null=True)
send_emails = models.BooleanField(default=True)
preferences = models.TextField(default="{}")
view_settings = models.TextField(default="{}")
@ -127,6 +130,8 @@ class Profile(models.Model):
self.is_premium = True
self.save()
self.setup_premium_history()
subs = UserSubscription.objects.filter(user=self.user)
for sub in subs:
sub.active = True
@ -139,6 +144,43 @@ class Profile(models.Model):
self.queue_new_feeds()
logging.user(self.user, "~BY~SK~FW~SBNEW PREMIUM ACCOUNT! WOOHOO!!! ~FR%s subscriptions~SN!" % (subs.count()))
def setup_premium_history(self):
existing_history = PaymentHistory.objects.filter(user=self.user)
print " ---> Deleting existing history: %s payments" % existing_history.count()
existing_history.delete()
# Record Paypal payments
paypal_payments = PayPalIPN.objects.filter(custom=self.user.username,
txn_type='subscr_payment')
for payment in paypal_payments:
PaymentHistory.objects.create(user=self.user,
payment_date=payment.payment_date,
payment_amount=payment.payment_gross,
payment_provider='paypal')
# Record Stripe payments
if self.stripe_id:
stripe.api_key = settings.STRIPE_SECRET
stripe_customer = stripe.Customer.retrieve(self.stripe_id)
stripe_payments = stripe.Charge.all(customer=stripe_customer.id).data
for payment in stripe_payments:
created = datetime.datetime.fromtimestamp(payment.created)
PaymentHistory.objects.create(user=self.user,
payment_date=created,
payment_amount=payment.amount / 100.0,
payment_provider='stripe')
# Calculate last payment date
payment_history = PaymentHistory.objects.filter(user=self.user)
most_recent_payment_date = None
for payment in payment_history:
if not most_recent_payment_date or payment.payment_date > most_recent_payment_date:
most_recent_payment_date = payment.payment_date
if most_recent_payment_date:
self.premium_expire = most_recent_payment_date + datetime.timedelta(year=1)
self.save()
def queue_new_feeds(self, new_feeds=None):
if not new_feeds:
@ -356,4 +398,12 @@ class MSentEmail(mongo.Document):
cls.objects.create(email_type=email_type,
receiver_user_id=receiver_user_id,
sending_user_id=sending_user_id)
class PaymentHistory(models.Model):
user = models.ForeignKey(User, related_name='payments')
payment_date = models.DateTimeField(auto_now=True)
payment_amount = models.IntegerField()
payment_provider = models.CharField(max_length=20)
class Meta:
ordering = ['-payment_date']

View file

@ -81,8 +81,7 @@ class StripeCustomerMixin(object):
class StripeSubscriptionMixin(object):
"""
Provides a property `stripe` that returns an instance of the Stripe module &
additionally adds a property `stripe_subscription` that returns a stripe
Provides a property `stripe_subscription` that returns a stripe
subscription instance.
Your class must have an attribute `stripe_customer` (method or property)
@ -99,9 +98,7 @@ class StripeSubscriptionMixin(object):
class StripePlanMixin(object):
"""
Provides a property `stripe` that returns an instance of the Stripe module &
additionally adds a property `stripe_plan` that returns a stripe plan
instance.
Provides a property `stripe_plan` that returns a stripe plan instance.
Your class must have an attribute `stripe_plan_id` (method or property)
to provide the plan id for the returned instance.
@ -113,9 +110,7 @@ class StripePlanMixin(object):
class StripeInvoiceMixin(object):
"""
Provides a property `stripe` that returns an instance of the Stripe module &
additionally adds a property `stripe_invoice` that returns a stripe invoice
instance.
Provides a property `stripe_invoice` that returns a stripe invoice instance.
Your class must have an attribute `stripe_invoice_id` (method or property)
to provide the invoice id for the returned instance.
@ -128,26 +123,23 @@ class StripeInvoiceMixin(object):
class StripeInvoiceItemMixin(object):
"""
Provides a property `stripe` that returns an instance of the Stripe module &
additionally adds a property `stripe_invoice_item` that returns a stripe
Provides a property `stripe_invoice_item` that returns a stripe
invoice item instance.
Your class must have an attribute `stripe_invoice_item_id` (method or
property) to provide the invoice id for the returned instance.
"""
def _get_stripe_invoice(self):
return stripe.Invoice.retrieve(_get_attr_value(self,
def _get_stripe_invoice_item(self):
return stripe.InvoiceItem.retrieve(_get_attr_value(self,
'stripe_invoice_item_id'))
stripe_invoice = property(_get_stripe_invoice)
stripe_invoice_item = property(_get_stripe_invoice_item)
class StripeChargeMixin(object):
"""
Provides a property `stripe` that returns an instance of the Stripe module &
additionally adds a property `stripe_invoice_item` that returns a stripe
invoice item instance.
Provides a property `stripe_charge` that returns a stripe charge instance.
Your class must have an attribute `stripe_invoice_item_id` (method or
Your class must have an attribute `stripe_charge_id` (method or
property) to provide the invoice id for the returned instance.
"""
def _get_stripe_charge(self):

View file

@ -1,3 +1,4 @@
{{zebra_form.media}}
<script type="text/javascript">
Stripe.setPublishableKey('{{STRIPE_PUBLISHABLE}}');
</script>

View file

@ -29,7 +29,6 @@ class NoNameWidget(object):
class NoNameTextInput(TextInput, NoNameWidget):
def render(self, name, *args, **kwargs):
print name, kwargs
kwargs = self._update_to_noname_class_name(name, kwargs)
return mark_safe(self._strip_name_attr(super(NoNameTextInput, self).render(name, *args, **kwargs), name))