Added set rate command.
authorEugen Sawin <sawine@me73.com>
Wed, 06 Oct 2010 16:07:36 +0200
changeset 134ef9c2142059
parent 12 28c80ae695dc
child 14 1b45f8231179
Added set rate command.
cronrec.py
db.py
     1.1 --- a/cronrec.py	Wed Oct 06 01:18:34 2010 +0200
     1.2 +++ b/cronrec.py	Wed Oct 06 16:07:36 2010 +0200
     1.3 @@ -1,4 +1,5 @@
     1.4  #!/usr/local/bin/python
     1.5 +# -*- coding: utf-8 -*-
     1.6  
     1.7  """
     1.8  tim - A time recording tool, because time is money.
     1.9 @@ -18,6 +19,11 @@
    1.10  CONFIG_SEP = "="
    1.11  DB_FILE = "tim.db"
    1.12  
    1.13 +CURRENCIES = {("EUR", "EURO", "€"): "EUR",
    1.14 +("US DOLLAR", "USD", "$"): "USD"}
    1.15 +
    1.16 +RATE_TYPES = {("h", "hourly"): "hourly"}
    1.17 +
    1.18  config = {}
    1.19  
    1.20  # Try to get the user's home directory path
    1.21 @@ -126,6 +132,23 @@
    1.22  	db.update_where(db_file(), "projects", "name", args.project, "customer", 
    1.23  				args.customer)
    1.24  
    1.25 +def map_name(map_base, name):
    1.26 +	mapped = None
    1.27 +	if name not in map_base.itervalues():
    1.28 +		for v in map_base.iterkeys():
    1.29 +			if name in v:
    1.30 +				mapped = map_base[v]
    1.31 +	else:
    1.32 +		mapped = name
    1.33 +	return mapped
    1.34 +
    1.35 +def set_rate(args):
    1.36 +	project, activity = parse_label(args.label)
    1.37 +	currency = map_name(CURRENCIES, args.currency)
    1.38 +	type = map_name(RATE_TYPES, args.type)
    1.39 +	print "setting rate for %s at %f %s %s" % (project, args.rate, currency, type)
    1.40 +	db.set_rate(db_file(), project, activity, args.rate, currency, type)
    1.41 +
    1.42  def status(args):
    1.43  	task = db.status(db_file())
    1.44  	if task:
    1.45 @@ -184,6 +207,13 @@
    1.46  	sub_set_customer.add_argument("customer")
    1.47  	sub_set_customer.set_defaults(func=set_customer)
    1.48  
    1.49 +	sub_set_rate = sub_set_subs.add_parser("rate")
    1.50 +	sub_set_rate.add_argument("label")
    1.51 +	sub_set_rate.add_argument("rate", type=float)
    1.52 +	sub_set_rate.add_argument("currency")
    1.53 +	sub_set_rate.add_argument("type")
    1.54 +	sub_set_rate.set_defaults(func=set_rate)
    1.55 +
    1.56  	sub_status = subs.add_parser("status")
    1.57  	sub_status.set_defaults(func=status)
    1.58  
     2.1 --- a/db.py	Wed Oct 06 01:18:34 2010 +0200
     2.2 +++ b/db.py	Wed Oct 06 16:07:36 2010 +0200
     2.3 @@ -25,11 +25,22 @@
     2.4  constraint customer_fk foreign key(customer) references companies(rowid) on delete
     2.5  cascade)"""
     2.6  
     2.7 +CURRENCIES_TABLE = """currencies(
     2.8 +name text not null)"""
     2.9 +
    2.10 +RATE_TYPES_TABLE = """rate_types(
    2.11 +name text not null)"""
    2.12 +
    2.13  RATES_TABLE = """rates(
    2.14 -rate smallmoney
    2.15 -flatrate smallmoney,
    2.16 -project int,
    2.17 +rate smallmoney not null,
    2.18 +currency int not null,
    2.19 +type int not null,
    2.20 +project int not null,
    2.21  activity int,
    2.22 +constraint currency_fk foreign key(currency) references currencies(rowid) on
    2.23 +delete cascade,
    2.24 +constraint type_fk foreign key(type) references rate_types(rowid) on delete
    2.25 +cascade,
    2.26  constraint project_fk foreign key(project) references projects(rowid) on delete
    2.27  cascade,
    2.28  constraint activity_fk foreign key(activity) references activities(rowid) on delete
    2.29 @@ -54,8 +65,8 @@
    2.30  constraint task_fk foreign key(task) references tasks(rowid) on delete
    2.31  cascade)"""
    2.32  
    2.33 -TABLES = (ACTIVITIES_TABLE, COMPANIES_TABLE, PROJECTS_TABLE, RATES_TABLE,
    2.34 -TASKS_TABLE, BREAKS_TABLE)
    2.35 +TABLES = (ACTIVITIES_TABLE, COMPANIES_TABLE, PROJECTS_TABLE, CURRENCIES_TABLE,
    2.36 +RATES_TABLE, RATE_TYPES_TABLE, TASKS_TABLE, BREAKS_TABLE)
    2.37  
    2.38  DEF_PROJECT = "default"
    2.39  DEF_ACTIVITY = "default"
    2.40 @@ -67,22 +78,33 @@
    2.41  def test(db_file):
    2.42  	con, cur = session(db_file)
    2.43  	cur.execute("select * from projects")
    2.44 -	print "\nprojects"
    2.45 +	print "\nPROJECTS"
    2.46  	for c in cur:
    2.47  		print c
    2.48  	cur.execute("select * from activities")
    2.49 -	print "\nactivities"
    2.50 +	print "\nACTIVITIES"
    2.51  	for c in cur:
    2.52  		print c
    2.53  	cur.execute("select * from tasks")
    2.54 -	print "\ntasks"
    2.55 +	print "\nTASKS"
    2.56  	for c in cur:
    2.57  		print c
    2.58  	cur.execute("select * from breaks")
    2.59 -	print "\nbreaks"
    2.60 +	print "\nBREAKS"
    2.61  	for c in cur:
    2.62  		print c
    2.63 -	con.commit()
    2.64 +	cur.execute("select * from currencies")
    2.65 +	print "\nCURRENCIES"
    2.66 +	for c in cur:
    2.67 +		print c
    2.68 +	cur.execute("select * from rate_types")
    2.69 +	print "\nRATE_TYPES"
    2.70 +	for c in cur:
    2.71 +		print c
    2.72 +	cur.execute("select * from rates")
    2.73 +	print "\nRATES"
    2.74 +	for c in cur:
    2.75 +		print c
    2.76  	cur.close()
    2.77  
    2.78  def init(db_file):
    2.79 @@ -102,6 +124,20 @@
    2.80  	cur.close()
    2.81  	test(db_file)
    2.82  
    2.83 +def set_rate(db_file, project, activity, rate, currency, type):
    2.84 +	if not activity:
    2.85 +		activity = DEF_ACTIVITY
    2.86 +	con, cur = session(db_file)
    2.87 +	values = (rate, row_id(db_file, "currencies", currency), 
    2.88 +				row_id(db_file, "rate_types", type),
    2.89 +				row_id(db_file, "projects", project), 
    2.90 +				row_id(db_file, "activities", activity))
    2.91 +	sql = "insert into rates values(?, ?, ?, ?, ?)"
    2.92 +	cur.execute(sql, values)
    2.93 +	con.commit()
    2.94 +	cur.close()
    2.95 +	test(db_file)
    2.96 +
    2.97  def find_last_project(db_file, time):
    2.98  	con, cur = session(db_file)
    2.99  	values = (time,)