From c26eb6aa08011f9f9f500d312a79dc8c8787d35c Mon Sep 17 00:00:00 2001 From: Cervinko Cera Date: Tue, 3 May 2016 14:27:38 +0200 Subject: [PATCH] add exclude tags to advanced search --- cps/templates/search_form.html | 22 +++++++++++++++++----- cps/web.py | 21 ++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/cps/templates/search_form.html b/cps/templates/search_form.html index a126dc52..e6a0fef5 100644 --- a/cps/templates/search_form.html +++ b/cps/templates/search_form.html @@ -10,12 +10,22 @@ - +
{% for tag in tags %} -
+
+ +
+
+ {% for tag in tags %} + {% endfor %}
@@ -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'); diff --git a/cps/web.py b/cps/web.py index b6ceaee2..85dce9d6 100755 --- a/cps/web.py +++ b/cps/web.py @@ -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()