Skip to content

Commit

Permalink
v2.2.1: Fix recursive fetch depth for ManyToOneRel (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger authored Jan 9, 2023
1 parent 8a7b641 commit 62ea6f1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ Using the following categories, list your changes in this order:

- Nothing (yet)

## [2.2.1] - 2022-01-09

### Fixed

- Fixed bug where `use_query` would not recursively fetch many-to-one relationships.
- IDOM preloader will now print out the exception stack when failing to import a module.

## [2.2.0] - 2022-12-28

### Added
Expand Down Expand Up @@ -184,7 +191,8 @@ Using the following categories, list your changes in this order:

- Support for IDOM within the Django

[unreleased]: https://github.com/idom-team/django-idom/compare/2.2.0...HEAD
[unreleased]: https://github.com/idom-team/django-idom/compare/2.2.1...HEAD
[2.2.1]: https://github.com/idom-team/django-idom/compare/2.2.0...2.2.1
[2.2.0]: https://github.com/idom-team/django-idom/compare/2.1.0...2.2.0
[2.1.0]: https://github.com/idom-team/django-idom/compare/2.0.1...2.1.0
[2.0.1]: https://github.com/idom-team/django-idom/compare/2.0.0...2.0.1
Expand Down
2 changes: 1 addition & 1 deletion src/django_idom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django_idom.websocket.paths import IDOM_WEBSOCKET_PATH


__version__ = "2.2.0"
__version__ = "2.2.1"
__all__ = [
"IDOM_WEBSOCKET_PATH",
"IdomWebsocket",
Expand Down
11 changes: 6 additions & 5 deletions src/django_idom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _register_components(self, components: set[str]) -> None:
_logger.info("IDOM preloader has detected component %s", component)
_register_component(component)
except Exception:
_logger.error(
_logger.exception(
"\033[91m"
"IDOM failed to register component '%s'! "
"This component path may not be valid, "
Expand Down Expand Up @@ -236,15 +236,16 @@ def django_query_postprocessor(

elif many_to_many and isinstance(field, ManyToManyField):
prefetch_fields.append(field.name)

if prefetch_fields:
prefetch_related_objects([data], *prefetch_fields)
for field_str in prefetch_fields:
django_query_postprocessor(
getattr(data, field.name).get_queryset(),
getattr(data, field_str).get_queryset(),
many_to_many=many_to_many,
many_to_one=many_to_one,
)

if prefetch_fields:
prefetch_related_objects([data], *prefetch_fields)

# Unrecognized type
else:
raise TypeError(
Expand Down
17 changes: 13 additions & 4 deletions tests/test_app/components.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
from pathlib import Path

from django.http import HttpRequest
from django.shortcuts import render
Expand Down Expand Up @@ -44,13 +45,21 @@ def parameterized_component(x, y):
)


victory = web.module_from_template("react", "victory-bar", fallback="...")
VictoryBar = web.export(victory, "VictoryBar")
SimpleButtonModule = web.module_from_file(
"SimpleButton",
Path(__file__).parent / "tests" / "js" / "simple-button.js",
resolve_exports=False,
fallback="...",
)
SimpleButton = web.export(SimpleButtonModule, "SimpleButton")


@component
def simple_bar_chart():
return html._(VictoryBar(), html.hr())
def simple_button():
return html._(
SimpleButton({"id": "simple-button"}),
html.hr(),
)


@component
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1>IDOM Test Page</h1>
<div>{% component "test_app.components.hello_world" class="hello-world" %}</div>
<div>{% component "test_app.components.button" class="button" %}</div>
<div>{% component "test_app.components.parameterized_component" class="parametarized-component" x=123 y=456 %}</div>
<div>{% component "test_app.components.simple_bar_chart" %}</div>
<div>{% component "test_app.components.simple_button" %}</div>
<div>{% component "test_app.components.use_websocket" %}</div>
<div>{% component "test_app.components.use_scope" %}</div>
<div>{% component "test_app.components.use_location" %}</div>
Expand Down
25 changes: 25 additions & 0 deletions tests/test_app/tests/js/simple-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { h, render } from "https://unpkg.com/preact?module";
import htm from "https://unpkg.com/htm?module";

const html = htm.bind(h);

export function bind(node, config) {
return {
create: (type, props, children) => h(type, props, ...children),
render: (element) => render(element, node),
unmount: () => render(null, node),
};
}

export function SimpleButton(props) {
return h(
"button",
{
id: props.id,
onClick(event) {
props.onClick({ data: props.eventResponseData });
},
},
"simple button"
);
}
2 changes: 1 addition & 1 deletion tests/test_app/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_parametrized_component(self):
self.page.locator("#parametrized-component[data-value='579']").wait_for()

def test_component_from_web_module(self):
self.page.wait_for_selector(".VictoryContainer")
self.page.wait_for_selector("#simple-button")

def test_use_websocket(self):
self.page.locator("#use-websocket[data-success=true]").wait_for()
Expand Down

0 comments on commit 62ea6f1

Please sign in to comment.