dispatch.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import csv
  2. from pathlib import Path
  3. from mail import mail
  4. from itertools import groupby
  5. import config
  6. import jinja2
  7. from datetime import datetime
  8. cfg = config.config('GAPS3.ini')
  9. html = """
  10. <!DOCTYPE html>
  11. <html><head>
  12. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  13. <style>
  14. .liste { border: 1px solid #888888; border-collapse: collapse; }
  15. .liste td, .liste th { padding: 3px 15px; border: 1px solid #888888; }
  16. </style>
  17. </head><body>
  18. <table class="liste">
  19. <tr>
  20. <th>Datei</th><th>Bericht</th><th>Schicht</th>
  21. <th>Stand</th><th>Empf&auml;nger</th>
  22. </tr>
  23. {% for row in reports %}
  24. <tr>
  25. <td>{{row.Name}}</td><td>{{row.Report}}</td><td>{{row.Schicht}}</td>
  26. <td>{{row.Stand}}</td><td>{{row.Empfaenger}}</td>
  27. </tr>
  28. {% endfor %}
  29. </table>
  30. </body></html> """
  31. def dispatch(xml_filename, recipient=None, report=None, cube=None):
  32. publish_subdir = config.joinpath(cfg.cognos7.publish_dir, xml_filename[:-4])
  33. with open(cfg.xml_dir + '/info/versand.csv', 'r') as frh:
  34. csv_reader = csv.DictReader(frh, delimiter=';')
  35. dispatch_list = [x for x in csv_reader]
  36. def dispatch_filter(x):
  37. return all([
  38. x['Datei'].lower() == xml_filename,
  39. x['Versand'] == 'J',
  40. (recipient is None or x['Empfaenger'] == recipient),
  41. (report is None or x['Report'].lower() == report.lower()),
  42. (cube is None or x['Cube'].lower() == cube.lower())
  43. ])
  44. dispatch_list = list(filter(dispatch_filter, dispatch_list))
  45. dispatch_group = groupby(dispatch_list, lambda x: x['Report'] + '_' + x['PDF-Schicht'])
  46. report_mails = {}
  47. for k, v in dispatch_group:
  48. report_mails[k] = '<br/>'.join([x['Empfaenger'] for x in v])
  49. template = jinja2.Template(html)
  50. mail_batch = []
  51. for group_name, v in groupby(dispatch_list, lambda x: x['Empfaenger']):
  52. group_filenames = []
  53. d_group = list(v)
  54. for row in d_group:
  55. ext = '.xls' if row['XLS'] == 'J' else '.pdf'
  56. filename = f"{publish_subdir}/{row['Report']}_{row['PDF-Schicht']}{ext}"
  57. row['Empfaenger'] = report_mails[row['Report'] + '_' + row['PDF-Schicht']]
  58. row['Dateiname'] = []
  59. row['Stand'] = 'nicht verf&uuml;gbar'
  60. row['Schicht'] = row['PDF-Schicht'] if row['PDF-Schicht'] != '0' else 'komplett'
  61. if Path(filename).exists():
  62. mtime = datetime.fromtimestamp(Path(filename).stat().st_mtime)
  63. row['Stand'] = mtime.strftime('%d.%m.%Y, %H:%M')
  64. if (datetime.now() - mtime).total_seconds() <= 60 * 60 * 12:
  65. row['Dateiname'].append((row['Name'] + ext, filename))
  66. group_filenames.append((row['Name'] + ext, filename))
  67. if cfg.versand_separat:
  68. mail_batch.append((group_name, row['Name'], template.render(reports=[row]), row['Dateiname']))
  69. if not cfg.versand_separat:
  70. mail_batch.append((group_name, 'GAPS-Versand', template.render(reports=d_group), group_filenames))
  71. return mail_batch
  72. def send_mail(mail_batch):
  73. with mail(cfg) as m:
  74. for e in mail_batch:
  75. m.send(*e)
  76. def main(xml_filename, recipient=None, report=None, cube=None):
  77. xml_filename = xml_filename.lower()
  78. if xml_filename[-4:] != '.xml':
  79. xml_filename = xml_filename + '.xml'
  80. mail_batch = dispatch(xml_filename, recipient, report, cube)
  81. for m in mail_batch:
  82. print(m[0])
  83. send_mail([mail_batch[0]])
  84. if __name__ == '__main__':
  85. main('GAPS_Vers_Tag', 'robert.bedner@gmail.com')