add files

This commit is contained in:
Josue Gomez 2019-05-31 21:18:14 -06:00
parent 70e77c024f
commit 88854e6b4f
3 changed files with 87 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
\.env
\.idea/
perdiem\.log
*.xlsx

View File

@ -1 +1,7 @@
perDiem
just create a crontab entry for the script
this will send per diem report every first of month at 2AM
0 2 1 * * /usr/bin/python3 /home/user/perDiem/perdiem.py >> /home/user/perDiem/perdiem.out 2>&1

73
perdiem.py Normal file
View File

@ -0,0 +1,73 @@
import os
import logging
from datetime import datetime
from pyutl.sendmail import SendMail
from pyutl.localenv import localenv
localenv.load()
HOME_DIR = os.path.dirname(os.path.abspath(__file__))
THIS_MONTH = datetime.now().strftime("%m%Y")
FILE = [os.path.join(HOME_DIR, f'viatico_{THIS_MONTH}.xlsx')]
LOG_FILE = os.path.join(HOME_DIR, 'perdiem.log')
logging.basicConfig(filename=LOG_FILE, filemode='a', format='%(asctime)s %(module)s %(message)s', level=logging.DEBUG)
MAIL_HOST = localenv.get('MAIL_HOST')
MAIL_PORT = localenv.get('MAIL_PORT')
MAIL_USR = localenv.get('MAIL_USR')
MAIL_PASS = localenv.get('MAIL_PASS')
OK_DESTINY = localenv.get('OK_DESTINY')
ERR_DESTINY = localenv.get('ERR_DESTINY')
ok_message = """<html>
<body>
<p>Buen Dia</p>
<br>
<p> Adjunto Reporte de Viatico.</p>
<br>
<p>Gracias & Saludos.</p>
<P>automatic mailer</p>
</body>
</html>
"""
err_message = """<html>
<body>
No has depositado el Reporte de Viatico.
</body>
</html>
"""
def validate():
auth_info = (MAIL_HOST, MAIL_PORT, MAIL_USR, MAIL_PASS)
try:
_ = open(FILE[0])
to_address = [OK_DESTINY]
message = ok_message
return True, auth_info, to_address, message
except FileNotFoundError:
to_address = [ERR_DESTINY]
message = err_message
return False, auth_info, to_address, message
def main():
try:
fe, ai, ta, msg = validate()
logging.info(f'validation has been done file exists {fe}')
logging.debug(f'authenticate using {ai}')
with SendMail(ai) as mail:
mail.content(MAIL_USR, ta, f'Solicitud de Viaticos {THIS_MONTH}', msg)
if fe:
mail.attach(FILE)
mail.send()
except Exception as e:
logging.exception(e)
if __name__ == '__main__':
main()