db.py
changeset 11 8a99ccb99361
parent 9 fab8e1981155
child 12 28c80ae695dc
     1.1 --- a/db.py	Tue Oct 05 15:19:14 2010 +0200
     1.2 +++ b/db.py	Tue Oct 05 15:38:38 2010 +0200
     1.3 @@ -129,10 +129,16 @@
     1.4  	cur.close()
     1.5  	return activity
     1.6  
     1.7 -def find_active_task(db_file):
     1.8 +def find_active_task(db_file, time=None):
     1.9  	con, cur = session(db_file)
    1.10 -	sql = "select project, activity from tasks where end is null"
    1.11 -	cur.execute(sql)	
    1.12 +	if time:
    1.13 +		values = (time, time)
    1.14 +		sql = "select project, activity from tasks where begin < ? and \
    1.15 +(end is null or end > ?)"
    1.16 +	else:
    1.17 +		values = ()
    1.18 +		sql = "select project, activity from tasks where end is null"
    1.19 +	cur.execute(sql, values)	
    1.20  	task = cur.fetchone()
    1.21  	if task:
    1.22  		task = task[0]	
    1.23 @@ -212,16 +218,17 @@
    1.24  
    1.25  def is_paused(db_file, time):
    1.26  	con, cur = session(db_file)
    1.27 -	sql = "select rowid from breaks where end is null"
    1.28 -	cur.execute(sql)
    1.29 +	values = (time,)
    1.30 +	sql = "select rowid from breaks where begin < ? and end is null"
    1.31 +	cur.execute(sql, values)
    1.32  	paused = cur.fetchone()
    1.33  	cur.close()
    1.34  	return paused
    1.35  
    1.36 -def pause(db_file, project, activity, time):
    1.37 +def pause(db_file, time):
    1.38  	print "pausing" 
    1.39  	if not is_paused(db_file, time):
    1.40 -		task = find_active_task(db_file)
    1.41 +		task = find_active_task(db_file, time)
    1.42  		con, cur = session(db_file)
    1.43  		values = (time, None, task, None)
    1.44  		sql = "insert into breaks values(?, ?, ?, ?)"
    1.45 @@ -232,10 +239,12 @@
    1.46  
    1.47  def resume(db_file, task, time, log):
    1.48  	print "resuming"
    1.49 -	if is_paused(db_file, time):		
    1.50 +	if is_paused(db_file, time):	
    1.51 +		if not task:
    1.52 +			task = find_active_task(db_file, time)	
    1.53  		con, cur = session(db_file)
    1.54  		values = (time, log, task)
    1.55 -		sql = "update breaks set end=?, log=? where task=?"
    1.56 +		sql = "update breaks set end=?, log=? where task=? and end is null"
    1.57  		cur.execute(sql, values)
    1.58  		con.commit()
    1.59  		cur.close()