chg: dev: use flask-wtf

This commit is contained in:
rachmadaniHaryono 2018-03-28 10:16:54 +08:00
parent a65766139c
commit 04a1ea8d6c
5 changed files with 45 additions and 21 deletions

9
bukuserver/forms.py Normal file
View File

@ -0,0 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import StringField, FieldList, validators, BooleanField
class SearchBookmarksForm(FlaskForm):
keywords = FieldList(StringField('Keywords'), min_entries=1)
all_keywords = BooleanField('Match all keywords')
deep = BooleanField('Deep search')
regex = BooleanField('Regex')

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python
"""Server module."""
import os
from buku import BukuDb
@ -11,9 +12,9 @@ import click
import flask
try:
from . import response
from . import response, forms
except ImportError:
from bukuserver import response
from bukuserver import response, forms
DEFAULT_PER_PAGE = 10
@ -92,7 +93,8 @@ def bookmarks():
res = render_template(
'bukuserver/bookmarks.html',
result=result,
pagination=pagination
pagination=pagination,
search_bookmarks_form=forms.SearchBookmarksForm(),
)
elif request.method == 'POST':
result_flag = bukudb.add_rec(
@ -273,13 +275,14 @@ def bookmark_range_operations(starting_id, ending_id):
def search_bookmarks():
arg_obj = request.form if request.method == 'DELETE' else request.args
keywords = arg_obj.getlist('keywords')
all_keywords = arg_obj.get('all_keywords')
deep = arg_obj.get('deep')
regex = arg_obj.get('regex')
search_bookmarks_form = forms.SearchBookmarksForm(request.args)
is_api_request_path = request.path.startswith('/api/')
# api request is more strict
if is_api_request_path:
keywords = arg_obj.getlist('keywords')
all_keywords = arg_obj.get('all_keywords')
deep = arg_obj.get('deep')
regex = arg_obj.get('regex')
# api request is more strict
all_keywords = False if all_keywords is None else all_keywords
deep = False if deep is None else deep
regex = False if regex is None else regex
@ -289,6 +292,11 @@ def search_bookmarks():
deep if type(deep) == bool else deep.lower() == 'true'
regex = \
regex if type(regex) == bool else regex.lower() == 'true'
else:
keywords = search_bookmarks_form.keywords.data
all_keywords = search_bookmarks_form.all_keywords.data
deep = search_bookmarks_form.deep.data
regex = search_bookmarks_form.regex.data
result = {'bookmarks': []}
bukudb = getattr(flask.g, 'bukudb', BukuDb())
@ -325,7 +333,10 @@ def search_bookmarks():
page=page, total=pagination_total, per_page=per_page,
search=False, record_name='bookmarks', bs_version=3
)
res = render_template('bukuserver/bookmarks.html', result=result, pagination=pagination)
res = render_template(
'bukuserver/bookmarks.html',
result=result, pagination=pagination,
search_bookmarks_form=search_bookmarks_form)
elif request.method == 'DELETE':
if found_bookmarks is not None:
for bookmark in found_bookmarks:
@ -342,6 +353,7 @@ def create_app(config_filename=None):
app = Flask(__name__)
app.config['BUKUSERVER_PER_PAGE'] = os.getenv(
'BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE)
app.config['SECRET_KEY'] = os.getenv('BUKUSERVER_SERVER_SECRET_KEY') or os.urandom(24)
bukudb = BukuDb()
app.app_context().push()
setattr(flask.g, 'bukudb', bukudb)
@ -368,7 +380,8 @@ def create_app(config_filename=None):
'bookmark_range_operations', bookmark_range_operations, methods=['GET', 'PUT', 'DELETE'])
app.add_url_rule('/api/bookmarks/search', 'search_bookmarks', search_bookmarks, methods=['GET', 'DELETE'])
app.add_url_rule('/bookmarks/search', 'search_bookmarks-html', search_bookmarks, methods=['GET'])
app.add_url_rule('/', 'index', lambda: render_template('bukuserver/index.html'))
app.add_url_rule('/', 'index', lambda: render_template(
'bukuserver/index.html', search_bookmarks_form=forms.SearchBookmarksForm()))
return app

View File

@ -27,7 +27,7 @@
</ul>
<form class="navbar-form navbar-right" action="{{url_for('search_bookmarks-html')}}" method="GET">
<div class="form-group">
<input type="text" class="form-control" id="inputKeywords" placeholder="Search bookmark" name="keywords">
<input type="text" class="form-control" id="inputKeywords" placeholder="Search bookmark" name="keywords-0">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
@ -72,16 +72,18 @@
</table>
{% endmacro %}
{% macro show_bookmarks_search_form() %}
<form action="{{url_for('search_bookmarks-html')}}" method="GET">
{% macro show_bookmarks_search_form(checkbox_class=None) %}
<form class="form-horizontal" action="{{url_for('search_bookmarks-html')}}" method="GET">
<div class="form-group">
<label for="">Bookmark keyword</label>
<input type="text" class="form-control" id="inputKeywords" placeholder="Keywords" name="keywords">
{{search_bookmarks_form.keywords.label}}
{% for entry in search_bookmarks_form.keywords %}
{{ entry() }}
{% endfor %}
</div>
<div class="text-left">
<div class="checkbox"> <label> <input type="checkbox" name="all_keywords"> Match all keyword </label> </div>
<div class="checkbox"> <label> <input type="checkbox" name="deep"> Deep search</label> </div>
<div class="checkbox"> <label> <input type="checkbox" name="regex"> Regex search </label> </div>
<div class="{{checkbox_class}}">
<div class="form-group"> {{search_bookmarks_form.all_keywords()}} {{search_bookmarks_form.all_keywords.label}} </div>
<div class="form-group"> {{search_bookmarks_form.deep()}} {{search_bookmarks_form.deep.label}} </div>
<div class="form-group"> {{search_bookmarks_form.regex()}} {{search_bookmarks_form.regex.label}} </div>
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>

View File

@ -10,7 +10,7 @@
<a class="btn btn-lg btn-success" href="{{url_for('get_tags-html')}}" role="button">Tags</a>
</p>
<div class=" col-md-4 col-md-offset-4">
{{show_bookmarks_search_form()}}
{{show_bookmarks_search_form(checkbox_class="text-left col-sm-offset-2")}}
</div>
</div>
</div>

View File

@ -30,10 +30,10 @@ server_require = [
'Flask-API>=0.6.9',
'Flask-Bootstrap>=3.3.7.1',
'flask-paginate>=0.5.1',
'Flask-WTF>=0.14.2',
'Flask>=0.12',
'requests>=2.18.4',
'Werkzeug>=0.11.15',
]
setup(