coderedcorp / coderedcorp/coderedcms
[BUG] Incorrect methodology for `is_menu_item_dropdown` simple tag
- Dominant language
- Python
- Stars
- 765
- Forks
- 154
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
A clear and concise description of what the bug is.
This code block:
@register.simple_tag
def is_menu_item_dropdown(value):
return \
len(value.get('sub_links', [])) > 0 or \
(
value.get('show_child_links', False) and \
len(value.get('page', []).get_children().live()) > 0
)
What if you actually hit the case where `value.get('page', [])` defaults to a list? Then `.get_children().live()` is being called on an object which doesn't have these methods. Also, probably shouldn't inline all of this for readability despite the faster evaluation.
One simple fix:
@register.simple_tag
def is_menu_item_dropdown(value):
has_sub_links = len(value.get('sub_links', [])) > 0
show_child_links = value.get('show_child_links', False)
page_has_children = False
if has_sub_links or show_child_links: # No need to evaluate the below if this case fails...
page = value.get('page')
if page is not None:
page_has_children= len(page.get_children().live()) > 0
return has_sub_links or (show_child_links and page_has_children)
This is also assuming you still want to be verbose with the evaluation into booleans here.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.