django-components 0.100__tar.gz → 0.102__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_components-0.102/MANIFEST.in +5 -0
- {django_components-0.100/src/django_components.egg-info → django_components-0.102}/PKG-INFO +95 -8
- {django_components-0.100 → django_components-0.102}/README.md +95 -8
- {django_components-0.100 → django_components-0.102}/pyproject.toml +1 -1
- {django_components-0.100 → django_components-0.102}/src/django_components/app_settings.py +3 -1
- {django_components-0.100 → django_components-0.102}/src/django_components/apps.py +1 -1
- {django_components-0.100 → django_components-0.102}/src/django_components/autodiscover.py +56 -14
- {django_components-0.100 → django_components-0.102}/src/django_components/component.py +0 -7
- {django_components-0.100 → django_components-0.102}/src/django_components/context.py +0 -4
- {django_components-0.100 → django_components-0.102}/src/django_components/management/commands/upgradecomponent.py +1 -1
- {django_components-0.100 → django_components-0.102}/src/django_components/node.py +3 -1
- {django_components-0.100 → django_components-0.102}/src/django_components/slots.py +33 -37
- django_components-0.102/src/django_components/static/django_components/django_components.min.js +1 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/template_loader.py +9 -17
- {django_components-0.100 → django_components-0.102/src/django_components.egg-info}/PKG-INFO +95 -8
- {django_components-0.100 → django_components-0.102}/src/django_components.egg-info/SOURCES.txt +3 -24
- django_components-0.100/tests/test_attributes.py +0 -422
- django_components-0.100/tests/test_autodiscover.py +0 -164
- django_components-0.100/tests/test_command_startcomponent.py +0 -113
- django_components-0.100/tests/test_component.py +0 -1202
- django_components-0.100/tests/test_component_as_view.py +0 -276
- django_components-0.100/tests/test_component_media.py +0 -864
- django_components-0.100/tests/test_context.py +0 -787
- django_components-0.100/tests/test_dependency_rendering.py +0 -454
- django_components-0.100/tests/test_expression.py +0 -846
- django_components-0.100/tests/test_finders.py +0 -210
- django_components-0.100/tests/test_registry.py +0 -250
- django_components-0.100/tests/test_settings.py +0 -19
- django_components-0.100/tests/test_tag_formatter.py +0 -402
- django_components-0.100/tests/test_template.py +0 -67
- django_components-0.100/tests/test_template_loader.py +0 -181
- django_components-0.100/tests/test_template_parser.py +0 -131
- django_components-0.100/tests/test_templatetags.py +0 -513
- django_components-0.100/tests/test_templatetags_component.py +0 -700
- django_components-0.100/tests/test_templatetags_provide.py +0 -739
- django_components-0.100/tests/test_templatetags_slot_fill.py +0 -1483
- django_components-0.100/tests/test_templatetags_templating.py +0 -807
- django_components-0.100/tests/test_utils.py +0 -20
- django_components-0.100/tests/testutils.py +0 -184
- {django_components-0.100 → django_components-0.102}/LICENSE +0 -0
- {django_components-0.100 → django_components-0.102}/setup.cfg +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/__init__.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/attributes.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/component_media.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/component_registry.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/components/__init__.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/components/dynamic.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/expression.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/finders.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/library.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/logger.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/management/__init__.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/middleware.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/provide.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/py.typed +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/tag_formatter.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/template.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/template_parser.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/templatetags/component_tags.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/types.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components/utils.py +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components.egg-info/requires.txt +0 -0
- {django_components-0.100 → django_components-0.102}/src/django_components.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.102
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
6
|
License: MIT
|
|
@@ -43,7 +43,7 @@ Potential benefits:
|
|
|
43
43
|
- 🔄 Reduced code duplication
|
|
44
44
|
- 🛠️ Improved maintainability through modular design
|
|
45
45
|
- 🧠 Easier management of complex UIs
|
|
46
|
-
- 🤝
|
|
46
|
+
- 🤝 Enhanced collaboration between frontend and backend developers
|
|
47
47
|
|
|
48
48
|
Django-components can be particularly useful for larger Django projects that require a more structured approach to UI development, without necessitating a shift to a separate frontend framework.
|
|
49
49
|
|
|
@@ -225,7 +225,7 @@ This change is done to simplify the API in anticipation of a 1.0 release of djan
|
|
|
225
225
|
|
|
226
226
|
## Security notes 🚨
|
|
227
227
|
|
|
228
|
-
|
|
228
|
+
_It is strongly recommended to read this section before using django-components in production._
|
|
229
229
|
|
|
230
230
|
### Static files
|
|
231
231
|
|
|
@@ -314,7 +314,7 @@ For a step-by-step guide on deploying production server with static files,
|
|
|
314
314
|
If `COMPONENTS.dirs` is omitted, django-components will by default look for a top-level `/components` directory,
|
|
315
315
|
`{BASE_DIR}/components`.
|
|
316
316
|
|
|
317
|
-
|
|
317
|
+
In addition to `COMPONENTS.dirs`, django_components will also load components from app-level directories, such as `my-app/components/`.
|
|
318
318
|
The directories within apps are configured with [`COMPONENTS.app_dirs`](#app_dirs), and the default is `[app]/components`.
|
|
319
319
|
|
|
320
320
|
NOTE: The input to `COMPONENTS.dirs` is the same as for `STATICFILES_DIRS`, and the paths must be full paths. [See Django docs](https://docs.djangoproject.com/en/5.0/ref/settings/#staticfiles-dirs).
|
|
@@ -1605,7 +1605,6 @@ This is what our example looks like with `component_vars.is_filled`.
|
|
|
1605
1605
|
</div>
|
|
1606
1606
|
{% endif %}
|
|
1607
1607
|
</div>
|
|
1608
|
-
```
|
|
1609
1608
|
|
|
1610
1609
|
Here's our example with more complex branching.
|
|
1611
1610
|
|
|
@@ -1823,7 +1822,7 @@ This means that you can use `self.input` inside:
|
|
|
1823
1822
|
- `get_template_name`
|
|
1824
1823
|
- `get_template`
|
|
1825
1824
|
|
|
1826
|
-
`self.input` is defined
|
|
1825
|
+
`self.input` is only defined during the execution of `Component.render`, and raises a `RuntimeError` when called outside of this context.
|
|
1827
1826
|
|
|
1828
1827
|
`self.input` has the same fields as the input to `Component.render`:
|
|
1829
1828
|
|
|
@@ -2743,6 +2742,30 @@ If you find yourself using the `only` modifier often, you can set the [context_b
|
|
|
2743
2742
|
|
|
2744
2743
|
Components can also access the outer context in their context methods like `get_context_data` by accessing the property `self.outer_context`.
|
|
2745
2744
|
|
|
2745
|
+
### Example of Accessing Outer Context
|
|
2746
|
+
|
|
2747
|
+
```django
|
|
2748
|
+
<div>
|
|
2749
|
+
{% component "calender" / %}
|
|
2750
|
+
</div>
|
|
2751
|
+
```
|
|
2752
|
+
|
|
2753
|
+
Assuming that the rendering context has variables such as `date`, you can use `self.outer_context` to access them from within `get_context_data`. Here's how you might implement it:
|
|
2754
|
+
|
|
2755
|
+
```python
|
|
2756
|
+
class Calender(Component):
|
|
2757
|
+
|
|
2758
|
+
...
|
|
2759
|
+
|
|
2760
|
+
def get_context_data(self):
|
|
2761
|
+
outer_field = self.outer_context["date"]
|
|
2762
|
+
return {
|
|
2763
|
+
"date": outer_fields,
|
|
2764
|
+
}
|
|
2765
|
+
```
|
|
2766
|
+
|
|
2767
|
+
However, as a best practice, it’s recommended not to rely on accessing the outer context directly through `self.outer_context`. Instead, explicitly pass the variables to the component. For instance, continue passing the variables in the component tag as shown in the previous examples.
|
|
2768
|
+
|
|
2746
2769
|
## Pre-defined template variables
|
|
2747
2770
|
|
|
2748
2771
|
Here is a list of all variables that are automatically available from within the component's template and `on_render_before` / `on_render_after` hooks.
|
|
@@ -3876,7 +3899,7 @@ One of our goals with `django-components` is to make it easy to share components
|
|
|
3876
3899
|
|
|
3877
3900
|
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
3878
3901
|
|
|
3879
|
-
##
|
|
3902
|
+
## Contributing and development
|
|
3880
3903
|
|
|
3881
3904
|
### Install locally and run the tests
|
|
3882
3905
|
|
|
@@ -3910,6 +3933,22 @@ pyenv local 3.8 3.9 3.10 3.11 3.12
|
|
|
3910
3933
|
tox -p
|
|
3911
3934
|
```
|
|
3912
3935
|
|
|
3936
|
+
### Running Playwright tests
|
|
3937
|
+
|
|
3938
|
+
We use [Playwright](https://playwright.dev/python/docs/intro) for end-to-end tests. You will therefore need to install Playwright to be able to run these tests.
|
|
3939
|
+
|
|
3940
|
+
Luckily, Playwright makes it very easy:
|
|
3941
|
+
|
|
3942
|
+
```sh
|
|
3943
|
+
pip install -r requirements-dev.txt
|
|
3944
|
+
playwright install chromium --with-deps
|
|
3945
|
+
```
|
|
3946
|
+
|
|
3947
|
+
After Playwright is ready, simply run the tests with `tox`:
|
|
3948
|
+
```sh
|
|
3949
|
+
tox
|
|
3950
|
+
```
|
|
3951
|
+
|
|
3913
3952
|
### Developing against live Django app
|
|
3914
3953
|
|
|
3915
3954
|
How do you check that your changes to django-components project will work in an actual Django project?
|
|
@@ -3945,6 +3984,54 @@ Once the server is up, it should be available at <http://127.0.0.1:8000>.
|
|
|
3945
3984
|
|
|
3946
3985
|
To display individual components, add them to the `urls.py`, like in the case of <http://127.0.0.1:8000/greeting>
|
|
3947
3986
|
|
|
3948
|
-
|
|
3987
|
+
### Building JS code
|
|
3988
|
+
|
|
3989
|
+
django_components uses a bit of JS code to:
|
|
3990
|
+
- Manage the loading of JS and CSS files used by the components
|
|
3991
|
+
- Allow to pass data from Python to JS
|
|
3992
|
+
|
|
3993
|
+
When you make changes to this JS code, you also need to compile it:
|
|
3994
|
+
|
|
3995
|
+
1. Make sure you are inside `src/django_components_js`:
|
|
3996
|
+
|
|
3997
|
+
```sh
|
|
3998
|
+
cd src/django_components_js
|
|
3999
|
+
```
|
|
4000
|
+
|
|
4001
|
+
2. Install the JS dependencies
|
|
4002
|
+
|
|
4003
|
+
```sh
|
|
4004
|
+
npm install
|
|
4005
|
+
```
|
|
4006
|
+
|
|
4007
|
+
3. Compile the JS/TS code:
|
|
4008
|
+
|
|
4009
|
+
```sh
|
|
4010
|
+
python build.py
|
|
4011
|
+
```
|
|
4012
|
+
|
|
4013
|
+
The script will combine all JS/TS code into a single `.js` file, minify it,
|
|
4014
|
+
and copy it to `django_components/static/django_components/django_components.min.js`.
|
|
4015
|
+
|
|
4016
|
+
### Packaging and publishing
|
|
4017
|
+
|
|
4018
|
+
To package the library into a distribution that can be published to PyPI, run:
|
|
4019
|
+
|
|
4020
|
+
```sh
|
|
4021
|
+
# Install pypa/build
|
|
4022
|
+
python -m pip install build --user
|
|
4023
|
+
# Build a binary wheel and a source tarball
|
|
4024
|
+
python -m build --sdist --wheel --outdir dist/ .
|
|
4025
|
+
```
|
|
4026
|
+
|
|
4027
|
+
To publish the package to PyPI, use `twine` ([See Python user guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/#uploading-the-distribution-archives)):
|
|
4028
|
+
```sh
|
|
4029
|
+
twine upload --repository pypi dist/* -u __token__ -p <PyPI_TOKEN>
|
|
4030
|
+
```
|
|
4031
|
+
|
|
4032
|
+
[See the full workflow here.](https://github.com/EmilStenstrom/django-components/discussions/557#discussioncomment-10179141)
|
|
4033
|
+
|
|
4034
|
+
### Development guides
|
|
3949
4035
|
|
|
3950
4036
|
- [Slot rendering flot](https://github.com/EmilStenstrom/django-components/blob/master/docs/slot_rendering.md)
|
|
4037
|
+
- [Slots and blocks](https://github.com/EmilStenstrom/django-components/blob/master/docs/slots_and_blocks.md)
|
|
@@ -19,7 +19,7 @@ Potential benefits:
|
|
|
19
19
|
- 🔄 Reduced code duplication
|
|
20
20
|
- 🛠️ Improved maintainability through modular design
|
|
21
21
|
- 🧠 Easier management of complex UIs
|
|
22
|
-
- 🤝
|
|
22
|
+
- 🤝 Enhanced collaboration between frontend and backend developers
|
|
23
23
|
|
|
24
24
|
Django-components can be particularly useful for larger Django projects that require a more structured approach to UI development, without necessitating a shift to a separate frontend framework.
|
|
25
25
|
|
|
@@ -201,7 +201,7 @@ This change is done to simplify the API in anticipation of a 1.0 release of djan
|
|
|
201
201
|
|
|
202
202
|
## Security notes 🚨
|
|
203
203
|
|
|
204
|
-
|
|
204
|
+
_It is strongly recommended to read this section before using django-components in production._
|
|
205
205
|
|
|
206
206
|
### Static files
|
|
207
207
|
|
|
@@ -290,7 +290,7 @@ For a step-by-step guide on deploying production server with static files,
|
|
|
290
290
|
If `COMPONENTS.dirs` is omitted, django-components will by default look for a top-level `/components` directory,
|
|
291
291
|
`{BASE_DIR}/components`.
|
|
292
292
|
|
|
293
|
-
|
|
293
|
+
In addition to `COMPONENTS.dirs`, django_components will also load components from app-level directories, such as `my-app/components/`.
|
|
294
294
|
The directories within apps are configured with [`COMPONENTS.app_dirs`](#app_dirs), and the default is `[app]/components`.
|
|
295
295
|
|
|
296
296
|
NOTE: The input to `COMPONENTS.dirs` is the same as for `STATICFILES_DIRS`, and the paths must be full paths. [See Django docs](https://docs.djangoproject.com/en/5.0/ref/settings/#staticfiles-dirs).
|
|
@@ -1581,7 +1581,6 @@ This is what our example looks like with `component_vars.is_filled`.
|
|
|
1581
1581
|
</div>
|
|
1582
1582
|
{% endif %}
|
|
1583
1583
|
</div>
|
|
1584
|
-
```
|
|
1585
1584
|
|
|
1586
1585
|
Here's our example with more complex branching.
|
|
1587
1586
|
|
|
@@ -1799,7 +1798,7 @@ This means that you can use `self.input` inside:
|
|
|
1799
1798
|
- `get_template_name`
|
|
1800
1799
|
- `get_template`
|
|
1801
1800
|
|
|
1802
|
-
`self.input` is defined
|
|
1801
|
+
`self.input` is only defined during the execution of `Component.render`, and raises a `RuntimeError` when called outside of this context.
|
|
1803
1802
|
|
|
1804
1803
|
`self.input` has the same fields as the input to `Component.render`:
|
|
1805
1804
|
|
|
@@ -2719,6 +2718,30 @@ If you find yourself using the `only` modifier often, you can set the [context_b
|
|
|
2719
2718
|
|
|
2720
2719
|
Components can also access the outer context in their context methods like `get_context_data` by accessing the property `self.outer_context`.
|
|
2721
2720
|
|
|
2721
|
+
### Example of Accessing Outer Context
|
|
2722
|
+
|
|
2723
|
+
```django
|
|
2724
|
+
<div>
|
|
2725
|
+
{% component "calender" / %}
|
|
2726
|
+
</div>
|
|
2727
|
+
```
|
|
2728
|
+
|
|
2729
|
+
Assuming that the rendering context has variables such as `date`, you can use `self.outer_context` to access them from within `get_context_data`. Here's how you might implement it:
|
|
2730
|
+
|
|
2731
|
+
```python
|
|
2732
|
+
class Calender(Component):
|
|
2733
|
+
|
|
2734
|
+
...
|
|
2735
|
+
|
|
2736
|
+
def get_context_data(self):
|
|
2737
|
+
outer_field = self.outer_context["date"]
|
|
2738
|
+
return {
|
|
2739
|
+
"date": outer_fields,
|
|
2740
|
+
}
|
|
2741
|
+
```
|
|
2742
|
+
|
|
2743
|
+
However, as a best practice, it’s recommended not to rely on accessing the outer context directly through `self.outer_context`. Instead, explicitly pass the variables to the component. For instance, continue passing the variables in the component tag as shown in the previous examples.
|
|
2744
|
+
|
|
2722
2745
|
## Pre-defined template variables
|
|
2723
2746
|
|
|
2724
2747
|
Here is a list of all variables that are automatically available from within the component's template and `on_render_before` / `on_render_after` hooks.
|
|
@@ -3852,7 +3875,7 @@ One of our goals with `django-components` is to make it easy to share components
|
|
|
3852
3875
|
|
|
3853
3876
|
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
3854
3877
|
|
|
3855
|
-
##
|
|
3878
|
+
## Contributing and development
|
|
3856
3879
|
|
|
3857
3880
|
### Install locally and run the tests
|
|
3858
3881
|
|
|
@@ -3886,6 +3909,22 @@ pyenv local 3.8 3.9 3.10 3.11 3.12
|
|
|
3886
3909
|
tox -p
|
|
3887
3910
|
```
|
|
3888
3911
|
|
|
3912
|
+
### Running Playwright tests
|
|
3913
|
+
|
|
3914
|
+
We use [Playwright](https://playwright.dev/python/docs/intro) for end-to-end tests. You will therefore need to install Playwright to be able to run these tests.
|
|
3915
|
+
|
|
3916
|
+
Luckily, Playwright makes it very easy:
|
|
3917
|
+
|
|
3918
|
+
```sh
|
|
3919
|
+
pip install -r requirements-dev.txt
|
|
3920
|
+
playwright install chromium --with-deps
|
|
3921
|
+
```
|
|
3922
|
+
|
|
3923
|
+
After Playwright is ready, simply run the tests with `tox`:
|
|
3924
|
+
```sh
|
|
3925
|
+
tox
|
|
3926
|
+
```
|
|
3927
|
+
|
|
3889
3928
|
### Developing against live Django app
|
|
3890
3929
|
|
|
3891
3930
|
How do you check that your changes to django-components project will work in an actual Django project?
|
|
@@ -3921,6 +3960,54 @@ Once the server is up, it should be available at <http://127.0.0.1:8000>.
|
|
|
3921
3960
|
|
|
3922
3961
|
To display individual components, add them to the `urls.py`, like in the case of <http://127.0.0.1:8000/greeting>
|
|
3923
3962
|
|
|
3924
|
-
|
|
3963
|
+
### Building JS code
|
|
3964
|
+
|
|
3965
|
+
django_components uses a bit of JS code to:
|
|
3966
|
+
- Manage the loading of JS and CSS files used by the components
|
|
3967
|
+
- Allow to pass data from Python to JS
|
|
3968
|
+
|
|
3969
|
+
When you make changes to this JS code, you also need to compile it:
|
|
3970
|
+
|
|
3971
|
+
1. Make sure you are inside `src/django_components_js`:
|
|
3972
|
+
|
|
3973
|
+
```sh
|
|
3974
|
+
cd src/django_components_js
|
|
3975
|
+
```
|
|
3976
|
+
|
|
3977
|
+
2. Install the JS dependencies
|
|
3978
|
+
|
|
3979
|
+
```sh
|
|
3980
|
+
npm install
|
|
3981
|
+
```
|
|
3982
|
+
|
|
3983
|
+
3. Compile the JS/TS code:
|
|
3984
|
+
|
|
3985
|
+
```sh
|
|
3986
|
+
python build.py
|
|
3987
|
+
```
|
|
3988
|
+
|
|
3989
|
+
The script will combine all JS/TS code into a single `.js` file, minify it,
|
|
3990
|
+
and copy it to `django_components/static/django_components/django_components.min.js`.
|
|
3991
|
+
|
|
3992
|
+
### Packaging and publishing
|
|
3993
|
+
|
|
3994
|
+
To package the library into a distribution that can be published to PyPI, run:
|
|
3995
|
+
|
|
3996
|
+
```sh
|
|
3997
|
+
# Install pypa/build
|
|
3998
|
+
python -m pip install build --user
|
|
3999
|
+
# Build a binary wheel and a source tarball
|
|
4000
|
+
python -m build --sdist --wheel --outdir dist/ .
|
|
4001
|
+
```
|
|
4002
|
+
|
|
4003
|
+
To publish the package to PyPI, use `twine` ([See Python user guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/#uploading-the-distribution-archives)):
|
|
4004
|
+
```sh
|
|
4005
|
+
twine upload --repository pypi dist/* -u __token__ -p <PyPI_TOKEN>
|
|
4006
|
+
```
|
|
4007
|
+
|
|
4008
|
+
[See the full workflow here.](https://github.com/EmilStenstrom/django-components/discussions/557#discussioncomment-10179141)
|
|
4009
|
+
|
|
4010
|
+
### Development guides
|
|
3925
4011
|
|
|
3926
|
-
- [Slot rendering flot](https://github.com/EmilStenstrom/django-components/blob/master/docs/slot_rendering.md)
|
|
4012
|
+
- [Slot rendering flot](https://github.com/EmilStenstrom/django-components/blob/master/docs/slot_rendering.md)
|
|
4013
|
+
- [Slots and blocks](https://github.com/EmilStenstrom/django-components/blob/master/docs/slots_and_blocks.md)
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "django_components"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.102"
|
|
8
8
|
requires-python = ">=3.8, <4.0"
|
|
9
9
|
description = "A way to create simple reusable template components in Django."
|
|
10
10
|
keywords = ["django", "components", "css", "js", "html"]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import re
|
|
2
2
|
from enum import Enum
|
|
3
|
+
from pathlib import Path
|
|
3
4
|
from typing import TYPE_CHECKING, Dict, List, Tuple, Union
|
|
4
5
|
|
|
5
6
|
from django.conf import settings
|
|
@@ -101,7 +102,8 @@ class AppSettings:
|
|
|
101
102
|
|
|
102
103
|
@property
|
|
103
104
|
def DIRS(self) -> List[Union[str, Tuple[str, str]]]:
|
|
104
|
-
|
|
105
|
+
base_dir_path = Path(settings.BASE_DIR)
|
|
106
|
+
return self.settings.get("dirs", [base_dir_path / "components"])
|
|
105
107
|
|
|
106
108
|
@property
|
|
107
109
|
def APP_DIRS(self) -> List[str]:
|
|
@@ -25,7 +25,7 @@ class ComponentsConfig(AppConfig):
|
|
|
25
25
|
# See https://github.com/EmilStenstrom/django-components/discussions/567#discussioncomment-10273632
|
|
26
26
|
# And https://stackoverflow.com/questions/42907285/66673186#66673186
|
|
27
27
|
if app_settings.RELOAD_ON_TEMPLATE_CHANGE:
|
|
28
|
-
dirs = get_dirs()
|
|
28
|
+
dirs = get_dirs(include_apps=False)
|
|
29
29
|
component_filepaths = search_dirs(dirs, "**/*")
|
|
30
30
|
watch_files_for_autoreload(component_filepaths)
|
|
31
31
|
|
|
@@ -4,8 +4,10 @@ import os
|
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
from typing import Callable, List, Optional, Union
|
|
6
6
|
|
|
7
|
+
from django.apps import apps
|
|
7
8
|
from django.conf import settings
|
|
8
9
|
|
|
10
|
+
from django_components.app_settings import app_settings
|
|
9
11
|
from django_components.logger import logger
|
|
10
12
|
from django_components.template_loader import get_dirs
|
|
11
13
|
|
|
@@ -22,16 +24,53 @@ def autodiscover(
|
|
|
22
24
|
You can map the module paths with `map_module` function. This serves
|
|
23
25
|
as an escape hatch for when you need to use this function in tests.
|
|
24
26
|
"""
|
|
25
|
-
dirs = get_dirs()
|
|
27
|
+
dirs = get_dirs(include_apps=False)
|
|
26
28
|
component_filepaths = search_dirs(dirs, "**/*.py")
|
|
27
29
|
logger.debug(f"Autodiscover found {len(component_filepaths)} files in component directories.")
|
|
28
30
|
|
|
31
|
+
if hasattr(settings, "BASE_DIR") and settings.BASE_DIR:
|
|
32
|
+
project_root = str(settings.BASE_DIR)
|
|
33
|
+
else:
|
|
34
|
+
# Fallback for getting the root dir, see https://stackoverflow.com/a/16413955/9788634
|
|
35
|
+
project_root = os.path.abspath(os.path.dirname(__name__))
|
|
36
|
+
|
|
29
37
|
modules: List[str] = []
|
|
38
|
+
|
|
39
|
+
# We handle dirs from `COMPONENTS.dirs` and from individual apps separately.
|
|
40
|
+
#
|
|
41
|
+
# Because for dirs in `COMPONENTS.dirs`, we assume they will be nested under `BASE_DIR`,
|
|
42
|
+
# and that `BASE_DIR` is the current working dir (CWD). So the path relatively to `BASE_DIR`
|
|
43
|
+
# is ALSO the python import path.
|
|
30
44
|
for filepath in component_filepaths:
|
|
31
|
-
module_path = _filepath_to_python_module(filepath)
|
|
32
|
-
# Ignore
|
|
33
|
-
|
|
34
|
-
|
|
45
|
+
module_path = _filepath_to_python_module(filepath, project_root, None)
|
|
46
|
+
# Ignore files starting with dot `.` or files in dirs that start with dot.
|
|
47
|
+
#
|
|
48
|
+
# If any of the parts of the path start with a dot, e.g. the filesystem path
|
|
49
|
+
# is `./abc/.def`, then this gets converted to python module as `abc..def`
|
|
50
|
+
#
|
|
51
|
+
# NOTE: This approach also ignores files:
|
|
52
|
+
# - with two dots in the middle (ab..cd.py)
|
|
53
|
+
# - an extra dot at the end (abcd..py)
|
|
54
|
+
# - files outside of the parent component (../abcd.py).
|
|
55
|
+
# But all these are NOT valid python modules so that's fine.
|
|
56
|
+
if ".." in module_path:
|
|
57
|
+
continue
|
|
58
|
+
|
|
59
|
+
modules.append(module_path)
|
|
60
|
+
|
|
61
|
+
# For for apps, the directories may be outside of the project, e.g. in case of third party
|
|
62
|
+
# apps. So we have to resolve the python import path relative to the package name / the root
|
|
63
|
+
# import path for the app.
|
|
64
|
+
# See https://github.com/EmilStenstrom/django-components/issues/669
|
|
65
|
+
for conf in apps.get_app_configs():
|
|
66
|
+
for app_dir in app_settings.APP_DIRS:
|
|
67
|
+
comps_path = Path(conf.path).joinpath(app_dir)
|
|
68
|
+
if not comps_path.exists():
|
|
69
|
+
continue
|
|
70
|
+
app_component_filepaths = search_dirs([comps_path], "**/*.py")
|
|
71
|
+
for filepath in app_component_filepaths:
|
|
72
|
+
app_component_module = _filepath_to_python_module(filepath, conf.path, conf.name)
|
|
73
|
+
modules.append(app_component_module)
|
|
35
74
|
|
|
36
75
|
return _import_modules(modules, map_module)
|
|
37
76
|
|
|
@@ -67,7 +106,11 @@ def _import_modules(
|
|
|
67
106
|
return imported_modules
|
|
68
107
|
|
|
69
108
|
|
|
70
|
-
def _filepath_to_python_module(
|
|
109
|
+
def _filepath_to_python_module(
|
|
110
|
+
file_path: Union[Path, str],
|
|
111
|
+
root_fs_path: Union[str, Path],
|
|
112
|
+
root_module_path: Optional[str],
|
|
113
|
+
) -> str:
|
|
71
114
|
"""
|
|
72
115
|
Derive python import path from the filesystem path.
|
|
73
116
|
|
|
@@ -77,13 +120,7 @@ def _filepath_to_python_module(file_path: Union[Path, str]) -> str:
|
|
|
77
120
|
- Then the path relative to project root is `app/components/mycomp.py`
|
|
78
121
|
- Which we then turn into python import path `app.components.mycomp`
|
|
79
122
|
"""
|
|
80
|
-
|
|
81
|
-
project_root = str(settings.BASE_DIR)
|
|
82
|
-
else:
|
|
83
|
-
# Fallback for getting the root dir, see https://stackoverflow.com/a/16413955/9788634
|
|
84
|
-
project_root = os.path.abspath(os.path.dirname(__name__))
|
|
85
|
-
|
|
86
|
-
rel_path = os.path.relpath(file_path, start=project_root)
|
|
123
|
+
rel_path = os.path.relpath(file_path, start=root_fs_path)
|
|
87
124
|
rel_path_without_suffix = str(Path(rel_path).with_suffix(""))
|
|
88
125
|
|
|
89
126
|
# NOTE: `Path` normalizes paths to use `/` as separator, while `os.path`
|
|
@@ -91,7 +128,12 @@ def _filepath_to_python_module(file_path: Union[Path, str]) -> str:
|
|
|
91
128
|
sep = os.path.sep if os.path.sep in rel_path_without_suffix else "/"
|
|
92
129
|
module_name = rel_path_without_suffix.replace(sep, ".")
|
|
93
130
|
|
|
94
|
-
|
|
131
|
+
# Combine with the base module path
|
|
132
|
+
full_module_name = f"{root_module_path}.{module_name}" if root_module_path else module_name
|
|
133
|
+
if full_module_name.endswith(".__init__"):
|
|
134
|
+
full_module_name = full_module_name[:-9] # Remove the trailing `.__init__
|
|
135
|
+
|
|
136
|
+
return full_module_name
|
|
95
137
|
|
|
96
138
|
|
|
97
139
|
def search_dirs(dirs: List[Path], search_glob: str) -> List[Path]:
|
|
@@ -40,7 +40,6 @@ from django_components.component_registry import ComponentRegistry
|
|
|
40
40
|
from django_components.component_registry import registry as registry_
|
|
41
41
|
from django_components.context import (
|
|
42
42
|
_FILLED_SLOTS_CONTENT_CONTEXT_KEY,
|
|
43
|
-
_PARENT_COMP_CONTEXT_KEY,
|
|
44
43
|
_REGISTRY_CONTEXT_KEY,
|
|
45
44
|
_ROOT_CTX_CONTEXT_KEY,
|
|
46
45
|
get_injected_context_var,
|
|
@@ -630,16 +629,10 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
|
|
|
630
629
|
else:
|
|
631
630
|
fill_content = self.fill_content
|
|
632
631
|
|
|
633
|
-
# If this is top-level component and it has no parent, use outer context instead
|
|
634
|
-
slot_context_data = context_data
|
|
635
|
-
if not context[_PARENT_COMP_CONTEXT_KEY]:
|
|
636
|
-
slot_context_data = self.outer_context.flatten()
|
|
637
|
-
|
|
638
632
|
_, resolved_fills = resolve_slots(
|
|
639
633
|
context,
|
|
640
634
|
template,
|
|
641
635
|
component_name=self.name,
|
|
642
|
-
context_data=slot_context_data,
|
|
643
636
|
fill_content=fill_content,
|
|
644
637
|
# Dynamic component has a special mark do it doesn't raise certain errors
|
|
645
638
|
is_dynamic_component=getattr(self, "_is_dynamic_component", False),
|
|
@@ -15,7 +15,6 @@ from django_components.utils import find_last_index
|
|
|
15
15
|
_FILLED_SLOTS_CONTENT_CONTEXT_KEY = "_DJANGO_COMPONENTS_FILLED_SLOTS"
|
|
16
16
|
_ROOT_CTX_CONTEXT_KEY = "_DJANGO_COMPONENTS_ROOT_CTX"
|
|
17
17
|
_REGISTRY_CONTEXT_KEY = "_DJANGO_COMPONENTS_REGISTRY"
|
|
18
|
-
_PARENT_COMP_CONTEXT_KEY = "_DJANGO_COMPONENTS_PARENT_COMP"
|
|
19
18
|
_CURRENT_COMP_CONTEXT_KEY = "_DJANGO_COMPONENTS_CURRENT_COMP"
|
|
20
19
|
_INJECT_CONTEXT_KEY_PREFIX = "_DJANGO_COMPONENTS_INJECT__"
|
|
21
20
|
|
|
@@ -57,9 +56,6 @@ def set_component_id(context: Context, component_id: str) -> None:
|
|
|
57
56
|
We use the Context object to pass down info on inside of which component
|
|
58
57
|
we are currently rendering.
|
|
59
58
|
"""
|
|
60
|
-
# Store the previous component so we can detect if the current component
|
|
61
|
-
# is the top-most or not. If it is, then "_parent_component_id" is None
|
|
62
|
-
context[_PARENT_COMP_CONTEXT_KEY] = context.get(_CURRENT_COMP_CONTEXT_KEY, None)
|
|
63
59
|
context[_CURRENT_COMP_CONTEXT_KEY] = component_id
|
|
64
60
|
|
|
65
61
|
|
|
@@ -19,7 +19,7 @@ class Command(BaseCommand):
|
|
|
19
19
|
def handle(self, *args: Any, **options: Any) -> None:
|
|
20
20
|
current_engine = Engine.get_default()
|
|
21
21
|
loader = Loader(current_engine)
|
|
22
|
-
dirs = loader.get_dirs()
|
|
22
|
+
dirs = loader.get_dirs(include_apps=False)
|
|
23
23
|
|
|
24
24
|
if settings.BASE_DIR:
|
|
25
25
|
dirs.append(Path(settings.BASE_DIR) / "templates")
|
|
@@ -25,9 +25,11 @@ class BaseNode(Node):
|
|
|
25
25
|
self.kwargs = kwargs or RuntimeKwargs({})
|
|
26
26
|
|
|
27
27
|
|
|
28
|
+
# NOTE: We consider Nodes to have content only if they have anything else
|
|
29
|
+
# beside whitespace and comments.
|
|
28
30
|
def nodelist_has_content(nodelist: NodeList) -> bool:
|
|
29
31
|
for node in nodelist:
|
|
30
|
-
if isinstance(node, TextNode) and node.s.isspace():
|
|
32
|
+
if isinstance(node, TextNode) and (not node.s or node.s.isspace()):
|
|
31
33
|
pass
|
|
32
34
|
elif isinstance(node, CommentNode):
|
|
33
35
|
pass
|