Skip to content

Commit

Permalink
Collections: Add ability to pin to profile
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Nov 7, 2023
1 parent 9b0f84b commit bd46943
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 36 deletions.
4 changes: 4 additions & 0 deletions app/blueprints/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -122,13 +123,15 @@ 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)
form.package_ids.append_entry(item.package.get_id())
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)
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions app/blueprints/users/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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

Expand Down Expand Up @@ -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"])
Expand Down
5 changes: 5 additions & 0 deletions app/template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
15 changes: 14 additions & 1 deletion app/templates/collections/create_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@ <h1>{{ self.title() }}</h1>
</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 %}
Expand Down
80 changes: 47 additions & 33 deletions app/templates/users/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,41 +213,55 @@ <h5 class="mt-0">
</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 %}
Expand Down

0 comments on commit bd46943

Please sign in to comment.