set permission check depends on membership

This commit is contained in:
2025-05-30 13:44:38 +03:00
parent dc09349cb6
commit 365fc0f495
5 changed files with 86 additions and 11 deletions

View File

@@ -1,14 +1,32 @@
from django.utils.timezone import now
from datetime import timedelta
from sitemanagement.models import RoutePromotionLog
MAX_ACTIONS_PER_MONTH = 5
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):
month_ago = now() - timedelta(days=30)
return RoutePromotionLog.objects.filter(
user=user,
route=route,
action_type=action_type,
created_at__gte=month_ago
).count() < MAX_ACTIONS_PER_MONTH
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