Files
slump-x-land-studio/shop/models.py
T

159 lines
7.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import uuid
from decimal import Decimal
from django.conf import settings
from django.core.validators import MinValueValidator
from django.db import models
from django.utils import timezone
class Product(models.Model):
class Kind(models.TextChoices):
PHYSICAL = 'physical', 'Physical Product'
DIGITAL = 'digital', 'Digital Download'
SUBSCRIPTION = 'subscription', 'Recurring Subscription'
class Interval(models.TextChoices):
MONTH = 'month', 'Monthly'
YEAR = 'year', 'Yearly'
title = models.CharField(max_length=160)
slug = models.SlugField(max_length=180, unique=True)
kind = models.CharField(max_length=20, choices=Kind.choices, default=Kind.PHYSICAL)
summary = models.CharField(max_length=280, blank=True)
description = models.TextField(blank=True)
image = models.ImageField(upload_to='shop/products/', blank=True)
price_usd = models.DecimalField(max_digits=9, decimal_places=2, validators=[MinValueValidator(Decimal('0.50'))])
active = models.BooleanField(default=True)
comparison = models.TextField(blank=True, max_length=3000,
help_text='One benefit per line, using Feature: Details. Used on the membership comparison chart.')
featured = models.BooleanField(default=False)
sample_chapter = models.ForeignKey('catalog.Chapter', null=True, blank=True, on_delete=models.SET_NULL,
related_name='shop_editions')
member_tier = models.PositiveSmallIntegerField(choices=[(1, 'Early Access'), (2, 'Studio Vault'), (3, 'Collector’s Circle')],
null=True, blank=True)
verified_by_seller = models.BooleanField(default=False, help_text="Show the Verified by Seller badge on this listing.")
position = models.PositiveIntegerField(default=0)
# Physical inventory & shipping
stock = models.PositiveIntegerField(default=0, help_text='Ignored for digital and subscription items.')
track_stock = models.BooleanField(default=True)
shipping_usd = models.DecimalField(max_digits=7, decimal_places=2, default=Decimal('0.00'),
help_text='Flat shipping fee added once if order contains physical items.')
# Digital asset (served privately via authenticated/order-token view, never public /media/)
digital_file = models.FileField(upload_to='shop/private_digital/', blank=True,
help_text='Private file unlocked after purchase.')
# Subscription gateway plan IDs
billing_interval = models.CharField(max_length=10, choices=Interval.choices, default=Interval.MONTH)
stripe_price_id = models.CharField(max_length=120, blank=True,
help_text='Optional Stripe recurring Price ID (price_...). If blank, created dynamically.')
paypal_plan_id = models.CharField(max_length=120, blank=True,
help_text='Required for PayPal recurring subscriptions (P-...).')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['position', '-created_at']
def __str__(self):
return f'{self.title} (${self.price_usd})'
@property
def in_stock(self):
if self.kind != self.Kind.PHYSICAL or not self.track_stock:
return True
return self.stock > 0
class Order(models.Model):
class Status(models.TextChoices):
PENDING = 'pending', 'Pending Payment'
PAID = 'paid', 'Paid / Processing'
SHIPPED = 'shipped', 'Shipped'
DELIVERED = 'delivered', 'Delivered'
REFUNDED = 'refunded', 'Refunded'
CANCELED = 'canceled', 'Canceled'
class Gateway(models.TextChoices):
STRIPE = 'stripe', 'Stripe'
PAYPAL = 'paypal', 'PayPal'
order_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name='shop_orders')
email = models.EmailField()
full_name = models.CharField(max_length=160, blank=True)
status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
gateway = models.CharField(max_length=20, choices=Gateway.choices)
subtotal_usd = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))
shipping_usd = models.DecimalField(max_digits=8, decimal_places=2, default=Decimal('0.00'))
total_usd = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))
# Shipping details (populated for physical orders)
shipping_line1 = models.CharField(max_length=200, blank=True)
shipping_line2 = models.CharField(max_length=200, blank=True)
shipping_city = models.CharField(max_length=120, blank=True)
shipping_state = models.CharField(max_length=120, blank=True)
shipping_postal = models.CharField(max_length=40, blank=True)
shipping_country = models.CharField(max_length=2, blank=True)
tracking_number = models.CharField(max_length=120, blank=True)
# Gateway tracking
gateway_session_id = models.CharField(max_length=200, blank=True, db_index=True)
gateway_payment_id = models.CharField(max_length=200, blank=True)
stripe_live = models.BooleanField(null=True, editable=False)
paid_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-created_at']
def __str__(self):
return f'Order {str(self.order_id)[:8]} ({self.get_status_display()})'
@property
def has_physical(self):
return self.items.filter(product__kind=Product.Kind.PHYSICAL).exists()
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='items')
product = models.ForeignKey(Product, on_delete=models.PROTECT)
chapter = models.ForeignKey('catalog.Chapter', null=True, blank=True, on_delete=models.PROTECT)
title_snapshot = models.CharField(max_length=160)
unit_price_usd = models.DecimalField(max_digits=9, decimal_places=2)
quantity = models.PositiveIntegerField(default=1)
@property
def line_total(self):
return self.unit_price_usd * self.quantity
class Subscription(models.Model):
class Status(models.TextChoices):
PENDING = 'pending', 'Pending'
ACTIVE = 'active', 'Active'
PAST_DUE = 'past_due', 'Past Due'
CANCELED = 'canceled', 'Canceled'
sub_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name='shop_subscriptions')
email = models.EmailField()
product = models.ForeignKey(Product, on_delete=models.PROTECT)
gateway = models.CharField(max_length=20, choices=Order.Gateway.choices)
gateway_sub_id = models.CharField(max_length=200, blank=True, db_index=True)
gateway_customer_id = models.CharField(max_length=200, blank=True)
status = models.CharField(max_length=20, choices=Status.choices, default=Status.PENDING)
stripe_live = models.BooleanField(null=True, editable=False)
entitlement_tier = models.PositiveSmallIntegerField(null=True, editable=False)
current_period_end = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-created_at']
def __str__(self):
return f'{self.email} — {self.product.title} ({self.status})'