2024-05-10 23:15:29 +08:00
|
|
|
from django.db import models
|
|
|
|
from accounts.models import CustomUser
|
|
|
|
from user_groups.models import UserGroup
|
|
|
|
from django.utils.timezone import now
|
|
|
|
|
|
|
|
|
|
|
|
class StripePrice(models.Model):
|
|
|
|
annual = models.BooleanField(default=False)
|
|
|
|
stripe_price_id = models.CharField(max_length=100)
|
|
|
|
price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
|
|
|
|
currency = models.CharField(max_length=20)
|
|
|
|
lookup_key = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
prorated = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.annual:
|
|
|
|
return f"{self.price}{self.currency}/year"
|
|
|
|
else:
|
|
|
|
return f"{self.price}{self.currency}/month"
|
|
|
|
|
|
|
|
|
|
|
|
class SubscriptionPlan(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
description = models.TextField(max_length=1024, null=True)
|
|
|
|
stripe_product_id = models.CharField(max_length=100)
|
|
|
|
annual_price = models.ForeignKey(
|
2024-10-30 22:09:58 +08:00
|
|
|
StripePrice, on_delete=models.SET_NULL, related_name="annual_plan", null=True
|
|
|
|
)
|
2024-05-10 23:15:29 +08:00
|
|
|
monthly_price = models.ForeignKey(
|
2024-10-30 22:09:58 +08:00
|
|
|
StripePrice, on_delete=models.SET_NULL, related_name="monthly_plan", null=True
|
|
|
|
)
|
2024-05-10 23:15:29 +08:00
|
|
|
group_exclusive = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.name}"
|
|
|
|
|
|
|
|
|
|
|
|
# Model for User Subscriptions
|
|
|
|
|
|
|
|
|
|
|
|
class UserSubscription(models.Model):
|
|
|
|
user = models.ForeignKey(
|
2024-10-30 22:09:58 +08:00
|
|
|
CustomUser, on_delete=models.CASCADE, blank=True, null=True
|
|
|
|
)
|
2024-05-10 23:15:29 +08:00
|
|
|
user_group = models.ForeignKey(
|
2024-10-30 22:09:58 +08:00
|
|
|
UserGroup, on_delete=models.CASCADE, blank=True, null=True
|
|
|
|
)
|
2024-05-10 23:15:29 +08:00
|
|
|
subscription = models.ForeignKey(
|
2024-10-30 22:09:58 +08:00
|
|
|
SubscriptionPlan, on_delete=models.SET_NULL, blank=True, null=True
|
|
|
|
)
|
2024-05-10 23:15:29 +08:00
|
|
|
stripe_id = models.CharField(max_length=100)
|
|
|
|
date = models.DateTimeField(default=now, editable=False)
|
|
|
|
valid = models.BooleanField()
|
|
|
|
annual = models.BooleanField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.user:
|
2024-10-30 22:09:58 +08:00
|
|
|
return f"Subscription {self.subscription.name} for {self.user}"
|
2024-05-10 23:15:29 +08:00
|
|
|
else:
|
2024-10-30 22:09:58 +08:00
|
|
|
return f"Subscription {self.subscription.name} for {self.user_group}"
|