Collections: Add ability to pin to profile

This commit is contained in:
rubenwardy 2023-11-07 23:25:53 +00:00
parent 9b0f84bac5
commit bd46943c63
5 changed files with 75 additions and 36 deletions

@ -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")

@ -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/<username>/check-forums/", methods=["POST"])

@ -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]

@ -32,7 +32,20 @@
</div>
{{ render_field(form.short_description) }}
{{ render_checkbox_field(form.private, class_="my-3") }}
<div class="row my-5 gap-5">
<div class="col-md-auto">
{{ render_checkbox_field(form.private) }}
</div>
{% if form.pinned %}
<div class="col-md-auto">
{{ render_checkbox_field(form.pinned) }}
<p class="form-text mb-0">
{{ _("This requires the collection to be public") }}
</p>
</div>
{% endif %}
</div>
{% if collection %}
{{ render_field(form.long_description, fieldclass="form-control markdown") }}
{% endif %}

@ -213,41 +213,55 @@
</div>
{% endif %}
<div class="float-end">
{% if packages %}
<div class="btn-group btn-group-sm me-2" role="group" aria-label="Sorting">
<span class="btn btn-sm btn-primary">
{{ _("Alphabetical") }}
</span>
<a href="{{ url_for('packages.list_all', author=user.username, sort='downloads', order='desc') }}"
class="btn btn-sm btn-secondary">
{{ _("Downloads") }}
</a>
<a href="{{ url_for('packages.list_all', author=user.username, sort='approved_at', order='desc') }}"
class="btn btn-sm btn-secondary">
{{ _("Newest") }}
</a>
</div>
{% if current_user == user or (current_user.is_authenticated and current_user.rank.at_least(current_user.rank.EDITOR)) %}
<a class="btn btn-sm btn-secondary me-2"
href="{{ url_for('todo.tags', author=user.username) }}">
{{ _("View list of tags") }}
</a>
{% endif %}
{% endif %}
{% if current_user == user or user.check_perm(current_user, "CHANGE_AUTHOR") %}
<a class="btn btn-sm btn-primary"
href="{{ url_for('packages.create_edit', author=user.username) }}">
<i class="fas fa-plus me-1"></i>
{{ _("Create package") }}
</a>
{% endif %}
</div>
<h2 class="my-3">{{ _("Packages") }}</h2>
{% from "macros/packagegridtile.html" import render_pkggrid %}
{{ render_pkggrid(packages, show_author=False) }}
{% for collection in pinned_collections %}
<section id="{{ collection.name }}" class="my-4">
<a class="float-end btn btn-primary btn-sm" href="{{ url_for('collections.view', author=collection.author.username, name=collection.name) }}">
View collection
</a>
<h2>{{ collection.title }}</h2>
{% set collection_packages = collection.packages | limit(4) %}
{{ render_pkggrid(collection_packages) }}
</section>
{% endfor %}
<section id="packages" class="my-4">
<div class="float-end">
{% if packages %}
<div class="btn-group btn-group-sm me-2" role="group" aria-label="Sorting">
<span class="btn btn-sm btn-primary">
{{ _("Alphabetical") }}
</span>
<a href="{{ url_for('packages.list_all', author=user.username, sort='downloads', order='desc') }}"
class="btn btn-sm btn-secondary">
{{ _("Downloads") }}
</a>
<a href="{{ url_for('packages.list_all', author=user.username, sort='approved_at', order='desc') }}"
class="btn btn-sm btn-secondary">
{{ _("Newest") }}
</a>
</div>
{% if current_user == user or (current_user.is_authenticated and current_user.rank.at_least(current_user.rank.EDITOR)) %}
<a class="btn btn-sm btn-secondary me-2"
href="{{ url_for('todo.tags', author=user.username) }}">
{{ _("View list of tags") }}
</a>
{% endif %}
{% endif %}
{% if current_user == user or user.check_perm(current_user, "CHANGE_AUTHOR") %}
<a class="btn btn-sm btn-primary"
href="{{ url_for('packages.create_edit', author=user.username) }}">
<i class="fas fa-plus me-1"></i>
{{ _("Create package") }}
</a>
{% endif %}
</div>
<h2 class="my-3">{{ _("Packages") }}</h2>
{{ render_pkggrid(packages, show_author=False) }}
</section>
{% if maintained_packages %}