new: dev: create bookmark form

This commit is contained in:
rachmadaniHaryono 2018-03-30 20:25:28 +08:00
parent 29b5c13743
commit f53c2335d8
4 changed files with 86 additions and 24 deletions

View File

@ -1,7 +1,7 @@
"""Forms module."""
# pylint: disable=too-few-public-methods
# pylint: disable=too-few-public-methods, missing-docstring
from flask_wtf import FlaskForm
from wtforms import StringField, FieldList, BooleanField
from wtforms import StringField, FieldList, BooleanField, validators
class SearchBookmarksForm(FlaskForm):
@ -9,3 +9,10 @@ class SearchBookmarksForm(FlaskForm):
all_keywords = BooleanField('Match all keywords')
deep = BooleanField('Deep search')
regex = BooleanField('Regex')
class CreateBookmarksForm(FlaskForm):
url = StringField(validators=[validators.required(), validators.URL(require_tld=False)])
title = StringField()
tags = StringField()
description = StringField()

View File

@ -1,15 +1,26 @@
#!/usr/bin/env python
# pylint: disable=wrong-import-order, ungrouped-imports
"""Server module."""
import os
from buku import BukuDb
from flask import Flask, jsonify, request, render_template, current_app
from flask.cli import FlaskGroup
from flask_api import status
from flask_bootstrap import Bootstrap
from flask_paginate import Pagination, get_page_parameter, get_per_page_parameter
from markupsafe import Markup
import click
import flask
from flask import (
current_app,
flash,
Flask,
jsonify,
redirect,
render_template,
request,
url_for,
)
try:
from . import response, forms
@ -59,6 +70,7 @@ def bookmarks():
default=int(
current_app.config.get('BUKUSERVER_PER_PAGE', DEFAULT_PER_PAGE))
)
create_bookmarks_form = forms.CreateBookmarksForm()
if request.method == 'GET':
all_bookmarks = bukudb.get_rec_all()
result = {
@ -100,17 +112,31 @@ def bookmarks():
result=result,
pagination=pagination,
search_bookmarks_form=forms.SearchBookmarksForm(),
create_bookmarks_form=create_bookmarks_form,
)
elif request.method == 'POST':
url_data = create_bookmarks_form.url.data
result_flag = bukudb.add_rec(
request.form['url'], request.form['title'], request.form['tags'], request.form['description'])
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'}
]
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 = [

View File

@ -8,6 +8,7 @@
{% endblock %}
{% block navbar %}
{{super()}}
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
@ -37,18 +38,19 @@
{% endblock %}
{% block content %}
<div class="container">
{%- with messages = get_flashed_messages(with_categories=True) %}
{%- if messages %}
<div class="row">
<div class="col-md-12">
{{utils.flashed_messages(messages)}}
</div>
</div>
{%- endif %}
{%- endwith %}
<div class="container">{{show_flash_message()}}</div>
{%- endblock %}
{% macro show_flash_message() %}
{%- with messages = get_flashed_messages(with_categories=True) %}
{%- if messages %}
{% for category, message in messages %}
<div class="alert alert-{{category}}">{{message}} </div>
{% endfor %}
{%- endif %}
{%- endwith %}
{% endmacro %}
{% macro show_bookmarks(bookmarks) %}
<table class="table" style="table-layout: fixed; width: 100%">
<caption>Bookmark table.</caption>

View File

@ -2,10 +2,37 @@
{% block content %}
<div class="container">
<div style="padding-top: 70px;">
{{show_bookmarks_search_form()}}
<h1 style="padding-top: 70px;">Bookmarks</h1>
{{show_flash_message()}}
<div>
<div class="col-sm-6">
<h2> Search Bookmarks</h2>
{{show_bookmarks_search_form()}}
</div>
<div class="col-sm-6">
<h2> Create Bookmarks</h2>
<form class="form-horizontal" action="{{url_for('bookmarks-html')}}" method="POST">
<div class="form-group">
{{create_bookmarks_form.url.label(class_="control-label col-sm-3")}}
{{create_bookmarks_form.url}}
</div>
<div class="form-group">
{{create_bookmarks_form.title.label(class_="control-label col-sm-3")}}
{{create_bookmarks_form.title}}
</div>
<div class="form-group">
{{create_bookmarks_form.tags.label(class_="control-label col-sm-3")}}
{{create_bookmarks_form.tags}}
</div>
<div class="form-group">
{{create_bookmarks_form.description.label(class_="control-label col-sm-3")}}
{{create_bookmarks_form.description}}
</div>
<button type="submit" class="btn btn-default">Create</button>
</form>
</div>
</div>
{{show_bookmarks(result.bookmarks)}}
{{show_bookmarks(result.bookmarks)}}
{{pagination.links}}
</div>