From 732c5362360226cbf70b2c2e82b68824332edef8 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Tue, 3 Apr 2018 18:01:03 +0800 Subject: [PATCH] new: dev: editing bookmark --- bukuserver/forms.py | 6 ++- bukuserver/server.py | 45 +++++++++++++++---- bukuserver/templates/bukuserver/base.html | 7 ++- .../templates/bukuserver/bookmark_edit.html | 14 ++++++ 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 bukuserver/templates/bukuserver/bookmark_edit.html diff --git a/bukuserver/forms.py b/bukuserver/forms.py index 5c8b45e..be2902a 100644 --- a/bukuserver/forms.py +++ b/bukuserver/forms.py @@ -1,7 +1,7 @@ """Forms module.""" # pylint: disable=too-few-public-methods, missing-docstring from flask_wtf import FlaskForm -from wtforms import StringField, FieldList, BooleanField, validators +from wtforms import StringField, FieldList, BooleanField, validators, HiddenField class SearchBookmarksForm(FlaskForm): @@ -16,3 +16,7 @@ class CreateBookmarksForm(FlaskForm): title = StringField() tags = StringField() description = StringField() + + +class EditBookmarksForm(CreateBookmarksForm): + bookmark_id = HiddenField(validators=[validators.required()]) diff --git a/bukuserver/server.py b/bukuserver/server.py index faab742..2349cf5 100644 --- a/bukuserver/server.py +++ b/bukuserver/server.py @@ -94,6 +94,8 @@ def bookmarks(): 'tags': list([_f for _f in bookmark[3].split(',') if _f]), 'description': bookmark[4] } + if not request.path.startswith('/api/'): + result_bookmark['id'] = bookmark [0] result['bookmarks'].append(result_bookmark) if request.path.startswith('/api/'): res = jsonify(result) @@ -181,6 +183,8 @@ def bookmark_api(id): return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \ {'ContentType': 'application/json'} bukudb = getattr(flask.g, 'bukudb', BukuDb()) + bookmark_form = forms.CreateBookmarksForm() + is_html_post_request = request.method == 'POST' and not request.path.startswith('/api/') if request.method == 'GET': bookmark = bukudb.get_rec_by_id(id) if bookmark is not None: @@ -190,18 +194,40 @@ def bookmark_api(id): 'tags': list([_f for _f in bookmark[3].split(',') if _f]), 'description': bookmark[4] } - return jsonify(result) + if request.path.startswith('/api/'): + res = jsonify(result) + else: + bookmark_form.url.data = result['url'] + bookmark_form.title.data = result['title'] + bookmark_form.tags.data = bookmark[3] + bookmark_form.description.data = result['description'] + res = render_template( + 'bukuserver/bookmark_edit.html', + result=result, + bookmark_form=bookmark_form, + bookmark_id=bookmark[0] + ) else: - return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \ + res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \ {'ContentType': 'application/json'} - elif request.method == 'PUT': - result_flag = bukudb.update_rec( - id, request.form['url'], request.form.get('title'), request.form['tags'], request.form['description']) - if result_flag: - res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'} + elif request.method == 'PUT' or is_html_post_request: + if request.method == 'PUT': + result_flag = bukudb.update_rec( + id, request.form['url'], request.form.get('title'), request.form['tags'], request.form['description']) + if result_flag and not is_html_post_request: + res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'} + elif not result_flag and not is_html_post_request: + res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'} + elif is_html_post_request: + result_flag = bukudb.update_rec( + id, bookmark_form.url.data, bookmark_form.title.data, bookmark_form.tags.data, bookmark_form.description.data) + if result_flag: + flash(Markup('Success edit bookmark, id:{}'.format(id)), 'success') + else: + flash(Markup('Failed edit bookmark, id:{}'.format(id)), 'danger') + res = redirect(url_for('bookmarks-html')) else: - res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'} - + abort(400, description="Unknown Condition") else: result_flag = bukudb.delete_rec(id) if result_flag: @@ -422,6 +448,7 @@ def create_app(config_filename=None): app.add_url_rule('/bookmarks', 'bookmarks-html', bookmarks, methods=['GET', 'POST', 'DELETE']) app.add_url_rule('/api/bookmarks/refresh', 'refresh_bookmarks', refresh_bookmarks, methods=['POST']) app.add_url_rule('/api/bookmarks/', 'bookmark_api', bookmark_api, methods=['GET', 'PUT', 'DELETE']) + app.add_url_rule('/bookmarks/', 'bookmark_api-html', bookmark_api, methods=['GET', 'POST']) app.add_url_rule('/api/bookmarks//refresh', 'refresh_bookmark', refresh_bookmark, methods=['POST']) app.add_url_rule('/api/bookmarks//tiny', 'get_tiny_url', get_tiny_url, methods=['GET']) app.add_url_rule('/api/bookmarks//long', 'get_long_url', get_long_url, methods=['GET']) diff --git a/bukuserver/templates/bukuserver/base.html b/bukuserver/templates/bukuserver/base.html index 145827d..5684ef6 100644 --- a/bukuserver/templates/bukuserver/base.html +++ b/bukuserver/templates/bukuserver/base.html @@ -58,7 +58,12 @@ {% for item in bookmarks %} -

{{item.title}}

+

+ {{item.title}} + + + +

{{item.url}}

{{item.description}}

diff --git a/bukuserver/templates/bukuserver/bookmark_edit.html b/bukuserver/templates/bukuserver/bookmark_edit.html new file mode 100644 index 0000000..9d227a6 --- /dev/null +++ b/bukuserver/templates/bukuserver/bookmark_edit.html @@ -0,0 +1,14 @@ +{% extends "bukuserver/base.html" %} + +{% block content %} +

+

Edit Bookmarks

+
+
{{ bookmark_form.title.label }} {{ bookmark_form.title(class_="form-control") }}
+
{{ bookmark_form.url.label }} {{ bookmark_form.url(class_="form-control") }}
+
{{ bookmark_form.tags.label }} {{ bookmark_form.tags(class_="form-control") }}
+
{{ bookmark_form.description.label }} {{ bookmark_form.description(class_="form-control") }}
+ +
+
+{% endblock %}