-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Settings module + Browsable API and debug settings #84
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,44 +8,32 @@ def pytest_configure(): | |
from django.conf import settings | ||
|
||
settings.configure( | ||
SECRET_KEY="secret", | ||
DEBUG=True, | ||
INSTALLED_APPS=[ | ||
"django.contrib.admin", | ||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
"django.contrib.sessions", | ||
"django.contrib.messages", | ||
"django.contrib.staticfiles", | ||
"tests", | ||
"worf", | ||
], | ||
MIDDLEWARE=[ | ||
"django.middleware.common.CommonMiddleware", | ||
"django.contrib.sessions.middleware.SessionMiddleware", | ||
"django.contrib.auth.middleware.AuthenticationMiddleware", | ||
"django.contrib.messages.middleware.MessageMiddleware", | ||
], | ||
ROOT_URLCONF="tests.urls", | ||
DATABASES={ | ||
"default": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": "db.sqlite3", | ||
} | ||
}, | ||
INSTALLED_APPS=[ | ||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
"tests", | ||
"worf", | ||
], | ||
PASSWORD_HASHERS=["django.contrib.auth.hashers.MD5PasswordHasher"], | ||
ROOT_URLCONF="tests.urls", | ||
TEMPLATES = [ | ||
{ | ||
"BACKEND": "django.template.backends.django.DjangoTemplates", | ||
"APP_DIRS": True, | ||
}, | ||
], | ||
TIME_ZONE="UTC", | ||
USE_I18N=True, | ||
USE_L10N=True, | ||
USE_I18N=False, | ||
USE_L10N=False, | ||
Comment on lines
-13
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stripped some of this down given it doesn't matter and/or saves on cpu. |
||
USE_TZ=True, | ||
STATIC_URL="/static/", | ||
WORF_API_NAME="Test API" | ||
WORF_API_NAME="Test API", | ||
WORF_DEBUG=True, | ||
) | ||
|
||
django.setup() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from worf.conf import settings | ||
|
||
|
||
def test_settings(): | ||
assert settings.WORF_API_NAME == "Test API" | ||
assert settings.WORF_API_ROOT == "/api/" | ||
assert settings.WORF_BROWSABLE_API is True | ||
assert settings.WORF_DEBUG is True |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from importlib import import_module | ||
|
||
from django.utils.functional import SimpleLazyObject | ||
|
||
|
||
settings = SimpleLazyObject(lambda: import_module("worf.settings")) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.conf import settings | ||
|
||
|
||
WORF_API_NAME = getattr(settings, "WORF_API_NAME", "Worf API") | ||
WORF_API_ROOT = getattr(settings, "WORF_API_ROOT", "/api/") | ||
|
||
WORF_BROWSABLE_API = getattr(settings, "WORF_BROWSABLE_API", True) | ||
|
||
WORF_DEBUG = getattr(settings, "WORF_DEBUG", settings.DEBUG) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
<meta name="robots" content="none,noarchive"/> | ||
{% endblock %} | ||
|
||
<title>{{ api_name }}: {{ request.get_full_path }}</title> | ||
<title>{{ settings.WORF_API_NAME }}: {{ request.get_full_path }}</title> | ||
|
||
{% block style %} | ||
<link href="https://unpkg.com/[email protected]/dist/tailwind.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" rel="stylesheet"> | ||
|
@@ -35,8 +35,8 @@ | |
<nav class="p-4"> | ||
<div class="max-w-5xl mx-auto"> | ||
{% block branding %} | ||
<a href="{{ api_root }}" class="font-medium text-lg text-white" rel="nofollow"> | ||
{{ api_name }} | ||
<a href="{{ settings.WORF_API_ROOT }}" class="font-medium text-lg text-white" rel="nofollow"> | ||
{{ settings.WORF_API_NAME }} | ||
</a> | ||
{% endblock %} | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ | |
from functools import reduce | ||
import warnings | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.core.exceptions import EmptyResultSet, ImproperlyConfigured | ||
from django.core.paginator import Paginator, EmptyPage | ||
from django.db.models import Q | ||
|
||
from worf.casing import camel_to_snake | ||
from worf.conf import settings | ||
from worf.exceptions import HTTP420 | ||
from worf.filters import apply_filterset, generate_filterset | ||
from worf.views.base import AbstractBaseAPI | ||
|
@@ -165,7 +165,7 @@ def get_processed_queryset(self): | |
if order_by: | ||
queryset = queryset.order_by(*order_by) | ||
except TypeError as e: | ||
if settings.DEBUG: | ||
if settings.WORF_DEBUG: | ||
raise HTTP420(f"Error, {self.lookup_kwargs}, {e.__cause__}") | ||
raise e | ||
|
||
|
@@ -181,8 +181,11 @@ def paginated_results(self): | |
queryset = self.get_processed_queryset() | ||
request = self.request | ||
|
||
if settings.DEBUG: | ||
self.query = str(queryset.query) | ||
if settings.WORF_DEBUG: | ||
try: | ||
self.query = str(queryset.query) | ||
except EmptyResultSet: | ||
self.query = None | ||
Comment on lines
+184
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might have seen this exception when using filters that create an impossible query. Outside of debug mode lists will yield The reason I've addressed this here is because now that debug mode is actually on during testing, one test was failing here given it exercises an impossible query. |
||
|
||
default_per_page = getattr(self, "results_per_page", self.per_page) | ||
per_page = max(int(request.GET.get("perPage") or default_per_page), 1) | ||
|
@@ -235,7 +238,7 @@ def serialize(self): | |
} | ||
) | ||
|
||
if not settings.DEBUG: | ||
if not settings.WORF_DEBUG: | ||
return payload | ||
|
||
if not hasattr(self, "lookup_kwargs"): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun. This was never actually
True
. Seemsconfigure
cannot override the default fromglobal_settings
.