Skip to content

Commit

Permalink
[#676] [bukuserver API] improve tag replacement/deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
2ynn committed Mar 11, 2023
1 parent 61f10d1 commit d33f352
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
24 changes: 16 additions & 8 deletions bukuserver/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,24 @@ def get(self, tag: T.Optional[str]):
return {"name": tag, "usage_count": tags[1][tag]}

def put(self, tag: str):
bukudb = get_bukudb()
try:
new_tags = request.data.get('tags') # type: ignore
if new_tags:
new_tags = new_tags.split(',')
else:
return response_bad()
new_tags = request.data.getlist('tags') # werkzeug.datastructures.MultiDict
except AttributeError as e:
raise exceptions.ParseError(detail=str(e))
return to_response(bukudb.replace_tag(tag, new_tags))
new_tags = request.data.get('tags', []) # List[str]
if not isinstance(new_tags, list) or new_tags == []:
return response_bad()
new_str = buku.DELIM.join([tag.strip(buku.DELIM) for tag in new_tags])
bukudb = get_bukudb()
return to_response(bukudb.replace_tag(tag, [new_str]))

def delete(self, tag: str):
bukudb = get_bukudb()
tags = search_tag(db=bukudb, stag=tag)
if tag not in tags[1]:
raise exceptions.NotFound()
if bukudb.delete_tag_at_index(0, tag, chatty=False):
return response_ok()
return response_bad()


class ApiBookmarkView(MethodView):
Expand Down
4 changes: 2 additions & 2 deletions bukuserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def shell_context():
# routing
# api
tag_api_view = api.ApiTagView.as_view('tag_api')
app.add_url_rule('/api/tags', defaults={'tag': None}, view_func=tag_api_view, methods=['GET'])
app.add_url_rule('/api/tags/<tag>', view_func=tag_api_view, methods=['GET', 'PUT'])
app.add_url_rule('/api/tags', defaults={'tag': None}, view_func=tag_api_view, methods=['GET'], strict_slashes=False)
app.add_url_rule('/api/tags/<tag>', view_func=tag_api_view, methods=['GET', 'PUT', 'DELETE'])
bookmark_api_view = api.ApiBookmarkView.as_view('bookmark_api')
app.add_url_rule('/api/bookmarks', defaults={'rec_id': None}, view_func=bookmark_api_view, methods=['GET', 'POST', 'DELETE'])
app.add_url_rule('/api/bookmarks/<int:rec_id>', view_func=bookmark_api_view, methods=['GET', 'PUT', 'DELETE'])
Expand Down
13 changes: 9 additions & 4 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,27 @@ def test_tag_api(client):
rd = client.get('/api/tags/tag1')
assert rd.status_code == 200
assert rd.get_json() == {'name': 'tag1', 'usage_count': 1}
rd = client.put('/api/tags/tag1', data={'tags': 'tag3,tag4'})
rd = client.put('/api/tags/tag1', data={'tags': ['tag3', 'tag4']})
print("\r\nrd", rd.__dict__, "\r\n")
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/tags')
assert rd.status_code == 200
assert rd.get_json() == {'tags': ['tag2', 'tag3 tag4']}
assert rd.get_json() == {'tags': ['tag2', 'tag3', 'tag4']}
rd = client.put('/api/tags/tag2', data={'tags': 'tag5'})
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/tags')
assert rd.status_code == 200
assert rd.get_json() == {'tags': ['tag3 tag4', 'tag5']}
assert rd.get_json() == {'tags': ['tag3', 'tag4', 'tag5']}
assert rd.status_code == 200
rd = client.delete('/api/tags/tag4')
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks/1')
assert rd.status_code == 200
assert rd.get_json() == {
'description': '', 'tags': ['tag3 tag4', 'tag5'], 'title': 'Google',
'description': '', 'tags': ['tag3', 'tag5'], 'title': 'Google',
'url': 'http://google.com'}


Expand Down

0 comments on commit d33f352

Please sign in to comment.