Skip to content

Commit

Permalink
fixes #121
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Nov 10, 2023
1 parent 556ac50 commit a3f4caf
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 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 `Provide switch to turn off toString() transpilation on enums_to_js <https://github.com/bckohan/django-render-static/issues/121>`_
* Implemented `Allow include_properties to be a list of properties on enums_to_js <https://github.com/bckohan/django-render-static/issues/119>`_


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load enum_test %}
{% block transpile_enums %}
{% enums_to_js enums=enums include_properties=include_properties exclude_properties=exclude_properties class_properties=class_properties properties=properties symmetric_properties=symmetric_properties %}
{% enums_to_js enums=enums include_properties=include_properties exclude_properties=exclude_properties class_properties=class_properties properties=properties symmetric_properties=symmetric_properties to_string=to_string|default_bool:True %}
{% endblock %}

{% enum_tests enums=test_enums|default:enums|enum_list class_properties=class_properties properties=test_properties|default:properties symmetric_properties=test_symmetric_properties|default:symmetric_properties class_name_map=class_name_map %}
{% enum_tests enums=test_enums|default:enums|enum_list class_properties=class_properties properties=test_properties|default:properties symmetric_properties=test_symmetric_properties|default:symmetric_properties class_name_map=class_name_map to_string=to_string|default_bool:True %}
22 changes: 18 additions & 4 deletions render_static/tests/enum_app/templatetags/enum_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
properties=True,
symmetric_properties=None,
class_name_map=None,
to_string=True,
**kwargs
):
self.name_map = name_map
Expand All @@ -45,6 +46,7 @@ def __init__(
if symmetric_properties:
self.symmetric_properties_ = symmetric_properties or []
self.class_name_map_ = class_name_map or self.class_name_map_
self.to_string_ = to_string
super().__init__(*args, **kwargs)

def start_visitation(self):
Expand Down Expand Up @@ -76,7 +78,8 @@ def visit(self, enum, is_bool, final):

yield f'enums.{enum.__name__} = {{'
self.indent()
yield 'strings: {},'
if self.to_string_:
yield 'strings: {},'
for prop in properties:
yield f'{prop}: [],'
yield 'getCheck: false'
Expand All @@ -86,7 +89,9 @@ def visit(self, enum, is_bool, final):
self.indent()
for prop in properties:
yield f'enums.{enum.__name__}.{prop}.push(en.{prop});'
yield f'enums.{enum.__name__}.strings[en.value] = en.toString();'

if self.to_string_:
yield f'enums.{enum.__name__}.strings[en.value] = en.toString();'

if class_properties:
yield f'enums.{enum.__name__}.class_props = {{'
Expand Down Expand Up @@ -127,7 +132,8 @@ def enum_tests(
class_properties=True,
properties=True,
symmetric_properties=False,
class_name_map=None
class_name_map=None,
to_string=True,
):
if name_map is None:
name_map = {en: en.__name__ for en in enums}
Expand All @@ -137,6 +143,14 @@ def enum_tests(
class_properties=class_properties,
properties=properties,
symmetric_properties=symmetric_properties,
class_name_map=class_name_map
class_name_map=class_name_map,
to_string=to_string
).transpile(enums)
)


@register.filter(name='default_bool')
def default_bool(value, default):
if value in [None, '']:
return default
return value
49 changes: 44 additions & 5 deletions render_static/tests/js_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2276,22 +2276,25 @@ def enums_compare(
js_file,
enum_classes,
class_properties=True,
properties=True
properties=True,
to_string=True
):
for enum in enum_classes:
self.enum_compare(
js_file,
enum,
class_properties=class_properties,
properties=properties
properties=properties,
to_string=to_string
)

def enum_compare(
self,
js_file,
cls,
class_properties=True,
properties=True
properties=True,
to_string=True
):
"""
Given a javascript file and a list of classes, evaluate the javascript
Expand Down Expand Up @@ -2329,10 +2332,11 @@ def to_js_test(value):
return value

enum_dict = {
'strings': {
**({'strings': {
en.value.isoformat()
if isinstance(en.value, date)
else str(en.value): str(en) for en in cls},
else str(en.value): str(en) for en in cls
}} if to_string else {}),
**{
prop: [
to_js_test(getattr(en, prop))
Expand Down Expand Up @@ -2556,6 +2560,41 @@ def test_class_prop_option(self):
'class_name',
get_content(ENUM_STATIC_DIR / 'enum_app/test.js')
)

@override_settings(
STATIC_TEMPLATES={
'context': {
'include_properties': True,
'class_properties': False,
'properties': True,
'symmetric_properties': False,
'to_string': False
},
'templates': [
('enum_app/test.js', {
'context': {
'enums': EnumTester.MapBoxStyle
}
}),
]
}
)
def test_disable_to_string(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,
to_string=False
)
self.assertNotIn(
'class_name',
get_content(ENUM_STATIC_DIR / 'enum_app/test.js')
)
self.assertNotIn(
'toString',
get_content(ENUM_STATIC_DIR / 'enum_app/test.js')
)

@override_settings(
STATIC_TEMPLATES={
Expand Down
10 changes: 8 additions & 2 deletions render_static/transpilers/enums_to_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class EnumClassWriter(EnumTranspiler): # pylint: disable=R0902
:param class_properties: If true, include all Django classproperties as
static members on the transpiled Enum class. May also be an iterable
of specific property names to include.
:param to_string: If true (default) include a toString() method that
returns a string representation of the enum.
:param kwargs: additional kwargs for the base transpiler classes.
"""

Expand All @@ -142,6 +144,7 @@ class EnumClassWriter(EnumTranspiler): # pylint: disable=R0902

str_prop_: Optional[str] = None
str_is_prop_: bool = False
to_string_: bool = True

def to_js(self, value: Any):
"""
Expand Down Expand Up @@ -349,6 +352,7 @@ def __init__( # pylint: disable=R0913
bool,
Collection[str]
] = class_properties_kwarg_,
to_string: bool = to_string_,
**kwargs
) -> None:
super().__init__(**kwargs)
Expand Down Expand Up @@ -385,6 +389,7 @@ def __init__( # pylint: disable=R0913
)
self.class_properties_kwarg_ = class_properties
self.class_name_map_ = {}
self.to_string_ = to_string

def visit(
self,
Expand Down Expand Up @@ -417,8 +422,9 @@ def visit(
yield ''
yield from self.constructor(enum)
yield ''
yield from self.to_string(enum)
yield ''
if self.to_string_:
yield from self.to_string(enum)
yield ''
yield from self.getter()
yield ''
yield from self.iterator(enum)
Expand Down

0 comments on commit a3f4caf

Please sign in to comment.