refactor
This commit is contained in:
@@ -128,3 +128,65 @@ def get_token_expiry(token_type='access'):
|
||||
return now + get_refresh_token_expiry()
|
||||
return now + get_access_token_expiry()
|
||||
|
||||
|
||||
|
||||
|
||||
def validate_account_and_plan(user_or_account):
|
||||
"""
|
||||
Validate account exists and has active plan.
|
||||
Allows trial, active, and pending_payment statuses.
|
||||
|
||||
Args:
|
||||
user_or_account: User or Account instance
|
||||
|
||||
Returns:
|
||||
tuple: (is_valid: bool, error_msg: str or None, http_status: int or None)
|
||||
"""
|
||||
from rest_framework import status
|
||||
from .models import User, Account
|
||||
|
||||
# Extract account from user or use directly
|
||||
if isinstance(user_or_account, User):
|
||||
try:
|
||||
account = getattr(user_or_account, 'account', None)
|
||||
except Exception:
|
||||
account = None
|
||||
elif isinstance(user_or_account, Account):
|
||||
account = user_or_account
|
||||
else:
|
||||
return (False, 'Invalid object type', status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Check account exists
|
||||
if not account:
|
||||
return (
|
||||
False,
|
||||
'Account not configured for this user. Please contact support.',
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
# Check account status - allow trial, active, pending_payment
|
||||
# Block only suspended and cancelled
|
||||
if hasattr(account, 'status') and account.status in ['suspended', 'cancelled']:
|
||||
return (
|
||||
False,
|
||||
f'Account is {account.status}. Please contact support.',
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
# Check plan exists and is active
|
||||
plan = getattr(account, 'plan', None)
|
||||
if not plan:
|
||||
return (
|
||||
False,
|
||||
'No subscription plan assigned. Visit igny8.com/pricing to subscribe.',
|
||||
status.HTTP_402_PAYMENT_REQUIRED
|
||||
)
|
||||
|
||||
if hasattr(plan, 'is_active') and not plan.is_active:
|
||||
return (
|
||||
False,
|
||||
'Active subscription required. Visit igny8.com/pricing to subscribe.',
|
||||
status.HTTP_402_PAYMENT_REQUIRED
|
||||
)
|
||||
|
||||
return (True, None, None)
|
||||
|
||||
Reference in New Issue
Block a user