From 0365d17095575351f94dea53fa86f30f1ba1922d Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Wed, 8 Apr 2026 11:23:11 +0330 Subject: [PATCH] conflict error --- apps/core/views.py | 1 + apps/gooyal_oauth2/tests.py | 3 ++- apps/gooyal_oauth2/throttling.py | 15 +++------------ utils/exceptions.py | 9 ++++++++- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/core/views.py b/apps/core/views.py index 66a1bc4..a052e5e 100644 --- a/apps/core/views.py +++ b/apps/core/views.py @@ -75,6 +75,7 @@ class StatusView(APIView): class HealthcheckView(APIView): permission_classes = [AllowAny] + def get(self, request, format=None): data = { "is_healthy": True diff --git a/apps/gooyal_oauth2/tests.py b/apps/gooyal_oauth2/tests.py index 4ff6f17..f2433c8 100644 --- a/apps/gooyal_oauth2/tests.py +++ b/apps/gooyal_oauth2/tests.py @@ -9,6 +9,7 @@ from django.test import TestCase from django.urls import reverse from django.utils import timezone from oauth2_provider.models import get_access_token_model, get_application_model +from rest_framework import status from rest_framework.test import APIClient, APITestCase @@ -120,7 +121,7 @@ class GooyalOAuth2Tests(APITestCase): self.assertEqual(response.status_code, 200) response = self.client.get(reverse("core:status"), HTTP_AUTHORIZATION=auth_2) - self.assertEqual(response.status_code, 503) + self.assertEqual(response.status_code, status.HTTP_409_CONFLICT) self.application.max_allowed_session = 0 self.application.save() diff --git a/apps/gooyal_oauth2/throttling.py b/apps/gooyal_oauth2/throttling.py index 6f5414a..03c5e70 100644 --- a/apps/gooyal_oauth2/throttling.py +++ b/apps/gooyal_oauth2/throttling.py @@ -1,6 +1,8 @@ from rest_framework.throttling import UserRateThrottle from apps.gooyal_oauth2.models import AccessToken +from utils.exceptions import Conflict + def get_application(request): try: @@ -31,18 +33,7 @@ class TokenLimitThrottle: if request.auth.token in active_tokens: return True else: - False - # raise ServiceUnavailable(code='max_allowed_session_reached') + raise Conflict(code='max_allowed_session_reached') else: return True - - def wait(self): - """ - Optionally, return a recommended number of seconds to wait before - the next request. - """ - return None - - -from rest_framework.exceptions import Throttled \ No newline at end of file diff --git a/utils/exceptions.py b/utils/exceptions.py index d85b168..8c9c36d 100644 --- a/utils/exceptions.py +++ b/utils/exceptions.py @@ -82,7 +82,7 @@ from django.utils.translation import gettext_lazy as _ class UnprocessableEntity(APIException): - status_code = 422 + status_code = status.HTTP_422_UNPROCESSABLE_ENTITY default_detail = 'The request was well-formed but cannot be processed due to semantic errors.' default_code = 'unprocessable_entity' @@ -91,3 +91,10 @@ class ServiceUnavailable(APIException): default_ = _('SERVICE_UNAVAILABLE') default_code = 'service_unavailable' default_detail = 'Service Unavailable' + + +class Conflict(APIException): + status_code = status.HTTP_409_CONFLICT + default_ = _('CONFLICT') + default_code = 'conflict' + default_detail = 'Conflict'