expanded search functionality

This commit is contained in:
nberting 2018-06-07 10:46:14 +02:00
parent 68c38ae198
commit 1a68728ed1
7 changed files with 50 additions and 40 deletions

View File

@ -8,7 +8,7 @@ import os
import click
from werkzeug.utils import secure_filename
from sqlalchemy.dialects import registry
import flask_whooshalchemyplus
# import flask_whooshalchemyplus not using whoosh anymore
registry.register("rqlite.pyrqlite", "sqlalchemy_rqlite.pyrqlite", "dialect")
@ -27,9 +27,6 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'rqlite+pyrqlite://localhost:4001/'
app.config['DEBUG'] = True
app.config['PORT'] = 80
# set the location for the whoosh index
app.config['WHOOSH_BASE'] = 'whoosh'
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'mydatabase.db')
db = SQLAlchemy(app)
@ -39,5 +36,7 @@ socketio = SocketIO(app)
app.config.from_object(__name__)
from app import views
flask_whooshalchemyplus.init_app(app) # initialize
# set the location for the whoosh index
# app.config['WHOOSH_BASE'] = 'whoosh'
# flask_whooshalchemyplus.init_app(app) # initialize

View File

@ -46,6 +46,9 @@ class EditStackForm(FlaskForm):
class SearchForm(FlaskForm):
choices = [('All', 'All'),
('Title', 'Title'),
('Category', 'Category')]
('Author', 'Author'),
('Category', 'Category'),
('Stack', 'Stack')]
select = SelectField('', choices=choices)
search = StringField('', validators=[InputRequired()])

View File

@ -17,7 +17,6 @@ stacks = db.Table('books_stacks',
class Book(db.Model):
__tablename__ = 'books'
__searchable__ = ['title', 'category', 'fileformat'] # these fields will be indexed by whoosh
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(255))

View File

@ -1,15 +1,4 @@
@import url("../fonts/fonts_style.css");
/*
Font-names:
junicoderegular
junicoderegularcondensed
junicodeitalic
junicodeitaliccondensed
junicodebold
junicodeboldcondensed
junicodebolditalic
junicodebolditaliccondensed
*/
*{
font-family: "Archivo Narrow";
@ -121,7 +110,7 @@ cursor: pointer;
.header input{
height:40px;
width: 500px;
font-size: 30px;
font-size: 24px;
font-weight: bold;
}
@ -136,10 +125,10 @@ margin: 0;
float: left;
width: 320px;
height: 36px;
font-size: 20px;
font-size: 18px;
font-weight: regular;
padding: 2px;
background:rgba(50, 50, 50, 0.2);
background:rgb(240, 240, 240);
border:0px;
box-shadow: inset 0 0 5px rgba(000,000,000, 0.2);
}
@ -200,7 +189,7 @@ font-size: 12px;
top:0;
left:0;
position: fixed;
font-size: 20px;
font-size: 18px;
background-color: yellow;
}

View File

@ -3,16 +3,21 @@
{% block main %}
<div class="container">
<div>
{% 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>
<button type="submit" class="button">browse</button>
</form>
<div class="container">
<h1 class="page-header">Search Results for: {{ query }}</h1>
</div>
<br>
<h4 style="line-height:0px;"">Results: "{{ query }}" included in {{ count }} out of {{ whole }} items </h4>
<div style="height: 20px; background-color: yellow; line-height:4px;">
<div style="background-color: black; width: {{ percentage }}%; height: 100%;"></div>
</div>
<br>
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-success">
@ -53,11 +58,11 @@
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %}
</td>
<td id='plus'><a href='{{url_for('add_to_stack', id=book.id)}}'>
<td id='plus'><a href="{{url_for('add_to_stack', id=book.id)}}">
==>
</a></td>
{% endfor %}
{% endfor %}
</table>
</div>
@ -92,11 +97,13 @@
<td> {% for stack in book.stacks %}
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %}
{% endfor %}
</td>
<td id='plus'><a href='{{url_for('add_to_stack', id=book.id)}}'>
<td id='plus'><a href="{{url_for('add_to_stack', id=book.id)}}">
==>
</a></td>
{% endfor %}
</table>
<p>

View File

@ -7,7 +7,7 @@
<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>
<button type="submit" class="button">browse</button>
</form>
<h1 class="page-header">All Books</h1>
@ -60,7 +60,7 @@
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %}
</td>
<td id='plus'><a href='{{url_for('add_to_stack', id=book.id)}}'>
<td id='plus'><a href="{{url_for('add_to_stack', id=book.id)}}">
==>
</a></td>

View File

@ -335,7 +335,6 @@ def edit_stack_by_id(id):
return redirect(url_for('show_stack_by_id', id=id))
return render_template('edit_stack_detail.html', stack=stack, form=form)
## search
## search
@ -352,18 +351,28 @@ def show_books():
def search_results(searchtype, query):
search = SearchForm(request.form)
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))
if searchtype == 'Title':
results=Book.query.filter(Book.title.contains(query)).all()
results=Book.query.filter(Book.title.contains(query))
if searchtype == 'Category':
results=Book.query.filter(Book.category.contains(query)).all()
results=Book.query.filter(Book.category.contains(query))
if searchtype== 'Author':
results=db.session.query(Book).join(Book.authors).filter(Author.author_name.contains(query))
if searchtype== 'Stack':
results=db.session.query(Book).join(Book.stacks).filter(Stack.stack_name.contains(query))
if searchtype== 'All':
results=Book.query.whoosh_search(query).all()
# results=Book.query.whoosh_search(query)
results=Book.query.filter(Book.title.contains(query))
results=results.union(Book.query.filter(Book.category.contains(query)))
results=results.union(db.session.query(Book).join(Book.authors).filter(Author.author_name.contains(query)))
results=results.union(db.session.query(Book).join(Book.stacks).filter(Stack.stack_name.contains(query)))
if not results:
if results.count() == 0:
upload_form = UploadForm(title= query, author='')
return render_template('red_link.html', form=upload_form, title=query)
@ -372,7 +381,11 @@ def search_results(searchtype, query):
results = []
return redirect((url_for('search_results', searchtype=search.select.data, query=search.search.data)))
return render_template('results.html', form=search, books=results, books_all=random_order, searchtype=search.select.data, query=query)
count = results.count()
whole = Book.query.count()
percentage = float(count / whole * 100)
return render_template('results.html', form=search, books=results, books_all=random_order, searchtype=search.select.data, query=query, count = count, whole = whole, percentage = percentage)
## Search - autocomplete
autocomplete_suggestions = []