expanded search functionality

master
nberting 7 years ago
parent 68c38ae198
commit 1a68728ed1

@ -8,7 +8,7 @@ import os
import click import click
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from sqlalchemy.dialects import registry from sqlalchemy.dialects import registry
import flask_whooshalchemyplus # import flask_whooshalchemyplus not using whoosh anymore
registry.register("rqlite.pyrqlite", "sqlalchemy_rqlite.pyrqlite", "dialect") 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['DEBUG'] = True
app.config['PORT'] = 80 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') #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'mydatabase.db')
db = SQLAlchemy(app) db = SQLAlchemy(app)
@ -39,5 +36,7 @@ socketio = SocketIO(app)
app.config.from_object(__name__) app.config.from_object(__name__)
from app import views 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

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

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

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

@ -3,16 +3,21 @@
{% block main %} {% block main %}
<div class="container"> <div class="container">
<div>
{% 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>{{ 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">browse</button>
</form> </form>
</div>
<div class="container"> <br>
<h1 class="page-header">Search Results for: {{ query }}</h1> <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() %} {% with messages = get_flashed_messages() %}
{% if messages %} {% if messages %}
<div class="alert alert-success"> <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> <li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %} {% endfor %}
</td> </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> </a></td>
{% endfor %} {% endfor %}
</table> </table>
</div> </div>
@ -92,11 +97,13 @@
<td> {% for stack in book.stacks %} <td> {% for stack in book.stacks %}
<li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li> <li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %} {% endfor %}
</td> </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> </a></td>
{% endfor %} {% endfor %}
</table> </table>
<p> <p>

@ -7,7 +7,7 @@
<div>{{ form.select(style="width: 100px; margin: 10px; float: left; font-size: 20px") }}</div> <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">browse</button>
</form> </form>
<h1 class="page-header">All Books</h1> <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> <li><a href="{{url_for('show_stack_by_id', id=stack.id)}}">{{ stack.stack_name }}</a> </li>
{% endfor %} {% endfor %}
</td> </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> </a></td>

@ -335,7 +335,6 @@ def edit_stack_by_id(id):
return redirect(url_for('show_stack_by_id', id=id)) return redirect(url_for('show_stack_by_id', id=id))
return render_template('edit_stack_detail.html', stack=stack, form=form) return render_template('edit_stack_detail.html', stack=stack, form=form)
## search
## search ## search
@ -352,18 +351,28 @@ def show_books():
def search_results(searchtype, query): def search_results(searchtype, query):
search = SearchForm(request.form) search = SearchForm(request.form)
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))
if searchtype == 'Title': if searchtype == 'Title':
results=Book.query.filter(Book.title.contains(query)).all() results=Book.query.filter(Book.title.contains(query))
if searchtype == 'Category': 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': 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='') upload_form = UploadForm(title= query, author='')
return render_template('red_link.html', form=upload_form, title=query) return render_template('red_link.html', form=upload_form, title=query)
@ -372,7 +381,11 @@ def search_results(searchtype, query):
results = [] results = []
return redirect((url_for('search_results', searchtype=search.select.data, query=search.search.data))) 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 ## Search - autocomplete
autocomplete_suggestions = [] autocomplete_suggestions = []

Loading…
Cancel
Save