Skip to content

Commit

Permalink
(WIP) Add filters to party list page
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekabyx committed Jul 20, 2023
1 parent 6059f98 commit baf111c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 33 deletions.
101 changes: 101 additions & 0 deletions wcivf/apps/parties/filters.py
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"]
49 changes: 49 additions & 0 deletions wcivf/apps/parties/templates/parties/parties_view.html
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 %}
36 changes: 6 additions & 30 deletions wcivf/apps/parties/templates/parties/party_list.html
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 %}
15 changes: 12 additions & 3 deletions wcivf/apps/parties/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from django.views.generic import ListView, DetailView
from django.views.generic import DetailView, TemplateView

from .models import Party
from .filters import PartyRegisterFilter


class PartiesView(ListView):
queryset = Party.objects.exclude(personpost=None)
class PartiesView(TemplateView):
template_name = "parties/parties_view.html"

def get_context_data(self, *args, **kwargs):
context = super(PartiesView, self).get_context_data(*args, **kwargs)
queryset = Party.objects.exclude(personpost=None).order_by("party_name")
f = PartyRegisterFilter(self.request.GET, queryset=queryset)
context["filter"] = f
context["queryset"] = f.qs
return context


class PartyView(DetailView):
Expand Down

0 comments on commit baf111c

Please sign in to comment.