Merge pull request #18 from cervinko/feature-advanced-search

add advanced search
This commit is contained in:
Jan B 2016-05-22 14:05:40 +02:00
commit db70e47557
4 changed files with 119 additions and 4 deletions

View File

@ -31,3 +31,9 @@ span.glyphicon.glyphicon-tags {padding-right: 5px;color: #999;vertical-align: te
.btn-file {position: relative; overflow: hidden;}
.btn-file input[type=file] {position: absolute; top: 0; right: 0; min-width: 100%; min-height: 100%; font-size: 100px; text-align: right; filter: alpha(opacity=0); opacity: 0; outline: none; background: white; cursor: inherit; display: block;}
.btn-toolbar .btn { margin-bottom: 5px; }
.btn-primary:hover, .btn-primary:focus, .btn-primary:active, .btn-primary.active, .open .dropdown-toggle.btn-primary{ background-color: #1C5484; }
.btn-primary.disabled, .btn-primary[disabled], fieldset[disabled] .btn-primary, .btn-primary.disabled:hover, .btn-primary[disabled]:hover, fieldset[disabled] .btn-primary:hover, .btn-primary.disabled:focus, .btn-primary[disabled]:focus, fieldset[disabled] .btn-primary:focus, .btn-primary.disabled:active, .btn-primary[disabled]:active, fieldset[disabled] .btn-primary:active, .btn-primary.disabled.active, .btn-primary[disabled].active, fieldset[disabled] .btn-primary.active { background-color: #89B9E2; }
.btn-toolbar>.btn+.btn, .btn-toolbar>.btn-group+.btn, .btn-toolbar>.btn+.btn-group, .btn-toolbar>.btn-group+.btn-group { margin-left:0px; }

View File

@ -57,9 +57,10 @@
<div class="form-group">
<input type="text" class="form-control" name="query" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
<button type="submit" class="btn btn-default">Go!</button>
</form>
</li>
<li><a href="{{url_for('advanced_search')}}"><span class="glyphicon glyphicon-search"></span> Advanced Search</a></li>
</ul>
<ul class="nav navbar-nav navbar-right" id="main-nav">
{% if g.user.is_authenticated() %}

View File

@ -0,0 +1,61 @@
{% extends "layout.html" %}
{% block body %}
<div class="col-sm-8">
<form role="form" action="{{ url_for('advanced_search') }}" method="GET">
<div class="form-group">
<label for="book_title">Book Title</label>
<input type="text" class="form-control" name="book_title" id="book_title" value="">
</div>
<div class="form-group">
<label for="bookAuthor">Author</label>
<input type="text" class="form-control typeahead" name="author_name" id="bookAuthor" value="" autocomplete="off">
</div>
<label for="Tags">Include Tags</label>
<div class="form-group">
<div class="btn-toolbar btn-toolbar-lg" data-toggle="buttons">
{% for tag in tags %}
<label id="tag_{{tag.id}}" class="btn btn-primary tags_click">
<input type="checkbox" autocomplete="off" name="include_tag" value="{{tag.id}}">{{tag.name}}</input>
</label>
{% endfor %}
</div>
</div>
<label for="Tags">Exclude Tags</label>
<div class="form-group">
<div class="btn-toolbar btn-toolbar-lg" data-toggle="buttons">
{% for tag in tags %}
<label id="tag_{{tag.id}}" class="btn btn-danger tags_click">
<input type="checkbox" autocomplete="off" name="exclude_tag" value="{{tag.id}}">{{tag.name}}</input>
</label>
{% endfor %}
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
{% endblock %}
{% block js %}
<script src="{{ url_for('static', filename='js/typeahead.bundle.js') }}"></script>
<script src="{{ url_for('static', filename='js/edit_books.js') }}"></script>
<script>
$('form').on('change input typeahead:selected', function() {
form = $('form').serialize();
$.getJSON( "{{ url_for('get_matching_tags') }}", form, function( data ) {
$('.tags_click').each(function() {
if ($.inArray(parseInt($(this).children('input').first().val(), 10), data.tags) == -1 ) {
if (!($(this).hasClass('active'))) {
$(this).addClass('disabled');
}
}
else {
$(this).removeClass('disabled');
}
});
});
});
</script>
{% endblock %}
{% block header %}
<link href="{{ url_for('static', filename='css/typeahead.css') }}" rel="stylesheet" media="screen">
{% endblock %}

View File

@ -318,6 +318,30 @@ def get_series_json():
entries = db.session.execute("select name from series where name like '%" + query + "%'")
json_dumps = json.dumps([dict(r) for r in entries])
return json_dumps
@app.route("/get_matching_tags", methods = ['GET', 'POST'])
@login_required_if_no_ano
def get_matching_tags():
tag_dict = {'tags': []}
if request.method == "GET":
q = db.session.query(db.Books)
author_input = request.args.get('author_name')
title_input = request.args.get('book_title')
include_tag_inputs = request.args.getlist('include_tag')
exclude_tag_inputs = request.args.getlist('exclude_tag')
q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_input + "%")), db.Books.title.like("%"+title_input+"%"))
if len(include_tag_inputs) > 0:
for tag in include_tag_inputs:
q = q.filter(db.Books.tags.any(db.Tags.id == tag))
if len(exclude_tag_inputs) > 0:
for tag in exclude_tag_inputs:
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag)))
for book in q:
for tag in book.tags:
if tag.id not in tag_dict['tags']:
tag_dict['tags'].append(tag.id)
json_dumps = json.dumps(tag_dict)
return json_dumps
@app.route("/", defaults={'page': 1})
@app.route('/page/<int:page>')
@ -421,6 +445,32 @@ def search():
return render_template('search.html', searchterm=term, entries=entries)
else:
return render_template('search.html', searchterm="")
@app.route("/advanced_search", methods=["GET"])
@login_required_if_no_ano
def advanced_search():
if request.method == 'GET':
q = db.session.query(db.Books)
include_tag_inputs = request.args.getlist('include_tag')
exclude_tag_inputs = request.args.getlist('exclude_tag')
author_name = request.args.get("author_name")
book_title = request.args.get("book_title")
if include_tag_inputs or exclude_tag_inputs or author_name or book_title:
searchterm = []
searchterm.extend((author_name, book_title))
tag_names = db.session.query(db.Tags).filter(db.Tags.id.in_(include_tag_inputs)).all()
searchterm.extend(tag.name for tag in tag_names)
searchterm = " + ".join(filter(None, searchterm))
q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_name + "%")), db.Books.title.like("%"+book_title+"%"))
random = db.session.query(db.Books).order_by(func.random()).limit(config.RANDOM_BOOKS)
for tag in include_tag_inputs:
q = q.filter(db.Books.tags.any(db.Tags.id == tag))
for tag in exclude_tag_inputs:
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag)))
q = q.all()
return render_template('search.html', searchterm=searchterm, entries=q)
tags = db.session.query(db.Tags).order_by(db.Tags.name).all()
return render_template('search_form.html', tags=tags)
@app.route("/author")
@login_required_if_no_ano
@ -920,7 +970,6 @@ def edit_book(book_id):
if len(book.ratings) > 0:
old_rating = book.ratings[0].rating
ratingx2 = int(float(to_save["rating"]) *2)
print ratingx2
if ratingx2 != old_rating:
is_rating = db.session.query(db.Ratings).filter(db.Ratings.rating == ratingx2).first()
if is_rating:
@ -945,7 +994,6 @@ def edit_book(book_id):
if to_save[cc_string].strip():
if c.datatype == 'rating':
to_save[cc_string] = str(int(float(to_save[cc_string]) *2))
print to_save[cc_string]
if to_save[cc_string].strip() != cc_db_value:
if cc_db_value != None:
#remove old cc_val
@ -1008,7 +1056,6 @@ def edit_book(book_id):
new_tag = db.session.query(db.cc_classes[c.id]).filter(db.cc_classes[c.id].value == add_tag).first()
# if no tag is found add it
if new_tag == None:
print add_tag
new_tag = db.cc_classes[c.id](value=add_tag)
db.session.add(new_tag)
new_tag = db.session.query(db.cc_classes[c.id]).filter(db.cc_classes[c.id].value == add_tag).first()