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 qb.show_discarded = True
topics = qb.buildTopicQuery().all() 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", \ return render_template("packages/list.html", \
title=title, packages=query.items, topics=topics, \ title=title, packages=query.items, pagination=query, \
query=search, tags=tags, type=type_name, \ query=search, tags=tags, selected_tags=selected_tags, type=type_name, \
authors=authors, packages_count=query.total, \ authors=authors, packages_count=query.total, topics=topics)
pagination=query)
def getReleases(package): def getReleases(package):

@ -19,7 +19,8 @@
</p> </p>
{% endif %} {% endif %}
<div class="row">
<div class="col-md-9">
{% from "macros/packagegridtile.html" import render_pkggrid %} {% from "macros/packagegridtile.html" import render_pkggrid %}
{{ render_pkggrid(packages) }} {{ render_pkggrid(packages) }}
@ -34,5 +35,32 @@
{% from "macros/topics.html" import render_topics %} {% from "macros/topics.html" import render_topics %}
{{ render_topics(topics, current_user) }} {{ render_topics(topics, current_user) }}
{% endif %} {% 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 %} {% endblock %}

@ -22,6 +22,7 @@ from .models import *
from . import app from . import app
import random, string, os, imghdr import random, string, os, imghdr
from urllib.parse import urljoin from urllib.parse import urljoin
from werkzeug.datastructures import MultiDict
# These are given to Jinja in template_filters.py # These are given to Jinja in template_filters.py
@ -33,9 +34,26 @@ def abs_url(path):
return urljoin(app.config["BASE_URL"], path) return urljoin(app.config["BASE_URL"], path)
def url_set_query(**kwargs): def url_set_query(**kwargs):
args = dict(request.args) args = MultiDict(request.args)
args.update(kwargs)
return url_for(request.endpoint, **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): def get_int_or_abort(v, default=None):
if v is None: if v is None: