Skip to content

Commit 8739324

Browse files
Remove ENABLE_AUTO_CONFIG setting and autoconfiguration functionality (#216)
* Remove ENABLE_AUTO_CONFIG setting and autoconfiguration functionality * update changelog * update readme * reorg config docs
1 parent 9dd2c81 commit 8739324

File tree

7 files changed

+91
-270
lines changed

7 files changed

+91
-270
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
🚨 This release contains some breaking changes. See the Removed section for more information. 🚨
22+
2123
### Added
2224

2325
- Added `get_component_names_used_in_template` method to ComponentRegistry to access component names used in a template
@@ -39,7 +41,9 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
3941
### Removed
4042

4143
- Removed automatic component scanning at application startup for faster initialization
44+
- Removed `ENABLE_AUTO_CONFIG` setting and autoconfiguration functionality (moved to django-bird-autoconf plugin)
4245
- **Internal**: Removed `ComponentRegistry.discover_components` method.
46+
- **Internal**: Removed `AutoConfigurator` class used for automatic setting configuration
4347

4448
## [0.16.2]
4549

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,34 @@ High-flying components for perfectionists with deadlines.
4343
]
4444
```
4545
46-
3. django-bird will automatically configure the necessary settings in your project.
47-
48-
If you need to customize the configuration or prefer to set up django-bird manually, you can set `DJANGO_BIRD["ENABLE_AUTO_CONFIG"] = False` in your settings.
49-
50-
For detailed instructions, please refer to the [Manual Setup](https://django-bird.readthedocs.io/configuration.html#manual-setup) section within the Configuration documentation.
46+
3. Set up django-bird in your project settings:
47+
48+
```python
49+
# Required: Add the asset finder to handle component assets
50+
STATICFILES_FINDERS = [
51+
"django.contrib.staticfiles.finders.FileSystemFinder",
52+
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
53+
"django_bird.staticfiles.BirdAssetFinder",
54+
]
55+
56+
# Optional: Add templatetags to builtins for convenience
57+
# (otherwise you'll need to {% load django_bird %} in each template)
58+
TEMPLATES = [
59+
{
60+
# ... other settings ...
61+
"OPTIONS": {
62+
"builtins": [
63+
"django_bird.templatetags.django_bird",
64+
],
65+
},
66+
}
67+
]
68+
```
69+
70+
For detailed instructions, please refer to the [Configuration](https://django-bird.readthedocs.io/configuration.html#configuration) section in the documentation.
71+
72+
> [!TIP]
73+
> For automatic configuration, you can use the [django-bird-autoconf](https://pypi.org/project/django-bird-autoconf/) plugin.
5174
5275
## Getting Started
5376

docs/assets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ The `{% bird:css %}` tag will include CSS and the `[% bird:js %}` tag will inclu
115115

116116
django-bird provides a custom staticfiles finder to serve component assets through Django's static files system. This allows you to collect all component assets using Django's `collectstatic` command and serve them efficiently in production.
117117

118-
To enable the custom finder, `BirdAssetFinder` must be in the list in your `STATICFILES_FINDERS` setting. This should be handled if are allowing django-bird to configure your project. If you are manually configuring the library by [setting `ENABLE_AUTO_CONFIG = False` in your `DJANGO_BIRD` settings](configuration.md#enable_auto_config), you will need to add the finder by hand:
118+
To enable the custom finder, `BirdAssetFinder` must be in the list in your `STATICFILES_FINDERS` setting. If you aren't using the [django-bird-autoconf](https://pypi.org/project/django-bird-autoconf/) plugin, you will need to add the finder manually:
119119

120120
```{code-block} python
121121
:caption: settings.py

docs/configuration.md

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
11
# Configuration
22

