2016-07-19 06:07:49 +00:00
|
|
|
from django.db import models
|
2016-07-27 05:08:45 +00:00
|
|
|
from membership.models import StripeCustomer, CustomUser
|
2016-07-19 06:07:49 +00:00
|
|
|
from utils.models import BillingAddress
|
|
|
|
|
|
|
|
|
2016-07-27 05:08:45 +00:00
|
|
|
class DonatorStatus(models.Model):
|
|
|
|
ACTIVE = 'active'
|
|
|
|
CANCELED = 'canceled'
|
|
|
|
|
|
|
|
STATUS_CHOICES = (
|
|
|
|
(ACTIVE, 'Active'),
|
|
|
|
(CANCELED, 'Canceled')
|
|
|
|
)
|
|
|
|
user = models.OneToOneField(CustomUser)
|
|
|
|
status = models.CharField(choices=STATUS_CHOICES, max_length=10, default=ACTIVE)
|
|
|
|
|
2016-07-29 05:17:34 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s - %s " % (self.user.email, self.status)
|
|
|
|
|
2016-07-27 05:08:45 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, user):
|
|
|
|
cls.objects.get_or_create(user=user)
|
2016-07-19 06:07:49 +00:00
|
|
|
|
2016-08-05 06:00:44 +00:00
|
|
|
def set_active(self):
|
|
|
|
self.status = DonatorStatus.ACTIVE
|
|
|
|
self.save()
|
|
|
|
|
2016-07-19 06:07:49 +00:00
|
|
|
|
|
|
|
class Donation(models.Model):
|
|
|
|
|
|
|
|
donation = models.FloatField()
|
2016-07-27 05:08:45 +00:00
|
|
|
donator = models.ForeignKey(StripeCustomer)
|
2016-07-19 06:07:49 +00:00
|
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
billing_address = models.ForeignKey(BillingAddress)
|
|
|
|
last4 = models.CharField(max_length=4)
|
|
|
|
cc_brand = models.CharField(max_length=10)
|
|
|
|
stripe_charge_id = models.CharField(max_length=100, null=True)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, data):
|
|
|
|
obj = cls.objects.create(**data)
|
|
|
|
return obj
|
2016-07-27 05:08:45 +00:00
|
|
|
|
2016-07-29 05:17:34 +00:00
|
|
|
@classmethod
|
|
|
|
def get_total_donations_amount(cls):
|
|
|
|
return sum(cls.objects.all().values_list('donation', flat=True))
|
|
|
|
|
2016-07-27 05:08:45 +00:00
|
|
|
def set_stripe_charge(self, stripe_charge):
|
|
|
|
self.stripe_charge_id = stripe_charge.id
|
|
|
|
self.last4 = stripe_charge.source.last4
|
|
|
|
self.cc_brand = stripe_charge.source.brand
|
|
|
|
self.save
|