From bd46943c636154c2309f1db3569755f41b215e88 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 7 Nov 2023 23:25:53 +0000 Subject: [PATCH] Collections: Add ability to pin to profile --- app/blueprints/collections/__init__.py | 4 ++ app/blueprints/users/profile.py | 7 +- app/template_filters.py | 5 ++ app/templates/collections/create_edit.html | 15 +++- app/templates/users/profile.html | 80 +++++++++++++--------- 5 files changed, 75 insertions(+), 36 deletions(-) diff --git a/app/blueprints/collections/__init__.py b/app/blueprints/collections/__init__.py index 693a7d9e..6b6eca04 100644 --- a/app/blueprints/collections/__init__.py +++ b/app/blueprints/collections/__init__.py @@ -80,6 +80,7 @@ class CollectionForm(FlaskForm): short_description = StringField(lazy_gettext("Short Description"), [Optional(), Length(0, 200)]) long_description = TextAreaField(lazy_gettext("Page Content"), [Optional()], filters=[nonempty_or_none]) private = BooleanField(lazy_gettext("Private")) + pinned = BooleanField(lazy_gettext("Pinned to my profile")) descriptions = FieldList( StringField(lazy_gettext("Short Description"), [Optional(), Length(0, 500)], filters=[nonempty_or_none]), min_entries=0) @@ -122,6 +123,7 @@ def create_edit(author=None, name=None): if request.method == "GET": # HACK: fix bug in wtforms form.private.data = collection.private if collection else False + form.pinned.data = collection.pinned if collection else False if collection: for item in collection.items: form.descriptions.append_entry(item.description) @@ -129,6 +131,7 @@ def create_edit(author=None, name=None): form.package_removed.append_entry("0") else: form.name = None + form.pinned = None if form.validate_on_submit(): ret = handle_create_edit(collection, form, initial_packages, author) @@ -319,6 +322,7 @@ def package_add(package): @login_required def package_toggle_favorite(package): collection, _is_new = get_or_create_favorites(db.session) + collection.author = current_user if toggle_package(collection, package): msg = gettext("Added package to favorites collection") diff --git a/app/blueprints/users/profile.py b/app/blueprints/users/profile.py index 4dd0e764..cb18fae8 100644 --- a/app/blueprints/users/profile.py +++ b/app/blueprints/users/profile.py @@ -22,7 +22,7 @@ from flask_babel import gettext from flask_login import current_user, login_required from sqlalchemy import func, text -from app.models import User, db, Package, PackageReview, PackageState, PackageType, UserRank +from app.models import User, db, Package, PackageReview, PackageState, PackageType, UserRank, Collection from app.utils import get_daterange_options from app.tasks.forumtasks import check_forum_account @@ -230,11 +230,14 @@ def profile(username): .filter(Package.author != user) \ .order_by(db.asc(Package.title)).all() + pinned_collections = user.collections.filter(Collection.private == False, + Collection.pinned == True, Collection.packages.any()).all() + unlocked, locked = get_user_medals(user) # Process GET or invalid POST return render_template("users/profile.html", user=user, packages=packages, maintained_packages=maintained_packages, - medals_unlocked=unlocked, medals_locked=locked) + medals_unlocked=unlocked, medals_locked=locked, pinned_collections=pinned_collections) @bp.route("/users//check-forums/", methods=["POST"]) diff --git a/app/template_filters.py b/app/template_filters.py index 87a38de0..b423628a 100644 --- a/app/template_filters.py +++ b/app/template_filters.py @@ -101,3 +101,8 @@ def timedelta(value): @app.template_filter() def abs_url(url): return utils.abs_url(url) + + +@app.template_filter() +def limit(arr, num): + return arr[:num] diff --git a/app/templates/collections/create_edit.html b/app/templates/collections/create_edit.html index b040dc61..a7b24317 100644 --- a/app/templates/collections/create_edit.html +++ b/app/templates/collections/create_edit.html @@ -32,7 +32,20 @@ {{ render_field(form.short_description) }} - {{ render_checkbox_field(form.private, class_="my-3") }} + +
+
+ {{ render_checkbox_field(form.private) }} +
+ {% if form.pinned %} +
+ {{ render_checkbox_field(form.pinned) }} +

+ {{ _("This requires the collection to be public") }} +

+
+ {% endif %} +
{% if collection %} {{ render_field(form.long_description, fieldclass="form-control markdown") }} {% endif %} diff --git a/app/templates/users/profile.html b/app/templates/users/profile.html index 469799ea..5c2ee112 100644 --- a/app/templates/users/profile.html +++ b/app/templates/users/profile.html @@ -213,41 +213,55 @@ {% endif %} -
- {% if packages %} -
- - {{ _("Alphabetical") }} - - - {{ _("Downloads") }} - - - {{ _("Newest") }} - -
- {% if current_user == user or (current_user.is_authenticated and current_user.rank.at_least(current_user.rank.EDITOR)) %} - - {{ _("View list of tags") }} - - {% endif %} - {% endif %} - {% if current_user == user or user.check_perm(current_user, "CHANGE_AUTHOR") %} - - - {{ _("Create package") }} - - {% endif %} -
- -

{{ _("Packages") }}

{% from "macros/packagegridtile.html" import render_pkggrid %} -{{ render_pkggrid(packages, show_author=False) }} + +{% for collection in pinned_collections %} +
+ + View collection + +

{{ collection.title }}

+ {% set collection_packages = collection.packages | limit(4) %} + {{ render_pkggrid(collection_packages) }} +
+{% endfor %} + +
+
+ {% if packages %} +
+ + {{ _("Alphabetical") }} + + + {{ _("Downloads") }} + + + {{ _("Newest") }} + +
+ {% if current_user == user or (current_user.is_authenticated and current_user.rank.at_least(current_user.rank.EDITOR)) %} + + {{ _("View list of tags") }} + + {% endif %} + {% endif %} + {% if current_user == user or user.check_perm(current_user, "CHANGE_AUTHOR") %} + + + {{ _("Create package") }} + + {% endif %} +
+ +

{{ _("Packages") }}

+ {{ render_pkggrid(packages, show_author=False) }} +
{% if maintained_packages %}