From ed65458410735a8fb603ea0c8d2d31dd30f21e0c Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 6 Nov 2024 12:40:37 +0100 Subject: [PATCH 1/4] feat(blocks): callback --- README.rst | 13 +++++++++++++ django_fastdev/apps.py | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/README.rst b/README.rst index c447554..2a5de28 100644 --- a/README.rst +++ b/README.rst @@ -98,6 +98,19 @@ Django will silently throw away `hello!` because you wrote :code:`contents` inst of :code:`content`. :code:`django-fastdev` will turn this into an error which lists the invalid and valid block names in alphabetical order. +You can customize the filter to understand also custom blocks in your `settings.py` file in this way (an example to integrate [Unfold](https://github.com/unfoldadmin/django-unfold)): + +settings.py: + +``` +FASTDEV_INVALID_BLOCKS_CALLBACK = "your_app.settings.fastdev_blocks" + +def fastdev_blocks(x, result): + if hasattr(x, 'varname') and x.varname is not None: + result.add(x.varname) + return result +``` + Better error messages for reverse ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/django_fastdev/apps.py b/django_fastdev/apps.py index 6aa682d..7ecdafd 100644 --- a/django_fastdev/apps.py +++ b/django_fastdev/apps.py @@ -4,6 +4,7 @@ import subprocess import sys import threading +import importlib from functools import cache from typing import Optional import warnings @@ -458,6 +459,11 @@ def collect_valid_blocks(template, context): # 'isinstance(x, (AutoEscapeControlNode, BlockNode, FilterNode, ForNode, IfNode, # IfChangedNode, SpacelessNode))' at the risk of missing some we don't know about result |= collect_nested_blocks(x) + else: + if settings.FASTDEV_INVALID_BLOCKS_CALLBACK is not None: + module, callback = settings.FASTDEV_INVALID_BLOCKS_CALLBACK.rsplit(".", 1) + module = importlib.import_module(module) + result = getattr(module, callback)(x, result) return result orig_extends_render = ExtendsNode.render From c28e5164f501d1c5d58419eb1a1d22c598ba4119 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 6 Nov 2024 12:56:10 +0100 Subject: [PATCH 2/4] feat(blocks): callback --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 2a5de28..41f90a6 100644 --- a/README.rst +++ b/README.rst @@ -108,6 +108,7 @@ FASTDEV_INVALID_BLOCKS_CALLBACK = "your_app.settings.fastdev_blocks" def fastdev_blocks(x, result): if hasattr(x, 'varname') and x.varname is not None: result.add(x.varname) + result.add(x.varname.replace('_', '-')) return result ``` From 8e37ba282f1372c7aca22d722379529418f19914 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 6 Nov 2024 15:03:27 +0100 Subject: [PATCH 3/4] feat(admin): unfold --- django_fastdev/apps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_fastdev/apps.py b/django_fastdev/apps.py index 7ecdafd..b5733f8 100644 --- a/django_fastdev/apps.py +++ b/django_fastdev/apps.py @@ -460,7 +460,7 @@ def collect_valid_blocks(template, context): # IfChangedNode, SpacelessNode))' at the risk of missing some we don't know about result |= collect_nested_blocks(x) else: - if settings.FASTDEV_INVALID_BLOCKS_CALLBACK is not None: + if getattr(settings, 'FASTDEV_INVALID_BLOCKS_CALLBACK', False) is False: module, callback = settings.FASTDEV_INVALID_BLOCKS_CALLBACK.rsplit(".", 1) module = importlib.import_module(module) result = getattr(module, callback)(x, result) From 415053136c3a42edebdb287db408e367738027d6 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 22 Nov 2024 12:16:19 +0100 Subject: [PATCH 4/4] Update apps.py --- django_fastdev/apps.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/django_fastdev/apps.py b/django_fastdev/apps.py index b5733f8..2118b0f 100644 --- a/django_fastdev/apps.py +++ b/django_fastdev/apps.py @@ -451,6 +451,10 @@ def get_extends_node_parent(extends_node, context): def collect_valid_blocks(template, context): result = set() for x in template.nodelist: + if getattr(settings, 'FASTDEV_INVALID_BLOCKS_CALLBACK', False) is not False: + module, callback = settings.FASTDEV_INVALID_BLOCKS_CALLBACK.rsplit(".", 1) + module = importlib.import_module(module) + result = getattr(module, callback)(x, result) if isinstance(x, ExtendsNode): result |= collect_nested_blocks(x) result |= collect_valid_blocks(get_extends_node_parent(x, context), context) @@ -459,11 +463,6 @@ def collect_valid_blocks(template, context): # 'isinstance(x, (AutoEscapeControlNode, BlockNode, FilterNode, ForNode, IfNode, # IfChangedNode, SpacelessNode))' at the risk of missing some we don't know about result |= collect_nested_blocks(x) - else: - if getattr(settings, 'FASTDEV_INVALID_BLOCKS_CALLBACK', False) is False: - module, callback = settings.FASTDEV_INVALID_BLOCKS_CALLBACK.rsplit(".", 1) - module = importlib.import_module(module) - result = getattr(module, callback)(x, result) return result orig_extends_render = ExtendsNode.render