From 05b77f99fe78d3e2dbb5e81f5d6f2b61a73cb75c Mon Sep 17 00:00:00 2001 From: posativ Date: Wed, 24 Oct 2012 20:09:22 +0200 Subject: [PATCH] add db.recent(mode, limit) method --- isso/db.py | 28 ++++++++++++++++++++++++++++ specs/test_db.py | 10 ++++++++++ 2 files changed, 38 insertions(+) diff --git a/isso/db.py b/isso/db.py index 426ea61..e42e1b5 100644 --- a/isso/db.py +++ b/isso/db.py @@ -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) diff --git a/specs/test_db.py b/specs/test_db.py index 1f91298..dc21920 100644 --- a/specs/test_db.py +++ b/specs/test_db.py @@ -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)