diff --git a/mobilizon_reshare/config/config.py b/mobilizon_reshare/config/config.py index 50c3d73..301d59b 100644 --- a/mobilizon_reshare/config/config.py +++ b/mobilizon_reshare/config/config.py @@ -20,6 +20,8 @@ base_validators = [ # url of the main Mobilizon instance to download events from Validator("source.mobilizon.url", must_exist=True, is_type_of=str), Validator("source.mobilizon.group", must_exist=True, is_type_of=str), + Validator("db_path", must_exist=True, is_type_of=str), + Validator("locale", must_exist=True, is_type_of=str, default="en-us"), ] activeness_validators = [ diff --git a/mobilizon_reshare/event/event.py b/mobilizon_reshare/event/event.py index b71b234..6830e1e 100644 --- a/mobilizon_reshare/event/event.py +++ b/mobilizon_reshare/event/event.py @@ -6,6 +6,8 @@ from uuid import UUID import arrow from jinja2 import Template +from mobilizon_reshare.config.config import get_settings + class EventPublicationStatus(IntEnum): WAITING = 1 @@ -43,7 +45,8 @@ class MobilizonEvent: ] def _fill_template(self, pattern: Template) -> str: - return pattern.render(**asdict(self)) + config = get_settings() + return pattern.render(locale=config["locale"], **asdict(self)) def format(self, pattern: Template) -> str: return self._fill_template(pattern) diff --git a/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 b/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 index 12484d7..aa361d6 100644 --- a/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/facebook.tmpl.j2 @@ -1,6 +1,6 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} diff --git a/mobilizon_reshare/publishers/templates/facebook_recap.tmpl.j2 b/mobilizon_reshare/publishers/templates/facebook_recap.tmpl.j2 index b908e24..6d58e19 100644 --- a/mobilizon_reshare/publishers/templates/facebook_recap.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/facebook_recap.tmpl.j2 @@ -1,6 +1,6 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} diff --git a/mobilizon_reshare/publishers/templates/mastodon.tmpl.j2 b/mobilizon_reshare/publishers/templates/mastodon.tmpl.j2 index 627a465..f540b6e 100644 --- a/mobilizon_reshare/publishers/templates/mastodon.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/mastodon.tmpl.j2 @@ -1,6 +1,6 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} diff --git a/mobilizon_reshare/publishers/templates/mastodon_recap.tmpl.j2 b/mobilizon_reshare/publishers/templates/mastodon_recap.tmpl.j2 index 6ce8dfd..eea82f0 100644 --- a/mobilizon_reshare/publishers/templates/mastodon_recap.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/mastodon_recap.tmpl.j2 @@ -1,5 +1,5 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %}📍 {{ location }}{% endif %} 🔗 {{mobilizon_link}} \ No newline at end of file diff --git a/mobilizon_reshare/publishers/templates/telegram.tmpl.j2 b/mobilizon_reshare/publishers/templates/telegram.tmpl.j2 index fa699c8..b4ae594 100644 --- a/mobilizon_reshare/publishers/templates/telegram.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/telegram.tmpl.j2 @@ -1,6 +1,6 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %}📍 {{ location }}{% endif %} {{ description }} diff --git a/mobilizon_reshare/publishers/templates/telegram_recap.tmpl.j2 b/mobilizon_reshare/publishers/templates/telegram_recap.tmpl.j2 index ebd0710..67884fb 100644 --- a/mobilizon_reshare/publishers/templates/telegram_recap.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/telegram_recap.tmpl.j2 @@ -1,5 +1,5 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %}📍 {{ location }}{% endif %} 🔗 Link \ No newline at end of file diff --git a/mobilizon_reshare/publishers/templates/twitter.tmpl.j2 b/mobilizon_reshare/publishers/templates/twitter.tmpl.j2 index b31272e..233e10d 100644 --- a/mobilizon_reshare/publishers/templates/twitter.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/twitter.tmpl.j2 @@ -1,6 +1,6 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} diff --git a/mobilizon_reshare/publishers/templates/twitter_recap.tmpl.j2 b/mobilizon_reshare/publishers/templates/twitter_recap.tmpl.j2 index 39c90b8..3768a2e 100644 --- a/mobilizon_reshare/publishers/templates/twitter_recap.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/twitter_recap.tmpl.j2 @@ -1,5 +1,5 @@ {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} {% endif %} diff --git a/mobilizon_reshare/publishers/templates/zulip.tmpl.j2 b/mobilizon_reshare/publishers/templates/zulip.tmpl.j2 index c07ad17..61b28dc 100644 --- a/mobilizon_reshare/publishers/templates/zulip.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/zulip.tmpl.j2 @@ -1,6 +1,6 @@ # {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} diff --git a/mobilizon_reshare/publishers/templates/zulip_recap.tmpl.j2 b/mobilizon_reshare/publishers/templates/zulip_recap.tmpl.j2 index 471c38e..b844e7b 100644 --- a/mobilizon_reshare/publishers/templates/zulip_recap.tmpl.j2 +++ b/mobilizon_reshare/publishers/templates/zulip_recap.tmpl.j2 @@ -1,6 +1,6 @@ # {{ name }} -🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm') }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm') }} +🕒 {{ begin_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} - {{ end_datetime.to('local').format('DD MMMM, HH:mm', locale=locale) }} {% if location %} 📍 {{ location }} diff --git a/mobilizon_reshare/settings.toml b/mobilizon_reshare/settings.toml index 3b91e40..eeda85f 100644 --- a/mobilizon_reshare/settings.toml +++ b/mobilizon_reshare/settings.toml @@ -1,10 +1,8 @@ [default] -debug = true -default = true local_state_dir = "/var/mobilizon_reshare" -log_dir = "/var/log/mobilizon_reshare" db_name = "events.db" db_path = "@format {this.local_state_dir}/{this.db_name}" +locale= "en-us" [default.source.mobilizon] url="https://some_mobilizon" @@ -34,7 +32,7 @@ stream = "ext://sys.stderr" level = "DEBUG" class = "logging.handlers.RotatingFileHandler" formatter = "standard" -filename = "@format {this.log_dir}/mobilizon_reshare.log" +filename = "/var/log/mobilizon_reshare/mobilizon_reshare.log" maxBytes = 52428800 backupCount = 500 encoding = "utf8" diff --git a/tests/event/test_event.py b/tests/event/test_event.py index 3b9fc31..89795d6 100644 --- a/tests/event/test_event.py +++ b/tests/event/test_event.py @@ -1,13 +1,33 @@ import pytest from jinja2 import Template +from mobilizon_reshare.config.config import get_settings + + +@pytest.fixture() +def set_locale(locale): + settings = get_settings() + old_locale = settings["locale"] + yield get_settings().update({"locale": locale}) + settings.update({"locale": old_locale}) + @pytest.fixture() def simple_template(): return Template( ( "{{name}}|{{description}}|{{location}}|{{begin_datetime.strftime('%Y-%m-%d, %H:%M')}}" - "|{{last_update_time.strftime('%Y-%m-%d, %H:%M')}}" + "|{{end_datetime.strftime('%Y-%m-%d, %H:%M')}}" + ) + ) + + +@pytest.fixture() +def template_with_locale(): + return Template( + ( + "{{name}}|{{description}}|{{location}}|{{begin_datetime.format('DD MMMM, HH:mm', locale=locale)}}" + "|{{end_datetime.format('DD MMMM, HH:mm', locale=locale)}}" ) ) @@ -15,12 +35,21 @@ def simple_template(): def test_fill_template(event, simple_template): assert ( event._fill_template(simple_template) - == "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 11:30" + == "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 12:30" ) def test_format(event, simple_template): assert ( event.format(simple_template) - == "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 11:30" + == "test event|description of the event|location|2021-01-01, 11:30|2021-01-01, 12:30" + ) + + +@pytest.mark.parametrize("locale", ["it-it"]) +def test_locale(event, template_with_locale, set_locale): + + assert ( + event.format(template_with_locale) + == "test event|description of the event|location|01 gennaio, 11:30|01 gennaio, 12:30" )