Files
slump-x-land-studio/catalog/chapter_access.py
T

34 lines
1.4 KiB
Python

from django.conf import settings
from django.db.models import Q
from .fans import live
from .models import Chapter
FREE_ARCHIVE_SIZE = 3
def rolling_chapter_ids():
return live(Chapter).order_by("-publish_at", "-number").values("pk")[:FREE_ARCHIVE_SIZE]
def stripe_live_mode():
return settings.SLX_STRIPE_SECRET_KEY.startswith(('sk_live_', 'rk_live_'))
def full_chapters(user):
from .membership import active_tier
from .models import ChapterPurchase
from shop.models import Order, OrderItem
chapters = live(Chapter)
if not user.is_authenticated or not user.is_active:
return chapters.filter(pk__in=rolling_chapter_ids())
if (user.is_staff and user.has_perm('catalog.view_chapter')) or active_tier(user):
return chapters
paid = ChapterPurchase.objects.filter(user=user, mode='sandbox' if settings.SLX_PAYPAL_MODE == 'sandbox' else 'live',
paid_at__isnull=False, revoked=False)
orders = OrderItem.objects.filter(order__user=user, order__gateway=Order.Gateway.STRIPE,
order__stripe_live=stripe_live_mode(), order__status__in=[
Order.Status.PAID, Order.Status.SHIPPED, Order.Status.DELIVERED])
return chapters.filter(Q(pk__in=rolling_chapter_ids()) | Q(pk__in=paid.values('chapter_id')) | Q(pk__in=orders.values('chapter_id')))