add exclude tags to advanced search

pull/18/head
Cervinko Cera 9 years ago
parent 80c3c19c01
commit c26eb6aa08

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

@ -327,11 +327,15 @@ def get_matching_tags():
q = db.session.query(db.Books) q = db.session.query(db.Books)
author_input = request.args.get('author_name') author_input = request.args.get('author_name')
title_input = request.args.get('book_title') 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+"%")) q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_input + "%")), db.Books.title.like("%"+title_input+"%"))
if len(tag_inputs) > 0: if len(include_tag_inputs) > 0:
for tag in tag_inputs: for tag in include_tag_inputs:
q = q.filter(db.Books.tags.any(db.Tags.id == tag)) 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 book in q:
for tag in book.tags: for tag in book.tags:
if tag.id not in tag_dict['tags']: if tag.id not in tag_dict['tags']:
@ -447,19 +451,22 @@ def search():
def advanced_search(): def advanced_search():
if request.method == 'GET': if request.method == 'GET':
q = db.session.query(db.Books) 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") author_name = request.args.get("author_name")
book_title = request.args.get("book_title") 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 = []
searchterm.extend((author_name, book_title)) 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.extend(tag.name for tag in tag_names)
searchterm = " + ".join(filter(None, searchterm)) searchterm = " + ".join(filter(None, searchterm))
q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_name + "%")), db.Books.title.like("%"+book_title+"%")) 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) 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)) 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() q = q.all()
return render_template('search.html', searchterm=searchterm, entries=q) return render_template('search.html', searchterm=searchterm, entries=q)
tags = db.session.query(db.Tags).order_by(db.Tags.name).all() tags = db.session.query(db.Tags).order_by(db.Tags.name).all()

Loading…
Cancel
Save