from maconomy import cli, create_db, TimeRegistrationRepository from maconomy.views import EmployeeEmailView, ManagerEmailView, CEOEmailView from collections import defaultdict def main(config, options): db = create_db(config) timereg_repo = TimeRegistrationRepository(db) timesheets = timereg_repo.all_active() if options.manager: manager(timesheets, config, options.dry) elif options.ceo: ceo(timesheets, config, options.dry) elif options.summary: summary(timesheets) else: normal(timesheets, config, options.dry) # Close stuff timereg_repo.close() db.close() def normal(timesheets, config, dry_run): 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 else: pass def manager(timesheets, config, dry_run): view = ManagerEmailView(config) employees = dict([(t.employee.id, t.employee) for t in timesheets]) per_manager = defaultdict(list) for timesheet in [t for t in timesheets if need_manager_mail(t)]: manager_id = timesheet.approver per_manager[manager_id].append(timesheet) for manager_id, relevant_timesheets in per_manager.items(): mail = view.render(relevant_timesheets) subject = "Warning: Timesheets overdue" manager = employees.get(manager_id) to = manager.email if manager else None if dry_run: print "TO: {}".format(to) print subject print mail def ceo(timesheets, config, dry_run): relevant = [t for t in timesheets if need_manager_mail(t)] view = CEOEmailView() mail = view.render(relevant) if dry_run: print mail def need_manager_mail(timesheet): return not timesheet.is_submitted() or not timesheet.is_approved() def summary(timesheets): for timesheet in timesheets: if timesheet.is_missing(): print u"[Not created] {}".format(timesheet.employee) elif not timesheet.is_submitted(): print u"[Unsubmitted] {}".format(timesheet.employee) elif not timesheet.is_approved(): print u"[Not approved] {}".format(timesheet.employee) if __name__ == '__main__': args = cli.parse() config = cli.load_config(args.config) main(config, args)