| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- '''
- 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)
- )
|