fix permissions check and add new transaction write action
This commit is contained in:
@@ -1,29 +1,47 @@
|
||||
from django.utils.timezone import now
|
||||
from datetime import timedelta
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from sitemanagement.models import RoutePromotionLog, Pricing
|
||||
from sitemanagement.models import RoutePromotionLog, Pricing, Transactions
|
||||
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)
|
||||
|
||||
# получаем последнюю успешную транзакцию пользователя
|
||||
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=month_ago
|
||||
created_at__gte=subscription_start,
|
||||
created_at__lte=subscription_end
|
||||
).count()
|
||||
|
||||
return actions_count < action_limit
|
||||
|
||||
Reference in New Issue
Block a user