Fixed major issues with add command. Added automatical deduction of last active project.
authorEugen Sawin <sawine@me73.com>
Fri, 01 Oct 2010 01:55:35 +0200
changeset 45e41c4b578da
parent 3 59413cc48bd3
child 5 9d0e6ae739cf
Fixed major issues with add command. Added automatical deduction of last active project.
cronrec.py
db.py
     1.1 --- a/cronrec.py	Fri Oct 01 00:45:07 2010 +0200
     1.2 +++ b/cronrec.py	Fri Oct 01 01:55:35 2010 +0200
     1.3 @@ -29,9 +29,6 @@
     1.4  
     1.5  CONFIG_FILE = "%s/.cronrecrc" % HOMEDIR
     1.6  
     1.7 -DEF_PROJECT = "default"
     1.8 -DEF_ACTIVITY = "default"
     1.9 -
    1.10  def read_config():
    1.11  	config = {}
    1.12  	with open(CONFIG_FILE, "r") as config_stream:
    1.13 @@ -89,10 +86,6 @@
    1.14  		project, activity = label.split(":")
    1.15  	else:
    1.16  		project = label
    1.17 -	if not project:
    1.18 -		project = DEF_PROJECT
    1.19 -	if not activity:
    1.20 -		activity = DEF_ACTIVITY
    1.21  	db.begin(db_file(), project, activity, datetime.now())	
    1.22  	print "begins %s:%s" % (project, activity)
    1.23      
     2.1 --- a/db.py	Fri Oct 01 00:45:07 2010 +0200
     2.2 +++ b/db.py	Fri Oct 01 01:55:35 2010 +0200
     2.3 @@ -49,6 +49,9 @@
     2.4  TABLES = (ACTIVITIES_TABLE, COMPANIES_TABLE, PROJECTS_TABLE, RATES_TABLE,
     2.5  TASKS_TABLE)
     2.6  
     2.7 +DEF_PROJECT = "default"
     2.8 +DEF_ACTIVITY = "default"
     2.9 +
    2.10  def session(db_file):
    2.11  	con = sqlite3.connect(db_file)
    2.12  	return con, con.cursor()
    2.13 @@ -68,9 +71,53 @@
    2.14  	con.commit()
    2.15  	cur.close()
    2.16  
    2.17 +def last_project(db_file, time):
    2.18 +	con, cur = session(db_file)
    2.19 +	values = (time,)
    2.20 +	sql = "select project from tasks where begin < ? order by begin desc"
    2.21 +	cur.execute(sql, values)
    2.22 +	id = cur.fetchone()
    2.23 +	if id:
    2.24 +		values = (id[0],)
    2.25 +		sql = "select name from projects where rowid=?"
    2.26 +		cur.execute(sql, values)
    2.27 +		project =  cur.fetchone()[0]
    2.28 +	else:
    2.29 +		project = DEF_PROJECT
    2.30 +	con.commit()
    2.31 +	cur.close()
    2.32 +	return project
    2.33 +
    2.34 +def row_id(db_file, table, name):
    2.35 +	con, cur = session(db_file)
    2.36 +	values = (name,)
    2.37 +	sql = "select rowid from %s where name=?" % table
    2.38 +	cur.execute(sql, values)
    2.39 +	id = cur.fetchone()
    2.40 +	if not id:
    2.41 +		sql = "insert into %s(name) values(?)" % table
    2.42 +		cur.execute(sql, values)
    2.43 +		con.commit()
    2.44 +		cur.close()
    2.45 +		return row_id(db_file, table, name)
    2.46 +	con.commit()
    2.47 +	cur.close()
    2.48 +	return id[0]
    2.49 +
    2.50 +def project_id(db_file, name):
    2.51 +	return row_id(db_file, "projects", name)
    2.52 +
    2.53 +def activity_id(db_file, name):
    2.54 +	return row_id(db_file, "activities", name)
    2.55 +
    2.56  def begin(db_file, project, activity, time):
    2.57 +	if not activity:
    2.58 +		activity = DEF_ACTIVITY
    2.59 +	if not project:
    2.60 +		project = last_project(db_file, time) 
    2.61  	con, cur = session(db_file)
    2.62 -	values = (time, None, project, activity, None)
    2.63 +	values = (time, None, project_id(db_file, project), 
    2.64 +				activity_id(db_file, activity), None)
    2.65  	sql = "insert into tasks values(?, ?, ?, ?, ?)"
    2.66  	cur.execute(sql, values)
    2.67  	con.commit()