ctrl_mailing.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. '''
  2. Created on 27 oct. 2017
  3. @author: olivier.massot
  4. '''
  5. import logging
  6. from core import logconf
  7. from core.mail import Mail
  8. from core.pde import ControlesDb
  9. logger = logging.getLogger("ctrl_mailing")
  10. logconf.start("ctrl_mailing", logging.DEBUG)
  11. # # CONFIG
  12. # Nombre de jours à partir duquel les mails sont envoyés
  13. SEUIL_DUREE = 15
  14. # Demarrer la requete de slection à partir du chantier:
  15. CHANTIER_DEPART = 175000
  16. # Contact
  17. CONTACT = "parc.erstein@bas-rhin.fr"
  18. # Adresse mail depuis laquelle les mails sont envoyés
  19. SENDER = "mail.auto@bas-rhin.fr"
  20. SUBJECT = "Mail automatique - Rappel"
  21. CONTENT = """Bonjour,
  22. -- Ceci est un rappel automatique, veuillez ne pas répondre à cette adresse --
  23. > le chantier numéro {chantier_id} est en attente d'une intervention depuis le {date_status}.
  24. Pour plus d'information: {contact}
  25. Merci,
  26. Le Parc Départemental d'Erstein
  27. """
  28. # NB: variables optionelles utilisables dans CONTENT: {chantier_id}, {date_status}, {contact}
  29. # # INITIALISATION
  30. db = ControlesDb()
  31. # Sous requête: liste les chantiers compactage/étanchéite/video.
  32. # si le chantier est en état A1, a1_status est Vrai
  33. subsql = """SELECT tblCompactageBases.lngChantierId,
  34. (tblCompactageBases.bytStatus=45 Or tblCompactageBases.bytStatus=46 Or tblCompactageBases.bytStatus=47) AS a1_status,
  35. tblCompactageBases.dtmStatus AS since
  36. FROM tblCompactageBases
  37. UNION
  38. SELECT tblEtancheiteBases.lngChantierId,
  39. (tblEtancheiteBases.bytStatus=45 Or tblEtancheiteBases.bytStatus=46 Or tblEtancheiteBases.bytStatus=47) AS a1_status,
  40. tblEtancheiteBases.dtmStatus AS since
  41. FROM tblEtancheiteBases
  42. UNION
  43. SELECT tblVideoBases.lngChantierId,
  44. (tblVideoBases.bytStatus=45 Or tblVideoBases.bytStatus=46 Or tblVideoBases.bytStatus=47) AS a1_status,
  45. tblVideoBases.dtmStatus AS since
  46. FROM tblVideoBases
  47. """
  48. # Selectionne les chantiers pour lesquels un mail doit être envoyé
  49. sql = """SELECT tblChantiers.lngChantierId, tblChantiers.mailContact,
  50. tblChantiers.stopMails, statuts.a1_status, statuts.since
  51. FROM tblChantiers INNER JOIN ({subsql}) as statuts ON tblChantiers.lngChantierId = statuts.lngChantierId
  52. WHERE statuts.a1_status=True
  53. AND tblChantiers.lngChantierId>={depart}
  54. AND statuts.since<=(Date()-{seuil})
  55. ;
  56. """.format(subsql=subsql,
  57. seuil=SEUIL_DUREE,
  58. depart=CHANTIER_DEPART)
  59. # # PROCESS
  60. logger.debug(sql)
  61. qry = db.execute(sql)
  62. for row in qry:
  63. chantier_id, mail_to, stop_mails, a1, since = row
  64. if stop_mails:
  65. logger.info("X Chantier %s: l'envoi de mail est bloqué", chantier_id)
  66. continue
  67. if not mail_to:
  68. logger.warning("X Chantier %s: pas d'adresse de contact", chantier_id)
  69. continue
  70. logger.info("> Chantier %s: envoi d'un mail à %s", chantier_id, mail_to)
  71. mail = Mail(SENDER,
  72. mail_to,
  73. SUBJECT,
  74. CONTENT.format(chantier_id=chantier_id,
  75. date_status=since,
  76. contact=CONTACT)
  77. )