Skip to content

Commit

Permalink
Allow to filter parties by country
Browse files Browse the repository at this point in the history
  • Loading branch information
homeworkprod committed May 16, 2024
1 parent 8ade0ff commit 8542ce4
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ol>
<li><a class="nav-item" href="{{ url_for('party.index') }}">{{ _('Parties') }}</a></li>
<li><a class="nav-item" href="{{ url_for('series.index') }}">{{ _('Series') }}</a></li>
<li><a class="nav-item" href="{{ url_for('party.index_countries') }}">{{ _('Countries') }}</a></li>
</ol>
</nav>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
</a>
</div>
<div class="column text--centered">
<div class="bignumber">{{ country_count }}</div>
<div class="bignumber-caption">countries</div>
<a href="{{ url_for('party.index_countries') }}">
<div class="bignumber">{{ country_count }}</div>
<div class="bignumber-caption">countries</div>
</a>
</div>
<div class="column text--centered">
<div class="bignumber">0</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends 'layout/base.html' %}

{% block body %}

<div class="breadcrumbs"><a href="{{ url_for('.index_countries') }}">{{ 'Countries' }}</a> /</div>
<h1>{{ country.flag }} {{ country.name }} <small>{{ pagination.total }}</small></h1>

<div class="block">
{{ pagination.links }}
</div>

{% include 'party/_list.html' %}

<div class="block text--centered">
{{ pagination.info }}
</div>

<div class="block">
{{ pagination.links }}
</div>

{%- endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends 'layout/base.html' %}

{% block body %}

<h1>{{ _('Countries') }} <small>{{ countries_with_party_count|length }}</small></h1>

<table class="table--index is-centered-h">
<thead>
<tr>
<th colspan="2">{{ _('Country') }}</th>
<th class="number">{{ _('Parties') }}</th>
</tr>
</thead>
<tbody>
{%- for country, party_count in countries_with_party_count|sort(attribute='0.name') %}
<tr>
<td>{{ country.flag }}</td>
<td><a href="{{ url_for('party.index_by_country', country_code=country.alpha_2.lower()) }}">{{ country.name }}</a></td>
<td class="number">{{ party_count }}</td>
</tr>
{%- endfor %}
</tbody>
</table>

{%- endblock %}
74 changes: 70 additions & 4 deletions src/lanpartydb_website/blueprints/party/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
:License: MIT
"""

from collections import Counter

from flask import abort, current_app, request
from flask_paginate import Pagination
from lanpartydb.models import Party
Expand All @@ -26,7 +28,7 @@ def index(page: int):
per_page = request.args.get('per_page', type=int, default=50)
offset = (page - 1) * per_page

parties = list(current_app.parties_by_slug.values())
parties = _get_parties()
parties.sort(key=lambda party: party.start_on, reverse=True)

pagination = Pagination(page=page, per_page=per_page, total=len(parties))
Expand All @@ -40,6 +42,54 @@ def index(page: int):
}


@blueprint.get('/-/by-country/')
@templated
def index_countries():
counter = Counter()
for party in _get_parties():
if party.location:
counter[party.location.country_code] += 1

country_codes_with_party_count = list(counter.items())

countries_with_party_count = [
(_find_country(country_code), party_count)
for country_code, party_count in country_codes_with_party_count
]

return {
'countries_with_party_count': countries_with_party_count,
}


@blueprint.get('/-/by-country/<country_code>/', defaults={'page': 1})
@blueprint.get('/-/by-country/<country_code>/pages/<int:page>/')
@templated
def index_by_country(country_code: str, page: int):
country = _find_country(country_code)

per_page = request.args.get('per_page', type=int, default=50)
offset = (page - 1) * per_page

parties = _get_parties()
parties = filter(
lambda party: _find_party_country_code(party) == country.alpha_2.lower(),
parties,
)
parties = list(sorted(parties, key=lambda party: party.start_on, reverse=True))

pagination = Pagination(page=page, per_page=per_page, total=len(parties))

parties_slice = parties[offset : offset + per_page]

return {
'country': country,
'parties': parties_slice,
'pagination': pagination,
'countries': countries,
}


@blueprint.get('/<slug>/')
@templated
def view(slug: str):
Expand All @@ -49,7 +99,7 @@ def view(slug: str):

series = current_app.series_by_slug.get(party.series_slug)

country = _get_country(party)
country = _find_party_country(party)

return {
'party': party,
Expand All @@ -58,8 +108,24 @@ def view(slug: str):
}


def _get_country(party: Party) -> Country | None:
def _get_parties() -> list[Party]:
return list(current_app.parties_by_slug.values())


def _find_party_country_code(party: Party) -> str | None:
if not party.location:
return None

return countries.get(alpha_2=party.location.country_code)
return party.location.country_code


def _find_party_country(party: Party) -> Country | None:
country_code = _find_party_country_code(party)
if not country_code:
return None

return _find_country(country_code)


def _find_country(country_code: str) -> Country | None:
return countries.get(alpha_2=country_code)

0 comments on commit 8542ce4

Please sign in to comment.