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 import FieldList
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
# - - - Forms - - -
@ -26,4 +26,7 @@ class EditForm(FlaskForm):
category = StringField('category', validators=[InputRequired()])
class SearchForm(FlaskForm):
choices = [('Title', 'Title'),
('Category', 'Category')]
select = SelectField('', choices=choices)
search = StringField('', validators=[InputRequired()])

3
app/static/css/style.css

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

6
app/templates/edit_book_detail.html

@ -11,9 +11,9 @@
{{ form.csrf_token }}
<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">
</object>
<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';">
<br> <br>
<div data-toggle="fieldset" id="phone-fieldset">
{{ form.author.label }} <button type="button" data-toggle="fieldset-add-row"

2
app/templates/header.html

@ -2,8 +2,8 @@
<ul>
<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('add_book') }}">Add Book</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>
</ul>
<div class="clearfix"></div>

42
app/templates/results.html

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

6
app/templates/show_book_detail.html

@ -4,15 +4,15 @@
<div class="container">
<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 %}
<li><a href="{{url_for('show_author_by_id', id=author.id)}}">{{ author.author_name }}</a> </li>
{% endfor %}</ul></p>
<p>{{ book.category }}</p>
<p>{{ book.year_published }}</p>
<p>Category: {{ book.category }}</p>
<p>Year published: {{ book.year_published }}</p>
<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>

5
app/templates/show_books.html

@ -2,9 +2,9 @@
{% block main %}
<div class="container">
{% from "_formhelpers.html" import render_field %}
<form method="POST">
<div>{{ form.select(style="width: 100px; margin: 10px; float: left; font-size: 20px") }}</div>
<div class="search">
{{ render_field(form.search) }} </div>
<button type="submit" class="button">Search</button>
@ -36,8 +36,7 @@
<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';">
<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">
<p hidden="True"></p>

57
app/views.py

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

Loading…
Cancel
Save