diff --git a/AGENTS.md b/AGENTS.md index b3e3b15b9..b85aae464 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -28,7 +28,7 @@ schema reset) without asking. ## Layout -``` +```text wger// ├── models/ one file per model (or group), re-exported from models/__init__.py ├── api/ DRF serializers, viewsets, filtersets diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ebcf6225..5656031e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Progression requirements can now reference the top of the prescribed range via t ### Bug fixes +* Counted messages for token expiry, confirmation links, inactive-member reminders, + and workout-session limits now use locale-aware plural forms. * Deleting a routine, day, slot, or slot entry now preserves completed workout sessions and logs, including performed and target values. Only the references to deleted routine structure are cleared. Explicit session deletion and account diff --git a/wger/core/templates/account/email/email_confirmation_message.html b/wger/core/templates/account/email/email_confirmation_message.html index 81dfceef4..f112e8e1b 100644 --- a/wger/core/templates/account/email/email_confirmation_message.html +++ b/wger/core/templates/account/email/email_confirmation_message.html @@ -107,7 +107,10 @@

- {% blocktranslate with time=expire_date|time:"TIME_FORMAT" date=expire_date|date:"DATE_FORMAT" trimmed %} + {% blocktranslate with time=expire_date|time:"TIME_FORMAT" date=expire_date|date:"DATE_FORMAT" count expire_hours=expire_hours trimmed %} + The link is valid for {{ expire_hours }} hour (until {{ date }}, {{ time }}). + You can request a new one from your profile settings. + {% plural %} The link is valid for {{ expire_hours }} hours (until {{ date }}, {{ time }}). You can request a new one from your profile settings. {% endblocktranslate %} diff --git a/wger/core/templates/account/email/email_confirmation_message.txt b/wger/core/templates/account/email/email_confirmation_message.txt index 6fc96d158..9abf122af 100644 --- a/wger/core/templates/account/email/email_confirmation_message.txt +++ b/wger/core/templates/account/email/email_confirmation_message.txt @@ -14,8 +14,12 @@ Your email verification code is listed below. Please enter it in your open brows {% blocktranslate trimmed %} To confirm this is correct, go to {{ activate_url }} {% endblocktranslate %} -{% blocktranslate with time=expire_date|time:"TIME_FORMAT" date=expire_date|date:"DATE_FORMAT" trimmed %} -The link is valid for {{ expire_hours }} hours (until {{ date }}, {{ time }}). You can request a new one from your profile settings. +{% blocktranslate with time=expire_date|time:"TIME_FORMAT" date=expire_date|date:"DATE_FORMAT" count expire_hours=expire_hours trimmed %} +The link is valid for {{ expire_hours }} hour (until {{ date }}, {{ time }}). +You can request a new one from your profile settings. +{% plural %} +The link is valid for {{ expire_hours }} hours (until {{ date }}, {{ time }}). +You can request a new one from your profile settings. {% endblocktranslate %} {% endif %} {% endautoescape %}{% endblock content %} diff --git a/wger/core/templates/user/api_key.html b/wger/core/templates/user/api_key.html index 986892803..453e50e91 100644 --- a/wger/core/templates/user/api_key.html +++ b/wger/core/templates/user/api_key.html @@ -53,7 +53,10 @@ {% if long_lived_sessions %}

