Skip to content

Commit 06cdfb4

Browse files
committed
Add javascript_exclude to view Meta.
1 parent 5ef7d5d commit 06cdfb4

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

django_unicorn/components.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import inspect
33
import logging
44
import pickle
5-
from typing import Any, Callable, Dict, List, Optional, Type, Union
5+
from typing import Any, Callable, Dict, List, Optional, Sequence, Type, Union
66

77
from django.core.exceptions import ImproperlyConfigured
88
from django.db.models import Model
@@ -471,6 +471,13 @@ def get_frontend_context_variables(self) -> str:
471471
attributes = self._attributes()
472472
frontend_context_variables.update(attributes)
473473

474+
# Remove any field in `javascript_exclude` from the `frontend_context_variables`
475+
if hasattr(self, "Meta") and hasattr(self.Meta, "javascript_exclude"):
476+
if isinstance(self.Meta.javascript_exclude, Sequence):
477+
for field_name in self.Meta.javascript_exclude:
478+
if field_name in frontend_context_variables:
479+
del frontend_context_variables[field_name]
480+
474481
# Add cleaned values to `frontend_content_variables` based on the widget in form's fields
475482
form = self._get_form(attributes)
476483

@@ -746,7 +753,8 @@ def _is_public(self, name: str) -> bool:
746753
excludes = []
747754

748755
if hasattr(self, "Meta") and hasattr(self.Meta, "exclude"):
749-
excludes = self.Meta.exclude
756+
if isinstance(self.Meta.exclude, Sequence):
757+
excludes = self.Meta.exclude
750758

751759
return not (
752760
name.startswith("_")

tests/components/test_component.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,27 @@ def test_is_public_protected(component):
101101

102102
def test_is_public_http_method_names(component):
103103
assert component._is_public("http_method_names") == False
104+
105+
106+
def test_meta_javascript_exclude():
107+
class TestComponent(UnicornView):
108+
name = "World"
109+
110+
class Meta:
111+
javascript_exclude = ("name",)
112+
113+
component = TestComponent(component_id="asdf1234", component_name="hello-world")
114+
assert "name" not in component.get_frontend_context_variables()
115+
assert "name" in component.get_context_data()
116+
117+
118+
def test_meta_exclude():
119+
class TestComponent(UnicornView):
120+
name = "World"
121+
122+
class Meta:
123+
exclude = ("name",)
124+
125+
component = TestComponent(component_id="asdf1234", component_name="hello-world")
126+
assert "name" not in component.get_frontend_context_variables()
127+
assert "name" not in component.get_context_data()

0 commit comments

Comments
 (0)