dispatch.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import pandas as pd
  2. from pathlib import Path
  3. from mail import mail
  4. import config
  5. cfg = config.config()
  6. report_dir = cfg.portal_dir + '\\System\\Report'
  7. publish_dir = cfg.portal_dir + '\\Publish\\daten'
  8. def html_mail(content):
  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"><tr><th>Datei</th><th>Bericht</th><th>Schicht</th><th>Stand</th><th>Empf&auml;nger</th></tr>
  19. {0}
  20. </table>
  21. </body></html> """
  22. return html.replace('{0}', content)
  23. xml_filename = 'GAPS_Vers_taeglich_MO'
  24. publish_subdir = publish_dir + '\\' + xml_filename
  25. xml_filename = xml_filename.lower() + '.xml'
  26. df = pd.read_csv(cfg.xml_dir + '\\info\\versand.csv', sep=';')
  27. xml_filter = (df['Datei'].str.lower() == xml_filename) & (df['Versand'] == 'J')
  28. df = df[xml_filter]
  29. report_mails = df.groupby(['Report', 'PDF-Schicht']).agg(lambda x: ',<br/> '.join(x))['Empfaenger']
  30. mail_batch = []
  31. for group_name, df_group in df.groupby('Empfaenger'):
  32. group_table = []
  33. group_filenames = []
  34. for i, row in df_group.iterrows():
  35. filename = f"{publish_subdir}\\{row['Report']}_{row['PDF-Schicht']}"
  36. filename = filename + '.xls' if row['XLS'] == 'J' else filename + '.pdf'
  37. row['Empfaenger'] = report_mails.at[row['Report'], row['PDF-Schicht']]
  38. row['Dateiname'] = []
  39. row['Stand'] = 'nicht verf&uuml;gbar'
  40. if Path(filename).exists():
  41. row['Stand'] = Path(filename).stat().st_mtime
  42. row['Dateiname'].append((row['Name'], filename))
  43. group_filenames.append((row['Name'], filename))
  44. table_row = f"<tr><td>{row['Name']}</td><td>{row['Report']}</td><td>{row['PDF-Schicht']}</td><td>{row['Stand']}</td><td>{row['Empfaenger']}</td></tr>"
  45. group_table.append(table_row)
  46. if cfg.versand_separat:
  47. mail_batch.append((group_name, row['Name'], html_mail(table_row), row['Dateiname']))
  48. if not cfg.versand_separat:
  49. mail_batch.append((group_name, 'GAPS-Versand', html_mail(''.join(group_table)), group_filenames))
  50. with mail() as m:
  51. for e in mail_batch:
  52. m.send(*e)