add exclude tags to advanced search

This commit is contained in:
Cervinko Cera 2016-05-03 14:27:38 +02:00
parent 80c3c19c01
commit c26eb6aa08
2 changed files with 31 additions and 12 deletions

View File

@ -10,12 +10,22 @@
<label for="bookAuthor">Author</label>
<input type="text" class="form-control typeahead" name="author_name" id="bookAuthor" value="" autocomplete="off">
</div>
<label for="Tags">Select Tags</label>
<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 tag_click">
<input type="checkbox" autocomplete="off" name="tag" value="{{tag.id}}">{{tag.name}}</input>
<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>
@ -32,9 +42,11 @@
$('form').on('change input typeahead:selected', function() {
form = $('form').serialize();
$.getJSON( "{{ url_for('get_matching_tags') }}", form, function( data ) {
$('.tag_click').each(function() {
$('.tags_click').each(function() {
if ($.inArray(parseInt($(this).children('input').first().val(), 10), data.tags) == -1 ) {
$(this).addClass('disabled');
if (!($(this).hasClass('active'))) {
$(this).addClass('disabled');
}
}
else {
$(this).removeClass('disabled');

View File

@ -327,11 +327,15 @@ def get_matching_tags():
q = db.session.query(db.Books)
author_input = request.args.get('author_name')
title_input = request.args.get('book_title')
tag_inputs = request.args.getlist('tag')
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(tag_inputs) > 0:
for tag in tag_inputs:
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']:
@ -447,19 +451,22 @@ def search():
def advanced_search():
if request.method == 'GET':
q = db.session.query(db.Books)
tag_inputs = request.args.getlist('tag')
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 tag_inputs or author_name or 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_(tag_inputs)).all()
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 tag_inputs:
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()