Skip to content

Commit

Permalink
Only return data that is related to a unicorn:model element.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill committed Oct 11, 2023
1 parent c22fcf0 commit bdb6925
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions django_unicorn/components/unicorn_template_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
)


def _has_unicorn_model_attribute(tag):
return tag.has_attr("unicorn:model") or tag.has_attr("u:model")


def is_html_well_formed(html: str) -> bool:
"""
Whether the passed-in HTML is missing any closing elements which can cause issues when rendering.
Expand Down Expand Up @@ -121,6 +125,14 @@ def render(self):
# despite https://thehftguy.com/2020/07/28/making-beautifulsoup-parsing-10-times-faster/
soup = BeautifulSoup(content, features="html.parser")
root_element = get_root_element(soup)

# Get all `unicorn:model`s to determine what data is necessary to send
unicorn_model_elements = root_element.find_all(_has_unicorn_model_attribute)
unicorn_models = [e.attrs.get("unicorn:model") or e.attrs.get("u:model") for e in unicorn_model_elements]

for key_in_context_not_a_model in set(frontend_context_variables_dict.keys()) - set(unicorn_models):
del frontend_context_variables_dict[key_in_context_not_a_model]

root_element["unicorn:id"] = self.component.component_id
root_element["unicorn:name"] = self.component.component_name
root_element["unicorn:key"] = self.component.component_key
Expand Down

0 comments on commit bdb6925

Please sign in to comment.