diff options
author | Markus Krogh <markus@nordu.net> | 2016-03-10 13:23:40 +0000 |
---|---|---|
committer | Markus Krogh <markus@nordu.net> | 2016-03-10 13:23:40 +0000 |
commit | 72a75a3e454cdf46dcb0a538f5263211f176995b (patch) | |
tree | df452ec5a13a6e1a1bd6e44a7459d64ca2043eb0 /maconomy_hours.py | |
parent | ab15931fb292b1d39b957b2977e556522fd26ed3 (diff) |
Adding mailer
Diffstat (limited to 'maconomy_hours.py')
-rw-r--r-- | maconomy_hours.py | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/maconomy_hours.py b/maconomy_hours.py index 1281823..214a0e7 100644 --- a/maconomy_hours.py +++ b/maconomy_hours.py @@ -1,4 +1,4 @@ -from maconomy import cli, create_db, TimeRegistrationRepository +from maconomy import cli, create_db, TimeRegistrationRepository, Mailer from maconomy.views import EmployeeEmailView, ManagerEmailView, CEOEmailView from collections import defaultdict @@ -6,55 +6,72 @@ def main(config, options): db = create_db(config) timereg_repo = TimeRegistrationRepository(db) timesheets = timereg_repo.all_active() + mailer = Mailer(config) if not options.dry else None if options.manager: - manager(timesheets, config, options.dry) + manager(timesheets, config, mailer) elif options.ceo: - ceo(timesheets, config, options.dry) + ceo(timesheets, config, mailer) elif options.summary: summary(timesheets) else: - normal(timesheets, config, options.dry) + normal(timesheets, config, mailer) # Close stuff + mailer.close() timereg_repo.close() db.close() -def normal(timesheets, config, dry_run): +def normal(timesheets, config, mailer): view = EmployeeEmailView(config) for timesheet in timesheets: if not timesheet.is_submitted(): mail = view.render(timesheet) - subjct = u"Timesheet reminder for {}".format(timesheet.employee) - if dry_run: - print subjct - print mail + subject = u"Timesheet reminder for {}".format(timesheet.employee) + to = timesheet.employee.email + if mailer: + if to and to.strip(): + mailer.send(to, subject, mail) + else: + print u"No email for: {}".format(timesheet.employee) else: - pass -def manager(timesheets, config, dry_run): + print subject + print mail + +def manager(timesheets, config, mailer): view = ManagerEmailView(config) + # extract employees for manager email lookup employees = dict([(t.employee.id, t.employee) for t in timesheets]) per_manager = defaultdict(list) + # filter timesheets per manager for timesheet in [t for t in timesheets if need_manager_mail(t)]: manager_id = timesheet.approver per_manager[manager_id].append(timesheet) + # send mails to managers for manager_id, relevant_timesheets in per_manager.items(): mail = view.render(relevant_timesheets) - subject = "Warning: Timesheets overdue" + subject = "Warning: Timesheets overdue for {} employees".format(len(relevant_timesheets)) manager = employees.get(manager_id) to = manager.email if manager else None - if dry_run: + if mailer: + mailer.send(to, subject, mail) + else: print "TO: {}".format(to) print subject print mail -def ceo(timesheets, config, dry_run): +def ceo(timesheets, config, mailer): + to = config.get("mail", "ceo") + # Filter only "bad" entries relevant = [t for t in timesheets if need_manager_mail(t)] view = CEOEmailView() mail = view.render(relevant) - if dry_run: + subject = "Warning: Timesheet overdue for {} employees".format(len(relevant)) + if mailer: + mailer.send(to, subject, mail) + else: print mail def need_manager_mail(timesheet): |