Add tag filter list to package page

This commit is contained in:
rubenwardy 2020-07-12 20:10:19 +01:00
parent 2faa0e4219
commit ae4352068e
3 changed files with 67 additions and 18 deletions

@ -80,12 +80,15 @@ def list_all():
qb.show_discarded = True
topics = qb.buildTopicQuery().all()
tags = Tag.query.all()
tags = db.session.query(func.count(Tags.c.tag_id), Tag) \
.select_from(Tag).outerjoin(Tags).group_by(Tag.id).order_by(db.asc(Tag.title))
selected_tags = set(qb.tags)
return render_template("packages/list.html", \
title=title, packages=query.items, topics=topics, \
query=search, tags=tags, type=type_name, \
authors=authors, packages_count=query.total, \
pagination=query)
title=title, packages=query.items, pagination=query, \
query=search, tags=tags, selected_tags=selected_tags, type=type_name, \
authors=authors, packages_count=query.total, topics=topics)
def getReleases(package):

@ -19,20 +19,48 @@
</p>
{% endif %}
{% from "macros/packagegridtile.html" import render_pkggrid %}
{{ render_pkggrid(packages) }}
<div class="row">
<div class="col-md-9">
{% from "macros/packagegridtile.html" import render_pkggrid %}
{{ render_pkggrid(packages) }}
{% from "macros/pagination.html" import render_pagination %}
{{ render_pagination(pagination, url_set_query) }}
{% from "macros/pagination.html" import render_pagination %}
{{ render_pagination(pagination, url_set_query) }}
{% if topics %}
<h2 style="margin-top:2em;">More content from the forums</h2>
{% if topics %}
<h2 style="margin-top:2em;">More content from the forums</h2>
{% from "macros/topics.html" import render_topics %}
{{ render_topics(topics, current_user) }}
{% endif %}
{% from "macros/topics.html" import render_topics %}
{{ render_topics(topics, current_user) }}
{% endif %}
</div>
<aside class="col-md-3">
<p class="text-muted">Filter by tags</p>
{% for pair in tags %}
{% set count = pair[0] %}
{% set tag = pair[1] %}
{% if tag in selected_tags %}
<a class="badge badge-primary"
href="{{ url_set_query(page=1, _remove={ 'tag': tag.name }) }}">
{{ tag.title }}
({{ count }})
</a>
{% else %}
<a class="badge badge-secondary"
href="{{ url_set_query(page=1, _add={ 'tag': tag.name }) }}">
{{ tag.title }}
({{ count }})
</a>
{% endif %}
{% endfor %}
</aside>
</div>
{% endblock %}

@ -22,6 +22,7 @@ from .models import *
from . import app
import random, string, os, imghdr
from urllib.parse import urljoin
from werkzeug.datastructures import MultiDict
# These are given to Jinja in template_filters.py
@ -33,9 +34,26 @@ def abs_url(path):
return urljoin(app.config["BASE_URL"], path)
def url_set_query(**kwargs):
args = dict(request.args)
args.update(kwargs)
return url_for(request.endpoint, **args)
args = MultiDict(request.args)
for key, value in kwargs.items():
if key == "_add":
for key2, value_to_add in value.items():
values = set(args.getlist(key2))
values.add(value_to_add)
args.setlist(key2, list(values))
elif key == "_remove":
for key2, value_to_remove in value.items():
values = set(args.getlist(key2))
values.discard(value_to_remove)
args.setlist(key2, list(values))
else:
args.setlist(key, [ value ])
dargs = dict(args.lists())
return url_for(request.endpoint, **dargs)
def get_int_or_abort(v, default=None):
if v is None: