summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Krogh <markus@nordu.net>2016-03-10 13:23:40 +0000
committerMarkus Krogh <markus@nordu.net>2016-03-10 13:23:40 +0000
commit72a75a3e454cdf46dcb0a538f5263211f176995b (patch)
treedf452ec5a13a6e1a1bd6e44a7459d64ca2043eb0
parentab15931fb292b1d39b957b2977e556522fd26ed3 (diff)
Adding mailer
-rw-r--r--maconomy/mailer.py4
-rw-r--r--maconomy/templates.py2
-rw-r--r--maconomy_hours.py47
-rw-r--r--templates/manager.html2
-rw-r--r--templates/missing.html2
-rw-r--r--templates/unsubmitted.html2
6 files changed, 38 insertions, 21 deletions
diff --git a/maconomy/mailer.py b/maconomy/mailer.py
index ab443c0..4f4070f 100644
--- a/maconomy/mailer.py
+++ b/maconomy/mailer.py
@@ -7,8 +7,8 @@ class Mailer:
server_addr = config.get("mail", "server")
self.server = smtplib.SMTP(server_addr)
- def send(to, subject, body):
- msg = MIMEText(body,'plain')
+ def send(self, to, subject, body):
+ msg = MIMEText(body,'html')
msg['To']=to
msg['From']=self.me
msg['Subject'] = subject
diff --git a/maconomy/templates.py b/maconomy/templates.py
index 4889c48..05c3319 100644
--- a/maconomy/templates.py
+++ b/maconomy/templates.py
@@ -34,7 +34,7 @@ class ManagerEmailTemplate(BaseTemplate):
def build(self, timesheets, maconomyurl):
statuslist = self.build_statuslist(timesheets)
- return self.template.substitute(statuslist=statuslist,maconomyurl=maconomyurl)
+ return self.template.substitute(statuslist=statuslist,maconomyurl=maconomyurl, amount=len(timesheets))
def build_statuslist(self, timesheets):
statuslist = [self.status_template.build(timesheet) for timesheet in timesheets]
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):
diff --git a/templates/manager.html b/templates/manager.html
index 2cb8054..9b46797 100644
--- a/templates/manager.html
+++ b/templates/manager.html
@@ -1,4 +1,4 @@
-<p>Missing time registrations for your employees, please submit and approve before tuesday 2 AM (CET)</p>
+<p>Missing time registrations for $amount of your employees, please submit and approve before tuesday 2 AM (CET)</p>
$statuslist
diff --git a/templates/missing.html b/templates/missing.html
index c17c8fc..dbe702d 100644
--- a/templates/missing.html
+++ b/templates/missing.html
@@ -2,5 +2,5 @@
<p>Please go to <a href="$maconomyurl">Maconomy Portal</a> to register time.</p>
-<p>For information on how to use time registration please see <a href="$helpurl">Time registration in Maconomy</a></p>
+<p>For information on how to use the time registration system please see <a href="$helpurl">Time registration in Maconomy</a></p>
diff --git a/templates/unsubmitted.html b/templates/unsubmitted.html
index 73af949..5fabaaf 100644
--- a/templates/unsubmitted.html
+++ b/templates/unsubmitted.html
@@ -2,4 +2,4 @@
<p>Please go to <a href="$maconomyurl">Maconomy Portal</a> to register time.</p>
-<p>For information on how to use time registration please see <a href="$helpurl">Time registration in Maconomy</a></p>
+<p>For information on how to use the time registration system please see <a href="$helpurl">Time registration in Maconomy</a></p>