diff options
author | Daniel Schadt <kingdread@gmx.de> | 2022-06-30 02:01:55 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2022-06-30 02:01:55 +0200 |
commit | afad8aeec67e4ba8181f3cdbbc237f4ec59558ac (patch) | |
tree | 47f2df2a55fec863bc840cac37fa89bc188dc294 | |
parent | f559c6721a59cae0dcafdf4f4c5a638691c17965 (diff) | |
download | fietsboek-afad8aeec67e4ba8181f3cdbbc237f4ec59558ac.tar.gz fietsboek-afad8aeec67e4ba8181f3cdbbc237f4ec59558ac.tar.bz2 fietsboek-afad8aeec67e4ba8181f3cdbbc237f4ec59558ac.zip |
use properly localized decimal separators
-rw-r--r-- | development.ini | 2 | ||||
-rw-r--r-- | fietsboek/jinja2.py | 20 | ||||
-rw-r--r-- | fietsboek/models/track.py | 12 | ||||
-rw-r--r-- | fietsboek/static/favicon.png | bin | 0 -> 20338 bytes | |||
-rw-r--r-- | fietsboek/static/pyramid-16x16.png | bin | 1319 -> 0 bytes | |||
-rw-r--r-- | fietsboek/static/pyramid.png | bin | 12901 -> 0 bytes | |||
-rw-r--r-- | fietsboek/templates/admin.jinja2 | 4 | ||||
-rw-r--r-- | fietsboek/templates/details.jinja2 | 16 | ||||
-rw-r--r-- | fietsboek/templates/edit_form.jinja2 | 2 | ||||
-rw-r--r-- | fietsboek/templates/home.jinja2 | 6 | ||||
-rw-r--r-- | fietsboek/templates/layout.jinja2 | 3 | ||||
-rw-r--r-- | fietsboek/templates/util.jinja2 | 2 | ||||
-rw-r--r-- | setup.py | 2 |
13 files changed, 47 insertions, 22 deletions
diff --git a/development.ini b/development.ini index 3cec096..608f400 100644 --- a/development.ini +++ b/development.ini @@ -13,6 +13,8 @@ pyramid.debug_routematch = false pyramid.default_locale_name = en pyramid.includes = pyramid_debugtoolbar +jinja2.filters = + format_decimal = fietsboek.jinja2.filter_format_decimal sqlalchemy.url = sqlite:///%(here)s/fietsboek.sqlite diff --git a/fietsboek/jinja2.py b/fietsboek/jinja2.py new file mode 100644 index 0000000..b940d74 --- /dev/null +++ b/fietsboek/jinja2.py @@ -0,0 +1,20 @@ +"""Custom filters for Jinja2.""" +import jinja2 + +from babel.numbers import format_decimal + + +@jinja2.pass_context +def filter_format_decimal(ctx, value): + """Format a decimal number according to the locale. + + This uses the right thousands grouping and the right decimal separator. + + :param ctx: The jinja context, passed automatically. + :type ctx: jinja2.runtime.Context + :param value: The value to format. + :type value: float + """ + request = ctx.get('request') + locale = request.localizer.locale_name + return format_decimal(value, locale=locale) diff --git a/fietsboek/models/track.py b/fietsboek/models/track.py index 6f500b9..66bf12e 100644 --- a/fietsboek/models/track.py +++ b/fietsboek/models/track.py @@ -20,6 +20,7 @@ import gzip import datetime from markupsafe import Markup +from babel.numbers import format_decimal from .meta import Base from .. import util @@ -244,14 +245,15 @@ class Track(Base): :return: The generated HTML. :rtype: Markup """ + number = lambda n: format_decimal(n, locale=localizer.locale_name) rows = [ - (_("tooltip.table.length"), f'{round(self.length / 1000, 2)} km'), - (_("tooltip.table.uphill"), f'{round(self.uphill, 2)} m'), - (_("tooltip.table.downhill"), f'{round(self.downhill, 2)} m'), + (_("tooltip.table.length"), f'{number(round(self.length / 1000, 2))} km'), + (_("tooltip.table.uphill"), f'{number(round(self.uphill, 2))} m'), + (_("tooltip.table.downhill"), f'{number(round(self.downhill, 2))} m'), (_("tooltip.table.moving_time"), f'{self.moving_time}'), (_("tooltip.table.stopped_time"), f'{self.stopped_time}'), - (_("tooltip.table.max_speed"), f'{round(util.mps_to_kph(self.max_speed), 2)} km/h'), - (_("tooltip.table.avg_speed"), f'{round(util.mps_to_kph(self.avg_speed), 2)} km/h'), + (_("tooltip.table.max_speed"), f'{number(round(util.mps_to_kph(self.max_speed), 2))} km/h'), + (_("tooltip.table.avg_speed"), f'{number(round(util.mps_to_kph(self.avg_speed), 2))} km/h'), ] rows = [ "<tr><td>{}</td><td>{}</td></tr>".format(localizer.translate(name), value) diff --git a/fietsboek/static/favicon.png b/fietsboek/static/favicon.png Binary files differnew file mode 100644 index 0000000..136fe91 --- /dev/null +++ b/fietsboek/static/favicon.png diff --git a/fietsboek/static/pyramid-16x16.png b/fietsboek/static/pyramid-16x16.png Binary files differdeleted file mode 100644 index 9792031..0000000 --- a/fietsboek/static/pyramid-16x16.png +++ /dev/null diff --git a/fietsboek/static/pyramid.png b/fietsboek/static/pyramid.png Binary files differdeleted file mode 100644 index 4ab837b..0000000 --- a/fietsboek/static/pyramid.png +++ /dev/null diff --git a/fietsboek/templates/admin.jinja2 b/fietsboek/templates/admin.jinja2 index a347ebf..04a9ac3 100644 --- a/fietsboek/templates/admin.jinja2 +++ b/fietsboek/templates/admin.jinja2 @@ -1,5 +1,5 @@ {% extends "layout.jinja2" %} -{% import "util.jinja2" as util %} +{% import "util.jinja2" as util with context %} {% block content %} <div class="container"> <h1>{{ _("page.admin.title") }}</h1> @@ -9,7 +9,7 @@ <div class="list-group"> {% for badge in badges %} <span href="#" class="list-group-item list-group-item-action d-flex admin-badge-list"> - {{ util.render_badge(request, badge) }} + {{ util.render_badge(badge) }} <form method="POST" enctype="multipart/form-data" action="{{ request.route_path('admin-badge-edit') }}"> <input type="hidden" name="badge-edit-id" value="{{ badge.id }}"> <div class="mb-3"> diff --git a/fietsboek/templates/details.jinja2 b/fietsboek/templates/details.jinja2 index 595f888..e335880 100644 --- a/fietsboek/templates/details.jinja2 +++ b/fietsboek/templates/details.jinja2 @@ -1,5 +1,5 @@ {% extends "layout.jinja2" %} -{% import "util.jinja2" as util %} +{% import "util.jinja2" as util with context %} {% block content %} <div class="container"> @@ -52,7 +52,7 @@ <div id="mainmap_hp" style="width:100%;height:300px"> </div> - <a class="btn btn-primary" href="{{ request.route_path('gpx', id=track.id) }}">{{ _("page.details.download") }}</a> + <a class="btn btn-primary" href="{{ request.route_path('gpx', id=track.id) }}"><i class="bi bi-download"></i> {{ _("page.details.download") }}</a> <table class="table table-hover table-sm"> <tbody> @@ -70,15 +70,15 @@ </tr> <tr> <th scope="row">{{ _("page.details.length") }}</th> - <td>{{ (track.length / 1000) | round(2) }} km</td> + <td>{{ (track.length / 1000) | round(2) | format_decimal }} km</td> </tr> <tr> <th scope="row">{{ _("page.details.uphill") }}</th> - <td>{{ track.uphill | round(2) }} m</td> + <td>{{ track.uphill | round(2) | format_decimal }} m</td> </tr> <tr> <th scope="row">{{ _("page.details.downhill") }}</th> - <td>{{ track.downhill | round(2) }} m</td> + <td>{{ track.downhill | round(2) | format_decimal }} m</td> </tr> <tr> <th scope="row">{{ _("page.details.moving_time") }}</th> @@ -90,18 +90,18 @@ </tr> <tr> <th scope="row">{{ _("page.details.max_speed") }}</th> - <td>{{ mps_to_kph(track.max_speed) | round(2) }} km/h</td> + <td>{{ mps_to_kph(track.max_speed) | round(2) | format_decimal }} km/h</td> </tr> <tr> <th scope="row">{{ _("page.details.avg_speed") }}</th> - <td>{{ mps_to_kph(track.avg_speed) | round(2) }} km/h</td> + <td>{{ mps_to_kph(track.avg_speed) | round(2) | format_decimal }} km/h</td> </tr> </tbody> </table> {% if track.badges %} <div class="d-flex"> {% for badge in track.badges %} - {{ util.render_badge(request, badge) }} + {{ util.render_badge(badge) }} {% endfor %} </div> {% endif %} diff --git a/fietsboek/templates/edit_form.jinja2 b/fietsboek/templates/edit_form.jinja2 index 7559fe7..0a52319 100644 --- a/fietsboek/templates/edit_form.jinja2 +++ b/fietsboek/templates/edit_form.jinja2 @@ -22,7 +22,7 @@ <div>{{ _("page.track.form.tags") }}</div> <div id="formTags"> {% for tag in tags %} - <span class="tag-badge badge rounded-pill bg-info text-dark">{{ tag }}</span> + <span class="tag-badge badge rounded-pill bg-info text-dark">{{ tag }} <i class="bi bi-x"></i></span> {% endfor %} </div> <div class="row"> diff --git a/fietsboek/templates/home.jinja2 b/fietsboek/templates/home.jinja2 index ccd9af0..e8c7d0c 100644 --- a/fietsboek/templates/home.jinja2 +++ b/fietsboek/templates/home.jinja2 @@ -6,10 +6,10 @@ {% if summary %} <div class="list-group list-group-root"> {% for year in summary %} - <a class="list-group-item list-group-item-primary">{{ year.year }} — {{ (year.total_length / 1000) | round(2) }} km</a> + <a class="list-group-item list-group-item-primary">{{ year.year }} — {{ (year.total_length / 1000) | round(2) | format_decimal }} km</a> <div class="list-group"> {% for month in year %} - <a class="list-group-item list-group-item-secondary">{{ month_name(request, month.month) }} — {{ (month.total_length / 1000) | round(2) }} km</a> + <a class="list-group-item list-group-item-secondary">{{ month_name(request, month.month) }} — {{ (month.total_length / 1000) | round(2) | format_decimal }} km</a> <div class="list-group"> {% for track in month %} <span class="list-group-item">{{ track.date.day }}: <a href="{{ request.route_url('details', id=track.id) }}" data-bs-toggle="tooltip" data-bs-container="body" data-bs-html="true" title="{{ track.html_tooltip(request.localizer) }}">{{ track.title | default(track.date, true) }}</a></span> @@ -20,7 +20,7 @@ {% endfor %} </div> <p> - {{ _("page.home.total") }}: {{ (summary.total_length / 1000) | round(2) }} km + {{ _("page.home.total") }}: {{ (summary.total_length / 1000) | round(2) | format_decimal }} km </p> {% endif %} </div> diff --git a/fietsboek/templates/layout.jinja2 b/fietsboek/templates/layout.jinja2 index 6b8c9b2..8551fb5 100644 --- a/fietsboek/templates/layout.jinja2 +++ b/fietsboek/templates/layout.jinja2 @@ -5,6 +5,7 @@ <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="Fietslog Tour Diary"> + <link rel="shortcut icon" href="{{ request.static_url('fietsboek:static/favicon.png') }}"> <title>{% block title %}Fietsboek{% endblock %}</title> @@ -28,7 +29,7 @@ <body> <nav class="navbar navbar-dark bg-dark navbar-expand-lg"> <div class="container-fluid"> - <span class="navbar-brand mb-0 h1">Fietsboek</span> + <span class="navbar-brand mb-0 h1"><i class="bi bi-bicycle"></i> Fietsboek</span> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="{{ _('page.navbar.toggle') }}"> <span class="navbar-toggler-icon"></span> </button> diff --git a/fietsboek/templates/util.jinja2 b/fietsboek/templates/util.jinja2 index 9fbf0ec..d61127d 100644 --- a/fietsboek/templates/util.jinja2 +++ b/fietsboek/templates/util.jinja2 @@ -1,3 +1,3 @@ -{% macro render_badge(request, badge) -%} +{% macro render_badge(badge) -%} <div class="badge-container"><img src="{{ request.route_url('badge', id=badge.id) }}" data-bs-toggle="tooltip" title="{{ badge.title }}"></div> {%- endmacro %} @@ -20,6 +20,7 @@ requires = [ 'SQLAlchemy', 'transaction', 'zope.sqlalchemy', + 'Babel', 'cryptography', 'gpxpy', 'markdown', @@ -27,7 +28,6 @@ requires = [ ] tests_require = [ - 'Babel', 'WebTest', 'pytest', 'pytest-cov', |