Implement subscription management and ordering
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
import logging
|
||||
|
||||
import stripe
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
from core.lib.customers import get_or_create, update_customer_fields
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Plan(models.Model):
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
description = models.CharField(max_length=1024, null=True, blank=True)
|
||||
cost = models.IntegerField()
|
||||
product_id = models.UUIDField(null=True, blank=True)
|
||||
product_id = models.CharField(max_length=255, unique=True, null=True, blank=True)
|
||||
image = models.CharField(max_length=1024, null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
@@ -23,11 +28,11 @@ class User(AbstractUser):
|
||||
last_payment = models.DateTimeField(null=True, blank=True)
|
||||
paid = models.BooleanField(null=True, blank=True)
|
||||
plans = models.ManyToManyField(Plan, blank=True)
|
||||
email = models.EmailField(unique=True)
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._original = self
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._original = self
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""
|
||||
@@ -48,8 +53,19 @@ def __init__(self, *args, **kwargs):
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
if self.stripe_id:
|
||||
stripe.Customer.delete(self.stripe_id)
|
||||
logger.info(f"Deleted Stripe customer {self.stripe_id}")
|
||||
super().delete(*args, **kwargs)
|
||||
|
||||
def has_plan(self, plan):
|
||||
if not self.paid: # We can't have any plans if we haven't paid
|
||||
return False
|
||||
plan_list = [plan.name for plan in self.plans.all()]
|
||||
return plan in plan_list
|
||||
|
||||
|
||||
class Session(models.Model):
|
||||
email = models.EmailField()
|
||||
session = models.CharField(max_length=255)
|
||||
|
||||
Reference in New Issue
Block a user