Skip to content

Commit

Permalink
feat: add is_admin_request, deprecate is_admin_site
Browse files Browse the repository at this point in the history
  • Loading branch information
hirotasoshu authored and wannacfuture committed May 14, 2023
1 parent 6e734c1 commit 88233f9
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions axes/handlers/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from abc import ABC, abstractmethod
from typing import Optional
from warnings import warn

from django.urls import reverse
from django.urls.exceptions import NoReverseMatch
Expand Down Expand Up @@ -81,7 +82,7 @@ def is_allowed(self, request, credentials: Optional[dict] = None) -> bool:
and inspiration on some common checks and access restrictions before writing your own implementation.
"""

if self.is_admin_site(request):
if settings.AXES_ONLY_ADMIN_SITE and not self.is_admin_request(request):
return True

if self.is_blacklisted(request, credentials):
Expand Down Expand Up @@ -134,10 +135,41 @@ def is_locked(self, request, credentials: Optional[dict] = None) -> bool:

return False

def is_admin_site(self, request) -> bool:
def get_admin_url(self) -> Optional[str]:
"""
Returns admin url if exists, otherwise returns None
"""
try:
return reverse("admin:index")
except NoReverseMatch:
return None

def is_admin_request(self, request) -> bool:
"""
Checks that request located under admin site
"""
Checks if the request is for admin site.
if hasattr(request, "path"):
admin_url = self.get_admin_url()
return (
admin_url is not None
and re.match(f"^{admin_url}", request.path) is not None
)

return False

def is_admin_site(self, request) -> bool:
"""
Checks if the request is NOT for admin site
if `settings.AXES_ONLY_ADMIN_SITE` is True.
"""
warn(
(
"This method is deprecated and will be removed in future versions. "
"If you looking for method that checks if `request.path` located under "
"admin site, use `is_admin_request` instead."
),
DeprecationWarning,
)
if settings.AXES_ONLY_ADMIN_SITE and hasattr(request, "path"):
try:
admin_url = reverse("admin:index")
Expand Down

0 comments on commit 88233f9

Please sign in to comment.