Browse Source

improvements to filters to allow multiple filters

joel
Simon Bowie 1 year ago
parent
commit
c86fd54a99
3 changed files with 47 additions and 42 deletions
  1. +15
    -13
      web/app/book.py
  2. +14
    -12
      web/app/templates/resources.html
  3. +18
    -17
      web/app/tool.py

+ 15
- 13
web/app/book.py View File

@book.route('/books') @book.route('/books')
def get_books(): def get_books():
view = request.args.get('view') view = request.args.get('view')
type = 'book'
books = Resource.query.filter_by(type=type).all()
resource_type = 'book'
books_query = Resource.query.filter_by(type=resource_type)
for key in request.args.keys(): for key in request.args.keys():
if key != 'view': if key != 'view':
if key == 'practice': if key == 'practice':
books = Resource.query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.second_resource_id==request.args.get(key)).all()
also_books = Resource.query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.first_resource_id==request.args.get(key)).all()
books = books + also_books
else:
kwargs = {'type': type, key: request.args.get(key)}
books = Resource.query.filter_by(**kwargs).all()
books_1 = books_query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Relationship.second_resource_id==request.args.get(key))
books_2 = books_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key))
books_query = books_1.union(books_2)
if key != 'practice':
kwargs = {key: request.args.get(key)}
books_query = books_query.filter_by(**kwargs)
# finalise the query
books = books_query.all()
# get number of books # get number of books
count = len(books) count = len(books)
if view != 'list': if view != 'list':
else: else:
# reorder books by book name # reorder books by book name
books = sorted(books, key=lambda d: d.__dict__['name']) books = sorted(books, key=lambda d: d.__dict__['name'])
# get filters
# get values for filters
# practices # practices
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name)
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all()
# year # year
year_filter = get_filter_values('year', type)
year_filter = get_filter_values('year', resource_type)
# typology # typology
typology_filter = get_filter_values('typology', type)
return render_template('resources.html', resources=books, type=type, practices_filter=practices_filter, year_filter=year_filter, typology_filter=typology_filter, count=count, view=view)
typology_filter = get_filter_values('typology', resource_type)
return render_template('resources.html', resources=books, type=resource_type, practices_filter=practices_filter, year_filter=year_filter, typology_filter=typology_filter, count=count, view=view)


# route for displaying a single book based on the ID in the database # route for displaying a single book based on the ID in the database
@book.route('/books/<int:book_id>') @book.route('/books/<int:book_id>')

+ 14
- 12
web/app/templates/resources.html View File

commodo nec est et, interdum varius risus. Curabitur vehicula consequat auctor. commodo nec est et, interdum varius risus. Curabitur vehicula consequat auctor.


<br/><br/> <br/><br/>
<a href="{{ url_for(request.endpoint, view='list') }}">LIST VIEW</a>
<a href="{{ request.base_url }}?view=list{% for key in request.args %}{% if key != 'view' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}">
LIST VIEW
</a>


