소스 검색

improvements to filters to allow multiple filters

joel
Simon Bowie 1 년 전
부모
커밋
c86fd54a99
3개의 변경된 파일47개의 추가작업 그리고 42개의 파일을 삭제
  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 파일 보기

@@ -20,17 +20,19 @@ book = Blueprint('book', __name__)
@book.route('/books')
def get_books():
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():
if key != 'view':
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
count = len(books)
if view != 'list':
@@ -39,14 +41,14 @@ def get_books():
else:
# reorder books by book name
books = sorted(books, key=lambda d: d.__dict__['name'])
# get filters
# get values for filters
# 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_filter = get_filter_values('year', type)
year_filter = get_filter_values('year', resource_type)
# 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
@book.route('/books/<int:book_id>')

+ 14
- 12
web/app/templates/resources.html 파일 보기

@@ -27,7 +27,9 @@
commodo nec est et, interdum varius risus. Curabitur vehicula consequat auctor.

<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 class="h-full ">
@@ -61,7 +63,7 @@
<div class="filter-options my-4" x-show="expandFilters">
{% for practice in practices_filter %}
<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>
{% endfor %}
</div>
@@ -72,8 +74,8 @@
<div class="filter-header">Year</div>
<div class="filter-options my-4" x-show="expandFilters">
{% 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>
{% endfor %}
</div>
@@ -84,8 +86,8 @@
<div class="filter-header">Typology category</div>
<div class="filter-options my-4" x-show="expandFilters">
{% 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>
{% endfor %}
</div>
@@ -96,8 +98,8 @@
<div class="filter-header">Scripting languages</div>
<div class="filter-options my-4" x-show="expandFilters">
{% 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>
{% endfor %}
</div>
@@ -108,8 +110,8 @@
<div class="filter-header">License</div>
<div class="filter-options my-4" x-show="expandFilters">
{% 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>
{% endfor %}
</div>
@@ -120,8 +122,8 @@
<div class="filter-header">Status</div>
<div class="filter-options my-4" x-show="expandFilters">
{% 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>
{% endfor %}
</div>

+ 18
- 17
web/app/tool.py 파일 보기

@@ -20,20 +20,22 @@ tool = Blueprint('tool', __name__)
@tool.route('/tools')
def get_tools():
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():
if key != 'view':
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\/"
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
count = len(tools)
if view != 'list':
@@ -42,17 +44,16 @@ def get_tools():
else:
# reorder tools by tools name
tools = sorted(tools, key=lambda d: d.__dict__['name'])
# get filters
# get values for filters
# 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
licenses_filter = get_filter_values('license', type)
licenses_filter = get_filter_values('license', resource_type)
# language
languages_filter = get_filter_values('scriptingLanguage', type)
languages_filter = get_filter_values('scriptingLanguage', resource_type)
# 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
@tool.route('/tools/<int:tool_id>')

Loading…
취소
저장