Skip to content

Commit

Permalink
Add filter to hide nonfree content
Browse files Browse the repository at this point in the history
Fixes #121
  • Loading branch information
rubenwardy committed Dec 30, 2023
1 parent ca3436b commit 785c931
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
13 changes: 13 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/blueprints/packages/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

@bp.route("/packages/")
def list_all():
qb = QueryBuilder(request.args)
qb = QueryBuilder(request.args, cookies=True)
query = qb.build_package_query()
title = qb.title

Expand Down
4 changes: 2 additions & 2 deletions app/blueprints/todo/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
7 changes: 5 additions & 2 deletions app/querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,7 +55,7 @@ def noindex(self):
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")
Expand Down Expand Up @@ -120,6 +120,9 @@ def __init__(self, args):
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
Expand Down
5 changes: 3 additions & 2 deletions app/template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,8 +30,8 @@


@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
Expand Down
9 changes: 9 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 785c931

Please sign in to comment.