59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
from django.shortcuts import get_object_or_404, render
|
|
from django.utils import timezone
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.http import require_safe
|
|
|
|
from .models import FanMembership, MemberPost
|
|
|
|
|
|
def active_tier(user):
|
|
from .chapter_access import stripe_live_mode
|
|
from shop.models import Order, Subscription
|
|
|
|
if not user.is_authenticated or not user.is_active:
|
|
return 0
|
|
now = timezone.now()
|
|
manual = FanMembership.objects.filter(user=user, subscriber_until__gt=now).values_list('member_tier', flat=True).first()
|
|
stripe = Subscription.objects.filter(user=user, gateway=Order.Gateway.STRIPE, stripe_live=stripe_live_mode(),
|
|
status=Subscription.Status.ACTIVE, current_period_end__gt=now,
|
|
entitlement_tier__isnull=False).order_by('-entitlement_tier').values_list('entitlement_tier', flat=True).first()
|
|
return max(manual or 0, stripe or 0)
|
|
|
|
|
|
def accessible_posts(user):
|
|
if not user.is_authenticated or not user.is_active:
|
|
return MemberPost.objects.none()
|
|
if user.is_staff and user.has_perm('catalog.view_memberpost'):
|
|
return MemberPost.objects.all()
|
|
now = timezone.now()
|
|
tier = active_tier(user)
|
|
return MemberPost.objects.filter(published=True, publish_at__lte=now, required_tier__lte=tier or 0)
|
|
|
|
|
|
@never_cache
|
|
@require_safe
|
|
def index(request):
|
|
from shop.models import Product
|
|
plans = Product.objects.filter(active=True, kind=Product.Kind.SUBSCRIPTION, member_tier__isnull=False)
|
|
return render(request, 'membership.html', {'posts': accessible_posts(request.user), 'plans': plans, 'comparison_rows': comparison_rows(plans)})
|
|
|
|
|
|
@never_cache
|
|
@require_safe
|
|
def detail(request, pk):
|
|
return render(request, 'member-post.html', {'post': get_object_or_404(accessible_posts(request.user), pk=pk)})
|
|
|
|
|
|
def comparison_rows(plans):
|
|
rows = {}
|
|
plans = list(plans)
|
|
for index, plan in enumerate(plans):
|
|
defaults = ['Published chapter library: Full access']
|
|
if plan.member_tier >= 2:
|
|
defaults.append('Draft pages and process notes: Included')
|
|
for line in (plan.comparison.splitlines() if plan.comparison else defaults):
|
|
title, sep, value = line.partition(':')
|
|
if sep and title.strip() and value.strip():
|
|
rows.setdefault(title.strip(), ['—'] * len(plans))[index] = value.strip()
|
|
return list(rows.items())
|