3+
## Django settings
4+
5+
To use django-bird, you need to configure a few settings in your project:
6+
7+
1. Add django-bird's static file finder to your STATICFILES_FINDERS.
8+
2. Add django-bird's template tags to Django's built-ins. **Note**: This is not required, but if you do not do this you will need to use `{% load django_bird %}` in any templates using components.
9+
10+
```{admonition} Auto Configuration
11+
:class: tip
12+
13+
For automatic configuration of these settings, you can use the [django-bird-autoconf](https://pypi.org/project/django-bird-autoconf/) plugin. This plugin will handle all the setup for you automatically.
14+
```
15+
16+
The complete setup in your settings file should look like this:
17+
18+
```{code-block} python
19+
:caption: settings.py
20+
21+
from pathlib import Path
22+
23+
BASE_DIR = Path(__file__).resolve(strict=True).parent
24+
25+
TEMPLATES = [
26+
{
27+
"BACKEND": "django.template.backends.django.DjangoTemplates",
28+
"DIRS": [
29+
BASE_DIR / "templates",
30+
],
31+
"OPTIONS": {
32+
"builtins": [
33+
"django_bird.templatetags.django_bird",
34+
],
35+
},
36+
}
37+
]
38+
39+
STATICFILES_FINDERS = [
40+
"django.contrib.staticfiles.finders.FileSystemFinder",
41+
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
42+
"django_bird.staticfiles.BirdAssetFinder",
43+
]
44+
```
45+
46+
This configuration ensures that django-bird's templatetags are available globally and component assets can be properly discovered.
347
Configuration of django-bird is done through a `DJANGO_BIRD` dictionary in your Django settings.
448

5-
All settings are optional. Here is an example configuration with the types and default values shown:
49+
## Application settings
50+
51+
All app settings are optional. Here is an example configuration with the types and default values shown:
652

753
```{code-block} python
854
:caption: settings.py
@@ -11,18 +57,17 @@ from pathlib import Path
1157
1258
DJANGO_BIRD = {
1359
"COMPONENT_DIRS": list[Path | str] = [],
14-
"ENABLE_AUTO_CONFIG": bool = True,
1560
"ENABLE_BIRD_ATTRS": bool = True,
1661
}
1762
```
1863

19-
## `COMPONENT_DIRS`
64+
### `COMPONENT_DIRS`
2065

2166
Additional directories to scan for components. Takes a list of paths relative to the base directory of your project's templates directory. A path can either be a `str` or `Path`.
2267

2368
By default, django-bird will look for components in a `bird` directory. Any directories specified here will take precedence and take priority when performing template resolution for components.
2469

25-
### Example
70+
#### Example
2671

2772
Suppose you want to store your components in a `components` directory, you're using a third-party library that provides its own bird components, and you have an alternate templates directory.
2873

@@ -59,49 +104,8 @@ With this setup, django-bird will search for components in the following order:
59104

60105
The default `bird` directory will always be checked last, ensuring that your custom directories take precedence in template resolution.
61106

62-
## `ENABLE_AUTO_CONFIG`
63-
64-
django-bird requires a few settings to be setup in your project's `DJANGO_SETTINGS_MODULE` before it will work properly. django-bird will automatically take care of this, during the app's initialization in `django_bird.apps.AppConfig.ready`.
65-
66-
If you would like to disable this behavior and perform the setup manually, setting `ENABLE_AUTO_CONFIG` to `False` will allow you to do so.
67-
68-
### Manual Setup
69-
70-
When `ENABLE_AUTO_CONFIG` is set to `False`, you need to manually configure the following:
71-
72-
1. Add django-bird's template tags to Django's built-ins.
73-
74-
The complete setup in your settings file should look like this:
75-
76-
```{code-block} python
77-
:caption: settings.py
78-
79-
from pathlib import Path
80-
81-
BASE_DIR = Path(__file__).resolve(strict=True).parent
82-
83-
DJANGO_BIRD = {
84-
"ENABLE_AUTO_CONFIG": False,
85-
}
86-
87-
TEMPLATES = [
88-
{
89-
"BACKEND": "django.template.backends.django.DjangoTemplates",
90-
"DIRS": [
91-
BASE_DIR / "templates",
92-
],
93-
"OPTIONS": {
94-
"builtins": [
95-
"django_bird.templatetags.django_bird",
96-
],
97-
},
98-
}
99-
]
100-
```
101-
102-
This configuration ensures that django-bird's templatetags are available globally.
103107

104-
## `ENABLE_BIRD_ATTRS`
108+
### `ENABLE_BIRD_ATTRS`
105109

106110
Controls whether components automatically receive data attributes related to django-bird in its `attrs` template context variable. Defaults to `True`.
107111

src/django_bird/apps.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ class DjangoBirdAppConfig(AppConfig):
1515

1616
@override
1717
def ready(self):
18-
from django_bird.conf import app_settings
1918
from django_bird.plugins import pm
2019
from django_bird.staticfiles import asset_types
2120

2221
for pre_ready in pm.hook.pre_ready():
2322
pre_ready()
2423

