From c00e57756336358e9800fc8fc89392253dbf4ed8 Mon Sep 17 00:00:00 2001 From: Markus Krogh Date: Tue, 8 Mar 2016 18:51:12 +0000 Subject: Initial commmit, still needs wireing for email --- maconomy/__init__.py | 4 ++++ maconomy/cli.py | 13 +++++++++++++ maconomy/mailer.py | 20 ++++++++++++++++++++ maconomy/models.py | 25 +++++++++++++++++++++++++ maconomy/repositories.py | 35 +++++++++++++++++++++++++++++++++++ maconomy/templates.py | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 maconomy/__init__.py create mode 100644 maconomy/cli.py create mode 100644 maconomy/mailer.py create mode 100644 maconomy/models.py create mode 100644 maconomy/repositories.py create mode 100644 maconomy/templates.py (limited to 'maconomy') diff --git a/maconomy/__init__.py b/maconomy/__init__.py new file mode 100644 index 0000000..09f2516 --- /dev/null +++ b/maconomy/__init__.py @@ -0,0 +1,4 @@ +from maconomy.models import Employee, Timesheet +from maconomy.repositories import create_db, TimeRegistrationRepository +from maconomy.mailer import Mailer +from maconomy.templates import UnsubmittedEmailTemplate, MissingEmailTemplate, ManagerEmailTemplate diff --git a/maconomy/cli.py b/maconomy/cli.py new file mode 100644 index 0000000..0a11db5 --- /dev/null +++ b/maconomy/cli.py @@ -0,0 +1,13 @@ +import argparse +from ConfigParser import SafeConfigParser + +def parse(): + parser = argparse.ArgumentParser(description="Notifies people of missing hours registration") + parser.add_argument("-c", "--config", required=True) + parser.add_argument("--dry", action="store_true", default=False, help="Do not send emails to users, but print affected users instead") + return parser.parse_args() + +def load_config(conf_file): + config = SafeConfigParser() + config.read(conf_file) + return config diff --git a/maconomy/mailer.py b/maconomy/mailer.py new file mode 100644 index 0000000..ab443c0 --- /dev/null +++ b/maconomy/mailer.py @@ -0,0 +1,20 @@ +import smtplib +from email.mime.text import MIMEText + +class Mailer: + def __init__(self, config): + self.me = config.get("mail", "from") + server_addr = config.get("mail", "server") + self.server = smtplib.SMTP(server_addr) + + def send(to, subject, body): + msg = MIMEText(body,'plain') + msg['To']=to + msg['From']=self.me + msg['Subject'] = subject + self.server.sendmail(self.me, to, msg.as_string()) + + def close(self): + self.server.quit() + + diff --git a/maconomy/models.py b/maconomy/models.py new file mode 100644 index 0000000..15fb46b --- /dev/null +++ b/maconomy/models.py @@ -0,0 +1,25 @@ + +class Employee: + def __init__(self, result): + self.id, self.name, self.email = result[:3] + + def __unicode__(self): + return u"{} ({})".format(self.name, self.id) + + +class Timesheet: + def __init__(self, result): + self.week, self.submitted, self.approved = result[:3] + + def status(self): + return "submitted" if self.is_submitted() else "unsubmitted" + def is_submitted(self): + return self.submitted == 1 + def is_missing(self): + return self.submitted is None + def is_approved(self): + return self.approved == 1 + def __str__(self): + return self.__unicode__() + def __unicode__(self): + u"{} ({})".format(self.week, self.status) diff --git a/maconomy/repositories.py b/maconomy/repositories.py new file mode 100644 index 0000000..fb143e8 --- /dev/null +++ b/maconomy/repositories.py @@ -0,0 +1,35 @@ +import cx_Oracle +from maconomy.models import Employee, Timesheet + +def create_db(config): + user = config.get("db", "user") + pw = config.get("db", "password") + server = config.get("db", "server") + return cx_Oracle.connect(user, pw, server) + +class DBRepository: + def __init__(self, dbcon): + self.db = dbcon + self.cursor = dbcon.cursor() + + def close(self): + self.cursor.close() + +class TimeRegistrationRepository(DBRepository): + def all_active(self): + query = """SELECT e.EMPLOYEENUMBER,e.NAME1,e.ELECTRONICMAILADDRESS, t.WEEKNUMBER, t.SUBMITTED, t.APPROVED from EMPLOYEE e + LEFT OUTER JOIN TIMESHEETHEADER t + ON e.EMPLOYEENUMBER = t.EMPLOYEENUMBER + and t.PERIODSTART=to_char(trunc(sysdate-7, 'IW'),'YYYY.MM.DD') + WHERE e.blocked=0 + and (e.DATEENDEMPLOYMENT >= to_char(sysdate,'YYYY.MM.DD') or e.DATEENDEMPLOYMENT = ' ') + and e.employeenumber <> '99' + and e.MUSTUSETIMESHEETS=1 + ORDER BY e.employeenumber + """ + + res = self.cursor.execute(query) + rows = res.fetchall() + + return [(Employee(r), Timesheet(r[3:])) for r in rows] + diff --git a/maconomy/templates.py b/maconomy/templates.py new file mode 100644 index 0000000..07cbeb1 --- /dev/null +++ b/maconomy/templates.py @@ -0,0 +1,40 @@ +from string import Template + +class BaseTemplate(object): + def __init__(self, template_file): + self.set_template(template_file) + + def build(self, **kwargs): + return self.template.substitute(**kwargs) + + def set_template(self, template_file): + with open(template_file, 'r') as f: + template_base = f.read() + self.template = Template(template_base) + + +class UnsubmittedEmailTemplate(BaseTemplate): + def __init__(self): + self.set_template("templates/unsubmitted.html") + + def build(self, week, maconomyurl, helpurl): + return self.template.substitute(week=week, maconomyurl=maconomyurl, helpurl=helpurl) + +class MissingEmailTemplate(BaseTemplate): + def __init__(self): + self.set_template("templates/missing.html") + + def build(self, maconomyurl, helpurl): + return self.template.substitute(maconomyurl=maconomyurl, helpurl=helpurl) + +class ManagerEmailTemplate(BaseTemplate): + def __init__(self): + self.set_template("templates/manager.html") + + def build(self, employee, week, maconomyurl, **kwargs): + submitted = '' if 'submitted' in kwargs and kwargs['submitted'] else 'not' + approved = '' if 'approved' in kwargs and kwargs['approved'] else 'not' + return self.template.substitute(employee=employee.__unicode__(),week=week,maconomyurl=maconomyurl, submitted=submitted, approved=approved) + + + -- cgit v1.1