- {% blocktranslate trimmed with days=refresh_token_lifetime_days %} + {% blocktranslate trimmed count days=refresh_token_lifetime_days %} + Refresh tokens expire {{ days }} day after they were + generated, unless you revoke them sooner. + {% plural %} Refresh tokens expire {{ days }} days after they were generated, unless you revoke them sooner. {% endblocktranslate %} diff --git a/wger/core/tests/test_apikey.py b/wger/core/tests/test_apikey.py index 27f4bdcf8..d4ce17394 100644 --- a/wger/core/tests/test_apikey.py +++ b/wger/core/tests/test_apikey.py @@ -26,6 +26,7 @@ from django.test import override_settings from django.urls import reverse from django.utils import timezone from django.utils.dateparse import parse_datetime +from django.utils.translation import override # Third Party from rest_framework.authtoken.models import Token @@ -56,6 +57,21 @@ class ApiKeyTestCase(WgerTestCase): Tests the API key page """ + def test_refresh_token_lifetime_pluralization(self): + self.user_login('test') + user = User.objects.get(username='test') + mint_long_lived_refresh_token(user, lifetime_seconds=120 * 86400) + + for days in (1, 2, 21): + with ( + self.subTest(days=days), + override('en'), + override_settings(HEADLESS_JWT_REFRESH_TOKEN_EXPIRES_IN=days * 86400), + ): + response = self.client.get(reverse('core:user:api-key')) + unit = 'day' if days == 1 else 'days' + self.assertContains(response, f'Refresh tokens expire {days} {unit} after') + def test_api_key_page(self): """ Tests the API key generation page diff --git a/wger/core/tests/test_plural_messages.py b/wger/core/tests/test_plural_messages.py new file mode 100644 index 000000000..006d8529c --- /dev/null +++ b/wger/core/tests/test_plural_messages.py @@ -0,0 +1,44 @@ +# This file is part of wger Workout Manager, licensed under AGPL-3.0-or-later. + +# Standard Library +from datetime import timedelta + +# Django +from django.contrib.auth.models import AnonymousUser +from django.template.loader import render_to_string +from django.test import SimpleTestCase +from django.utils import timezone +from django.utils.html import strip_tags +from django.utils.translation import override + + +class CountedTemplateMessagesTestCase(SimpleTestCase): + """Counted messages in pages and emails select the appropriate plural form.""" + + def render_message(self, template: str, context: dict) -> str: + with override('en'): + rendered = render_to_string(template, {'user': AnonymousUser(), **context}) + return ' '.join(strip_tags(rendered).split()) + + def test_confirmation_link_lifetime(self): + for hours in (1, 2, 24): + for extension in ('html', 'txt'): + with self.subTest(hours=hours, extension=extension): + text = self.render_message( + f'account/email/email_confirmation_message.{extension}', + { + 'expire_hours': hours, + 'expire_date': timezone.now() + timedelta(hours=hours), + }, + ) + unit = 'hour' if hours == 1 else 'hours' + self.assertIn(f'The link is valid for {hours} {unit} (until', text) + + def test_inactive_member_reminder(self): + for weeks in (1, 2, 21): + with self.subTest(weeks=weeks): + text = self.render_message('gym/email_inactive_members.html', {'weeks': weeks}) + unit = 'week' if weeks == 1 else 'weeks' + self.assertIn(f'in the last {weeks} {unit}', text) + if weeks == 1: + self.assertNotIn('in the last 1 weeks', text) diff --git a/wger/gym/templates/gym/email_inactive_members.html b/wger/gym/templates/gym/email_inactive_members.html index 531dc660b..56da49bb2 100644 --- a/wger/gym/templates/gym/email_inactive_members.html +++ b/wger/gym/templates/gym/email_inactive_members.html @@ -1,5 +1,9 @@ {% load i18n wger_extras %} -{% blocktranslate %}The following users have not visited the gym in the last {{ weeks }} weeks{% endblocktranslate %} +{% blocktranslate trimmed count weeks=weeks %} +The following users have not visited the gym in the last {{ weeks }} week +{% plural %} +The following users have not visited the gym in the last {{ weeks }} weeks +{% endblocktranslate %} {% translate "User"|ljust:"25" %} {% translate "Last activity" %} --------------------------------------------------------- diff --git a/wger/manager/models/session.py b/wger/manager/models/session.py index 879966388..01a7ee49c 100644 --- a/wger/manager/models/session.py +++ b/wger/manager/models/session.py @@ -23,7 +23,7 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models from django.utils import timezone -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, ngettext # wger from wger.utils.uuid import uuid7 @@ -190,7 +190,11 @@ class WorkoutSession(models.Model): if self.datetime_end - self.datetime_start > self.max_duration(): raise ValidationError( - _('A session cannot be longer than %(hours)s hours.') + ngettext( + 'A session cannot be longer than %(hours)s hour.', + 'A session cannot be longer than %(hours)s hours.', + settings.WGER_MAX_SESSION_LENGTH_HOURS, + ) % {'hours': settings.WGER_MAX_SESSION_LENGTH_HOURS} ) diff --git a/wger/manager/tests/test_workout_session.py b/wger/manager/tests/test_workout_session.py index c679b4cfb..604fbddf6 100644 --- a/wger/manager/tests/test_workout_session.py +++ b/wger/manager/tests/test_workout_session.py @@ -18,6 +18,7 @@ import zoneinfo # Django from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from django.db import ( IntegrityError, transaction, @@ -25,6 +26,7 @@ from django.db import ( from django.test import override_settings from django.urls import reverse from django.utils import timezone +from django.utils.translation import override # Third Party from rest_framework import status @@ -60,6 +62,24 @@ class WorkoutSessionDurationTestCase(WgerTestCase): SESSION = 'bbbbbbbb-bbbb-bbbb-bbbb-000000000005' ROUTINE = 3 + def test_limit_message_uses_the_configured_count(self): + start = timezone.now() + for hours in (1, 2, 21): + with ( + self.subTest(hours=hours), + override('en'), + override_settings(WGER_MAX_SESSION_LENGTH_HOURS=hours), + ): + session = WorkoutSession( + datetime_start=start, + datetime_end=start + datetime.timedelta(hours=hours + 1), + ) + unit = 'hour' if hours == 1 else 'hours' + with self.assertRaisesMessage( + ValidationError, f'A session cannot be longer than {hours} {unit}.' + ): + session.clean() + def setUp(self): super().setUp() self.user_login('test')