from django.utils.timezone import now from datetime import timedelta from django.core.exceptions import ObjectDoesNotExist from sitemanagement.models import RoutePromotionLog, Pricing from api.models import UserProfile def check_monthly_limit(user, route, action_type): try: month_ago = now() - timedelta(days=30) # получаем профиль пользователя и его тарифный план user_profile = UserProfile.objects.get(user=user) pricing_plan = Pricing.objects.get(plan=user_profile.account_type) # определяем лимит в зависимости от типа действия и тарифного плана 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=month_ago ).count() return actions_count < action_limit except ObjectDoesNotExist: # если профиль или тарифный план не найден, запрещаем действие return False