Improve thread list appearance

This commit is contained in:
rubenwardy
2020-12-22 12:22:52 +00:00
parent 1064885a2c
commit df79159e2e
7 changed files with 108 additions and 45 deletions

@ -39,9 +39,16 @@ def list_all():
pid = get_int_or_abort(pid) pid = get_int_or_abort(pid)
query = query.filter_by(package_id=pid) query = query.filter_by(package_id=pid)
query = query.filter_by(review_id=None)
query = query.order_by(db.desc(Thread.created_at)) query = query.order_by(db.desc(Thread.created_at))
return render_template("threads/list.html", threads=query.all()) page = get_int_or_abort(request.args.get("page"), 1)
num = min(40, get_int_or_abort(request.args.get("n"), 100))
pagination = query.paginate(page, num, True)
return render_template("threads/list.html", pagination=pagination, threads=pagination.items)
@bp.route("/threads/<int:id>/subscribe/", methods=["POST"]) @bp.route("/threads/<int:id>/subscribe/", methods=["POST"])

@ -91,6 +91,9 @@ class Thread(db.Model):
else: else:
raise Exception("Permission {} is not related to threads".format(perm.name)) raise Exception("Permission {} is not related to threads".format(perm.name))
def get_latest_reply(self):
return ThreadReply.query.filter_by(thread_id=self.id).order_by(db.desc(ThreadReply.id)).first()
class ThreadReply(db.Model): class ThreadReply(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)

@ -4,6 +4,7 @@ from .utils import abs_url_for, url_set_query
from flask_login import current_user from flask_login import current_user
from flask_babel import format_timedelta from flask_babel import format_timedelta
from urllib.parse import urlparse from urllib.parse import urlparse
from datetime import datetime as dt
@app.context_processor @app.context_processor
def inject_debug(): def inject_debug():
@ -37,7 +38,11 @@ def date(value):
@app.template_filter() @app.template_filter()
def datetime(value): def datetime(value):
return value.strftime("%Y-%m-%d %H:%M") + " UTC" delta = dt.utcnow() - value
if delta.days == 0:
return format_timedelta(value)
else:
return value.strftime("%Y-%m-%d %H:%M") + " UTC"
@app.template_filter() @app.template_filter()
def timedelta(value): def timedelta(value):

@ -95,46 +95,90 @@
{% endif %} {% endif %}
{% endmacro %} {% endmacro %}
{% macro render_threadlist(threads, compact=False) -%} {% macro render_compact_threadlist(threads) -%}
{% for t in threads %} {% for t in threads %}
<a class="list-group-item list-group-item-action" <a class="list-group-item list-group-item-action"
href="{{ url_for('threads.view', id=t.id) }}"> href="{{ url_for('threads.view', id=t.id) }}">
{% if compact %} {% if t.private %}&#x1f512; {% endif %}
{% if t.private %}&#x1f512; {% endif %} <strong>{{ t.title }}</strong>
<strong>{{ t.title }}</strong> by {{ t.author.display_name }}
by {{ t.author.display_name }} </a>
{% else %} {% else %}
<div class="row"> <p class="list-group-item"><i>No threads found</i></p>
<div class="col-sm"> {% endfor %}
<span class="mr-3"> {% endmacro %}
{% if not t.review and t.private %}
<i class="fas fa-lock" style="color:#ffac33;"></i> {% macro render_threadlist(threads) -%}
{% elif not t.review %} <div class="list-group-item">
<i class="fas fa-comment-alt" style="color:#666;"></i> <div class="row text-muted">
{% elif t.review.recommends %} <span class="col-md">
<i class="fas fa-thumbs-up" style="color:#6f6;"></i> {{ _("Thread") }}
{% else %} </span>
<i class="fas fa-thumbs-down" style="color:#f66;"></i>
{% endif %} <span class="col-md">
</span> {{ _("Last Reply") }}
</span>
<strong>{{ t.title }}</strong>
by {{ t.author.display_name }} <span class="col-md-2"></span>
</div> </div>
</div>
<div class="col-sm">
{% if t.package %} {% for t in threads %}
{{ _("%(title)s by %(author)s", {% set replies = t.replies.count() - 1 %}
title="<b>" | safe + t.package.title + "</b>" | safe,
author=t.package.author.display_name) }} <a class="list-group-item list-group-item-action"
{% endif %} href="{{ url_for('threads.view', id=t.id) }}">
</div> <div class="row">
<div class="col-md">
<div class="col-sm-auto text-muted text-right"> {% if not t.review and t.private %}
{{ t.created_at | datetime }} <i class="fas fa-lock" style="color:#ffac33;"></i>
</div> {% elif not t.review %}
</div> <i class="fas fa-comment-alt" style="color:#666;"></i>
{% endif %} {% elif t.review.recommends %}
<i class="fas fa-thumbs-up" style="color:#6f6;"></i>
{% else %}
<i class="fas fa-thumbs-down" style="color:#f66;"></i>
{% endif %}
<strong class="ml-1">
{{ t.title }}
</strong><br />
<span>
{{ t.author.display_name }}
</span>
<span class="text-muted ml-3">
{{ t.created_at | datetime }}
</span>
<span class="text-muted ml-3">
{{ replies }}
<i class="fas fa-comment ml-1"></i>
</span>
</div>
<div class="col-md">
{% if replies > 0 %}
{% set latest = t.get_latest_reply() %}
<span>
{{ latest.author.display_name }}
</span><br />
<span class="text-muted">
{{ latest.created_at | datetime }}
</span>
{% endif %}
</div>
{% if t.package %}
<div class="col-md-2 text-muted text-right">
<img
class="img-fluid"
style="max-height: 22px; max-width: 22px;"
src="{{ t.package.getThumbnailURL(1) }}" /><br />
<span class="pl-2">
{{ t.package.title }}
</span>
</div>
{% endif %}
</div>
</a> </a>
{% else %} {% else %}
<p class="list-group-item"><i>No threads found</i></p> <p class="list-group-item"><i>No threads found</i></p>

@ -6,11 +6,9 @@
{% block content %} {% block content %}
{% from "macros/pagination.html" import render_pagination %} {% from "macros/pagination.html" import render_pagination %}
{{ render_pagination(pagination, url_set_query) }}
{% from "macros/reviews.html" import render_reviews %} {% from "macros/reviews.html" import render_reviews %}
{{ render_reviews(reviews, current_user, True) }}
{% from "macros/pagination.html" import render_pagination %} {{ render_pagination(pagination, url_set_query) }}
{{ render_reviews(reviews, current_user, True) }}
{{ render_pagination(pagination, url_set_query) }} {{ render_pagination(pagination, url_set_query) }}
{% endblock %} {% endblock %}

@ -365,8 +365,8 @@
Threads Threads
</div> </div>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
{% from "macros/threads.html" import render_threadlist %} {% from "macros/threads.html" import render_compact_threadlist %}
{{ render_threadlist(threads, compact=True) }} {{ render_compact_threadlist(threads) }}
</ul> </ul>
</div> </div>

@ -7,8 +7,14 @@ Threads
{% block content %} {% block content %}
<h1>Threads</h1> <h1>Threads</h1>
{% from "macros/pagination.html" import render_pagination %}
{% from "macros/threads.html" import render_threadlist %} {% from "macros/threads.html" import render_threadlist %}
{{ render_pagination(pagination, url_set_query) }}
<div class="list-group"> <div class="list-group">
{{ render_threadlist(threads) }} {{ render_threadlist(threads) }}
</div> </div>
{{ render_pagination(pagination, url_set_query) }}
{% endblock %} {% endblock %}