got more working... Jobs open, jobs save, what_work() and what_time() work now.

This commit is contained in:
Andrew Davidson 2012-08-16 18:54:59 -07:00
parent d0ebd37c79
commit 70d297054c
2 changed files with 89 additions and 42 deletions

View file

@ -116,10 +116,10 @@ class Job:
runs a query to populate with the information of an existing job
in the database
"""
print uuid
print "Opening"
data = self._query('SELECT * FROM jobs WHERE uuid = "' + uuid + '" LIMIT 1;', returns="one")
data = self._query('SELECT uuid,command,arguments,due,originator,completed FROM jobs WHERE uuid = "' + uuid + '" LIMIT 1;', returns="one")
print data
if data:
(self.uuid, self.command, self.arguments, self.due, self.originator, self.completed) = data
def save(self):
@ -168,7 +168,7 @@ class Job:
""" what_time()
returns a prettified version of the due date.
"""
return pretty(self.due)
return pretty_date(self.due)
def what_work(self):
@ -181,41 +181,74 @@ class Job:
def pretty_date(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
"""
from datetime import datetime
now = datetime.now()
if type(time) is int:
d = datetime.fromtimestamp(time)
elif type(time) is float:
d = datetime.fromtimestamp(time)
elif not time:
diff = now - now
def pretty(time):
'''Returns a pretty formatted date.
Inputs:
time is a datetime object or an int timestamp
asdays is True if you only want to measure days, not seconds
short is True if you want "1d ago", "2d ago", etc. False if you want
'''
import datetime
now = datetime.datetime.now()
if type(time) is float: time = datetime.datetime.fromtimestamp(time)
elif not time: time = now
if time > now: past, diff = False, time - now
else: past, diff = True, now - time
seconds = diff.seconds
days = diff.days
if days == 0 and not asdays:
if seconds < 10: return 'now'
elif seconds < 60: return _df(seconds, 1, ' seconds', past)
elif seconds < 120: return past and 'a minute ago' or 'in a minute'
elif seconds < 3600: return _df(seconds, 60, ' minutes', past)
elif seconds < 7200: return past and 'an hour ago' or'in an hour'
else: return _df(seconds, 3600, ' hours', past)
if now > d:
diff = now - d
elif d > now:
diff = d - now
else:
if days == 0: return 'today'
elif days == 1: return past and 'yesterday' or'tomorrow'
elif days == 2: return past and 'day before' or 'day after'
elif days < 7: return _df(days, 1, ' days', past)
elif days < 14: return past and 'last week' or 'next week'
elif days < 31: return _df(days, 7, ' weeks', past)
elif days < 61: return past and 'last month' or 'next month'
elif days < 365: return _df(days, 30, ' months', past)
elif days < 730: return past and 'last year' or 'next year'
else: return _df(days, 365, ' years', past)
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if now > d:
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff/7) + " weeks ago"
if day_diff < 365:
return str(day_diff/30) + " months ago"
return str(day_diff/365) + " years ago"
elif d > now:
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds from now"
if second_diff < 120:
return "a minute from now"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes from now"
if second_diff < 7200:
return "an hour from now"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours from now"
if day_diff == 1:
return "Tomorrow"
if day_diff < 7:
return str(day_diff) + " days from now"
if day_diff < 31:
return str(day_diff/7) + " weeks from now"
if day_diff < 365:
return str(day_diff/30) + " months from now"
return str(day_diff/365) + " years from now"

18
test.py
View file

@ -17,7 +17,21 @@ print 'Open job'
j.open(uuid="amd1")
print 'what_work()'
j.what_work()
print j.what_work()
print 'what_time()'
j.what_time()
print j.what_time()
print "add 3600 seconds"
j.due = j.due + 3600
print j.what_time()
print "saving"
j.save()
print "re-open"
j.open(uuid="amd1")
print "what_time()"
print j.what_time()