new: dev: editing bookmark

This commit is contained in:
rachmadaniHaryono 2018-04-03 18:01:03 +08:00
parent 9d4af71ea0
commit 732c536236
4 changed files with 61 additions and 11 deletions

View File

@ -1,7 +1,7 @@
"""Forms module.""" """Forms module."""
# pylint: disable=too-few-public-methods, missing-docstring # pylint: disable=too-few-public-methods, missing-docstring
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, FieldList, BooleanField, validators from wtforms import StringField, FieldList, BooleanField, validators, HiddenField
class SearchBookmarksForm(FlaskForm): class SearchBookmarksForm(FlaskForm):
@ -16,3 +16,7 @@ class CreateBookmarksForm(FlaskForm):
title = StringField() title = StringField()
tags = StringField() tags = StringField()
description = StringField() description = StringField()
class EditBookmarksForm(CreateBookmarksForm):
bookmark_id = HiddenField(validators=[validators.required()])

View File

@ -94,6 +94,8 @@ def bookmarks():
'tags': list([_f for _f in bookmark[3].split(',') if _f]), 'tags': list([_f for _f in bookmark[3].split(',') if _f]),
'description': bookmark[4] 'description': bookmark[4]
} }
if not request.path.startswith('/api/'):
result_bookmark['id'] = bookmark [0]
result['bookmarks'].append(result_bookmark) result['bookmarks'].append(result_bookmark)
if request.path.startswith('/api/'): if request.path.startswith('/api/'):
res = jsonify(result) res = jsonify(result)
@ -181,6 +183,8 @@ def bookmark_api(id):
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \ return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
{'ContentType': 'application/json'} {'ContentType': 'application/json'}
bukudb = getattr(flask.g, 'bukudb', BukuDb()) 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': if request.method == 'GET':
bookmark = bukudb.get_rec_by_id(id) bookmark = bukudb.get_rec_by_id(id)
if bookmark is not None: if bookmark is not None:
@ -190,18 +194,40 @@ def bookmark_api(id):
'tags': list([_f for _f in bookmark[3].split(',') if _f]), 'tags': list([_f for _f in bookmark[3].split(',') if _f]),
'description': bookmark[4] '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: 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'} {'ContentType': 'application/json'}
elif request.method == 'PUT': elif request.method == 'PUT' or is_html_post_request:
result_flag = bukudb.update_rec( if request.method == 'PUT':
id, request.form['url'], request.form.get('title'), request.form['tags'], request.form['description']) result_flag = bukudb.update_rec(
if result_flag: id, request.form['url'], request.form.get('title'), request.form['tags'], request.form['description'])
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'} 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: else:
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'} abort(400, description="Unknown Condition")
else: else:
result_flag = bukudb.delete_rec(id) result_flag = bukudb.delete_rec(id)
if result_flag: 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('/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/refresh', 'refresh_bookmarks', refresh_bookmarks, methods=['POST'])
app.add_url_rule('/api/bookmarks/<id>', 'bookmark_api', bookmark_api, methods=['GET', 'PUT', 'DELETE']) app.add_url_rule('/api/bookmarks/<id>', 'bookmark_api', bookmark_api, methods=['GET', 'PUT', 'DELETE'])
app.add_url_rule('/bookmarks/<id>', 'bookmark_api-html', bookmark_api, methods=['GET', 'POST'])
app.add_url_rule('/api/bookmarks/<id>/refresh', 'refresh_bookmark', refresh_bookmark, methods=['POST']) app.add_url_rule('/api/bookmarks/<id>/refresh', 'refresh_bookmark', refresh_bookmark, methods=['POST'])
app.add_url_rule('/api/bookmarks/<id>/tiny', 'get_tiny_url', get_tiny_url, methods=['GET']) app.add_url_rule('/api/bookmarks/<id>/tiny', 'get_tiny_url', get_tiny_url, methods=['GET'])
app.add_url_rule('/api/bookmarks/<id>/long', 'get_long_url', get_long_url, methods=['GET']) app.add_url_rule('/api/bookmarks/<id>/long', 'get_long_url', get_long_url, methods=['GET'])

View File

@ -58,7 +58,12 @@
<tbody> <tbody>
{% for item in bookmarks %} {% for item in bookmarks %}
<tr> <td style="overflow-wrap: break-word;"> <tr> <td style="overflow-wrap: break-word;">
<h3><a href="{{item.url}}">{{item.title}}</a></h3> <h3>
<a href="{{item.url}}">{{item.title}}</a>
<a href="{{url_for('bookmark_api-html', id=item.id)}}">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
</a>
</h3>
<p><a href="{{item.url}}">{{item.url}}</a></p> <p><a href="{{item.url}}">{{item.url}}</a></p>
<p>{{item.description}}</p> <p>{{item.description}}</p>
<p> <p>

View File

@ -0,0 +1,14 @@
{% extends "bukuserver/base.html" %}
{% block content %}
<div class="container">
<h1 style="padding-top: 70px;">Edit Bookmarks</h1>
<form method="POST" action="{{url_for('bookmark_api-html', id=bookmark_id)}}">
<div class="form-group"> {{ bookmark_form.title.label }} {{ bookmark_form.title(class_="form-control") }} </div>
<div class="form-group"> {{ bookmark_form.url.label }} {{ bookmark_form.url(class_="form-control") }} </div>
<div class="form-group"> {{ bookmark_form.tags.label }} {{ bookmark_form.tags(class_="form-control") }} </div>
<div class="form-group"> {{ bookmark_form.description.label }} {{ bookmark_form.description(class_="form-control") }} </div>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
{% endblock %}