Bookmarks.

def bookmarks():
    """Bookmarks."""
    res = None
    bukudb = getattr(flask.g, 'bukudb', get_bukudb())
    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', views.DEFAULT_PER_PAGE))
    )
    url_render_mode = current_app.config['BUKUSERVER_URL_RENDER_MODE']
    create_bookmarks_form = forms.BookmarkForm()
    if request.method == 'GET':
        all_bookmarks = bukudb.get_rec_all()
        result = {
            'bookmarks': []
        }
        for bookmark in all_bookmarks:
            result_bookmark = {
                'url': bookmark[1],
                'title': bookmark[2],
                '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)
        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']))
                ]
            current_app.logger.debug('total bookmarks:{}'.format(len(result['bookmarks'])))
            current_app.logger.debug('per page:{}'.format(per_page))
            pagination_total = len(result['bookmarks'])
            bms = list(views.chunks(result['bookmarks'], per_page))
            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
                ))
            pagination = Pagination(
                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,
                search_bookmarks_form=forms.SearchBookmarksForm(),
                create_bookmarks_form=create_bookmarks_form,
                url_render_mode=url_render_mode,
            )
    elif request.method == 'POST':
        url_data = create_bookmarks_form.url.data
        result_flag = bukudb.add_rec(
            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'))
    elif request.method == 'DELETE':
        result_flag = bukudb.cleardb()
        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