25-
app_settings.autoconfigure()
2624
pm.hook.register_asset_types(register_type=asset_types.register_type)
2725

2826
for ready in pm.hook.ready():

src/django_bird/conf.py

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,32 @@
11
from __future__ import annotations
22

3-
import warnings
4-
from contextlib import suppress
53
from dataclasses import dataclass
64
from dataclasses import field
75
from pathlib import Path
8-
from typing import Any
9-
from typing import final
106

11-
import django.template
127
from django.conf import settings
138

14-
from django_bird import hookimpl
15-
169
from ._typing import override
1710
from .utils import unique_ordered
1811

1912
DJANGO_BIRD_SETTINGS_NAME = "DJANGO_BIRD"
2013

14+
DJANGO_BIRD_BUILTINS = "django_bird.templatetags.django_bird"
15+
DJANGO_BIRD_FINDER = "django_bird.staticfiles.BirdAssetFinder"
16+
2117

2218
@dataclass
2319
class AppSettings:
2420
COMPONENT_DIRS: list[Path | str] = field(default_factory=list)
25-
ENABLE_AUTO_CONFIG: bool = True
2621
ENABLE_BIRD_ATTRS: bool = True
27-
_configurator: AutoConfigurator = field(init=False)
28-
29-
def __post_init__(self):
30-
self._configurator = AutoConfigurator(self)
3122

3223
@override
3324
def __getattribute__(self, __name: str) -> object:
3425
user_settings = getattr(settings, DJANGO_BIRD_SETTINGS_NAME, {})
3526
return user_settings.get(__name, super().__getattribute__(__name))
3627

37-
def autoconfigure(self) -> None:
38-
if not self.ENABLE_AUTO_CONFIG:
39-
return
40-
41-
warnings.warn(
42-
"Autoconfiguration of django-bird is deprecated and has been moved to the "
43-
"django-bird-autoconf plugin. Please install the new plugin from PyPI in your "
44-
"project if you wish to keep this behavior, as this will be removed from the "
45-
"core library in a future version.",
46-
DeprecationWarning,
47-
stacklevel=2,
48-
)
49-
self._configurator.autoconfigure()
50-
5128
def get_component_directory_names(self):
5229
return unique_ordered([*self.COMPONENT_DIRS, "bird"])
5330

5431

55-
@hookimpl
56-
def ready():
57-
app_settings.autoconfigure()
58-
59-
60-
DJANGO_BIRD_BUILTINS = "django_bird.templatetags.django_bird"
61-
DJANGO_BIRD_FINDER = "django_bird.staticfiles.BirdAssetFinder"
62-
63-
64-
@final
65-
class AutoConfigurator:
66-
def __init__(self, app_settings: AppSettings) -> None:
67-
self.app_settings = app_settings
68-
self._configured = False
69-
70-
def autoconfigure(self) -> None:
71-
self.configure_templates()
72-
self.configure_staticfiles()
73-
self._configured = True
74-
75-
def configure_templates(self) -> None:
76-
template_config = None
77-
78-
for config in settings.TEMPLATES:
79-
engine_name = config.get("NAME") or config["BACKEND"].split(".")[-2]
80-
if engine_name == "django":
81-
template_config = config
82-
break
83-
84-
if template_config is None:
85-
return
86-
87-
options = template_config.setdefault("OPTIONS", {})
88-
89-
self.configure_builtins(options)
90-
91-
# Force re-evaluation of settings.TEMPLATES because EngineHandler caches it.
92-
with suppress(AttributeError): # pragma: no cover
93-
del django.template.engines.templates
94-
django.template.engines._engines = {} # type:ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue]
95-
96-
def configure_builtins(self, options: dict[str, Any]) -> None:
97-
builtins = options.setdefault("builtins", [])
98-
99-
builtins_already_configured = DJANGO_BIRD_BUILTINS in builtins
100-
101-
if not builtins_already_configured:
102-
builtins.append(DJANGO_BIRD_BUILTINS)
103-
104-
def configure_staticfiles(self) -> None:
105-
finders_already_configured = DJANGO_BIRD_FINDER in settings.STATICFILES_FINDERS
106-
107-
if not finders_already_configured:
108-
settings.STATICFILES_FINDERS.append(DJANGO_BIRD_FINDER)
109-
110-
11132
app_settings = AppSettings()

0 commit comments

Comments
 (0)