From 785c931890cc6c4a372af18925c329a259b5e5b2 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 30 Dec 2023 18:07:00 +0000 Subject: [PATCH] Add filter to hide nonfree content Fixes #121 --- app/__init__.py | 13 +++++++++++++ app/blueprints/packages/packages.py | 2 +- app/blueprints/todo/editor.py | 4 ++-- app/querybuilder.py | 7 +++++-- app/template_filters.py | 5 +++-- app/templates/base.html | 9 +++++++++ 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 939ebb6b..e4360853 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py index a6ccef39..ab96e966 100644 --- a/app/blueprints/packages/packages.py +++ b/app/blueprints/packages/packages.py @@ -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 diff --git a/app/blueprints/todo/editor.py b/app/blueprints/todo/editor.py index 96ed6754..a2a12999 100644 --- a/app/blueprints/todo/editor.py +++ b/app/blueprints/todo/editor.py @@ -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() diff --git a/app/querybuilder.py b/app/querybuilder.py index 143a7418..50357cc5 100644 --- a/app/querybuilder.py +++ b/app/querybuilder.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -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 diff --git a/app/template_filters.py b/app/template_filters.py index af170b0f..e3ff6e72 100644 --- a/app/template_filters.py +++ b/app/template_filters.py @@ -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 diff --git a/app/templates/base.html b/app/templates/base.html index 3c3de58f..32916b9c 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -269,6 +269,15 @@
  • {{ _("Source Code") }}
  • +
    + + {% if hide_nonfree %} + + {% else %} + + {% endif %} +
    + {% if debug %}

    DEBUG MODE ENABLED