-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace international investment filters accordion with DS accordion
- Loading branch information
1 parent
94053ea
commit a6ac257
Showing
4 changed files
with
62 additions
and
98 deletions.
There are no files selected for viewing
97 changes: 3 additions & 94 deletions
97
international_investment/templates/investment/includes/filters.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
57 changes: 57 additions & 0 deletions
57
international_investment/templatetags/investment_filters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from django import template | ||
from django.template.defaultfilters import urlencode | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter | ||
def get_url(value, param=None): | ||
if param is None: | ||
return value | ||
return f"{value}?{urlencode(param)}" | ||
|
||
|
||
@register.simple_tag | ||
def get_filter_accordion_items(form): | ||
items = [] | ||
|
||
for field in form: | ||
if field.field.widget.input_type == 'checkbox': | ||
items.append( | ||
{ | ||
'heading': {'text': field.label}, | ||
'content': {'html': _create_checkbox_html(field, _get_field_values(field))}, | ||
} | ||
) | ||
|
||
return items | ||
|
||
|
||
def _get_field_values(field): | ||
if hasattr(field, 'value'): | ||
if callable(field.value): | ||
return field.value() or [] | ||
return field.value or [] | ||
return [] | ||
|
||
|
||
def _create_checkbox_html(field, field_values): | ||
checkboxes_html = ( | ||
'<div class="govuk-checkboxes govuk-checkboxes--small fixed-height-scroll govuk-!-padding-left-2" ' | ||
'data-module="govuk-checkboxes">' | ||
) | ||
|
||
for action in field: | ||
value = action.data['value'] if isinstance(action.data, dict) else action.data.value | ||
checked = 'checked' if value in field_values else '' | ||
checkboxes_html += ( | ||
f'<div class="govuk-checkboxes__item">' | ||
f'<input {checked} type="checkbox" name="{field.name}" ' | ||
f'value="{value}" class="govuk-checkboxes__input" ' | ||
f'id="{action.id_for_label}">' | ||
f'<label class="govuk-label govuk-checkboxes__label" ' | ||
f'for="{action.id_for_label}">{action.choice_label}</label>' | ||
f'</div>' | ||
) | ||
|
||
return checkboxes_html + '</div>' |