add db.recent(mode, limit) method

pull/16/head
posativ 12 years ago
parent 2bfbad39ca
commit 05b77f99fe

@ -52,8 +52,21 @@ class Abstract:
@abc.abstractmethod
def retrieve(self, path, mode):
"""
Return all comments for `path` with `mode`.
"""
return
@abc.abstractmethod
def recent(self, mode=7, limit=None):
"""
Return most recent comments with `mode`. If `limit` is None, return
*all* comments that are currently stored, otherwise limit by `limit`.
"""
return
class SQLite(Abstract):
"""A basic :class:`Abstract` implementation using SQLite3. All comments
@ -152,3 +165,18 @@ class SQLite(Abstract):
for item in rv:
yield self.query2comment(item)
def recent(self, mode=7, limit=None):
sql = 'SELECT * FROM comments WHERE (? | mode) = ? ORDER BY created ASC'
args = [mode, mode]
if limit:
sql += ' LIMIT ?'
args.append(limit)
with sqlite3.connect(self.dbpath) as con:
rv = con.execute(sql + ';', args).fetchall()
for item in rv:
yield self.query2comment(item)

@ -70,6 +70,16 @@ class TestSQLite(unittest.TestCase):
text='F**CK', author='P*NIS', website='http://somebadhost.org/'))
assert self.db.delete('/', rv.id) == None
def test_recent(self):
self.db.add('/path/', comment(text='2'))
for x in range(5):
self.db.add('/', comment(text='%i' % (x+1)))
assert len(list(self.db.recent(mode=7))) == 6
assert len(list(self.db.recent(mode=7, limit=5))) == 5
def tearDown(self):
os.unlink(self.path)

Loading…
Cancel
Save