new: dev: editing bookmark
This commit is contained in:
parent
9d4af71ea0
commit
732c536236
@ -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()])
|
||||
|
@ -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:
|
||||
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
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:
|
||||
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
||||
{'ContentType': 'application/json'}
|
||||
elif request.method == 'PUT':
|
||||
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:
|
||||
if result_flag and not is_html_post_request:
|
||||
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
||||
else:
|
||||
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:
|
||||
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/<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>/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'])
|
||||
|
@ -58,7 +58,12 @@
|
||||
<tbody>
|
||||
{% for item in bookmarks %}
|
||||
<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>{{item.description}}</p>
|
||||
<p>
|
||||
|
14
bukuserver/templates/bukuserver/bookmark_edit.html
Normal file
14
bukuserver/templates/bukuserver/bookmark_edit.html
Normal 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 %}
|
Loading…
Reference in New Issue
Block a user