50 lines
2.3 KiB
Python
50 lines
2.3 KiB
Python
from django.utils.timezone import now
|
||
from datetime import timedelta
|
||
from django.core.exceptions import ObjectDoesNotExist
|
||
from sitemanagement.models import RoutePromotionLog, Pricing, Transactions
|
||
from api.models import UserProfile
|
||
|
||
def check_monthly_limit(user, route, action_type):
|
||
try:
|
||
# получаем профиль пользователя и его тарифный план
|
||
user_profile = UserProfile.objects.get(user=user)
|
||
pricing_plan = Pricing.objects.get(plan=user_profile.account_type)
|
||
|
||
# получаем последнюю успешную транзакцию пользователя
|
||
last_transaction = Transactions.objects.filter(
|
||
user=user,
|
||
plan=pricing_plan,
|
||
status='success' # предполагаем, что успешные транзакции имеют статус 'success'
|
||
).order_by('-created_at').first()
|
||
|
||
if not last_transaction:
|
||
return False
|
||
|
||
# определяем период действия подписки
|
||
subscription_start = last_transaction.created_at
|
||
subscription_period = timedelta(hours=pricing_plan.duration_hours)
|
||
subscription_end = subscription_start + subscription_period
|
||
|
||
# проверяем, не истекла ли подписка
|
||
if now() > subscription_end:
|
||
return False
|
||
|
||
# определяем лимит в зависимости от типа действия и тарифного плана
|
||
if action_type == 'highlight':
|
||
action_limit = pricing_plan.highlight_limit
|
||
else: # rising
|
||
action_limit = pricing_plan.rising_limit
|
||
|
||
# проверяем количество действий за текущий период подписки
|
||
actions_count = RoutePromotionLog.objects.filter(
|
||
user=user,
|
||
route=route,
|
||
action_type=action_type,
|
||
created_at__gte=subscription_start,
|
||
created_at__lte=subscription_end
|
||
).count()
|
||
|
||
return actions_count < action_limit
|
||
except ObjectDoesNotExist:
|
||
# если профиль или тарифный план не найден, запрещаем действие
|
||
return False |