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 12, 2023
1 parent 61f10d1 commit 94522a0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
5 changes: 3 additions & 2 deletions buku
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ class BukuDb:
"""

orig = delim_wrap(orig)
newtags = parse_tags(new) if new else DELIM
newtags = parse_tags(DELIM.join(new)) if new else DELIM

if orig == newtags:
print('Tags are same.')
Expand Down Expand Up @@ -5981,7 +5981,8 @@ POSITIONAL ARGUMENTS:
if len(args.replace) == 1:
bdb.delete_tag_at_index(0, args.replace[0])
else:
bdb.replace_tag(args.replace[0], args.replace[1:])
new = [' '.join(args.replace[1:])]
bdb.replace_tag(args.replace[0], new)

# Export bookmarks
if args.export is not None and not search_opted:
Expand Down
26 changes: 17 additions & 9 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()
except AttributeError as e:
raise exceptions.ParseError(detail=str(e))
return to_response(bukudb.replace_tag(tag, new_tags))
new_tags = request.data.getlist('tags') # werkzeug.datastructures.MultiDict
except AttributeError:
new_tags = request.data.get('tags', []) # List[str]
if not isinstance(new_tags, list) or new_tags == []:
return response_bad()
bukudb = get_bukudb()
new = [tag.strip(buku.DELIM) for tag in new_tags]
return to_response(bukudb.replace_tag(tag, new))

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 94522a0

Please sign in to comment.