Browse Source

Translate email notification

Gautier P 10 months ago
parent
commit
713912cfbc
3 changed files with 48 additions and 11 deletions
  1. 4
    1
      flatisfy/cmds.py
  2. 4
    0
      flatisfy/config.py
  3. 40
    10
      flatisfy/email.py

+ 4
- 1
flatisfy/cmds.py View File

@@ -161,6 +161,7 @@ def import_and_filter(config, load_from_db=False, new_only=False):
161 161
     get_session = database.init_db(config["database"], config["search_index"])
162 162
 
163 163
     new_flats = []
164
+    result = []
164 165
 
165 166
     LOGGER.info("Merging fetched flats in database...")
166 167
     # Flatten the flats_by_status dict
@@ -210,13 +211,14 @@ def import_and_filter(config, load_from_db=False, new_only=False):
210 211
                 flat.status = getattr(flat_model.FlatStatus, status)
211 212
                 if flat.status == flat_model.FlatStatus.new:
212 213
                     new_flats.append(flat)
214
+                    result.append(flat.id)
213 215
 
214 216
             session.add_all(flats_objects.values())
215 217
 
216 218
         if config["send_email"]:
217 219
             email.send_notification(config, new_flats)
218 220
 
219
-    LOGGER.info(f"Found {len(new_flats)} new flats.")
221
+    LOGGER.info(f"Found {len(result)} new flats.")
220 222
 
221 223
     # Touch a file to indicate last update timestamp
222 224
     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):
224 226
         os.utime(ts_file, None)
225 227
 
226 228
     LOGGER.info("Done!")
229
+    return result
227 230
 
228 231
 
229 232
 def purge_db(config):

+ 4
- 0
flatisfy/config.py View File

@@ -97,6 +97,7 @@ DEFAULT_CONFIG = {
97 97
     "smtp_password": None,
98 98
     "smtp_from": "noreply@flatisfy.org",
99 99
     "smtp_to": [],
100
+    "notification_lang": "en",
100 101
     # The web site url, to be used in email notifications. (doesn't matter
101 102
     # whether the trailing slash is present or not)
102 103
     "website_url": "http://127.0.0.1:8080",
@@ -176,6 +177,9 @@ def validate_config(config, check_with_data):
176 177
             config["smtp_password"], str
177 178
         )  # noqa: E501
178 179
         assert config["smtp_to"] is None or isinstance(config["smtp_to"], list)
180
+        assert config["notification_lang"] is None or isinstance(
181
+            config["notification_lang"], str
182
+        )
179 183
 
180 184
         assert isinstance(config["store_personal_data"], bool)
181 185
         assert isinstance(config["max_distance_housing_station"], (int, float))

+ 40
- 10
flatisfy/email.py View File

@@ -63,13 +63,29 @@ def send_notification(config, flats):
63 63
     if not flats:
64 64
         return
65 65
 
66
-    txt = "Hello dear user,\n\nThe following new flats have been found:\n\n"
67
-    html = """
66
+    i18n = {
67
+        "subject": [
68
+            f"{len(flats)} new flats found!",
69
+            f"{len(flats)} nouvelles annonces disponibles !",
70
+        ],
71
+        "hello": ["Hello dear user", "Bonjour cher utilisateur"],
72
+        "following_new_flats": [
73
+            "The following new flats have been found:",
74
+            "Voici les nouvelles annonces :",
75
+        ],
76
+        "area": ["area", "surface"],
77
+        "cost": ["cost", "coût"],
78
+        "signature": ["Hope you'll find what you were looking for.", "Bonne recherche"],
79
+    }
80
+    l = 1 if config["notification_lang"] == "fr" else 0
81
+
82
+    txt = i18n["hello"][l] + ",\n\n\n\n"
83
+    html = f"""
68 84
     <html>
69 85
       <head></head>
70 86
       <body>
71
-        <p>Hello dear user!</p>
72
-        <p>The following new flats have been found:
87
+        <p>{i18n["hello"][l]}!</p>
88
+        <p>{i18n["following_new_flats"][l]}
73 89
 
74 90
             <ul>
75 91
     """
@@ -83,22 +99,36 @@ def send_notification(config, flats):
83 99
         cost = str(flat.cost)
84 100
         currency = str(flat.currency)
85 101
 
86
-        txt += "- {}: {}#/flat/{} (area: {}, cost: {} {})\n".format(
87
-            title, website_url, flat_id, area, cost, currency
102
+        txt += "- {}: {}#/flat/{} ({}: {}, {}: {} {})\n".format(
103
+            title,
104
+            website_url,
105
+            flat_id,
106
+            i18n["area"][l],
107
+            area,
108
+            i18n["cost"][l],
109
+            cost,
110
+            currency,
88 111
         )
89 112
 
90 113
         html += """
91 114
             <li>
92 115
                 <a href="{}#/flat/{}">{}</a>
93
-                (area: {}, cost: {} {})
116
+                ({}: {}, {}: {} {})
94 117
             </li>
95 118
         """.format(
96
-            website_url, flat_id, title, area, cost, currency
119
+            website_url,
120
+            flat_id,
121
+            title,
122
+            i18n["area"][l],
123
+            area,
124
+            i18n["cost"][l],
125
+            cost,
126
+            currency,
97 127
         )
98 128
 
99 129
     html += "</ul>"
100 130
 
101
-    signature = "\nHope you'll find what you were looking for.\n\nBye!\nFlatisfy"
131
+    signature = f"\n{i18n['signature'][l]}\n\nBye!\nFlatisfy"
102 132
     txt += signature
103 133
     html += signature.replace("\n", "<br>")
104 134
 
@@ -109,7 +139,7 @@ def send_notification(config, flats):
109 139
     send_email(
110 140
         config["smtp_server"],
111 141
         config["smtp_port"],
112
-        "New flats found!",
142
+        i18n["subject"][l],
113 143
         config["smtp_from"],
114 144
         config["smtp_to"],
115 145
         txt,