chg: dev: use flask-wtf
This commit is contained in:
parent
a65766139c
commit
04a1ea8d6c
9
bukuserver/forms.py
Normal file
9
bukuserver/forms.py
Normal 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')
|
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
"""Server module."""
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from buku import BukuDb
|
from buku import BukuDb
|
||||||
@ -11,9 +12,9 @@ import click
|
|||||||
import flask
|
import flask
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from . import response
|
from . import response, forms
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from bukuserver import response
|
from bukuserver import response, forms
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PER_PAGE = 10
|
DEFAULT_PER_PAGE = 10
|
||||||
@ -92,7 +93,8 @@ def bookmarks():
|
|||||||
res = render_template(
|
res = render_template(
|
||||||
'bukuserver/bookmarks.html',
|
'bukuserver/bookmarks.html',
|
||||||
result=result,
|
result=result,
|
||||||
pagination=pagination
|
pagination=pagination,
|
||||||
|
search_bookmarks_form=forms.SearchBookmarksForm(),
|
||||||
)
|
)
|
||||||
elif request.method == 'POST':
|
elif request.method == 'POST':
|
||||||
result_flag = bukudb.add_rec(
|
result_flag = bukudb.add_rec(
|
||||||
@ -273,13 +275,14 @@ def bookmark_range_operations(starting_id, ending_id):
|
|||||||
|
|
||||||
def search_bookmarks():
|
def search_bookmarks():
|
||||||
arg_obj = request.form if request.method == 'DELETE' else request.args
|
arg_obj = request.form if request.method == 'DELETE' else request.args
|
||||||
keywords = arg_obj.getlist('keywords')
|
search_bookmarks_form = forms.SearchBookmarksForm(request.args)
|
||||||
all_keywords = arg_obj.get('all_keywords')
|
|
||||||
deep = arg_obj.get('deep')
|
|
||||||
regex = arg_obj.get('regex')
|
|
||||||
is_api_request_path = request.path.startswith('/api/')
|
is_api_request_path = request.path.startswith('/api/')
|
||||||
# api request is more strict
|
|
||||||
if is_api_request_path:
|
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
|
all_keywords = False if all_keywords is None else all_keywords
|
||||||
deep = False if deep is None else deep
|
deep = False if deep is None else deep
|
||||||
regex = False if regex is None else regex
|
regex = False if regex is None else regex
|
||||||
@ -289,6 +292,11 @@ def search_bookmarks():
|
|||||||
deep if type(deep) == bool else deep.lower() == 'true'
|
deep if type(deep) == bool else deep.lower() == 'true'
|
||||||
regex = \
|
regex = \
|
||||||
regex if type(regex) == bool else regex.lower() == 'true'
|
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': []}
|
result = {'bookmarks': []}
|
||||||
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
||||||
@ -325,7 +333,10 @@ def search_bookmarks():
|
|||||||
page=page, total=pagination_total, per_page=per_page,
|
page=page, total=pagination_total, per_page=per_page,
|
||||||
search=False, record_name='bookmarks', bs_version=3
|
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':
|
elif request.method == 'DELETE':
|
||||||
if found_bookmarks is not None:
|
if found_bookmarks is not None:
|
||||||
for bookmark in found_bookmarks:
|
for bookmark in found_bookmarks:
|
||||||
@ -342,6 +353,7 @@ def create_app(config_filename=None):
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['BUKUSERVER_PER_PAGE'] = os.getenv(
|
app.config['BUKUSERVER_PER_PAGE'] = os.getenv(
|
||||||
'BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE)
|
'BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE)
|
||||||
|
app.config['SECRET_KEY'] = os.getenv('BUKUSERVER_SERVER_SECRET_KEY') or os.urandom(24)
|
||||||
bukudb = BukuDb()
|
bukudb = BukuDb()
|
||||||
app.app_context().push()
|
app.app_context().push()
|
||||||
setattr(flask.g, 'bukudb', bukudb)
|
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'])
|
'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('/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('/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
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
</ul>
|
</ul>
|
||||||
<form class="navbar-form navbar-right" action="{{url_for('search_bookmarks-html')}}" method="GET">
|
<form class="navbar-form navbar-right" action="{{url_for('search_bookmarks-html')}}" method="GET">
|
||||||
<div class="form-group">
|
<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>
|
</div>
|
||||||
<button type="submit" class="btn btn-default">Search</button>
|
<button type="submit" class="btn btn-default">Search</button>
|
||||||
</form>
|
</form>
|
||||||
@ -72,16 +72,18 @@
|
|||||||
</table>
|
</table>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro show_bookmarks_search_form() %}
|
{% macro show_bookmarks_search_form(checkbox_class=None) %}
|
||||||
<form action="{{url_for('search_bookmarks-html')}}" method="GET">
|
<form class="form-horizontal" action="{{url_for('search_bookmarks-html')}}" method="GET">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="">Bookmark keyword</label>
|
{{search_bookmarks_form.keywords.label}}
|
||||||
<input type="text" class="form-control" id="inputKeywords" placeholder="Keywords" name="keywords">
|
{% for entry in search_bookmarks_form.keywords %}
|
||||||
|
{{ entry() }}
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<div class="text-left">
|
<div class="{{checkbox_class}}">
|
||||||
<div class="checkbox"> <label> <input type="checkbox" name="all_keywords"> Match all keyword </label> </div>
|
<div class="form-group"> {{search_bookmarks_form.all_keywords()}} {{search_bookmarks_form.all_keywords.label}} </div>
|
||||||
<div class="checkbox"> <label> <input type="checkbox" name="deep"> Deep search</label> </div>
|
<div class="form-group"> {{search_bookmarks_form.deep()}} {{search_bookmarks_form.deep.label}} </div>
|
||||||
<div class="checkbox"> <label> <input type="checkbox" name="regex"> Regex search </label> </div>
|
<div class="form-group"> {{search_bookmarks_form.regex()}} {{search_bookmarks_form.regex.label}} </div>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" class="btn btn-default">Search</button>
|
<button type="submit" class="btn btn-default">Search</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
<a class="btn btn-lg btn-success" href="{{url_for('get_tags-html')}}" role="button">Tags</a>
|
<a class="btn btn-lg btn-success" href="{{url_for('get_tags-html')}}" role="button">Tags</a>
|
||||||
</p>
|
</p>
|
||||||
<div class=" col-md-4 col-md-offset-4">
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
2
setup.py
2
setup.py
@ -30,10 +30,10 @@ server_require = [
|
|||||||
'Flask-API>=0.6.9',
|
'Flask-API>=0.6.9',
|
||||||
'Flask-Bootstrap>=3.3.7.1',
|
'Flask-Bootstrap>=3.3.7.1',
|
||||||
'flask-paginate>=0.5.1',
|
'flask-paginate>=0.5.1',
|
||||||
|
'Flask-WTF>=0.14.2',
|
||||||
'Flask>=0.12',
|
'Flask>=0.12',
|
||||||
'requests>=2.18.4',
|
'requests>=2.18.4',
|
||||||
'Werkzeug>=0.11.15',
|
'Werkzeug>=0.11.15',
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user