Add filter to hide nonfree content

Fixes #121
This commit is contained in:
rubenwardy 2023-12-30 18:07:00 +00:00
parent ca3436be0c
commit 785c931890
6 changed files with 33 additions and 7 deletions

@ -198,3 +198,16 @@ def set_locale():
models.db.session.commit()
return resp
@app.route("/set-nonfree/", methods=["POST"])
def set_nonfree():
resp = redirect(url_for("homepage.home"))
if request.cookies.get("hide_nonfree") == "1":
resp.set_cookie("hide_nonfree", "0", expires=0)
else:
expire_date = datetime.datetime.now()
expire_date = expire_date + datetime.timedelta(days=5*365)
resp.set_cookie("hide_nonfree", "1", expires=expire_date)
return resp

@ -49,7 +49,7 @@ from app.utils import is_user_bot, get_int_or_abort, is_package_page, abs_url_fo
@bp.route("/packages/")
def list_all():
qb = QueryBuilder(request.args)
qb = QueryBuilder(request.args, cookies=True)
query = qb.build_package_query()
title = qb.title

@ -93,7 +93,7 @@ def view_editor():
@bp.route("/todo/topics/")
@login_required
def topics():
qb = QueryBuilder(request.args)
qb = QueryBuilder(request.args, cookies=True)
qb.set_sort_if_none("date")
query = qb.build_topic_query()
@ -125,7 +125,7 @@ def topics():
@bp.route("/todo/tags/")
@login_required
def tags():
qb = QueryBuilder(request.args)
qb = QueryBuilder(request.args, cookies=True)
qb.set_sort_if_none("score", "desc")
query = qb.build_package_query()

@ -14,7 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import abort, current_app
from flask import abort, current_app, request
from flask_babel import lazy_gettext
from sqlalchemy import or_
from sqlalchemy.orm import subqueryload
@ -55,7 +55,7 @@ class QueryBuilder:
return (self.search is not None or len(self.tags) > 1 or len(self.types) > 1 or len(self.hide_flags) > 0 or
self.random or self.lucky or self.author or self.version or self.game)
def __init__(self, args):
def __init__(self, args, cookies=False):
# Get request types
types = args.getlist("type")
@ -120,6 +120,9 @@ class QueryBuilder:
if self.game:
self.game = Package.get_by_key(self.game)
if cookies and request.cookies.get("hide_nonfree") == "1":
self.hide_nonfree = True
def set_sort_if_none(self, name, dir="desc"):
if self.order_by is None:
self.order_by = name

@ -17,6 +17,7 @@
from datetime import datetime as dt
from urllib.parse import urlparse
from flask import request
from flask_babel import format_timedelta, gettext
from flask_login import current_user
from markupsafe import Markup
@ -29,8 +30,8 @@ from .utils.minetest_hypertext import normalize_whitespace as do_normalize_white
@app.context_processor
def inject_debug():
return dict(debug=app.debug)
def inject_misc():
return dict(debug=app.debug, hide_nonfree=request.cookies.get("hide_nonfree") == "1")
@app.context_processor

@ -269,6 +269,15 @@
<li class="list-inline-item"><a href="https://github.com/minetest/contentdb">{{ _("Source Code") }}</a></li>
</ul>
<form method="POST" action="{{ url_for('set_nonfree') }}" class="my-3">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
{% if hide_nonfree %}
<input type="submit" class="btn btn-sm btn-secondary" value="{{ _('Show non-free packages') }}">
{% else %}
<input type="submit" class="btn btn-sm btn-secondary" value="{{ _('Hide non-free packages') }}">
{% endif %}
</form>
{% if debug %}
<p style="color: red">
DEBUG MODE ENABLED