Translate email notification

This commit is contained in:
Gautier P 2021-01-26 14:42:53 +01:00
parent 42909bd46f
commit 713912cfbc
3 changed files with 48 additions and 11 deletions

View File

@ -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"]) get_session = database.init_db(config["database"], config["search_index"])
new_flats = [] new_flats = []
result = []
LOGGER.info("Merging fetched flats in database...") LOGGER.info("Merging fetched flats in database...")
# Flatten the flats_by_status dict # 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) flat.status = getattr(flat_model.FlatStatus, status)
if flat.status == flat_model.FlatStatus.new: if flat.status == flat_model.FlatStatus.new:
new_flats.append(flat) new_flats.append(flat)
result.append(flat.id)
session.add_all(flats_objects.values()) session.add_all(flats_objects.values())
if config["send_email"]: if config["send_email"]:
email.send_notification(config, new_flats) 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 # Touch a file to indicate last update timestamp
ts_file = os.path.join(config["data_directory"], "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) os.utime(ts_file, None)
LOGGER.info("Done!") LOGGER.info("Done!")
return result
def purge_db(config): def purge_db(config):

View File

@ -97,6 +97,7 @@ DEFAULT_CONFIG = {
"smtp_password": None, "smtp_password": None,
"smtp_from": "noreply@flatisfy.org", "smtp_from": "noreply@flatisfy.org",
"smtp_to": [], "smtp_to": [],
"notification_lang": "en",
# The web site url, to be used in email notifications. (doesn't matter # The web site url, to be used in email notifications. (doesn't matter
# whether the trailing slash is present or not) # whether the trailing slash is present or not)
"website_url": "http://127.0.0.1:8080", "website_url": "http://127.0.0.1:8080",
@ -176,6 +177,9 @@ def validate_config(config, check_with_data):
config["smtp_password"], str config["smtp_password"], str
) # noqa: E501 ) # noqa: E501
assert config["smtp_to"] is None or isinstance(config["smtp_to"], list) 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["store_personal_data"], bool)
assert isinstance(config["max_distance_housing_station"], (int, float)) assert isinstance(config["max_distance_housing_station"], (int, float))

View File

@ -63,13 +63,29 @@ def send_notification(config, flats):
if not flats: if not flats:
return return
txt = "Hello dear user,\n\nThe following new flats have been found:\n\n" i18n = {
html = """ "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"""
<html> <html>
<head></head> <head></head>
<body> <body>
<p>Hello dear user!</p> <p>{i18n["hello"][l]}!</p>
<p>The following new flats have been found: <p>{i18n["following_new_flats"][l]}
<ul> <ul>
""" """
@ -83,22 +99,36 @@ def send_notification(config, flats):
cost = str(flat.cost) cost = str(flat.cost)
currency = str(flat.currency) currency = str(flat.currency)
txt += "- {}: {}#/flat/{} (area: {}, cost: {} {})\n".format( txt += "- {}: {}#/flat/{} ({}: {}, {}: {} {})\n".format(
title, website_url, flat_id, area, cost, currency title,
website_url,
flat_id,
i18n["area"][l],
area,
i18n["cost"][l],
cost,
currency,
) )
html += """ html += """
<li> <li>
<a href="{}#/flat/{}">{}</a> <a href="{}#/flat/{}">{}</a>
(area: {}, cost: {} {}) ({}: {}, {}: {} {})
</li> </li>
""".format( """.format(
website_url, flat_id, title, area, cost, currency website_url,
flat_id,
title,
i18n["area"][l],
area,
i18n["cost"][l],
cost,
currency,
) )
html += "</ul>" html += "</ul>"
signature = "\nHope you'll find what you were looking for.\n\nBye!\nFlatisfy" signature = f"\n{i18n['signature'][l]}\n\nBye!\nFlatisfy"
txt += signature txt += signature
html += signature.replace("\n", "<br>") html += signature.replace("\n", "<br>")
@ -109,7 +139,7 @@ def send_notification(config, flats):
send_email( send_email(
config["smtp_server"], config["smtp_server"],
config["smtp_port"], config["smtp_port"],
"New flats found!", i18n["subject"][l],
config["smtp_from"], config["smtp_from"],
config["smtp_to"], config["smtp_to"],
txt, txt,