Skip to content

Commit

Permalink
fixes #119
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Nov 10, 2023
1 parent ce22fe9 commit 556ac50
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v2.1.0
* Implemented `Add a pass through getter for enums_to_js transpilation. <https://github.com/bckohan/django-render-static/issues/126>`_
* Implemented `enum transpilation should iterate through value properties instead of hardcoding a switch statement. <https://github.com/bckohan/django-render-static/issues/125>`
* Implemented `Add type check and return to getter on transpiled enum classes.. <https://github.com/bckohan/django-render-static/issues/122>`_
* Implemented `Allow include_properties to be a list of properties on enums_to_js <https://github.com/bckohan/django-render-static/issues/119>`_


v2.0.3
Expand Down
41 changes: 41 additions & 0 deletions render_static/tests/js_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2664,6 +2664,47 @@ def test_exclude_props_param(self):
self.assertIn('this.slug = ', contents)
self.assertIn('this.label = ', contents)


@override_settings(
STATIC_TEMPLATES={
'context': {
'include_properties': ['slug', 'label'],
'properties': True,
'test_properties': [
'slug',
'label',
'value'
],
'symmetric_properties': False
},
'templates': [
('enum_app/test.js', {
'context': {
'enums': [
EnumTester.MapBoxStyle
]
}
}),
]
}
)
def test_include_props_list(self):
call_command('renderstatic', 'enum_app/test.js')
self.enums_compare(
js_file=ENUM_STATIC_DIR / 'enum_app/test.js',
enum_classes=[EnumTester.MapBoxStyle],
class_properties=False,
properties=['slug', 'label', 'value']
)
contents = get_content(ENUM_STATIC_DIR / 'enum_app/test.js')
self.assertNotIn('uri', contents)
self.assertNotIn('version', contents)
self.assertNotIn('name', contents)
self.assertIn('this.value = ', contents)
self.assertIn('this.slug = ', contents)
self.assertIn('this.label = ', contents)


@override_settings(
STATIC_TEMPLATES={
'ENGINES': [{
Expand Down
20 changes: 16 additions & 4 deletions render_static/transpilers/enums_to_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ class EnumClassWriter(EnumTranspiler): # pylint: disable=R0902
:py:class:`render_static.transpilers.enums_to_js.UnrecognizedBehavior`.
:param export: If true the classes will be exported - Default: False
:param include_properties: If true, any python properties present on the
enums will be included in the transpiled javascript enums.
enums will be included in the transpiled javascript enums. May also
be an iterable of property names to include. `value` will always
be included.
:param symmetric_properties: If true, properties that the enums may be
instantiated from will be automatically determined and included in the
get() function. If False (default), enums will not be instantiable
Expand Down Expand Up @@ -133,7 +135,7 @@ class EnumClassWriter(EnumTranspiler): # pylint: disable=R0902
class_properties_kwarg_: Union[bool, Collection[str]] = True
class_properties_: List[str] = []

include_properties_: bool = True
include_properties_: Union[Collection[str], bool] = True
builtins_: List[str] = ['value', 'name']
properties_: List[str] = []
exclude_properties_: Set[str]
Expand Down Expand Up @@ -189,7 +191,7 @@ def properties(self, enum: Type[Enum]):
bltin for bltin in self.builtins_
if bltin not in self.exclude_properties_
]
if self.include_properties_:
if self.include_properties_ is True:
if (
hasattr(list(enum)[0], 'label') and
'label' not in builtins and
Expand All @@ -215,6 +217,8 @@ def properties(self, enum: Type[Enum]):
and prop not in builtins and prop not in props_on_class
]
]
elif self.include_properties_:
self.properties_ = list({*self.include_properties_, 'value'})
else:
self.properties_ = builtins

Expand Down Expand Up @@ -332,7 +336,10 @@ def __init__( # pylint: disable=R0913
UnrecognizedBehavior
] = on_unrecognized_,
export: bool = export_,
include_properties: bool = include_properties_,
include_properties: Union[
bool,
Collection[str]
] = include_properties_,
symmetric_properties: Union[
bool,
Collection[str]
Expand Down Expand Up @@ -366,6 +373,11 @@ def __init__( # pylint: disable=R0913
)
self.export_ = export
self.include_properties_ = include_properties
self.include_properties_ = (
set(include_properties)
if isinstance(include_properties, Collection) else
include_properties
)
self.symmetric_properties_kwarg_ = symmetric_properties
self.exclude_properties_ = (
set(exclude_properties)
Expand Down

0 comments on commit 556ac50

Please sign in to comment.