2018-03-22 22:30:13 -05:00
|
|
|
#!/usr/bin/env python
|
2018-03-30 07:25:28 -05:00
|
|
|
# pylint: disable=wrong-import-order, ungrouped-imports
|
2018-03-27 21:16:54 -05:00
|
|
|
"""Server module."""
|
2018-03-26 07:06:12 -05:00
|
|
|
import os
|
2018-04-19 07:33:43 -05:00
|
|
|
from collections import Counter
|
2018-04-19 19:17:16 -05:00
|
|
|
from urllib.parse import urlparse
|
2018-03-26 07:06:12 -05:00
|
|
|
|
2017-03-28 13:28:23 -05:00
|
|
|
from buku import BukuDb
|
2018-03-22 22:30:13 -05:00
|
|
|
from flask.cli import FlaskGroup
|
2017-03-28 13:28:23 -05:00
|
|
|
from flask_api import status
|
2018-03-23 07:05:31 -05:00
|
|
|
from flask_bootstrap import Bootstrap
|
2018-03-26 07:06:12 -05:00
|
|
|
from flask_paginate import Pagination, get_page_parameter, get_per_page_parameter
|
2018-03-30 07:25:28 -05:00
|
|
|
from markupsafe import Markup
|
2018-04-19 19:17:16 -05:00
|
|
|
import arrow
|
2018-03-22 22:30:13 -05:00
|
|
|
import click
|
|
|
|
import flask
|
2018-03-30 07:25:28 -05:00
|
|
|
from flask import (
|
2018-03-31 23:41:23 -05:00
|
|
|
abort,
|
2018-03-30 07:25:28 -05:00
|
|
|
current_app,
|
|
|
|
flash,
|
|
|
|
Flask,
|
|
|
|
jsonify,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
|
|
|
request,
|
|
|
|
url_for,
|
|
|
|
)
|
2017-03-28 13:28:23 -05:00
|
|
|
|
2018-03-23 19:21:45 -05:00
|
|
|
try:
|
2018-03-27 21:16:54 -05:00
|
|
|
from . import response, forms
|
2018-03-23 19:21:45 -05:00
|
|
|
except ImportError:
|
2018-03-27 21:16:54 -05:00
|
|
|
from bukuserver import response, forms
|
2017-03-28 13:28:23 -05:00
|
|
|
|
2018-03-26 07:06:12 -05:00
|
|
|
|
|
|
|
DEFAULT_PER_PAGE = 10
|
2018-04-19 19:17:16 -05:00
|
|
|
STATISTIC_DATA = None
|
|
|
|
|
2018-03-26 07:06:12 -05:00
|
|
|
|
2017-03-28 13:28:23 -05:00
|
|
|
def get_tags():
|
2018-03-23 07:05:31 -05:00
|
|
|
"""get tags."""
|
2018-03-23 07:31:59 -05:00
|
|
|
tags = getattr(flask.g, 'bukudb', BukuDb()).get_tag_all()
|
2017-03-28 13:28:23 -05:00
|
|
|
result = {
|
|
|
|
'tags': tags[0]
|
|
|
|
}
|
2018-03-23 07:05:31 -05:00
|
|
|
if request.path.startswith('/api/'):
|
|
|
|
res = jsonify(result)
|
|
|
|
else:
|
2018-03-23 19:07:45 -05:00
|
|
|
res = render_template('bukuserver/tags.html', result=result)
|
2018-03-23 07:05:31 -05:00
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def update_tag(tag):
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2018-03-31 23:41:23 -05:00
|
|
|
if request.method in ('PUT', 'POST'):
|
|
|
|
new_tags = request.form.getlist('tags')
|
|
|
|
result_flag = getattr(flask.g, 'bukudb', BukuDb()).replace_tag(tag, new_tags)
|
|
|
|
op_text = 'replace tag [{}] with [{}]'.format(tag, ', '.join(new_tags))
|
|
|
|
if request.method == 'PUT' and result_flag and request.path.startswith('/api/'):
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
2018-03-31 23:41:23 -05:00
|
|
|
elif request.method == 'PUT' and request.path.startswith('/api/'):
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
2018-03-31 23:41:23 -05:00
|
|
|
elif request.method == 'POST' and result_flag:
|
|
|
|
flash(Markup('Success {}'.format(op_text)), 'success')
|
|
|
|
res = redirect(url_for('get_tags-html'))
|
|
|
|
elif request.method == 'POST':
|
|
|
|
flash(Markup('Failed {}'.format(op_text)), 'danger')
|
|
|
|
res = redirect(url_for('get_tags-html'))
|
|
|
|
else:
|
|
|
|
abort(400, description="Unknown Condition")
|
2018-03-26 07:30:40 -05:00
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
2018-03-26 07:06:12 -05:00
|
|
|
def chunks(l, n):
|
|
|
|
n = max(1, n)
|
|
|
|
return (l[i:i+n] for i in range(0, len(l), n))
|
|
|
|
|
|
|
|
|
2017-03-28 13:28:23 -05:00
|
|
|
def bookmarks():
|
2018-03-23 07:05:31 -05:00
|
|
|
"""Bookmarks."""
|
|
|
|
res = None
|
2018-03-23 07:42:28 -05:00
|
|
|
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
2018-03-26 07:06:12 -05:00
|
|
|
page = request.args.get(get_page_parameter(), type=int, default=1)
|
|
|
|
per_page = request.args.get(
|
|
|
|
get_per_page_parameter(),
|
|
|
|
type=int,
|
|
|
|
default=int(
|
|
|
|
current_app.config.get('BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE))
|
|
|
|
)
|
2018-03-30 07:25:28 -05:00
|
|
|
create_bookmarks_form = forms.CreateBookmarksForm()
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'GET':
|
2018-03-23 07:42:28 -05:00
|
|
|
all_bookmarks = bukudb.get_rec_all()
|
2017-03-28 13:28:23 -05:00
|
|
|
result = {
|
|
|
|
'bookmarks': []
|
|
|
|
}
|
|
|
|
for bookmark in all_bookmarks:
|
|
|
|
result_bookmark = {
|
|
|
|
'url': bookmark[1],
|
|
|
|
'title': bookmark[2],
|
2017-05-18 20:40:43 -05:00
|
|
|
'tags': list([_f for _f in bookmark[3].split(',') if _f]),
|
2017-03-28 13:28:23 -05:00
|
|
|
'description': bookmark[4]
|
|
|
|
}
|
2018-04-03 05:01:03 -05:00
|
|
|
if not request.path.startswith('/api/'):
|
2018-04-03 05:13:22 -05:00
|
|
|
result_bookmark['id'] = bookmark[0]
|
2017-03-28 13:28:23 -05:00
|
|
|
result['bookmarks'].append(result_bookmark)
|
2018-03-23 07:05:31 -05:00
|
|
|
if request.path.startswith('/api/'):
|
|
|
|
res = jsonify(result)
|
|
|
|
else:
|
|
|
|
if request.args.getlist('tag'):
|
|
|
|
tags = request.args.getlist('tag')
|
|
|
|
result['bookmarks'] = [
|
|
|
|
x for x in result['bookmarks']
|
|
|
|
if set(tags).issubset(set(x['tags']))
|
|
|
|
]
|
2018-03-26 07:30:40 -05:00
|
|
|
current_app.logger.debug('total bookmarks:{}'.format(len(result['bookmarks'])))
|
|
|
|
current_app.logger.debug('per page:{}'.format(per_page))
|
2018-03-26 07:06:12 -05:00
|
|
|
pagination_total = len(result['bookmarks'])
|
|
|
|
bms = list(chunks(result['bookmarks'], per_page))
|
2018-03-28 15:40:00 -05:00
|
|
|
try:
|
|
|
|
result['bookmarks'] = bms[page-1]
|
|
|
|
except IndexError as err:
|
|
|
|
current_app.logger.debug('{}:{}, result bookmarks:{}, page:{}'.format(
|
|
|
|
type(err), err, len(result['bookmarks']), page
|
|
|
|
))
|
2018-03-26 07:06:12 -05:00
|
|
|
pagination = Pagination(
|
|
|
|
page=page, total=pagination_total, per_page=per_page,
|
|
|
|
search=False, record_name='bookmarks', bs_version=3
|
|
|
|
)
|
2018-03-23 07:05:31 -05:00
|
|
|
res = render_template(
|
2018-03-26 07:06:12 -05:00
|
|
|
'bukuserver/bookmarks.html',
|
|
|
|
result=result,
|
2018-03-27 21:16:54 -05:00
|
|
|
pagination=pagination,
|
|
|
|
search_bookmarks_form=forms.SearchBookmarksForm(),
|
2018-03-30 07:25:28 -05:00
|
|
|
create_bookmarks_form=create_bookmarks_form,
|
2018-03-26 07:06:12 -05:00
|
|
|
)
|
2017-03-28 13:28:23 -05:00
|
|
|
elif request.method == 'POST':
|
2018-03-30 07:25:28 -05:00
|
|
|
url_data = create_bookmarks_form.url.data
|
2018-03-23 07:42:28 -05:00
|
|
|
result_flag = bukudb.add_rec(
|
2018-03-30 07:25:28 -05:00
|
|
|
url_data,
|
|
|
|
create_bookmarks_form.title.data,
|
|
|
|
create_bookmarks_form.tags.data,
|
|
|
|
create_bookmarks_form.description.data
|
|
|
|
)
|
|
|
|
if request.path.startswith('/api/'):
|
|
|
|
res = [
|
|
|
|
jsonify(response.response_template['success']), status.HTTP_200_OK,
|
|
|
|
{'ContentType': 'application/json'}
|
|
|
|
] if result_flag != -1 else [
|
|
|
|
jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST,
|
|
|
|
{'ContentType': 'application/json'}
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
bm_text = '[<a href="{0}">{0}</a>]'.format(url_data)
|
|
|
|
if result_flag != -1:
|
|
|
|
flash(Markup('Success creating bookmark {}.'.format(bm_text)), 'success')
|
|
|
|
else:
|
|
|
|
flash(Markup('Failed creating bookmark {}.'.format(bm_text)), 'danger')
|
|
|
|
return redirect(url_for('bookmarks-html'))
|
2017-03-28 13:28:23 -05:00
|
|
|
elif request.method == 'DELETE':
|
2018-03-23 07:42:28 -05:00
|
|
|
result_flag = bukudb.cleardb()
|
2018-03-23 07:05:31 -05:00
|
|
|
res = [
|
|
|
|
jsonify(response.response_template['success']), status.HTTP_200_OK,
|
|
|
|
{'ContentType': 'application/json'}
|
|
|
|
] if result_flag else [
|
|
|
|
jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST,
|
|
|
|
{'ContentType': 'application/json'}
|
|
|
|
]
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def refresh_bookmarks():
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'POST':
|
|
|
|
print(request.form['index'])
|
|
|
|
print(request.form['threads'])
|
2018-03-23 07:31:59 -05:00
|
|
|
result_flag = getattr(flask.g, 'bukudb', BukuDb()).refreshdb(request.form['index'], request.form['threads'])
|
2017-03-28 13:28:23 -05:00
|
|
|
if result_flag:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def bookmark_api(id):
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2017-03-28 13:28:23 -05:00
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
2018-03-23 07:42:28 -05:00
|
|
|
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
2018-04-03 05:01:03 -05:00
|
|
|
bookmark_form = forms.CreateBookmarksForm()
|
|
|
|
is_html_post_request = request.method == 'POST' and not request.path.startswith('/api/')
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'GET':
|
2018-03-23 07:42:28 -05:00
|
|
|
bookmark = bukudb.get_rec_by_id(id)
|
2017-03-28 13:28:23 -05:00
|
|
|
if bookmark is not None:
|
|
|
|
result = {
|
|
|
|
'url': bookmark[1],
|
|
|
|
'title': bookmark[2],
|
2017-05-18 20:40:43 -05:00
|
|
|
'tags': list([_f for _f in bookmark[3].split(',') if _f]),
|
2017-03-28 13:28:23 -05:00
|
|
|
'description': bookmark[4]
|
|
|
|
}
|
2018-04-03 05:01:03 -05:00
|
|
|
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]
|
|
|
|
)
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-04-03 05:01:03 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
2017-03-28 13:28:23 -05:00
|
|
|
{'ContentType': 'application/json'}
|
2018-04-03 05:01:03 -05:00
|
|
|
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'))
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-04-03 05:01:03 -05:00
|
|
|
abort(400, description="Unknown Condition")
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-03-23 07:42:28 -05:00
|
|
|
result_flag = bukudb.delete_rec(id)
|
2017-03-28 13:28:23 -05:00
|
|
|
if result_flag:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def refresh_bookmark(id):
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'POST':
|
2018-03-23 07:31:59 -05:00
|
|
|
result_flag = getattr(flask.g, 'bukudb', BukuDb()).refreshdb(id, request.form['threads'])
|
2017-03-28 13:28:23 -05:00
|
|
|
if result_flag:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_tiny_url(id):
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'GET':
|
2018-03-23 07:31:59 -05:00
|
|
|
shortened_url = getattr(flask.g, 'bukudb', BukuDb()).tnyfy_url(id)
|
2017-03-28 13:28:23 -05:00
|
|
|
if shortened_url is not None:
|
|
|
|
result = {
|
|
|
|
'url': shortened_url
|
|
|
|
}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(result)
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_long_url(id):
|
|
|
|
try:
|
|
|
|
id = int(id)
|
|
|
|
except ValueError:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
|
|
|
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'GET':
|
2018-03-23 07:31:59 -05:00
|
|
|
bookmark = getattr(flask.g, 'bukudb', BukuDb()).get_rec_by_id(id)
|
2017-03-28 13:28:23 -05:00
|
|
|
if bookmark is not None:
|
|
|
|
result = {
|
|
|
|
'url': bookmark[1],
|
|
|
|
}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(result)
|
2017-03-28 13:28:23 -05:00
|
|
|
else:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def bookmark_range_operations(starting_id, ending_id):
|
|
|
|
|
|
|
|
try:
|
|
|
|
starting_id = int(starting_id)
|
|
|
|
ending_id = int(ending_id)
|
|
|
|
except ValueError:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2018-03-23 07:42:28 -05:00
|
|
|
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
|
|
|
max_id = bukudb.get_max_id()
|
2017-03-28 13:28:23 -05:00
|
|
|
if starting_id > max_id or ending_id > max_id:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
result = {
|
|
|
|
'bookmarks': {}
|
|
|
|
}
|
|
|
|
for i in range(starting_id, ending_id + 1, 1):
|
2018-03-23 07:42:28 -05:00
|
|
|
bookmark = bukudb.get_rec_by_id(i)
|
2018-03-22 22:30:13 -05:00
|
|
|
result['bookmarks'][i] = {
|
2017-03-28 13:28:23 -05:00
|
|
|
'url': bookmark[1],
|
|
|
|
'title': bookmark[2],
|
2017-05-18 20:40:43 -05:00
|
|
|
'tags': list([_f for _f in bookmark[3].split(',') if _f]),
|
2017-03-28 13:28:23 -05:00
|
|
|
'description': bookmark[4]
|
|
|
|
}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(result)
|
2017-03-28 13:28:23 -05:00
|
|
|
elif request.method == 'DELETE':
|
|
|
|
for i in range(starting_id, ending_id + 1, 1):
|
2018-03-23 07:42:28 -05:00
|
|
|
result_flag = bukudb.delete_rec(i)
|
2017-03-28 13:28:23 -05:00
|
|
|
if result_flag is False:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
2017-03-28 13:28:23 -05:00
|
|
|
elif request.method == 'PUT':
|
|
|
|
for i in range(starting_id, ending_id + 1, 1):
|
|
|
|
updated_bookmark = request.form[str(i)]
|
2018-03-23 07:42:28 -05:00
|
|
|
result_flag = bukudb.update_rec(
|
2018-03-22 22:30:13 -05:00
|
|
|
i, updated_bookmark['url'], updated_bookmark['title'], updated_bookmark['tags'], updated_bookmark['description'])
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
if result_flag is False:
|
|
|
|
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
|
|
|
|
{'ContentType': 'application/json'}
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, \
|
2017-03-28 13:28:23 -05:00
|
|
|
{'ContentType': 'application/json'}
|
2018-03-26 07:30:40 -05:00
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def search_bookmarks():
|
2018-03-26 20:08:58 -05:00
|
|
|
arg_obj = request.form if request.method == 'DELETE' else request.args
|
2018-03-27 21:16:54 -05:00
|
|
|
search_bookmarks_form = forms.SearchBookmarksForm(request.args)
|
2018-03-26 20:08:58 -05:00
|
|
|
is_api_request_path = request.path.startswith('/api/')
|
|
|
|
if is_api_request_path:
|
2018-03-27 21:16:54 -05:00
|
|
|
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
|
2018-03-26 20:08:58 -05:00
|
|
|
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
|
|
|
|
all_keywords = \
|
|
|
|
all_keywords if type(all_keywords) == bool else all_keywords.lower() == 'true'
|
|
|
|
deep = \
|
|
|
|
deep if type(deep) == bool else deep.lower() == 'true'
|
|
|
|
regex = \
|
|
|
|
regex if type(regex) == bool else regex.lower() == 'true'
|
2018-03-27 21:16:54 -05:00
|
|
|
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
|
2017-03-28 13:28:23 -05:00
|
|
|
|
2018-03-26 20:12:22 -05:00
|
|
|
result = {'bookmarks': []}
|
2018-03-23 07:42:28 -05:00
|
|
|
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
|
|
|
found_bookmarks = bukudb.searchdb(keywords, all_keywords, deep, regex)
|
2018-03-26 20:08:58 -05:00
|
|
|
found_bookmarks = [] if found_bookmarks is None else found_bookmarks
|
|
|
|
page = request.args.get(get_page_parameter(), type=int, default=1)
|
|
|
|
per_page = request.args.get(
|
|
|
|
get_per_page_parameter(),
|
|
|
|
type=int,
|
|
|
|
default=int(
|
|
|
|
current_app.config.get('BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE))
|
|
|
|
)
|
2017-03-28 13:28:23 -05:00
|
|
|
|
2018-03-26 07:30:40 -05:00
|
|
|
res = None
|
2017-03-28 13:28:23 -05:00
|
|
|
if request.method == 'GET':
|
|
|
|
if bookmarks is not None:
|
|
|
|
for bookmark in found_bookmarks:
|
|
|
|
result_bookmark = {
|
|
|
|
'id': bookmark[0],
|
|
|
|
'url': bookmark[1],
|
|
|
|
'title': bookmark[2],
|
2017-05-18 20:40:43 -05:00
|
|
|
'tags': list([_f for _f in bookmark[3].split(',') if _f]),
|
2017-03-28 13:28:23 -05:00
|
|
|
'description': bookmark[4]
|
|
|
|
}
|
2018-03-26 20:12:22 -05:00
|
|
|
result['bookmarks'].append(result_bookmark)
|
|
|
|
current_app.logger.debug('total bookmarks:{}'.format(len(result['bookmarks'])))
|
2018-03-26 20:08:58 -05:00
|
|
|
if is_api_request_path:
|
2018-03-26 20:12:22 -05:00
|
|
|
res = jsonify(result)
|
2018-03-26 20:08:58 -05:00
|
|
|
else:
|
2018-03-26 20:12:22 -05:00
|
|
|
pagination_total = len(result['bookmarks'])
|
|
|
|
bms = list(chunks(result['bookmarks'], per_page))
|
2018-03-28 15:49:02 -05:00
|
|
|
try:
|
|
|
|
result['bookmarks'] = bms[page-1]
|
|
|
|
except IndexError as err:
|
|
|
|
current_app.logger.debug('{}:{}, result bookmarks:{}, page:{}'.format(
|
|
|
|
type(err), err, len(result['bookmarks']), page
|
|
|
|
))
|
2018-03-26 20:08:58 -05:00
|
|
|
pagination = Pagination(
|
|
|
|
page=page, total=pagination_total, per_page=per_page,
|
|
|
|
search=False, record_name='bookmarks', bs_version=3
|
|
|
|
)
|
2018-03-27 21:16:54 -05:00
|
|
|
res = render_template(
|
|
|
|
'bukuserver/bookmarks.html',
|
|
|
|
result=result, pagination=pagination,
|
2018-03-31 03:43:38 -05:00
|
|
|
search_bookmarks_form=search_bookmarks_form,
|
|
|
|
create_bookmarks_form=forms.CreateBookmarksForm(),
|
|
|
|
)
|
2017-03-28 13:28:23 -05:00
|
|
|
elif request.method == 'DELETE':
|
|
|
|
if found_bookmarks is not None:
|
|
|
|
for bookmark in found_bookmarks:
|
2018-03-23 07:42:28 -05:00
|
|
|
result_flag = bukudb.delete_rec(bookmark[0])
|
2017-03-28 13:28:23 -05:00
|
|
|
if result_flag is False:
|
2018-03-26 07:30:40 -05:00
|
|
|
res = jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, {'ContentType': 'application/json'}
|
|
|
|
if res is None:
|
|
|
|
res = jsonify(response.response_template['success']), status.HTTP_200_OK, {'ContentType': 'application/json'}
|
|
|
|
return res
|
2017-03-28 13:28:23 -05:00
|
|
|
|
|
|
|
|
2018-04-19 07:33:43 -05:00
|
|
|
def view_statistic():
|
|
|
|
bukudb = getattr(flask.g, 'bukudb', BukuDb())
|
2018-04-19 19:17:16 -05:00
|
|
|
global STATISTIC_DATA
|
|
|
|
statistic_data = STATISTIC_DATA
|
|
|
|
if not statistic_data or request.method == 'POST':
|
|
|
|
all_bookmarks = bukudb.get_rec_all()
|
|
|
|
netloc = [urlparse(x[1]).netloc for x in all_bookmarks]
|
|
|
|
statistic_datetime = arrow.now()
|
|
|
|
STATISTIC_DATA = {
|
|
|
|
'datetime': statistic_datetime,
|
|
|
|
'netloc': netloc,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
netloc = statistic_data['netloc']
|
|
|
|
statistic_datetime = statistic_data['datetime']
|
2018-04-19 07:33:43 -05:00
|
|
|
netloc_counter = Counter(netloc)
|
2018-04-19 19:17:16 -05:00
|
|
|
unique_netloc_len = len(set(netloc))
|
2018-04-19 07:33:43 -05:00
|
|
|
colors = [
|
|
|
|
"#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA",
|
|
|
|
"#ABCDEF", "#DDDDDD", "#ABCABC", "#4169E1",
|
|
|
|
"#C71585", "#FF4500", "#FEDCBA", "#46BFBD"]
|
2018-04-19 19:17:16 -05:00
|
|
|
if unique_netloc_len > len(colors):
|
|
|
|
max_item = len(colors)
|
|
|
|
else:
|
|
|
|
colors = colors[:unique_netloc_len]
|
|
|
|
max_item = unique_netloc_len
|
|
|
|
most_common_netlocs = netloc_counter.most_common(max_item)
|
2018-04-19 07:33:43 -05:00
|
|
|
most_common_netlocs = [[val[0], val[1], colors[idx]] for idx, val in enumerate(most_common_netlocs)]
|
|
|
|
return render_template(
|
|
|
|
'bukuserver/statistic.html',
|
2018-04-19 19:17:16 -05:00
|
|
|
most_common_netlocs=most_common_netlocs,
|
|
|
|
datetime=statistic_datetime,
|
|
|
|
datetime_text=statistic_datetime.humanize(arrow.now(), granularity='second'),
|
2018-04-19 07:33:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-03-22 22:30:13 -05:00
|
|
|
def create_app(config_filename=None):
|
|
|
|
"""create app."""
|
|
|
|
app = Flask(__name__)
|
2018-03-26 07:06:12 -05:00
|
|
|
app.config['BUKUSERVER_PER_PAGE'] = os.getenv(
|
|
|
|
'BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE)
|
2018-03-27 21:16:54 -05:00
|
|
|
app.config['SECRET_KEY'] = os.getenv('BUKUSERVER_SERVER_SECRET_KEY') or os.urandom(24)
|
2018-03-22 22:30:13 -05:00
|
|
|
bukudb = BukuDb()
|
|
|
|
app.app_context().push()
|
|
|
|
setattr(flask.g, 'bukudb', bukudb)
|
|
|
|
|
|
|
|
@app.shell_context_processor
|
|
|
|
def shell_context():
|
|
|
|
"""Shell context definition."""
|
|
|
|
return {'app': app, 'bukudb': bukudb}
|
|
|
|
|
2018-03-23 07:05:31 -05:00
|
|
|
Bootstrap(app)
|
2018-03-22 22:30:13 -05:00
|
|
|
# routing
|
|
|
|
app.add_url_rule('/api/tags', 'get_tags', get_tags, methods=['GET'])
|
2018-03-23 07:05:31 -05:00
|
|
|
app.add_url_rule('/tags', 'get_tags-html', get_tags, methods=['GET'])
|
2018-03-22 22:30:13 -05:00
|
|
|
app.add_url_rule('/api/tags/<tag>', 'update_tag', update_tag, methods=['PUT'])
|
2018-03-31 23:41:23 -05:00
|
|
|
app.add_url_rule('/tags/<tag>', 'update_tag-html', update_tag, methods=['POST'])
|
2018-03-22 22:30:13 -05:00
|
|
|
app.add_url_rule('/api/bookmarks', 'bookmarks', bookmarks, methods=['GET', 'POST', 'DELETE'])
|
2018-03-23 07:05:31 -05:00
|
|
|
app.add_url_rule('/bookmarks', 'bookmarks-html', bookmarks, methods=['GET', 'POST', 'DELETE'])
|
2018-03-22 22:30:13 -05:00
|
|
|
app.add_url_rule('/api/bookmarks/refresh', 'refresh_bookmarks', refresh_bookmarks, methods=['POST'])
|
2018-03-23 07:05:31 -05:00
|
|
|
app.add_url_rule('/api/bookmarks/<id>', 'bookmark_api', bookmark_api, methods=['GET', 'PUT', 'DELETE'])
|
2018-04-03 05:01:03 -05:00
|
|
|
app.add_url_rule('/bookmarks/<id>', 'bookmark_api-html', bookmark_api, methods=['GET', 'POST'])
|
2018-03-22 22:30:13 -05:00
|
|
|
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'])
|
|
|
|
app.add_url_rule(
|
|
|
|
'/api/bookmarks/<starting_id>/<ending_id>',
|
|
|
|
'bookmark_range_operations', bookmark_range_operations, methods=['GET', 'PUT', 'DELETE'])
|
|
|
|
app.add_url_rule('/api/bookmarks/search', 'search_bookmarks', search_bookmarks, methods=['GET', 'DELETE'])
|
2018-03-26 20:08:58 -05:00
|
|
|
app.add_url_rule('/bookmarks/search', 'search_bookmarks-html', search_bookmarks, methods=['GET'])
|
2018-03-27 21:16:54 -05:00
|
|
|
app.add_url_rule('/', 'index', lambda: render_template(
|
|
|
|
'bukuserver/index.html', search_bookmarks_form=forms.SearchBookmarksForm()))
|
2018-04-19 19:17:16 -05:00
|
|
|
app.add_url_rule('/statistic', 'statistic', view_statistic, methods=['GET', 'POST'])
|
2018-03-22 22:30:13 -05:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
@click.group(cls=FlaskGroup, create_app=create_app)
|
|
|
|
def cli():
|
|
|
|
"""This is a management script for the wiki application."""
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cli()
|