# HG changeset patch # User Eugen Sawin # Date 1286374056 -7200 # Node ID 4ef9c214205981aa9422bab98b365a000607acf2 # Parent 28c80ae695dc2cc04adb4d4e7665f9cdc5d5df87 Added set rate command. diff -r 28c80ae695dc -r 4ef9c2142059 cronrec.py --- a/cronrec.py Wed Oct 06 01:18:34 2010 +0200 +++ b/cronrec.py Wed Oct 06 16:07:36 2010 +0200 @@ -1,4 +1,5 @@ #!/usr/local/bin/python +# -*- coding: utf-8 -*- """ tim - A time recording tool, because time is money. @@ -18,6 +19,11 @@ CONFIG_SEP = "=" DB_FILE = "tim.db" +CURRENCIES = {("EUR", "EURO", "€"): "EUR", +("US DOLLAR", "USD", "$"): "USD"} + +RATE_TYPES = {("h", "hourly"): "hourly"} + config = {} # Try to get the user's home directory path @@ -126,6 +132,23 @@ db.update_where(db_file(), "projects", "name", args.project, "customer", args.customer) +def map_name(map_base, name): + mapped = None + if name not in map_base.itervalues(): + for v in map_base.iterkeys(): + if name in v: + mapped = map_base[v] + else: + mapped = name + return mapped + +def set_rate(args): + project, activity = parse_label(args.label) + currency = map_name(CURRENCIES, args.currency) + type = map_name(RATE_TYPES, args.type) + print "setting rate for %s at %f %s %s" % (project, args.rate, currency, type) + db.set_rate(db_file(), project, activity, args.rate, currency, type) + def status(args): task = db.status(db_file()) if task: @@ -184,6 +207,13 @@ sub_set_customer.add_argument("customer") sub_set_customer.set_defaults(func=set_customer) + sub_set_rate = sub_set_subs.add_parser("rate") + sub_set_rate.add_argument("label") + sub_set_rate.add_argument("rate", type=float) + sub_set_rate.add_argument("currency") + sub_set_rate.add_argument("type") + sub_set_rate.set_defaults(func=set_rate) + sub_status = subs.add_parser("status") sub_status.set_defaults(func=status) diff -r 28c80ae695dc -r 4ef9c2142059 db.py --- a/db.py Wed Oct 06 01:18:34 2010 +0200 +++ b/db.py Wed Oct 06 16:07:36 2010 +0200 @@ -25,11 +25,22 @@ constraint customer_fk foreign key(customer) references companies(rowid) on delete cascade)""" +CURRENCIES_TABLE = """currencies( +name text not null)""" + +RATE_TYPES_TABLE = """rate_types( +name text not null)""" + RATES_TABLE = """rates( -rate smallmoney -flatrate smallmoney, -project int, +rate smallmoney not null, +currency int not null, +type int not null, +project int not null, activity int, +constraint currency_fk foreign key(currency) references currencies(rowid) on +delete cascade, +constraint type_fk foreign key(type) references rate_types(rowid) on delete +cascade, constraint project_fk foreign key(project) references projects(rowid) on delete cascade, constraint activity_fk foreign key(activity) references activities(rowid) on delete @@ -54,8 +65,8 @@ constraint task_fk foreign key(task) references tasks(rowid) on delete cascade)""" -TABLES = (ACTIVITIES_TABLE, COMPANIES_TABLE, PROJECTS_TABLE, RATES_TABLE, -TASKS_TABLE, BREAKS_TABLE) +TABLES = (ACTIVITIES_TABLE, COMPANIES_TABLE, PROJECTS_TABLE, CURRENCIES_TABLE, +RATES_TABLE, RATE_TYPES_TABLE, TASKS_TABLE, BREAKS_TABLE) DEF_PROJECT = "default" DEF_ACTIVITY = "default" @@ -67,22 +78,33 @@ def test(db_file): con, cur = session(db_file) cur.execute("select * from projects") - print "\nprojects" + print "\nPROJECTS" for c in cur: print c cur.execute("select * from activities") - print "\nactivities" + print "\nACTIVITIES" for c in cur: print c cur.execute("select * from tasks") - print "\ntasks" + print "\nTASKS" for c in cur: print c cur.execute("select * from breaks") - print "\nbreaks" + print "\nBREAKS" for c in cur: print c - con.commit() + cur.execute("select * from currencies") + print "\nCURRENCIES" + for c in cur: + print c + cur.execute("select * from rate_types") + print "\nRATE_TYPES" + for c in cur: + print c + cur.execute("select * from rates") + print "\nRATES" + for c in cur: + print c cur.close() def init(db_file): @@ -102,6 +124,20 @@ cur.close() test(db_file) +def set_rate(db_file, project, activity, rate, currency, type): + if not activity: + activity = DEF_ACTIVITY + con, cur = session(db_file) + values = (rate, row_id(db_file, "currencies", currency), + row_id(db_file, "rate_types", type), + row_id(db_file, "projects", project), + row_id(db_file, "activities", activity)) + sql = "insert into rates values(?, ?, ?, ?, ?)" + cur.execute(sql, values) + con.commit() + cur.close() + test(db_file) + def find_last_project(db_file, time): con, cur = session(db_file) values = (time,)