Skip to content
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

(Improvement) Debug toolbar #134

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ 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
Worf exposes the parsed bundle, lookup kwargs and skips some exception handling
[when in debug mode](#settings).


Expand Down
53 changes: 50 additions & 3 deletions worf/renderers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
from django.http import HttpResponse, JsonResponse
from django.template.response import TemplateResponse

from worf.casing import snake_to_camel
from worf.conf import settings
from worf.shortcuts import field_list


def browsable_response(request, response, status_code=200):
def browsable_response(request, response, status_code, view):
template = "worf/api.html"
serializer = view.get_serializer()

include = field_list(view.bundle.get("include", []))
search = field_list(view.bundle.get("search", []), delimiter="__")

filter_fields = [
(transform_field(field), bool(field in view.bundle))
for field in getattr(view, "filter_fields", [])
]
include_fields = [
(transform_field(field, "."), bool(field in include or not include))
for field in getattr(view, "include_fields", {}).keys()
]
search_fields = [
(transform_field(field, "."), bool(field in search or not search))
for field in getattr(view, "search_fields", [])
]

context = dict(
content=response.content.decode("utf-8"),
fields=[
(
"Filters",
sorted(filter_fields),
len([field for field, active in filter_fields if active]),
),
(
"Include",
sorted(include_fields),
len(include),
),
(
"Search",
sorted(search_fields),
len(search or search_fields) if view.bundle.get("q") else 0,
),
],
lookup_kwargs=getattr(view, "lookup_kwargs", {}),
payload=view.bundle,
response=response,
serializer=serializer,
serializer_name=type(serializer).__name__,
settings=settings,
view=view,
view_name=type(view).__name__,
)

response = TemplateResponse(request, template, context=context)
Expand All @@ -19,7 +62,7 @@ def browsable_response(request, response, status_code=200):
return response


def render_response(request, data, status_code=200):
def render_response(request, data, status_code, view):
is_browsable = (
settings.WORF_BROWSABLE_API
and "text/html" in request.headers.get("Accept", "")
Expand All @@ -35,6 +78,10 @@ def render_response(request, data, status_code=200):
response.status_code = status_code

if is_browsable:
response = browsable_response(request, response, status_code)
response = browsable_response(request, response, status_code, view)

return response


def transform_field(field, delimiter="__"):
return delimiter.join(map(snake_to_camel, field.split("__")))
15 changes: 5 additions & 10 deletions worf/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import marshmallow
from marshmallow.decorators import * # noqa: F401, F403

from django.core.exceptions import ImproperlyConfigured
from django.db.models.fields.files import FieldFile

from worf import fields
Expand All @@ -16,26 +15,22 @@ class SerializeModels:
serializer = None
staff_serializer = None

def get_serializer(self):
def get_serializer(self, **kwargs):
serializer = self.serializer

if self.staff_serializer and self.request.user.is_staff: # pragma: no cover
serializer = self.staff_serializer

if not serializer: # pragma: no cover
msg = f"{self.__class__.__name__}.get_serializer() did not return a serializer"
raise ImproperlyConfigured(msg)

return serializer(**self.get_serializer_kwargs())
return serializer and serializer(**self.get_serializer_kwargs(**kwargs))

def get_serializer_context(self):
return {}

def get_serializer_kwargs(self):
def get_serializer_kwargs(self, include=[], exclude=[]):
context = dict(request=self.request, **self.get_serializer_context())
only = set(field_list(self.bundle.get("fields", [])))
include = field_list(self.bundle.get("include", []))
exclude = set(self.include_fields.keys()) - set(include)
include = include or field_list(self.bundle.get("include", []))
exclude = exclude or set(self.include_fields.keys()) - set(include)
return dict(context=context, only=only, exclude=exclude)

def load_serializer(self):
Expand Down
5 changes: 3 additions & 2 deletions worf/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from worf.casing import camel_to_snake


def field_list(value):
def field_list(value, delimiter="."):
return [
".".join(map(camel_to_snake, field.split("."))) for field in string_list(value)
delimiter.join(map(camel_to_snake, field.split(".")))
for field in string_list(value)
]


Expand Down
87 changes: 83 additions & 4 deletions worf/templates/worf/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<meta name="robots" content="none,noarchive"/>
{% endblock %}

<title>{{ settings.WORF_API_NAME }}: {{ request.get_full_path }}</title>
<title>{{ view_name }} | {{ 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">
<link href="https://unpkg.com/[email protected]/prism-js-fold.css" crossorigin="anonymous" referrerpolicy="no-referrer" rel="stylesheet">
<style>
body { background: #131417; color: #fff; }
a { color: #58A6FF; }
a:hover { text-decoration: underline; }
code, pre { font-family: inconsolata, monaco, consolas, courier, monospace; color: #c5c8c6; }
nav, pre { background: #202126; }
details, nav, pre { background: #202126; }
details { box-shadow: 0 0 50px #131417 }
strong { font-weight: 500; }
.token a { color: #58a6ff; }
.token.boolean { color: #fbbf24; }
Expand All @@ -32,6 +32,7 @@
{% endblock %}

{% block script %}
<script src="https://cdn.tailwindcss.com" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/[email protected]" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/[email protected]/components/prism-json.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/[email protected]/plugins/keep-markup/prism-keep-markup.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Expand Down Expand Up @@ -67,7 +68,85 @@

<code class="language-json">{{ content }}</code></pre>
</div>
{% endblock content %}
{% endblock %}

{% if settings.WORF_DEBUG %}
{% block toolbar %}
<div class="fixed max-w-sm max-h-screen bottom-0 right-0 pl-8 pt-8 overflow-auto">
{% if view %}
<details class="m-2">
<summary class="font-medium px-4 py-2 cursor-pointer">
<span class="px-1">{{ view_name }}</span>
</summary>
<div class="px-4 py-2">
{{ view }}
</div>
</details>
{% endif %}

{% if serializer %}
<details class="m-2">
<summary class="font-medium px-4 py-2 cursor-pointer">
<span class="px-1">{{ serializer_name }}</span>
</summary>
<div class="px-4 py-2">
{{ serializer }}
</div>
</details>
{% endif %}

{% if payload %}
<details class="m-2">
<summary class="font-medium px-4 py-2 cursor-pointer">
<span class="px-1">
Payload
<span class="bg-zinc-700 mx-1 px-1">{{ payload|length }}</span>
</span>
</summary>
<div class="px-4 py-2 break-all">
{{ payload }}
</div>
</details>
{% endif %}

{% if lookup_kwargs %}
<details class="m-2">
<summary class="font-medium px-4 py-2 cursor-pointer">
<span class="px-1">
Lookups
<span class="bg-zinc-700 mx-1 px-1">{{ lookup_kwargs|length }}</span>
</span>
</summary>
<div class="px-4 py-2 break-all">
{{ lookup_kwargs }}
</div>
</details>
{% endif %}

{% for name, items, count in fields %}
{% if items %}
<details class="m-2">
<summary class="font-medium px-4 py-2 cursor-pointer">
<span class="px-1">
{{ name }}
{% if count %}<span class="bg-zinc-700 mx-1 px-1">{{ count }}</span>{% endif %}
</span>
</summary>
<div class="px-4 py-2">
<table>
{% for field, active in items %}
<tr class="{% if count and not active %}opacity-50{% endif %}">
<th class="text-left pr-4">{{ field }}</th>
</tr>
{% endfor %}
</table>
</div>
</details>
{% endif %}
{% endfor %}
</div>
{% endblock %}
{% endif %}
</main>
</div>
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion worf/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def render_to_response(self, data=None, status_code=200):
msg += "render_to_response, nor did its serializer method"
raise ImproperlyConfigured(msg)

return render_response(self.request, data, status_code)
return render_response(self.request, data, status_code, self)


class AbstractBaseAPI(SerializeModels, ValidateFields, APIResponse):
Expand Down
6 changes: 3 additions & 3 deletions worf/views/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def create(self, *args, **kwargs):
self.instance.refresh_from_db()
return self.instance

def get_serializer(self):
def get_serializer(self, **kwargs):
if self.create_serializer and self.request.method == "POST":
return self.create_serializer(**self.get_serializer_kwargs())
return super().get_serializer()
return self.create_serializer(**self.get_serializer_kwargs(**kwargs))
return super().get_serializer(**kwargs)

def new_instance(self):
return self.model()
Expand Down
33 changes: 9 additions & 24 deletions worf/views/list.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import operator
from functools import reduce

from django.core.exceptions import EmptyResultSet, ImproperlyConfigured
from django.core.exceptions import ImproperlyConfigured
from django.core.paginator import EmptyPage, Paginator
from django.db.models import F, OrderBy, Prefetch, Q

from worf.casing import camel_to_snake
from worf.conf import settings
from worf.exceptions import FieldError
from worf.filters import apply_filterset, generate_filterset
from worf.shortcuts import field_list, string_list
Expand Down Expand Up @@ -70,18 +69,19 @@ def set_search_lookup_kwargs(self):
if not self.filter_fields and not self.search_fields: # pragma: no cover
return

bundle = self.bundle.copy()

# Whatever is not q or page as a querystring param will
# be used for key-value search.
query = self.bundle.pop("q", "").strip()
query = bundle.pop("q", "").strip()

self.bundle.pop("page", None)
self.bundle.pop("p", None)
bundle.pop("page", None)

if query:
search_fields = self.search_fields

if self.bundle.get("search", []):
search_fields = field_list(self.bundle["search"])
if bundle.get("search", []):
search_fields = field_list(bundle["search"], delimiter="__")
invalid_fields = set(search_fields) - set(self.search_fields)
if invalid_fields:
raise FieldError(f"Invalid fields: {invalid_fields}")
Expand All @@ -93,10 +93,10 @@ def set_search_lookup_kwargs(self):
)
self.search_query = reduce(operator.or_, search_icontains)

if not self.filter_fields or not self.bundle: # pragma: no cover
if not self.filter_fields or not bundle: # pragma: no cover
return

for key in self.bundle.keys():
for key in bundle.keys():
filter_key = key[:-1] if key.endswith("!") else key

if filter_key not in self.filter_fields:
Expand Down Expand Up @@ -173,12 +173,6 @@ def paginated_results(self):
queryset = self.get_processed_queryset()
request = self.request

if settings.WORF_DEBUG: # pragma: no cover
try:
self.query = str(queryset.query)
except EmptyResultSet:
self.query = None

default_per_page = getattr(self, "results_per_page", self.per_page)
per_page = max(int(request.GET.get("perPage") or default_per_page), 1)
max_per_page = self.max_per_page or default_per_page
Expand Down Expand Up @@ -208,15 +202,6 @@ def serialize(self):
page=self.page_num,
)

if settings.WORF_DEBUG: # pragma: no cover
payload["debug"] = {
"bundle": self.bundle,
"lookup_kwargs": getattr(self, "lookup_kwargs", {}),
"query": self.query,
"search_query": str(self.search_query),
"serializer": str(serializer).strip("<>"),
}

return payload


Expand Down
6 changes: 3 additions & 3 deletions worf/views/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class UpdateAPI(AssignAttributes, FindInstance, AbstractBaseAPI):
update_serializer = None

def get_serializer(self):
def get_serializer(self, **kwargs):
if self.update_serializer and self.request.method in ("PATCH", "PUT"):
return self.update_serializer(**self.get_serializer_kwargs())
return super().get_serializer()
return self.update_serializer(**self.get_serializer_kwargs(**kwargs))
return super().get_serializer(**kwargs)

def patch(self, *args, **kwargs):
self.update(*args, **kwargs)
Expand Down