Skip to content

Commit

Permalink
fix multiple issues introduced by settings.ANGLES.default_component_m…
Browse files Browse the repository at this point in the history
…apper
  • Loading branch information
nanuxbe committed Oct 21, 2024
1 parent 2fbdc5f commit 1dd53dc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/source/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ Provide additional mappers. `Dictionary` which defaults to `{}`. More details ab

## `default_component_mapper`

Provide a default mapper. This defaults to `dj_angles.mappers.include.map_include`
Provide a default mapper. This defaults to `dj_angles.mappers.angle.map_include_when_no_tag`
12 changes: 11 additions & 1 deletion src/dj_angles/mappers/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
from django.template.exceptions import TemplateDoesNotExist
from minestrone import HTML

from dj_angles.mappers.include import get_include_template_file
from dj_angles.mappers.include import get_include_template_file, map_include
from dj_angles.strings import dequotify

if TYPE_CHECKING:
from dj_angles.tags import Tag


def map_include_when_no_tag(tag: "Tag") -> str:
tag.attributes.prepend(tag.component_name)
django_template_tag = map_include(tag)

if tag.is_end and tag.is_shadow or (tag.start_tag and tag.start_tag.is_shadow):
django_template_tag = f"</template>{django_template_tag}"

return django_template_tag


def map_angles_include(tag: "Tag") -> str:
template_file = dequotify(get_include_template_file(tag))
wrapping_tag_name = tag.get_wrapping_tag_name(name=template_file)
Expand Down
15 changes: 1 addition & 14 deletions src/dj_angles/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,12 @@ def get_django_template_tag(self, slots: Optional[list[tuple[str, Element]]] = N
param slots: List of slots which is a tuple of slot name and inner html.
"""

if self.django_template_tag is None and self.is_end:
wrapping_tag_name = self.get_wrapping_tag_name()

django_template_tag = ""

if self.is_shadow or (self.start_tag and self.start_tag.is_shadow):
django_template_tag = "</template>"

return f"{django_template_tag}</{wrapping_tag_name}>"

if self.django_template_tag is None:
# Assume any missing template tag should use the fallback mapper
self.django_template_tag = import_string(
get_setting("default_component_mapper", "dj_angles.mappers.include.map_include")
get_setting("default_component_mapper", "dj_angles.mappers.angles.map_include_when_no_tag")
)

# Add component name to the template tags
self.attributes.prepend(self.component_name)

if slots and self.is_include:
self.django_template_tag = map_angles_include
self.slots = slots
Expand Down
54 changes: 41 additions & 13 deletions tests/dj_angles/mappers/thirdparty/test_map_bird_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@
from tests.dj_angles.tags import create_tag


def override_setting(settings, key, value):
old_value = getattr(settings, key, None)
setattr(settings, key, value)
def override_setting(settings, value):
old_value = settings.ANGLES.get("default_component_mapper", "dj_angles.mappers.include.map_include")
settings.ANGLES["default_component_mapper"] = "dj_angles.mappers.thirdparty.map_bird_component"
return old_value


def restore_setting(settings, key, old_value):
setattr(settings, key, old_value)
def restore_setting(settings, old_value):
settings.ANGLES["default_component_mapper"] = old_value


def test_not_self_closing(settings):
initial_setting = override_setting(settings, "default_component_mapper", "dj_angles.mappers.thirdparty.map_bird_component")
assert True == True
restore_setting(settings, "default_component_mapper", initial_setting)
expected = "{% bird partial %}"

html = "<dj-partial>"
Expand All @@ -27,8 +24,6 @@ def test_not_self_closing(settings):


def test_is_closing(settings):
initial_setting = override_setting(settings, "default_component_mapper", "dj_angles.mappers.thirdparty.map_bird_component")

expected = "{% endbird %}"

html = "</dj-partial>"
Expand All @@ -37,11 +32,9 @@ def test_is_closing(settings):
actual = map_bird_component(tag=tag)

assert actual == expected
restore_setting(settings, "default_component_mapper", initial_setting)


def test_self_closing(settings):
initial_setting = override_setting(settings, "default_component_mapper", "dj_angles.mappers.thirdparty.map_bird_component")

expected = "{% bird partial / %}"

Expand All @@ -51,4 +44,39 @@ def test_self_closing(settings):
actual = map_bird_component(tag=tag)

assert actual == expected
restore_setting(settings, "default_component_mapper", initial_setting)


def test_not_self_closing_from_settings(settings):
settings.ANGLES["default_component_mapper"] = "dj_angles.mappers.thirdparty.map_bird_component"
expected = "{% bird partial %}"

html = "<dj-partial>"
tag = create_tag(html)

actual = tag.get_django_template_tag()

assert actual == expected


def test_self_closing_from_settings(settings):
settings.ANGLES["default_component_mapper"] = "dj_angles.mappers.thirdparty.map_bird_component"
expected = "{% bird partial / %}"

html = "<dj-partial />"

tag = create_tag(html)
actual = tag.get_django_template_tag()

assert expected == actual


def test_closing_from_settings(settings):
settings.ANGLES["default_component_mapper"] = "dj_angles.mappers.thirdparty.map_bird_component"
expected = "{% endbird %}"

html = "</dj-partial>"

tag = create_tag(html)
actual = tag.get_django_template_tag()

assert expected == actual
12 changes: 12 additions & 0 deletions tests/dj_angles/tags/test_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_get_wrapping_tag_name_with_name():
assert expected == actual



def test_get_wrapping_tag_name_component():
expected = "dj-partial"

Expand All @@ -35,3 +36,14 @@ def test_get_wrapping_tag_name_component_with_key():
actual = tag.get_wrapping_tag_name()

assert expected == actual


def test_default_mapping(settings):
expected = "<dj-partial>{% include 'partial.html' %}</dj-partial>"

html = "<dj-partial />"

tag = create_tag(html)
actual = tag.get_django_template_tag()

assert expected == actual

0 comments on commit 1dd53dc

Please sign in to comment.