Просмотр исходного кода

ajoute ctrl_mailing - mails auto pour rappel aux maitres d'oeuvre

olivier.massot 8 лет назад
Родитель
Сommit
f2de77b6fa
6 измененных файлов с 140 добавлено и 7 удалено
  1. 1 0
      .gitignore
  2. 0 1
      core/logconf.py
  3. 5 2
      core/logging.yaml
  4. 35 0
      core/mail.py
  5. 98 0
      ctrl_mailing.py
  6. 1 4
      gf2factures.py

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 *.pyc
 .project
 .pydevproject
+.settings/
 Output/
 htmlcov/
 *.log

+ 0 - 1
core/logconf.py

@@ -13,7 +13,6 @@ import yaml
 
 from core.pde import DEFAULT_LOG_DIR
 
-
 SYS_EXCEPT_HOOK = sys.excepthook
 
 def start(name="main", level=0, filename=""):

+ 5 - 2
core/logging.yaml

@@ -39,7 +39,6 @@ loggers:
         level: INFO
         handlers: [console, file, mail]
         propagate: no
-
     gf2factures:
         level: INFO
         handlers: [console, file, mail]
@@ -47,7 +46,11 @@ loggers:
     pda2suiviactivite:
         level: INFO
         handlers: [console]
-        propagate: no    
+        propagate: no
+    ctrl_mailing:
+        level: INFO
+        handlers: [console, file, mail]
+        propagate: no       
           
 root:
     level: INFO

+ 35 - 0
core/mail.py

@@ -0,0 +1,35 @@
+'''
+
+usage:
+
+    m = Mail("mail-auto@bas-rhin.fr", ["destinataire@bas-rhin.fr"], "mon mail", "mon message")
+    m.send()
+
+'''
+from email.mime.text import MIMEText
+import smtplib
+
+class Mail():
+    HOST = "smtp.bas-rhin.fr"
+
+    def __init__(self, fromaddr, toaddrs, subject, msg):
+        self.mailport = None
+        self.fromaddr = fromaddr
+        self.toaddrs = toaddrs
+        self.subject = subject
+        self.msg = msg
+
+    def send(self):
+        port = self.mailport if self.mailport else smtplib.SMTP_PORT
+
+        msg = MIMEText(self.msg.encode('utf-8'), _charset='utf-8')
+        msg['Subject'] = self.subject
+        msg['From'] = self.fromaddr
+        msg['To'] = ",".join(self.toaddrs)
+
+        smtp = smtplib.SMTP(self.HOST, port)
+        smtp.sendmail(self.fromaddr, self.toaddrs, msg.as_string())
+        smtp.quit()
+
+
+

+ 98 - 0
ctrl_mailing.py

@@ -0,0 +1,98 @@
+'''
+Created on 27 oct. 2017
+
+@author: olivier.massot
+'''
+import logging
+
+from core import logconf
+from core.mail import Mail
+from core.pde import ControlesDb
+
+logger = logging.getLogger("ctrl_mailing")
+logconf.start("ctrl_mailing", logging.DEBUG)
+
+# #     CONFIG
+# Nombre de jours à partir duquel les mails sont envoyés
+SEUIL_DUREE = 15
+
+# Demarrer la requete de slection à partir du chantier:
+CHANTIER_DEPART = 175000
+
+# Contact
+CONTACT = "parc.erstein@bas-rhin.fr"
+
+# Adresse mail depuis laquelle les mails sont envoyés
+SENDER = "mail.auto@bas-rhin.fr"
+SUBJECT = "Mail automatique - Rappel"
+CONTENT = """Bonjour,
+-- Ceci est un rappel automatique, veuillez ne pas répondre à cette adresse --
+
+> le chantier numéro {chantier_id} est en attente d'une intervention depuis le {date_status}.
+
+Pour plus d'information: {contact}
+
+Merci,
+Le Parc Départemental d'Erstein
+"""
+#   NB: variables optionelles utilisables dans CONTENT: {chantier_id}, {date_status}, {contact}
+
+
+# #     INITIALISATION
+
+db = ControlesDb()
+
+# Sous requête: liste les chantiers compactage/étanchéite/video.
+# si le chantier est en état A1, a1_status est Vrai
+subsql = """SELECT tblCompactageBases.lngChantierId,
+                (tblCompactageBases.bytStatus=45 Or tblCompactageBases.bytStatus=46 Or tblCompactageBases.bytStatus=47) AS a1_status,
+                tblCompactageBases.dtmStatus AS since
+            FROM tblCompactageBases
+            UNION
+            SELECT tblEtancheiteBases.lngChantierId,
+                (tblEtancheiteBases.bytStatus=45 Or tblEtancheiteBases.bytStatus=46 Or tblEtancheiteBases.bytStatus=47) AS a1_status,
+                tblEtancheiteBases.dtmStatus AS since
+            FROM tblEtancheiteBases
+            UNION
+            SELECT tblVideoBases.lngChantierId,
+                (tblVideoBases.bytStatus=45 Or tblVideoBases.bytStatus=46 Or tblVideoBases.bytStatus=47) AS a1_status,
+                tblVideoBases.dtmStatus AS since
+            FROM tblVideoBases
+            """
+
+# Selectionne les chantiers pour lesquels un mail doit être envoyé
+sql = """SELECT tblChantiers.lngChantierId, tblChantiers.mailContact,
+            tblChantiers.stopMails, statuts.a1_status, statuts.since
+        FROM tblChantiers INNER JOIN ({subsql}) as statuts ON tblChantiers.lngChantierId = statuts.lngChantierId
+        WHERE statuts.a1_status=True
+            AND tblChantiers.lngChantierId>={depart}
+            AND statuts.since<=(Date()-{seuil})
+            ;
+        """.format(subsql=subsql,
+                   seuil=SEUIL_DUREE,
+                   depart=CHANTIER_DEPART)
+
+# #    PROCESS
+logger.debug(sql)
+
+qry = db.execute(sql)
+
+for row in qry:
+    chantier_id, mail_to, stop_mails, a1, since = row
+
+    if stop_mails:
+        logger.info("X Chantier %s: l'envoi de mail est bloqué", chantier_id)
+        continue
+
+    if not mail_to:
+        logger.warning("X Chantier %s: pas d'adresse de contact", chantier_id)
+        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)
+                               )

+ 1 - 4
gf2factures.py

@@ -57,7 +57,4 @@ for titre in ws:
 
     updated += 1
 
-logger.info("Operation ended: {} lines analysed, {} updated".format(analysed, updated))
-
-
-
+logger.info("Operation ended: {} lines analysed, {} updated".format(analysed, updated))