根据用户群限制用户

时间:2020-09-15 12:40:44

标签: python django django-rest-framework

我想基于Saas应用程序(免费,付费)的用户群来限制用户的表单请求,因此,如果自由用户达到其限制,他们将被重定向到另一个页面或显示弹出窗口

我尝试使用节流,但我不知道如何实现条件

settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.BasicAuthentication',
                                        'rest_framework.authentication.TokenAuthentication'],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
    ],
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.UserRateThrottle',
        'contact.throttling.FreeDailyRateThrottle'
    ],
    'DEFAULT_THROTTLE_RATES': {
        'user': '5/second',
        'Free': '200/day'
    }
}

views.py

class FreeDailyRateThrottle(UserRateThrottle):
    scope = 'Free'
    user = request.user
    if user.groups.filter(name=Free).exists() :
        #i'm not sure what to do here!
    else :
        #redirect to a different page



@login_required(login_url='login')
@api_view(['POST'])
@throttle_classes([FreeDailyRateThrottle])
def ad_gen(request):

    context ={}

    if request.method!="POST":
        return render(request , 'Ad Gen.html' , context)

    else:
         // some logic
        return render(request , 'Ad result.html' ,context)

0 个答案:

没有答案