set permission check depends on membership
This commit is contained in:
@@ -227,7 +227,16 @@ class PricingSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Pricing
|
||||
fields = ['plan', 'price', 'features', 'isPopular', 'duration', 'duration_hours']
|
||||
fields = [
|
||||
'plan',
|
||||
'price',
|
||||
'features',
|
||||
'isPopular',
|
||||
'duration',
|
||||
'duration_hours',
|
||||
'highlight_limit',
|
||||
'rising_limit'
|
||||
]
|
||||
|
||||
def get_features(self, obj):
|
||||
return list(obj.features.values_list('name', flat=True))
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 5.2.1 on 2025-05-30 10:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('sitemanagement', '0016_feature_pricing_features_delete_membershipfeature'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='pricing',
|
||||
name='highlight_limit',
|
||||
field=models.IntegerField(default=5, verbose_name='Лимит выделений в месяц'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='pricing',
|
||||
name='rising_limit',
|
||||
field=models.IntegerField(default=5, verbose_name='Лимит поднятий в месяц'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 5.2.1 on 2025-05-30 10:17
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('sitemanagement', '0017_pricing_highlight_limit_pricing_rising_limit'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='pricing',
|
||||
name='highlight_limit',
|
||||
field=models.IntegerField(default=5, null=True, verbose_name='Лимит выделений в месяц'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='pricing',
|
||||
name='rising_limit',
|
||||
field=models.IntegerField(default=5, null=True, verbose_name='Лимит поднятий в месяц'),
|
||||
),
|
||||
]
|
||||
@@ -67,6 +67,8 @@ class Pricing(models.Model):
|
||||
duration = models.CharField(default='7 дней', verbose_name='Длительность подписки (в днях)')
|
||||
duration_hours = models.IntegerField(default=168, verbose_name='Длительность подписки (в часах)')
|
||||
features = models.ManyToManyField('Feature', related_name='pricing_plans', verbose_name='Свойства')
|
||||
highlight_limit = models.IntegerField(default=5, null=True, verbose_name='Лимит выделений в месяц')
|
||||
rising_limit = models.IntegerField(default=5, null=True, verbose_name='Лимит поднятий в месяц')
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Тарифный план'
|
||||
|
||||
Reference in New Issue
Block a user