add update and delete views
This commit is contained in:
parent
1b2b4da490
commit
e2198ed74a
@ -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/<string:path>/', endpoint='comment.get'),
|
||||
Rule('/comment/<string:path>/new', endpoint='comment.create', methods=['POST']),
|
||||
Rule('/comment/<string:path>/<int:id>', endpoint='comment.get'),
|
||||
Rule('/comment/<string:path>/<int:id>', endpoint='comment.modify',
|
||||
methods=['PUT', 'DELETE']),
|
||||
url('/comment/<string:path>/', 'comment.get', ['GET']),
|
||||
url('/comment/<string:path>/new', 'comment.create', ['POST']),
|
||||
url('/comment/<string:path>/<int:id>', 'comment.get', ['GET']),
|
||||
url('/comment/<string:path>/<int:id>', 'comment.modify', ['PUT', 'DELETE']),
|
||||
])
|
||||
|
||||
|
||||
|
@ -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')
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user