Browse Source

FIX Modification du script de mailing pour qu'il utilise l'API Facteur

olivier.massot 7 years ago
parent
commit
4c941eda12
3 changed files with 50 additions and 65 deletions
  1. 49 35
      mails_rappel_ctrl.py
  2. 1 0
      requirements.txt
  3. 0 30
      resources/mail.html

+ 49 - 35
mails_rappel_ctrl.py

@@ -7,13 +7,15 @@
 
 @author: olivier.massot, oct. 2017
 '''
+import html
 import logging
 
 from path import Path  # @UnusedImport
+import requests
+from requests_ntlm.requests_ntlm import HttpNtlmAuth
 
 from core import logconf
-from core.mail import Mail
-from core.pde import ControlesDb, RSCDIR
+from core.pde import ControlesDb
 from core.pde import mk_workdir  # @UnusedImport
 
 
@@ -23,38 +25,46 @@ logconf.start("mails_rappel_ctrl", logging.DEBUG)
 DEBUG = False
 wrkdir = ""
 
-# # POUR TESTER, décommenter les lignes suivantes
-##-----------------------------------------------
-
-ControlesDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\cg67Parc_data.mdb")
-DEBUG = True
-wrkdir = mk_workdir("mails_rappel_ctrl_test")
-logger.handlers = [h for h in logger.handlers if (type(h) == logging.StreamHandler)]
-logger.warning("Mode TEST")
-
-##-----------------------------------------------
-
 # #     CONFIG
 
+# Url de l'API Rest Facteur
+FACTEUR_URL = "http://referentiel.bas-rhin.fr/Facteur/Poste/Contenu"
+TEMPLATE_URL = "http://referentiel.bas-rhin.fr/Facteur/Template/CD67"
+AUTH = HttpNtlmAuth(r'CG67\service.scriptapp', '8KEBKX9bO8r4N5JZXT74')
+
 # Nombre de jours à partir duquel les mails sont envoyés
 SEUIL_DUREE = 21
 
 # Demarrer la requete de sélection à partir du chantier:
 CHANTIER_DEPART = 175000
 
-# Adresse mail depuis laquelle les mails sont envoyés
-SENDER = "noreply@bas-rhin.fr"
-
-# Objet des mails automatiques
-SUBJECT = "Mail automatique - Rappel"
-
 # Contact
 CONTACT = "jacky.klein@bas-rhin.fr"
 
 # Contenu des mails automatiques
 #   NB: variables optionelles utilisables dans CONTENT: {chantier_id}, {date_status}, {contact}
-with open(RSCDIR / "mail.html", encoding="utf-8") as f:
-    content = f.read()
+CONTENT = """<p>Bonjour,</p>
+            <p>
+                Le chantier numéro {chantier_id} est en attente d'une intervention depuis le {date_status:%d-%m-%Y}.
+            </p>
+            <p>Pour plus d'information, ou si vous ne souhaitez plus recevoir de rappel pour ce chantier: <a href="mailto:{contact}">{contact}</a></p>
+            <p>
+                Merci,<br/>
+                Le Parc Départemental d'Erstein
+            </p>
+"""
+
+# # POUR TESTER, décommenter les lignes suivantes
+##-----------------------------------------------
+
+# ControlesDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\cg67Parc_data.mdb")
+# DEBUG = True
+# FACTEUR_URL = "http://t-referentiel.bas-rhin.fr/Facteur/Poste/Contenu"
+# wrkdir = mk_workdir("mails_rappel_ctrl_test")
+# logger.handlers = [h for h in logger.handlers if (type(h) == logging.StreamHandler)]
+# logger.warning("Mode TEST")
+
+##-----------------------------------------------
 
 # #     INITIALISATION
 
@@ -79,7 +89,7 @@ subsql = """SELECT tblCompactageBases.lngChantierId,
             """
 
 # Selectionne les chantiers pour lesquels un mail doit être envoyé
-sql = """SELECT tblChantiers.lngChantierId, tblChantiers.mailContact,
+sql = """SELECT tblChantiers.lngChantierId, tblChantiers.mailContact, tblChantiers.strInterlEntreprise,
             tblChantiers.stopMails, statuts.a1_status, statuts.since
         FROM tblChantiers INNER JOIN ({subsql}) as statuts ON tblChantiers.lngChantierId = statuts.lngChantierId
         WHERE statuts.a1_status=True
@@ -91,14 +101,12 @@ sql = """SELECT tblChantiers.lngChantierId, tblChantiers.mailContact,
                    depart=CHANTIER_DEPART)
 
 # #    PROCESS
-logger.debug(sql)
-
 qry = db.execute(sql)
 
 for row in qry:
-    chantier_id, mail_to, stop_mails, a1, since = row
+    chantier_id, mail_to, nom_dest, stop_mails, a1, since = row
     if DEBUG:
-        mail_to = "olinox14@yahoo.fr"
+        mail_to = "olivier.massot@bas-rhin.fr"
 
     if stop_mails:
         logger.info("X Chantier %s: l'envoi de mail est bloqué", chantier_id)
@@ -109,15 +117,21 @@ for row in qry:
         continue
 
     logger.info("> Chantier %s: envoi d'un mail à %s", chantier_id, mail_to)
-    mail = Mail(SENDER,
-                [mail_to],
-                SUBJECT,
-                content.format(
-                               chantier_id=chantier_id,
-                               date_status=since,
-                               contact=CONTACT)
-                               )
-    mail.send()
+
+    data = {
+            "Application": "scripts-pde",
+            "Sujet": "Rappel Chantier {chantier_id}".format(chantier_id=chantier_id),
+            "MessageURL": TEMPLATE_URL,
+            "Message": html.escape(CONTENT.format(chantier_id=chantier_id, date_status=since, contact=CONTACT), quote=False),
+            "Email": mail_to,
+            "NomExpediteur": "Script - Parc Départemental d'Erstein",
+            "NomDestinataire": nom_dest
+           }
+
+    r = requests.post("http://t-referentiel.bas-rhin.fr/Facteur/Poste/Contenu", auth=AUTH, data=data)
+
+    logger.info("Transmission du mail au serveur: %s %s", r.status_code, r.text)
+    r.raise_for_status()
 
     if DEBUG:
         break

+ 1 - 0
requirements.txt

@@ -5,6 +5,7 @@ python-dateutil
 pyyaml
 pyshp
 python-dateutil
+requests
 
 # Pour l'edition manuelle
 PyQt5

+ 0 - 30
resources/mail.html

@@ -1,30 +0,0 @@
-<!DOCTYPE html>
-
-<html lang="fr" xmlns="http://www.w3.org/1999/xhtml">
-    <head>
-        <meta charset="utf-8" />
-        <title>Parc Départemental d'Erstein - Rappel</title>
-    </head>
-    
-    <body>
-        <div style="background-color:#f2f2f2;text-align:center;">
-            <i>** Ceci est un mail automatique, veuillez ne pas répondre. **</i>
-        </div>
-
-        <h1 style="font-size: 150%;color:#325d81;">Rappel Chantier {chantier_id}</h1>
-	
-		<p>Bonjour,</p>
-
-        <p style="display: flex;justify-content: flex-start;margin:30px 0 5px 12px;">
-			> le chantier numéro {chantier_id} est en attente d'une intervention depuis le {date_status:%d-%m-%Y}.
-        </p>
-
-        <p style="margin:5px 0 30px 12px;">Pour plus d'information, ou si vous ne souhaitez plus recevoir de rappel pour ce chantier: <a href="mailto:{contact}">{contact}</a></p>
-
-		<p>
-			Merci,<br/>
-			Le Parc Départemental d'Erstein
-		</p>
-
-    </body>
-</html>