Added basic end command.
authorEugen Sawin <sawine@me73.com>
Fri, 01 Oct 2010 17:09:30 +0200
changeset 59d0e6ae739cf
parent 4 5e41c4b578da
child 6 6f89b07e12cc
Added basic end command.
cronrec.py
db.py
     1.1 --- a/cronrec.py	Fri Oct 01 01:55:35 2010 +0200
     1.2 +++ b/cronrec.py	Fri Oct 01 17:09:30 2010 +0200
     1.3 @@ -90,7 +90,15 @@
     1.4  	print "begins %s:%s" % (project, activity)
     1.5      
     1.6  def end(args):
     1.7 -	print "ends"
     1.8 +	project = None
     1.9 +	activity = None
    1.10 +	label = args.label.strip()
    1.11 +	if ":" in label:
    1.12 +		project, activity = label.split(":")
    1.13 +	else:
    1.14 +		project = label
    1.15 +	db.end(db_file(), project, activity, datetime.now())	
    1.16 +	print "ends %s:%s" % (project, activity)
    1.17  
    1.18  def main():
    1.19  	global config
     2.1 --- a/db.py	Fri Oct 01 01:55:35 2010 +0200
     2.2 +++ b/db.py	Fri Oct 01 17:09:30 2010 +0200
     2.3 @@ -71,23 +71,42 @@
     2.4  	con.commit()
     2.5  	cur.close()
     2.6  
     2.7 -def last_project(db_file, time):
     2.8 +def find_last_project(db_file, time):
     2.9  	con, cur = session(db_file)
    2.10  	values = (time,)
    2.11 -	sql = "select project from tasks where begin < ? order by begin desc"
    2.12 +	sql = "select project from tasks where begin < ? and end is null order by \
    2.13 +begin desc"
    2.14  	cur.execute(sql, values)
    2.15  	id = cur.fetchone()
    2.16  	if id:
    2.17  		values = (id[0],)
    2.18  		sql = "select name from projects where rowid=?"
    2.19  		cur.execute(sql, values)
    2.20 -		project =  cur.fetchone()[0]
    2.21 +		project = cur.fetchone()[0]
    2.22  	else:
    2.23  		project = DEF_PROJECT
    2.24  	con.commit()
    2.25  	cur.close()
    2.26  	return project
    2.27  
    2.28 +def find_last_activity(db_file, time):
    2.29 +	con, cur = session(db_file)
    2.30 +	values = (time,)
    2.31 +	sql = "select activity from tasks where begin < ? and end is null order by \
    2.32 +begin desc"
    2.33 +	cur.execute(sql, values)
    2.34 +	id = cur.fetchone()
    2.35 +	if id:
    2.36 +		values = (id[0],)
    2.37 +		sql = "select name from activities where rowid=?"
    2.38 +		cur.execute(sql, values)
    2.39 +		activity = cur.fetchone()[0]
    2.40 +	else:
    2.41 +		activity = DEF_ACTIVITY
    2.42 +	con.commit()
    2.43 +	cur.close()
    2.44 +	return activity
    2.45 +
    2.46  def row_id(db_file, table, name):
    2.47  	con, cur = session(db_file)
    2.48  	values = (name,)
    2.49 @@ -110,11 +129,30 @@
    2.50  def activity_id(db_file, name):
    2.51  	return row_id(db_file, "activities", name)
    2.52  
    2.53 +def end(db_file, project, activity, time):
    2.54 +	if not activity:
    2.55 +		activity = find_last_activity(db_file, time)
    2.56 +	if not project:
    2.57 +		project = find_last_project(db_file, time) 
    2.58 +	con, cur = session(db_file)
    2.59 +	values = (time, project_id(db_file, project), activity_id(db_file, activity))
    2.60 +	sql = "update tasks set end=? where project=? and activity=?"
    2.61 +	cur.execute(sql, values)
    2.62 +	con.commit()
    2.63 +	cur.execute("select * from tasks")
    2.64 +	for c in cur:
    2.65 +		print c
    2.66 +	cur.close()
    2.67 +
    2.68  def begin(db_file, project, activity, time):
    2.69 +	last_project = find_last_project(db_file, time) 
    2.70 +	last_activity = find_last_activity(db_file, time)
    2.71  	if not activity:
    2.72  		activity = DEF_ACTIVITY
    2.73  	if not project:
    2.74 -		project = last_project(db_file, time) 
    2.75 +		project = last_project
    2.76 +	if activity != last_activity or project != last_project:
    2.77 +		end(db_file, project, last_activity, time)
    2.78  	con, cur = session(db_file)
    2.79  	values = (time, None, project_id(db_file, project), 
    2.80  				activity_id(db_file, activity), None)