-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(WIP) Add filters to party list page
- Loading branch information
Showing
4 changed files
with
168 additions
and
33 deletions.
There are no files selected for viewing
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,101 @@ | ||
from urllib.parse import urlencode | ||
from django.db.models import BLANK_CHOICE_DASH | ||
from django.utils.encoding import force_str | ||
from django.utils.safestring import mark_safe | ||
from django_filters.widgets import LinkWidget | ||
|
||
import django_filters | ||
|
||
from parties.models import Party | ||
|
||
|
||
def party_register_choices(): | ||
return [ | ||
("GB", "Great Britain"), | ||
("NI", "Northern Ireland"), | ||
] | ||
|
||
|
||
def party_status_choices(): | ||
return [ | ||
("Registered", "Registered"), | ||
("Deregistered", "Deregistered") | ||
] | ||
|
||
|
||
class DSLinkWidget(LinkWidget): | ||
""" | ||
The LinkWidget doesn't allow iterating over choices in the template layer | ||
to change the HTML wrapping the widget. | ||
This breaks the way that Django *should* work, so we have to subclass | ||
and alter the HTML in Python :/ | ||
https://github.com/carltongibson/django-filter/issues/880 | ||
""" | ||
|
||
def render(self, name, value, attrs=None, choices=(), renderer=None): | ||
if not hasattr(self, "data"): | ||
self.data = {} | ||
if value is None: | ||
value = "" | ||
self.build_attrs(self.attrs, extra_attrs=attrs) | ||
output = [] | ||
options = self.render_options(choices, [value], name) | ||
if options: | ||
output.append(options) | ||
# output.append('</ul>') | ||
return mark_safe("\n".join(output)) | ||
|
||
def render_option(self, name, selected_choices, option_value, option_label): | ||
option_value = force_str(option_value) | ||
if option_label == BLANK_CHOICE_DASH[0][1]: | ||
option_label = "All" | ||
data = self.data.copy() | ||
data[name] = option_value | ||
selected = data == self.data or option_value in selected_choices | ||
try: | ||
url = data.urlencode() | ||
except AttributeError: | ||
url = urlencode(data) | ||
return self.option_string() % { | ||
"attrs": selected and ' aria-current="true"' or "", | ||
"query_string": url, | ||
"label": force_str(option_label), | ||
} | ||
|
||
|
||
class PartyRegisterFilter(django_filters.FilterSet): | ||
def party_register_filter(self, queryset, name, value): | ||
return queryset.filter(register=value) | ||
|
||
def party_status_filter(self, queryset, name, value): | ||
return queryset.filter(status=value) | ||
|
||
def party_nations_filter(self, queryset, name, value): | ||
return queryset.filter(nations__contains=[value]) | ||
|
||
register = django_filters.ChoiceFilter( | ||
widget=DSLinkWidget, | ||
method="party_register_filter", | ||
choices=party_register_choices, | ||
label="Party Register", | ||
) | ||
|
||
status = django_filters.ChoiceFilter( | ||
widget=DSLinkWidget(), | ||
method="party_status_filter", | ||
choices=party_status_choices, | ||
label="Party Status", | ||
) | ||
|
||
nations = django_filters.ChoiceFilter( | ||
widget=DSLinkWidget(), | ||
method="party_nations_filter", | ||
choices=[("ENG", "England"), ("WAL", "Wales"), ("SCO", "Scotland")], | ||
label="Nation", | ||
) | ||
|
||
class Meta: | ||
model = Party | ||
fields = ["register", "status", "nations"] |
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,49 @@ | ||
{% extends "base.html" %} | ||
{% load i18n %} | ||
|
||
{% block page_title %}{% trans "UK Political Parties" %}{% endblock page_title %} | ||
{% block og_title_content %}{% trans "UK Political Parties" %}{% endblock og_title_content %} | ||
{% block og_description_content %}{% trans "List of candidates for UK Political Parties" %}{% endblock og_description_content %} | ||
|
||
{% block content %} | ||
<nav class="ds-breadcrumbs ds-stack" aria-label="{% trans 'You are here' %}: {{ request.path }}"> | ||
<ol> | ||
<li> | ||
<a href="{% url 'home_view' %}">Home</a> | ||
</li> | ||
<li>{% trans "Current: Parties" %}</li> | ||
</ol> | ||
</nav> | ||
|
||
|
||
<div class="ds-stack-smaller"> | ||
<div class="ds-card"> | ||
<div class="ds-card-body"> | ||
<h1>{% trans "UK political parties" %}</h1> | ||
{% blocktrans trimmed %} | ||
<p>The following is a list of all political parties represented by candidates in Democracy Club's UK election | ||
database (est. 2015). The list includes active, deregistered, and defunct parties.</p> | ||
|
||
<p>Party names and emblems are taken from the <a href="http://www.electoralcommission.org.uk/">Electoral | ||
Commission's register of political parties.</a></p> | ||
{% endblocktrans %} | ||
<aside class="ds-filter" aria-labelledby="filter-label"> | ||
<div class="ds-filter-cluster"> | ||
<ul aria-labelledby="filter-label"> | ||
<li id="filter-label" class="ds-filter-label" aria-hidden="true">Filters:</li> | ||
</ul> | ||
</div> | ||
{% for field in filter.form %} | ||
<div class="ds-filter-cluster"> | ||
<ul aria-label="{{ field.label }}"> | ||
<li>{{ field.label }}: {{ field }}</li> | ||
</ul> | ||
</div> | ||
{% endfor %} | ||
</aside> | ||
{% include "parties/party_list.html" with parties=queryset %} | ||
</div> | ||
</div> | ||
</div> | ||
|
||
{% endblock content %} |
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 |
---|---|---|
@@ -1,35 +1,11 @@ | ||
{% extends "base.html" %} | ||
{% load i18n %} | ||
} | ||
{% block page_title %}{% trans "UK Political Parties" %}{% endblock page_title %} | ||
{% block og_title_content %}{% trans "UK Political Parties" %}{% endblock og_title_content %} | ||
{% block og_description_content %}{% trans "List of candidates for UK Political Parties" %}{% endblock og_description_content %} | ||
|
||
|
||
{% block content %} | ||
<nav class="ds-breadcrumbs ds-stack" aria-label="{% trans 'You are here' %}: {{ request.path }}"> | ||
<ol> | ||
<li> | ||
<a href="{% url 'home_view' %}">Home</a> | ||
</li> | ||
<li>{% trans "Current: Parties" %}</li> | ||
</ol> | ||
</nav> | ||
<div class="ds-card ds-stack"> | ||
<div class="ds-card-body"> | ||
{% blocktrans trimmed %} | ||
<h2>UK political parties</h2> | ||
<p>The following is a list of all political parties represented by candidates in Democracy Club's UK election | ||
database (est. 2015). The list includes active, deregistered, and defunct parties.</p> | ||
|
||
<p>Party names and emblems are taken from the <a href="http://www.electoralcommission.org.uk/">Electoral | ||
Commission's register of political parties.</a></p> | ||
{% endblocktrans %} | ||
<div id="election-urls" > | ||
{% for party in parties %} | ||
<ul> | ||
{% for party in object_list %} | ||
<li><a href="{{ party.get_absolute_url }}">{{ party.party_name }}</a></li> | ||
{% endfor %} | ||
<li> | ||
<a href="{{ party.get_absolute_url }}">{{ party.format_name }}</a> | ||
</li> | ||
</ul> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
{% endblock content %} |
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