diff --git a/README.md b/README.md index 1971546..6115005 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,11 @@ Table of contents - [UpdateAPI](#updateapi) - [Browsable API](#browsable-api) - [Bundle loading](#bundle-loading) + - [Debugging](#debugging) - [Field casing](#field-casing) - [File uploads](#file-uploads) - [Internal naming](#internal-naming) + - [Settings](#settings) - [Credits](#credits) @@ -217,8 +219,8 @@ It is not recommended to use this abstract view directly. | Name | Type | Default | Description | | -- | -- | -- | -- | | filters | dict | {} | Pass key/value pairs that you wish to further filter the queryset beyond the `lookup_url_kwarg` | -| lookup_field | str | None | Use these two settings in tandem in order to filter `get_queryset` based on a URL field. `lookup_url_kwarg` is required if this is set. | -| lookup_url_kwarg | str | None | Use these two settings in tandem in order to filter `get_queryset` based on a URL field. `lookup_field` is required if this is set. | +| lookup_field | str | None | Use these two attributes in tandem in order to filter `get_queryset` based on a URL field. `lookup_url_kwarg` is required if this is set. | +| lookup_url_kwarg | str | None | Use these two attributes in tandem in order to filter `get_queryset` based on a URL field. `lookup_field` is required if this is set. | | payload_key | str | verbose_name_plural | Use in order to rename the key for the results array | | ordering | list | [] | Pass a list of fields to default the queryset order by. | | filter_fields | list | [] | Pass a list of fields to support filtering via query params. | @@ -292,20 +294,14 @@ Browsable API Similar to other popular REST frameworks; Worf exposes a browsable API which adds syntax highlighting, linkified URLs and supports Django Debug Toolbar. -### Format - -To override the default browser behaviour pass `?format=json`. +To override the default browser behaviour pass `?format=json`, or [disable the +feature entirely from settings](#settings). ### Theme The theme is built with [Tailwind](https://tailwindcss.com/), making it easy to customize the look-and-feel. -For quick and easy branding, there are a couple of Django settings that tweak the navbar: - -| Name | Default | -| ------------- | -------- | -| WORF_API_NAME | Worf API | -| WORF_API_ROOT | /api/ | +For quick and easy branding, there are a couple of [settings that tweak the navbar](#settings). To customize the markup create a template called `worf/api.html` that extends from `worf/base.html`: @@ -347,6 +343,13 @@ be changed during processing. You may also append or remove attributes to the bundle before saving the object via `post`, `patch`, or other methods. +Debugging +--------- + +Worf exposes the parsed bundle, lookup kwargs, sql and skips some exception handling +[when in debug mode](#settings). + + Field casing ------------ @@ -387,6 +390,18 @@ this codebase for clarity: - `payload` is what the backend returns. +Settings +-------- + +| Name | Default | Description | +| ------------------ | -------------- | ----------------------------------- | +| WORF_API_NAME | Worf API | See [Browsable API](#browsable-api) | +| WORF_API_ROOT | /api/ | See [Browsable API](#browsable-api) | +| WORF_BROWSABLE_API | True | See [Browsable API](#browsable-api) | +| WORF_DEBUG | settings.DEBUG | See [Debugging](#debugging) | + + + Credits ------- diff --git a/pytest.ini b/pytest.ini index 7029cc5..8cc2105 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,7 +1,7 @@ [pytest] addopts = --cov - --cov-fail-under 81 + --cov-fail-under 82 --cov-report term:skip-covered --cov-report html --no-cov-on-fail diff --git a/tests/conftest.py b/tests/conftest.py index f29c5b8..ffae878 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,32 +8,20 @@ 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", @@ -41,11 +29,11 @@ def pytest_configure(): }, ], TIME_ZONE="UTC", - USE_I18N=True, - USE_L10N=True, + USE_I18N=False, + USE_L10N=False, USE_TZ=True, - STATIC_URL="/static/", - WORF_API_NAME="Test API" + WORF_API_NAME="Test API", + WORF_DEBUG=True, ) django.setup() diff --git a/tests/test_conf.py b/tests/test_conf.py new file mode 100644 index 0000000..9c36ccf --- /dev/null +++ b/tests/test_conf.py @@ -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 diff --git a/worf/conf.py b/worf/conf.py new file mode 100644 index 0000000..fb0dab5 --- /dev/null +++ b/worf/conf.py @@ -0,0 +1,6 @@ +from importlib import import_module + +from django.utils.functional import SimpleLazyObject + + +settings = SimpleLazyObject(lambda: import_module("worf.settings")) diff --git a/worf/serializers.py b/worf/serializers.py index d8c6a23..133d76b 100644 --- a/worf/serializers.py +++ b/worf/serializers.py @@ -1,11 +1,11 @@ import marshmallow -from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db.models.fields.files import FieldFile from worf import fields # noqa: F401 from worf.casing import snake_to_camel +from worf.conf import settings class SerializerOptions(marshmallow.SchemaOpts): diff --git a/worf/settings.py b/worf/settings.py new file mode 100644 index 0000000..883ff5b --- /dev/null +++ b/worf/settings.py @@ -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) diff --git a/worf/templates/worf/base.html b/worf/templates/worf/base.html index 185b203..cf4b273 100644 --- a/worf/templates/worf/base.html +++ b/worf/templates/worf/base.html @@ -7,7 +7,7 @@ {% endblock %} - {{ api_name }}: {{ request.get_full_path }} + {{ settings.WORF_API_NAME }}: {{ request.get_full_path }} {% block style %} @@ -35,8 +35,8 @@