most database operations now return Comment object
This commit is contained in:
parent
3d398341a3
commit
af8fcdab19
@ -12,7 +12,7 @@ def create(app, environ, request, path):
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return Response(unicode(e), 400)
|
return Response(unicode(e), 400)
|
||||||
|
|
||||||
return Response(json.dumps(app.db.get(*rv)), 201, content_type='application/json')
|
return Response(json.dumps(rv), 201, content_type='application/json')
|
||||||
|
|
||||||
|
|
||||||
def get(app, environ, request, path, id=None):
|
def get(app, environ, request, path, id=None):
|
||||||
@ -32,4 +32,4 @@ def modify(app, environ, request, path, id):
|
|||||||
return Response(unicode(e), 400)
|
return Response(unicode(e), 400)
|
||||||
else:
|
else:
|
||||||
rv = app.db.delete(path, id)
|
rv = app.db.delete(path, id)
|
||||||
return Response(json.dumps(app.db.get(*rv)), 200, content_type='application/json')
|
return Response(json.dumps(rv), 200, content_type='application/json')
|
||||||
|
15
isso/db.py
15
isso/db.py
@ -97,7 +97,8 @@ class SQLite(Abstract):
|
|||||||
)
|
)
|
||||||
|
|
||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
return con.execute('SELECT path, MAX(id) FROM comments;').fetchone()
|
return self.query2comment(
|
||||||
|
con.execute('SELECT *, MAX(id) FROM comments;').fetchone())
|
||||||
|
|
||||||
def update(self, path, id, comment):
|
def update(self, path, id, comment):
|
||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
@ -108,7 +109,7 @@ class SQLite(Abstract):
|
|||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
con.execute('UPDATE comments SET modified=? WHERE path=? AND id=?',
|
con.execute('UPDATE comments SET modified=? WHERE path=? AND id=?',
|
||||||
(time.time(), path, id))
|
(time.time(), path, id))
|
||||||
return path, id
|
return self.get(path, id)
|
||||||
|
|
||||||
def get(self, path, id):
|
def get(self, path, id):
|
||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
@ -117,14 +118,20 @@ class SQLite(Abstract):
|
|||||||
|
|
||||||
def delete(self, path, id):
|
def delete(self, path, id):
|
||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
|
refs = con.execute('SELECT id FROM comments WHERE id=?', (id, )).fetchone()
|
||||||
|
|
||||||
|
if len(refs) == 0:
|
||||||
|
con.execute('DELETE FROM comments WHERE path=? AND id=?', (path, id))
|
||||||
|
return None
|
||||||
|
|
||||||
con.execute('UPDATE comments SET text=? WHERE path=? AND id=?', ('', path, id))
|
con.execute('UPDATE comments SET text=? WHERE path=? AND id=?', ('', path, id))
|
||||||
con.execute('UPDATE comments SET mode=? WHERE path=? AND id=?', (2, path, id))
|
con.execute('UPDATE comments SET mode=? WHERE path=? AND id=?', (2, path, id))
|
||||||
for field in set(Comment.fields) - set(['text', 'parent']):
|
for field in set(Comment.fields) - set(['text', 'parent']):
|
||||||
con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?' % field,
|
con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?' % field,
|
||||||
(None, path, id))
|
(None, path, id))
|
||||||
return path, id
|
return self.get(path, id)
|
||||||
|
|
||||||
def retrieve(self, path, limit=20):
|
def retrieve(self, path, limit=20, mode=None):
|
||||||
with sqlite3.connect(self.dbpath) as con:
|
with sqlite3.connect(self.dbpath) as con:
|
||||||
rv = con.execute("SELECT * FROM comments WHERE path = ?" \
|
rv = con.execute("SELECT * FROM comments WHERE path = ?" \
|
||||||
+ " ORDER BY id DESC LIMIT ?;", (path, limit)).fetchall()
|
+ " ORDER BY id DESC LIMIT ?;", (path, limit)).fetchall()
|
||||||
|
@ -84,9 +84,14 @@ class TestComments(unittest.TestCase):
|
|||||||
def testDelete(self):
|
def testDelete(self):
|
||||||
|
|
||||||
self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
||||||
|
assert self.delete('/comment/path/1').status_code == 200
|
||||||
|
assert self.get('/comment/path/1').status_code == 404
|
||||||
|
|
||||||
r = self.delete('/comment/path/1')
|
def testDeleteWithReference(self):
|
||||||
|
|
||||||
|
self.post('/comment/path/new', data=json.dumps(comment(text='First')))
|
||||||
|
self.post('/comment/path/new', data=json.dumps(comment(text='Second', parent=1)))
|
||||||
|
|
||||||
|
r = self.delete('/comment/path/2')
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
|
assert Comment(**json.loads(r.data)).deleted
|
||||||
c = Comment(**json.loads(r.data))
|
|
||||||
assert c.deleted
|
|
||||||
|
@ -23,14 +23,14 @@ class TestSQLite(unittest.TestCase):
|
|||||||
def test_get(self):
|
def test_get(self):
|
||||||
|
|
||||||
rv = self.db.add('/', comment(text='Spam'))
|
rv = self.db.add('/', comment(text='Spam'))
|
||||||
c = self.db.get(*rv)
|
c = self.db.get('/', rv.id)
|
||||||
|
|
||||||
assert c.id == 1
|
assert c.id == 1
|
||||||
assert c.text == 'Spam'
|
assert c.text == 'Spam'
|
||||||
|
|
||||||
def test_add(self):
|
def test_add(self):
|
||||||
|
|
||||||
x = self.db.add('/', comment(text='Foo'))
|
self.db.add('/', comment(text='Foo'))
|
||||||
self.db.add('/', comment(text='Bar'))
|
self.db.add('/', comment(text='Bar'))
|
||||||
self.db.add('/path/', comment(text='Baz'))
|
self.db.add('/path/', comment(text='Baz'))
|
||||||
|
|
||||||
@ -47,10 +47,10 @@ class TestSQLite(unittest.TestCase):
|
|||||||
|
|
||||||
def test_update(self):
|
def test_update(self):
|
||||||
|
|
||||||
path, id = self.db.add('/', comment(text='Foo'))
|
rv = self.db.add('/', comment(text='Foo'))
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
path, id = self.db.update(path, id, comment(text='Bla'))
|
rv = self.db.update('/', rv.id, comment(text='Bla'))
|
||||||
c = self.db.get(path, id)
|
c = self.db.get('/', rv.id)
|
||||||
|
|
||||||
assert c.id == 1
|
assert c.id == 1
|
||||||
assert c.text == 'Bla'
|
assert c.text == 'Bla'
|
||||||
@ -58,11 +58,11 @@ class TestSQLite(unittest.TestCase):
|
|||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
|
|
||||||
path, id = self.db.add('/', comment(
|
rv = self.db.add('/', comment(
|
||||||
text='F**CK', author='P*NIS', website='http://somebadhost.org/'))
|
text='F**CK', author='P*NIS', website='http://somebadhost.org/'))
|
||||||
|
|
||||||
self.db.delete(path, id)
|
self.db.delete('/', rv.id)
|
||||||
c = self.db.get(path, id)
|
c = self.db.get('/', rv.id)
|
||||||
|
|
||||||
assert c.id == 1
|
assert c.id == 1
|
||||||
assert c.text == ''
|
assert c.text == ''
|
||||||
|
Loading…
Reference in New Issue
Block a user