From 713912cfbc7f298c6b6475f7f93aeb79db5a6f1f Mon Sep 17 00:00:00 2001 From: Gautier P Date: Tue, 26 Jan 2021 14:42:53 +0100 Subject: [PATCH] Translate email notification --- flatisfy/cmds.py | 5 ++++- flatisfy/config.py | 4 ++++ flatisfy/email.py | 50 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/flatisfy/cmds.py b/flatisfy/cmds.py index ad1916d..493ce84 100644 --- a/flatisfy/cmds.py +++ b/flatisfy/cmds.py @@ -161,6 +161,7 @@ def import_and_filter(config, load_from_db=False, new_only=False): get_session = database.init_db(config["database"], config["search_index"]) new_flats = [] + result = [] LOGGER.info("Merging fetched flats in database...") # Flatten the flats_by_status dict @@ -210,13 +211,14 @@ def import_and_filter(config, load_from_db=False, new_only=False): flat.status = getattr(flat_model.FlatStatus, status) if flat.status == flat_model.FlatStatus.new: new_flats.append(flat) + result.append(flat.id) session.add_all(flats_objects.values()) if config["send_email"]: email.send_notification(config, new_flats) - LOGGER.info(f"Found {len(new_flats)} new flats.") + LOGGER.info(f"Found {len(result)} new flats.") # Touch a file to indicate last update timestamp ts_file = os.path.join(config["data_directory"], "timestamp") @@ -224,6 +226,7 @@ def import_and_filter(config, load_from_db=False, new_only=False): os.utime(ts_file, None) LOGGER.info("Done!") + return result def purge_db(config): diff --git a/flatisfy/config.py b/flatisfy/config.py index aa32db8..263c3d5 100644 --- a/flatisfy/config.py +++ b/flatisfy/config.py @@ -97,6 +97,7 @@ DEFAULT_CONFIG = { "smtp_password": None, "smtp_from": "noreply@flatisfy.org", "smtp_to": [], + "notification_lang": "en", # The web site url, to be used in email notifications. (doesn't matter # whether the trailing slash is present or not) "website_url": "http://127.0.0.1:8080", @@ -176,6 +177,9 @@ def validate_config(config, check_with_data): config["smtp_password"], str ) # noqa: E501 assert config["smtp_to"] is None or isinstance(config["smtp_to"], list) + assert config["notification_lang"] is None or isinstance( + config["notification_lang"], str + ) assert isinstance(config["store_personal_data"], bool) assert isinstance(config["max_distance_housing_station"], (int, float)) diff --git a/flatisfy/email.py b/flatisfy/email.py index af34efa..9ea5e7e 100644 --- a/flatisfy/email.py +++ b/flatisfy/email.py @@ -63,13 +63,29 @@ def send_notification(config, flats): if not flats: return - txt = "Hello dear user,\n\nThe following new flats have been found:\n\n" - html = """ + i18n = { + "subject": [ + f"{len(flats)} new flats found!", + f"{len(flats)} nouvelles annonces disponibles !", + ], + "hello": ["Hello dear user", "Bonjour cher utilisateur"], + "following_new_flats": [ + "The following new flats have been found:", + "Voici les nouvelles annonces :", + ], + "area": ["area", "surface"], + "cost": ["cost", "coût"], + "signature": ["Hope you'll find what you were looking for.", "Bonne recherche"], + } + l = 1 if config["notification_lang"] == "fr" else 0 + + txt = i18n["hello"][l] + ",\n\n\n\n" + html = f""" -

Hello dear user!

-

The following new flats have been found: +

{i18n["hello"][l]}!

+

{i18n["following_new_flats"][l]}

" - signature = "\nHope you'll find what you were looking for.\n\nBye!\nFlatisfy" + signature = f"\n{i18n['signature'][l]}\n\nBye!\nFlatisfy" txt += signature html += signature.replace("\n", "
") @@ -109,7 +139,7 @@ def send_notification(config, flats): send_email( config["smtp_server"], config["smtp_port"], - "New flats found!", + i18n["subject"][l], config["smtp_from"], config["smtp_to"], txt,