</div> </div>
<div class="h-full "> <div class="h-full ">
<div class="filter-options my-4" x-show="expandFilters"> <div class="filter-options my-4" x-show="expandFilters">
{% for practice in practices_filter %} {% for practice in practices_filter %}
<div {% if request.args.get('practice')==practice[0]|string %} class="active" {% endif %}> <div {% if request.args.get('practice')==practice[0]|string %} class="active" {% endif %}>
<a href="{{ url_for(request.endpoint, practice=practice[0]) }}" hx-target="#resources" hx-select="#resources">{{ practice[1] }}</a>
<a href="{{ request.base_url }}?practice={{ practice[0] }}{% for key in request.args %}{% if key != 'practice' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}" hx-target="#resources" hx-select="#resources">{{ practice[1] }}</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<div class="filter-header">Year</div> <div class="filter-header">Year</div>
<div class="filter-options my-4" x-show="expandFilters"> <div class="filter-options my-4" x-show="expandFilters">
{% for year in year_filter %} {% for year in year_filter %}
<div {% if request.args.get('year')==year %} class="active" {% endif %}>
<a href="{{ url_for(request.endpoint, year=year) }}" hx-target="#resources" hx-select="#resources">{{ year }}</a>
<div {% if year in request.args.getlist('year') %} class="active" {% endif %}>
<a href="{{ request.base_url }}?year={{ year }}{% for key in request.args %}{% if key != 'year' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}" hx-target="#resources" hx-select="#resources">{{ year }}</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<div class="filter-header">Typology category</div> <div class="filter-header">Typology category</div>
<div class="filter-options my-4" x-show="expandFilters"> <div class="filter-options my-4" x-show="expandFilters">
{% for typology in typology_filter %} {% for typology in typology_filter %}
<div {% if request.args.get('typology')==typology %} class="active" {% endif %}>
<a href="{{ url_for(request.endpoint, typology=typology) }}" hx-target="#resources" hx-select="#resources">{{ typology }}</a>
<div {% if typology in request.args.getlist('typology') %} class="active" {% endif %}>
<a href="{{ request.base_url }}?typology={{ typology }}{% for key in request.args %}{% if key != 'typology' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}" hx-target="#resources" hx-select="#resources">{{ typology }}</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<div class="filter-header">Scripting languages</div> <div class="filter-header">Scripting languages</div>
<div class="filter-options my-4" x-show="expandFilters"> <div class="filter-options my-4" x-show="expandFilters">
{% for language in languages_filter %} {% for language in languages_filter %}
<div {% if request.args.get('scriptingLanguage')==language %} class="active" {% endif %}>
<a href="{{ url_for(request.endpoint, scriptingLanguage=language) }}" hx-target="#resources" hx-select="#resources">{{ language }}</a>
<div {% if language in request.args.getlist('scriptingLanguage') %} class="active" {% endif %}>
<a href="{{ request.base_url }}?scriptingLanguage={{ language }}{% for key in request.args %}{% if key != 'scriptingLanguage' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}" hx-target="#resources" hx-select="#resources">{{ language }}</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<div class="filter-header">License</div> <div class="filter-header">License</div>
<div class="filter-options my-4" x-show="expandFilters"> <div class="filter-options my-4" x-show="expandFilters">
{% for license in licenses_filter %} {% for license in licenses_filter %}
<div {% if request.args.get('license')==license %} class="active" {% endif %}>
<a href="{{ url_for(request.endpoint, license=license) }}" hx-target="#resources" hx-select="#resources">{{ license }}</a>
<div {% if license in request.args.getlist('license') %} class="active" {% endif %}>
<a href="{{ request.base_url }}?license={{ license }}{% for key in request.args %}{% if key != 'license' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}" hx-target="#resources" hx-select="#resources">{{ license }}</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<div class="filter-header">Status</div> <div class="filter-header">Status</div>
<div class="filter-options my-4" x-show="expandFilters"> <div class="filter-options my-4" x-show="expandFilters">
{% for status in status_filter %} {% for status in status_filter %}
<div {% if request.args.get('status')==status %} class="active" {% endif %}>
<a href="{{ url_for(request.endpoint, status=status) }}" hx-target="#resources" hx-select="#resources">{{ status }}</a>
<div {% if status in request.args.getlist('status') %} class="active" {% endif %}>
<a href="{{ request.base_url }}?status={{ status }}{% for key in request.args %}{% if key != 'status' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}" hx-target="#resources" hx-select="#resources">{{ status }}</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

+ 18
- 17
web/app/tool.py View File

@tool.route('/tools') @tool.route('/tools')
def get_tools(): def get_tools():
view = request.args.get('view') view = request.args.get('view')
type = 'tool'
tools = Resource.query.filter_by(type=type).all()
resource_type = 'tool'
tools_query = Resource.query.filter_by(type=resource_type)
for key in request.args.keys(): for key in request.args.keys():
if key != 'view': if key != 'view':
if key == 'practice': if key == 'practice':
tools = Resource.query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.second_resource_id==request.args.get(key)).all()
also_tools = Resource.query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.first_resource_id==request.args.get(key)).all()
tools = tools + also_tools
elif key == 'scriptingLanguage':
tools_1 = tools_query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Relationship.second_resource_id==request.args.get(key))
tools_2 = tools_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key))
tools_query = tools_1.union(tools_2)
if key == 'scriptingLanguage':
regex = request.args.get(key) + "$|" + request.args.get(key) + "\s\/" regex = request.args.get(key) + "$|" + request.args.get(key) + "\s\/"
tools = Resource.query.filter_by(type=type).filter(Resource.scriptingLanguage.regexp_match(regex)).all()
else:
kwargs = {'type': type, key: request.args.get(key)}
tools = Resource.query.filter_by(**kwargs).all()
tools_query = tools_query.filter(Resource.scriptingLanguage.regexp_match(regex))
if key != 'practice' and key != 'scriptingLanguage':
kwargs = {key: request.args.get(key)}
tools_query = tools_query.filter_by(**kwargs)
# finalise the query
tools = tools_query.all()
# get number of tools # get number of tools
count = len(tools) count = len(tools)
if view != 'list': if view != 'list':
else: else:
# reorder tools by tools name # reorder tools by tools name
tools = sorted(tools, key=lambda d: d.__dict__['name']) tools = sorted(tools, key=lambda d: d.__dict__['name'])
# get filters
# get values for filters
# practices # practices
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name)
#FOR LATER: SELECT Resource.name, second.name FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id LEFT JOIN Resource second ON Relationship.second_resource_id=second.id;
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all()
# license # license
licenses_filter = get_filter_values('license', type)
licenses_filter = get_filter_values('license', resource_type)
# language # language
languages_filter = get_filter_values('scriptingLanguage', type)
languages_filter = get_filter_values('scriptingLanguage', resource_type)
# status # status
status_filter = get_filter_values('status', type)
return render_template('resources.html', resources=tools, type=type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter, status_filter=status_filter, count=count, view=view)
status_filter = get_filter_values('status', resource_type)
return render_template('resources.html', resources=tools, type=resource_type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter, status_filter=status_filter, count=count, view=view)


# route for displaying a single tool based on the ID in the database # route for displaying a single tool based on the ID in the database
@tool.route('/tools/<int:tool_id>') @tool.route('/tools/<int:tool_id>')

Loading…
Cancel
Save