diff --git a/isso/__init__.py b/isso/__init__.py index c33723c..afac795 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -35,16 +35,16 @@ _dumps = json.dumps setattr(json, 'dumps', lambda obj: _dumps(obj, cls=utils.IssoEncoder)) +url = lambda path, endpoint, methods: Rule(path, endpoint=endpoint, methods=methods) url_map = Map([ # moderation panel - Rule('/', endpoint='admin.index', methods=['GET', 'POST']), + url('/', 'admin.index', ['GET', 'POST']), # comments API - Rule('/comment//', endpoint='comment.get'), - Rule('/comment//new', endpoint='comment.create', methods=['POST']), - Rule('/comment//', endpoint='comment.get'), - Rule('/comment//', endpoint='comment.modify', - methods=['PUT', 'DELETE']), + url('/comment//', 'comment.get', ['GET']), + url('/comment//new', 'comment.create', ['POST']), + url('/comment//', 'comment.get', ['GET']), + url('/comment//', 'comment.modify', ['PUT', 'DELETE']), ]) diff --git a/isso/comment.py b/isso/comment.py index 8ea47d9..9e8be56 100644 --- a/isso/comment.py +++ b/isso/comment.py @@ -21,3 +21,15 @@ def get(app, environ, request, path, id=None): if not rv: abort(404) return Response(json.dumps(rv), 200, content_type='application/json') + + +def modify(app, environ, request, path, id): + + if request.method == 'PUT': + try: + rv = app.db.update(path, id, models.Comment.fromjson(request.data)) + except ValueError as e: + return Response(unicode(e), 400) + else: + rv = app.db.delete(path, id) + return Response(json.dumps(app.db.get(*rv)), 200, content_type='application/json') diff --git a/isso/db.py b/isso/db.py index 3d7af07..9c1db52 100644 --- a/isso/db.py +++ b/isso/db.py @@ -110,6 +110,7 @@ class SQLite(Abstract): def delete(self, path, id): with sqlite3.connect(self.dbpath) as con: 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)) for field in Comment.fields: if field == 'text': continue con.execute('UPDATE comments SET %s=? WHERE path=? AND id=?' % field, diff --git a/specs/test_comment.py b/specs/test_comment.py index 07b9b39..49df0d5 100644 --- a/specs/test_comment.py +++ b/specs/test_comment.py @@ -18,6 +18,7 @@ class TestComments(unittest.TestCase): get = lambda self, *x, **z: Client(self.app, Response).get(*x, **z) put = lambda self, *x, **z: Client(self.app, Response).put(*x, **z) post = lambda self, *x, **z: Client(self.app, Response).post(*x, **z) + delete = lambda self, *x, **z: Client(self.app, Response).delete(*x, **z) def setUp(self): fd, self.path = tempfile.mkstemp() @@ -57,9 +58,35 @@ class TestComments(unittest.TestCase): rv = json.loads(r.data) assert len(rv) == 20 + # XXX limit=100 def testGetInvalid(self): assert self.get('/comment/path/123').status_code == 404 assert self.get('/comment/path/spam').status_code == 404 assert self.get('/comment/foo/').status_code == 404 + + def testUpdate(self): + + self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...'))) + self.put('/comment/path/1', data=json.dumps(comment( + text='Hello World', author='me', website='http://example.com/'))) + + r = self.get('/comment/path/1') + assert r.status_code == 200 + + rv = json.loads(r.data) + assert rv['text'] == 'Hello World' + assert rv['author'] == 'me' + assert rv['website'] == 'http://example.com/' + assert 'modified' in rv + + def testDelete(self): + + self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...'))) + + r = self.delete('/comment/path/1') + assert r.status_code == 200 + + c = Comment(**json.loads(r.data)) + assert c.deleted