conflict error
This commit is contained in:
parent
7256578ec7
commit
0e9ca49ca0
5 changed files with 15 additions and 2 deletions
|
|
@ -21,7 +21,7 @@ class Migration(migrations.Migration):
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='application',
|
model_name='application',
|
||||||
name='max_allowed_session',
|
name='max_allowed_session',
|
||||||
field=models.PositiveIntegerField(default=1),
|
field=models.PositiveIntegerField(default=0),
|
||||||
),
|
),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='application',
|
model_name='application',
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ class Application(AbstractApplication, BaseModel):
|
||||||
allowed_scope = models.TextField(blank=True)
|
allowed_scope = models.TextField(blank=True)
|
||||||
|
|
||||||
avatar = models.ImageField(upload_to='avatars', null=True, blank=True)
|
avatar = models.ImageField(upload_to='avatars', null=True, blank=True)
|
||||||
max_allowed_session = models.PositiveIntegerField(default=1)
|
max_allowed_session = models.PositiveIntegerField(default=0)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,9 @@ class TokenLimitThrottle:
|
||||||
if max_allowed_session >= session_count:
|
if max_allowed_session >= session_count:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
from utils.exceptions import Conflict
|
||||||
|
raise Conflict(code='max_allowed_session_reached')
|
||||||
|
|
||||||
active_tokens = AccessToken.objects.filter(
|
active_tokens = AccessToken.objects.filter(
|
||||||
application=application, user=request.user
|
application=application, user=request.user
|
||||||
).order_by("created")[:max_allowed_session].values_list("token", flat=True)
|
).order_by("created")[:max_allowed_session].values_list("token", flat=True)
|
||||||
|
|
@ -37,3 +40,8 @@ class TokenLimitThrottle:
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def wait(self):
|
||||||
|
pass
|
||||||
|
|
|
||||||
|
|
@ -187,6 +187,8 @@ class UserSessionListView(generics.ListAPIView):
|
||||||
serializer_class = SessionSerializer
|
serializer_class = SessionSerializer
|
||||||
filter_backends = (DjangoFilterBackend,)
|
filter_backends = (DjangoFilterBackend,)
|
||||||
|
|
||||||
|
max_allowed_session = 0
|
||||||
|
|
||||||
required_alternate_scopes = {
|
required_alternate_scopes = {
|
||||||
"GET": [["accounts.account:retrieve"]],
|
"GET": [["accounts.account:retrieve"]],
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@ def exception_handler(exc, context):
|
||||||
# در فایل middleware.py
|
# در فایل middleware.py
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
|
||||||
class ErrorMiddleware:
|
class ErrorMiddleware:
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
@ -76,6 +77,7 @@ class ErrorMiddleware:
|
||||||
status=500
|
status=500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
@ -86,6 +88,7 @@ class UnprocessableEntity(APIException):
|
||||||
default_detail = 'The request was well-formed but cannot be processed due to semantic errors.'
|
default_detail = 'The request was well-formed but cannot be processed due to semantic errors.'
|
||||||
default_code = 'unprocessable_entity'
|
default_code = 'unprocessable_entity'
|
||||||
|
|
||||||
|
|
||||||
class ServiceUnavailable(APIException):
|
class ServiceUnavailable(APIException):
|
||||||
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||||
default_ = _('SERVICE_UNAVAILABLE')
|
default_ = _('SERVICE_UNAVAILABLE')
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue