Browse Source

added category & title options to search

ansible-setup-and-deploy
nberting 6 years ago
parent
commit
07dcffefa1
  1. 5
      app/forms.py
  2. 3
      app/static/css/style.css
  3. 6
      app/templates/edit_book_detail.html
  4. 2
      app/templates/header.html
  5. 42
      app/templates/results.html
  6. 6
      app/templates/show_book_detail.html
  7. 5
      app/templates/show_books.html
  8. 57
      app/views.py

5
app/forms.py

@ -3,7 +3,7 @@ from wtforms import StringField, FileField, validators
from wtforms.validators import InputRequired, DataRequired from wtforms.validators import InputRequired, DataRequired
from wtforms import FieldList from wtforms import FieldList
from wtforms import Form as NoCsrfForm from wtforms import Form as NoCsrfForm
from wtforms.fields import StringField, FormField, SubmitField from wtforms.fields import StringField, FormField, SubmitField, SelectField
from app.models import Book, BookSchema, Author from app.models import Book, BookSchema, Author
# - - - Forms - - - # - - - Forms - - -
@ -26,4 +26,7 @@ class EditForm(FlaskForm):
category = StringField('category', validators=[InputRequired()]) category = StringField('category', validators=[InputRequired()])
class SearchForm(FlaskForm): class SearchForm(FlaskForm):
choices = [('Title', 'Title'),
('Category', 'Category')]
select = SelectField('', choices=choices)
search = StringField('', validators=[InputRequired()]) search = StringField('', validators=[InputRequired()])

3
app/static/css/style.css

@ -74,13 +74,14 @@ font-size: 16px;
.search input{ .search input{
margin: 0; margin: 0;
float: left; float: left;
width: 400px; width: 320px;
height: 36px; height: 36px;
font-size: 20px; font-size: 20px;
font-weight: regular; font-weight: regular;
padding: 2px; padding: 2px;
background:rgba(50, 50, 50, 0.2); background:rgba(50, 50, 50, 0.2);
border:0px; border:0px;
box-shadow: inset 0 0 5px rgba(000,000,000, 0.2);
} }
.button { .button {

6
app/templates/edit_book_detail.html

@ -11,9 +11,9 @@
{{ form.csrf_token }} {{ form.csrf_token }}
<div class="form-group"><h1 class="header">{{ form.title.label }} {{ form.title(size=20, class="form-control") }}</h1></div> <div class="form-group"><h1 class="header">{{ form.title.label }} {{ form.title(size=20, class="form-control") }}</h1></div>
<object class="no_cover" data="../../static/img/default_cover.png" type="image/png" width="150">
<img src="../../uploads/cover/{{ book.cover }}" width="200"> <img class="no_cover" id="{{ book.title }}" src="/uploads/cover/{{ book.cover }}" width="150" onerror="if (this.src != '/uploads/cover/{{ book.cover }}') this.src = '/static/img/default_cover.png';">
</object>
<br> <br> <br> <br>
<div data-toggle="fieldset" id="phone-fieldset"> <div data-toggle="fieldset" id="phone-fieldset">
{{ form.author.label }} <button type="button" data-toggle="fieldset-add-row" {{ form.author.label }} <button type="button" data-toggle="fieldset-add-row"

2
app/templates/header.html

@ -2,8 +2,8 @@
<ul> <ul>
<li><a href="{{ url_for('home') }}">Home</a></li> <li><a href="{{ url_for('home') }}">Home</a></li>
<li><a href="{{ url_for('show_books') }}">Show Books</a></li> <li><a href="{{ url_for('show_books') }}">Show Books</a></li>
<li><a href="{{ url_for('add_book') }}">Add Book</a></li>
<li><a href="{{ url_for('show_stacks') }}">Show Stacks</a></li> <li><a href="{{ url_for('show_stacks') }}">Show Stacks</a></li>
<li><a href="{{ url_for('add_book') }}">Add Book</a></li>
<li><a href="{{ url_for('about') }}">About</a></li> <li><a href="{{ url_for('about') }}">About</a></li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>

42
app/templates/results.html

@ -5,13 +5,12 @@
<div class="container"> <div class="container">
{% from "_formhelpers.html" import render_field %} {% from "_formhelpers.html" import render_field %}
<form method="POST"> <form method="POST">
<div>{{ form.select(style="width: 100px; margin: 10px; float: left; font-size: 20px") }}</div>
<div class="search"> <div class="search">
{{ render_field(form.search) }} </div> {{ render_field(form.search) }} </div>
<button type="submit" class="button">Search</button> <button type="submit" class="button">Search</button>
</form> </form>
</div>
<div class="container"> <div class="container">
<h1 class="page-header">Search Results for: {{ query }}</h1> <h1 class="page-header">Search Results for: {{ query }}</h1>
{% with messages = get_flashed_messages() %} {% with messages = get_flashed_messages() %}
@ -26,17 +25,18 @@
{% endif %} {% endif %}
{% endwith %} {% endwith %}
<table style="width:100%"> <table class="library_table" id="table" style="width:100%">
<tr> <tr id="header">
<th>Cover</th> <th>Cover</th>
<th>Title</th> <th>Title</th>
<th>Author</th> <th>Author</th>
<th>Filetype</th> <th>Filetype</th>
<th>Tag</th> <th>Category</th>
<th>Stack</th>
</tr> </tr>
{% for book in books %} {% for book in books %}
<tr> <tr>
<td><img class="no_cover" id="{{ book.title }}" src="../../uploads/cover/{{ book.cover }}" width="80" onerror="if (this.src != '../../static/img/default_cover.png') this.src = '../../static/img/default_cover.png';"></td> <td><img class="no_cover" id="{{ book.title }}" src="/uploads/cover/{{ book.cover }}" width="80" onerror="if (this.src != '/static/img/default_cover.png') this.src = '/static/img/default_cover.png';"></td>
<td><a href="{{url_for('show_book_by_id', id=book.id)}}">{{ book.title }}</a></td> <td><a href="{{url_for('show_book_by_id', id=book.id)}}">{{ book.title }}</a></td>
<td> {% for author in book.authors %} <td> {% for author in book.authors %}
@ -45,8 +45,12 @@
{% endfor %}</td> {% endfor %}</td>
<td>{{ book.fileformat }}</td> <td>{{ book.fileformat }}</td>
<td>{{ book.tag}}</td> <td>{{ book.category}}</td>
</tr> <td> {% for stack in book.stacks %}
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %}
</td>
{% endfor %} {% endfor %}
</table> </table>
@ -56,28 +60,32 @@
<hr> <hr>
<h2> Other books </h2> <h2> Other books </h2>
<table style="width:100%; "> <table class="library_table" id="table" style="width:100%">
<tr> <tr id="header">
<th>Cover</th> <th>Cover</th>
<th>Title</th> <th>Title</th>
<th>Author</th> <th>Author</th>
<th>Filetype</th> <th>Filetype</th>
<th>Tag</th> <th>Category</th>
<th>Stack</th>
</tr> </tr>
{% for book in books_all %} {% for book in books_all %}
<tr> <tr>
<td><img class="no_cover" id="{{ book.title }}" src="../../uploads/cover/{{ book.cover }}" width="40" onerror="if (this.src != '../../static/img/default_cover.png') this.src = '../../static/img/default_cover.png';"></td> <td><img class="no_cover" id="{{ book.title }}" src="/uploads/cover/{{ book.cover }}" width="80" onerror="if (this.src != '/static/img/default_cover.png') this.src = '/static/img/default_cover.png';"></td>
<td><a href="{{url_for('show_book_by_id', id=book.id)}}">{{ book.title }}</a></td> <td><a href="{{url_for('show_book_by_id', id=book.id)}}">{{ book.title }}</a></td>
<td> {% for author in book.authors %} <td> {% for author in book.authors %}
<li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li> <li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li>
{% endfor %}</td> {% endfor %}</td>
<td>{{ book.fileformat }}</td> <td>{{ book.fileformat }}</td>
<td>{{ book.tag}}</td> <td>{{ book.category}}</td>
</tr> <td> {% for stack in book.stacks %}
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %}
</td>
{% endfor %} {% endfor %}
</table> </table>
<p> <p>

6
app/templates/show_book_detail.html

@ -4,15 +4,15 @@
<div class="container"> <div class="container">
<h1 class="header">{{ book.title }}</h1> <h1 class="header">{{ book.title }}</h1>
<img class="no_cover" id="{{ book.title }}" src="../uploads/cover/{{ book.cover }}" width="150" onerror="if (this.src != '../static/img/default_cover.png') this.src = '../static/img/default_cover.png';"> <img class="no_cover" id="{{ book.title }}" src="../uploads/cover/{{ book.cover }}" width="150" onerror="if (this.src != '../static/img/{{ book.cover }}') this.src = '../static/img/default_cover.png';">
<p>Author(s): <ul>{% for author in book.authors %} <p>Author(s): <ul>{% for author in book.authors %}
<li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li> <li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li>
{% endfor %}</ul></p> {% endfor %}</ul></p>
<p>{{ book.category }}</p> <p>Category: {{ book.category }}</p>
<p>{{ book.year_published }}</p> <p>Year published: {{ book.year_published }}</p>
<p>Stack(s): <ul>{% for stack in book.stacks %} <p>Stack(s): <ul>{% for stack in book.stacks %}
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> <li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a>

5
app/templates/show_books.html

@ -2,9 +2,9 @@
{% block main %} {% block main %}
<div class="container"> <div class="container">
{% from "_formhelpers.html" import render_field %} {% from "_formhelpers.html" import render_field %}
<form method="POST"> <form method="POST">
<div>{{ form.select(style="width: 100px; margin: 10px; float: left; font-size: 20px") }}</div>
<div class="search"> <div class="search">
{{ render_field(form.search) }} </div> {{ render_field(form.search) }} </div>
<button type="submit" class="button">Search</button> <button type="submit" class="button">Search</button>
@ -36,8 +36,7 @@
<tr> <tr>
<td> <td>
<img class="no_cover" id="{{ book.title }}" src="../uploads/cover/{{ book.cover }}" width="40" onerror="if (this.src != '../static/img/default_cover.png') this.src = '../static/img/default_cover.png';"> <img class="no_cover" id="{{ book.title }}" src="/uploads/cover/{{ book.cover }}" width="40" onerror="if (this.src != '/static/img/default_cover.png') this.src = '/static/img/default_cover.png';">
<!-- <object class="no_cover" data="../static/img/default_cover.png" type="image/png" width="65"> <!-- <object class="no_cover" data="../static/img/default_cover.png" type="image/png" width="65">
<p hidden="True"></p> <p hidden="True"></p>

57
app/views.py

@ -60,7 +60,11 @@ def show_books():
books = db.session.query(Book).all() books = db.session.query(Book).all()
search = SearchForm(request.form) search = SearchForm(request.form)
if request.method == 'POST': if request.method == 'POST':
return redirect((url_for('search_results', query=search.search.data))) if search.select.data == 'Title':
return redirect((url_for('search_results', query=search.search.data)))
if search.select.data == 'Category':
return redirect((url_for('search_cat', query=search.search.data)))
return render_template('show_books.html', books=books, form=search) return render_template('show_books.html', books=books, form=search)
@app.route('/scape', methods=['POST', 'GET']) @app.route('/scape', methods=['POST', 'GET'])
@ -155,6 +159,10 @@ def add_book():
upload_form = UploadForm() upload_form = UploadForm()
if request.method == 'POST': if request.method == 'POST':
title = upload_form.title.data
authors = upload_form.author.data
category = upload_form.category.data
year_published = upload_form.year_published.data
if upload_form.validate_on_submit(): if upload_form.validate_on_submit():
if upload_form.upload.data: if upload_form.upload.data:
@ -177,13 +185,6 @@ def add_book():
name, file_extension = os.path.splitext(new_filename) name, file_extension = os.path.splitext(new_filename)
file.save(fullpath) file.save(fullpath)
cover = get_cover(fullpath, name) cover = get_cover(fullpath, name)
title = upload_form.title.data # You could also have used request.form['name']
authors = upload_form.author.data # You could also have used
category = upload_form.category.data
year_published = upload_form.year_published.data
#print(author)
#print(len(author))
book = Book(title, filename, cover, file_extension, category, year_published) book = Book(title, filename, cover, file_extension, category, year_published)
db.session.add(book) db.session.add(book)
for author in authors: for author in authors:
@ -197,7 +198,6 @@ def add_book():
db.session.commit() db.session.commit()
# save user to database # save user to database
flash("%s added to the library" % (title)) flash("%s added to the library" % (title))
return redirect(url_for('show_books')) return redirect(url_for('show_books'))
else: else:
@ -207,15 +207,7 @@ def add_book():
file = open('app/uploads/potential.pdf') file = open('app/uploads/potential.pdf')
filename = 'potential.pdf' filename = 'potential.pdf'
file_extension = '.pdf' file_extension = '.pdf'
cover = get_cover('app/uploads/potential.pdf', 'default') cover = ''
title = upload_form.title.data # You could also have used request.form['name']
authors = upload_form.author.data # You could also have used
category = upload_form.category.data
year_published = upload_form.year_published.data
#print(author)
#print(len(author))
book = Book(title, filename, cover, file_extension, category,year_published) book = Book(title, filename, cover, file_extension, category,year_published)
db.session.add(book) db.session.add(book)
for author in authors: for author in authors:
@ -231,7 +223,6 @@ def add_book():
flash("%s added to the library" % (title)) flash("%s added to the library" % (title))
return redirect(url_for('show_books')) return redirect(url_for('show_books'))
flash_errors(upload_form) flash_errors(upload_form)
return render_template('add_book.html', form=upload_form) return render_template('add_book.html', form=upload_form)
@ -277,10 +268,9 @@ def show_stack_by_id(id):
## search ## search
@app.route('/search/<query>/', methods=['POST', 'GET']) @app.route('/search/titles/<query>/', methods=['POST', 'GET'])
def search_results(query): def search_results(query):
search = SearchForm(request.form) search = SearchForm(request.form)
books_all=Book.query.all()
random_order=Book.query.order_by(func.random()).limit(10) random_order=Book.query.order_by(func.random()).limit(10)
results=Book.query.filter(Book.title.contains(query)).all() results=Book.query.filter(Book.title.contains(query)).all()
@ -291,11 +281,32 @@ def search_results(query):
if request.method == 'POST': if request.method == 'POST':
query = search.search.data query = search.search.data
results = [] results = []
results=Book.query.filter(Book.title.contains(query)).all() if search.select.data == 'Title':
return redirect((url_for('search_results', query=search.search.data))) return redirect((url_for('search_results', query=search.search.data)))
if search.select.data == 'Category':
return redirect((url_for('search_cat', query=search.search.data)))
return render_template('results.html', form=search, books=results, books_all=random_order, query=query) return render_template('results.html', form=search, books=results, books_all=random_order, query=query)
@app.route('/search/cat/<query>/', methods=['POST', 'GET'])
def search_cat(query):
search = SearchForm(request.form)
random_order=Book.query.order_by(func.random()).limit(10)
results=Book.query.filter(Book.category.contains(query)).all()
if not results:
upload_form = UploadForm(category=query)
return render_template('red_link.html', form=upload_form, category=query)
if request.method == 'POST':
query = search.search.data
results = []
if search.select.data == 'Title':
return redirect((url_for('search_results', query=search.search.data)))
if search.select.data == 'Category':
return redirect((url_for('search_cat', query=search.search.data)))
return render_template('results.html', form=search, books=results, books_all=random_order, query=query)
### ###
# The API # The API

Loading…
Cancel
Save