diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index ab09f68..795069f 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -1,4 +1,4 @@ -name: django-limits +name: django-ratelimiter on: [push] @@ -29,5 +29,5 @@ jobs: uses: codecov/codecov-action@v4.0.1 with: token: ${{ secrets.CODECOV_TOKEN }} - slug: andriykohut/django-limits + slug: andriykohut/django-ratelimiter diff --git a/Makefile b/Makefile index 6a47f38..983bbcb 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ lint: poetry run mypy . test: run-backends - poetry run pytest --cov django_limits + poetry run pytest --cov django_ratelimiter test-ci: - poetry run pytest --cov django_limits --cov-report=xml + poetry run pytest --cov django_ratelimiter --cov-report=xml html-cov: test poetry run coverage html diff --git a/README.md b/README.md index 31bde9f..a188285 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# django-limits +# django-ratelimiter TBD @@ -6,7 +6,7 @@ TBD ```py from django.http import HttpRequest, HttpResponse -from django_limits import ratelimit +from django_ratelimiter import ratelimit from limits.storage import RedisStorage # defaults, limit all requests diff --git a/django_limits/__init__.py b/django_limits/__init__.py deleted file mode 100644 index fb53527..0000000 --- a/django_limits/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from django_limits.decorator import ratelimit -from django_limits.storage import CacheStorage - -__all__ = ["ratelimit", "CacheStorage"] diff --git a/django_ratelimiter/__init__.py b/django_ratelimiter/__init__.py new file mode 100644 index 0000000..804bded --- /dev/null +++ b/django_ratelimiter/__init__.py @@ -0,0 +1,4 @@ +from django_ratelimiter.decorator import ratelimit +from django_ratelimiter.storage import CacheStorage + +__all__ = ["ratelimit", "CacheStorage"] diff --git a/django_limits/decorator.py b/django_ratelimiter/decorator.py similarity index 91% rename from django_limits/decorator.py rename to django_ratelimiter/decorator.py index 18df97f..8925541 100644 --- a/django_limits/decorator.py +++ b/django_ratelimiter/decorator.py @@ -7,8 +7,8 @@ from limits.strategies import STRATEGIES, RateLimiter from limits.storage import Storage from limits import parse -from django_limits.storage import CacheStorage -from django_limits.types import ViewFunc, P +from django_ratelimiter.storage import CacheStorage +from django_ratelimiter.types import ViewFunc, P def build_identifiers( @@ -34,7 +34,7 @@ def get_rate_limiter(strategy: str, storage: Storage | None = None) -> RateLimit f"Unknown strategy {strategy}, must be one of {STRATEGIES.keys()}" ) if not storage: - default_storage = getattr(settings, "DJANGO_LIMITS_CACHE", None) + default_storage = getattr(settings, "DJANGO_RATELIMITER_CACHE", None) storage = ( default_storage if isinstance(default_storage, Storage) else CacheStorage() ) @@ -58,7 +58,7 @@ def wrapper( ) -> HttpResponse: rate_str = rate(request) if callable(rate) else rate parsed_rate = parse(rate_str) - if getattr(settings, "DJANGO_LIMITS_ENABLE", True) and ( + if getattr(settings, "DJANGO_RATELIMITER_CACHE", True) and ( not methods or request.method in methods ): identifiers = build_identifiers(func, methods) diff --git a/django_limits/storage.py b/django_ratelimiter/storage.py similarity index 92% rename from django_limits/storage.py rename to django_ratelimiter/storage.py index 3247483..d9347fe 100644 --- a/django_limits/storage.py +++ b/django_ratelimiter/storage.py @@ -16,7 +16,7 @@ def __init__( **options: float | str | bool, ) -> None: self.cache_name = cache or cast( - str, getattr(settings, "DJANGO_LIMITS_CACHE", "default") + str, getattr(settings, "DJANGO_RATELIMITER_CACHE", "default") ) self.cache: BaseCache = caches[self.cache_name] super().__init__(uri=None, wrap_exceptions=wrap_exceptions, **options) @@ -47,7 +47,7 @@ def get_expiry(self, key: str) -> int: def check(self) -> bool: try: - self.cache.get("django-limits-check") + self.cache.get("django-ratelimiter-check") return True except: # noqa: E722 return False diff --git a/django_limits/types.py b/django_ratelimiter/types.py similarity index 100% rename from django_limits/types.py rename to django_ratelimiter/types.py diff --git a/pyproject.toml b/pyproject.toml index d4e93fa..1664e4a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.poetry] -name = "django-limits" +name = "django-ratelimiter" version = "0.1.0" description = "Rate-limiting for django" authors = ["Andrii Kohut "] diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 1ab3da6..60da29a 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -10,8 +10,8 @@ from limits import parse from limits.storage import MemoryStorage, RedisStorage, MemcachedStorage -from django_limits import ratelimit -from django_limits.decorator import get_rate_limiter +from django_ratelimiter import ratelimit +from django_ratelimiter.decorator import get_rate_limiter @pytest.fixture diff --git a/tests/test_storage.py b/tests/test_storage.py index 606b182..e64cda6 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -3,7 +3,7 @@ import uuid import pytest -from django_limits.storage import CacheStorage +from django_ratelimiter.storage import CacheStorage @pytest.mark.parametrize(