Add list of relevant forum topics to last page of results

This commit is contained in:
rubenwardy 2018-08-25 18:20:45 +01:00
parent b296b9b299
commit 36000b1592
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
7 changed files with 83 additions and 25 deletions

@ -133,7 +133,7 @@ def parseForumListPage(id, page, out, extra=None):
out[id] = row
return True
return False
def getTopicsFromForum(id, out={}, extra=None):
print("Fetching all topics from forum {}".format(id))

@ -1,4 +1,4 @@
{% macro render_topictable(topics, show_author=True) -%}
{% macro render_topics_table(topics, show_author=True) -%}
<table>
<tr>
<th>Id</th>
@ -31,3 +31,22 @@
{% endfor %}
</table>
{% endmacro %}
{% macro render_topics(topics, current_user, show_author=True) -%}
<ul>
{% for topic in topics %}
<li{% if topic.wip %} class="wiptopic"{% endif %}>
<a href="https://forum.minetest.net/viewtopic.php?t={{ topic.topic_id}}">{{ topic.title }}</a>
{% if topic.wip %}[WIP]{% endif %}
{% if topic.name %}[{{ topic.name }}]{% endif %}
{% if show_author %}
by <a href="{{ url_for('user_profile_page', username=topic.author.username) }}">{{ topic.author.display_name }}</a>
{% endif %}
{% if not topic.author.checkPerm(current_user, "CHANGE_AUTHOR") %}
<a href="{{ url_for('create_edit_package_page', author=topic.author.username, repo=topic.getRepoURL(), forums=topic.topic_id, title=topic.title, bname=topic.name) }}">Create</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% endmacro %}

@ -37,4 +37,12 @@
<li>{{ page }} / {{ page_max }}</li>
{% if next_url %}<li><a href="{{ next_url }}">Next</a></li> {% endif %}
</ul>
{% if topics %}
<h2 style="margin-top:2em;">More content from the forums</h2>
{% from "macros/topics.html" import render_topics %}
{{ render_topics(topics, current_user) }}
{% endif %}
{% endblock %}

@ -12,6 +12,6 @@ Topics to be Added
{{ topics | count }} remaining.
</p>
{% from "macros/topictable.html" import render_topictable %}
{{ render_topictable(topics) }}
{% from "macros/topics.html" import render_topics_table %}
{{ render_topics_table(topics) }}
{% endblock %}

@ -107,8 +107,8 @@
List of your forum topics which do not have a matching package.
</p>
{% from "macros/topictable.html" import render_topictable %}
{{ render_topictable(topics_to_add, show_author=False) }}
{% from "macros/topics.html" import render_topics_table %}
{{ render_topics_table(topics_to_add, show_author=False) }}
</div>
</div>
{% endif %}

@ -20,11 +20,13 @@ from flask_user import *
from app import app
from app.models import *
from app.utils import is_package_page
from .packages import build_packages_query
from .packages import QueryBuilder
@app.route("/api/packages/")
def api_packages_page():
query, _ = build_packages_query()
qb = QueryBuilder()
query = qb.buildPackageQuery()
pkgs = [package.getAsDictionaryShort(app.config["BASE_URL"]) \
for package in query.all() if package.getDownloadRelease() is not None]
return jsonify(pkgs)

@ -31,28 +31,39 @@ from wtforms.validators import *
from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
from sqlalchemy import or_, any_
def build_packages_query():
title = "Packages"
query = Package.query.filter_by(soft_deleted=False, approved=True)
class QueryBuilder:
title = None
types = None
search = None
# Filter by requested type(s)
types = request.args.getlist("type")
types = [PackageType.get(tname) for tname in types]
types = [type for type in types if type is not None]
if len(types) > 0:
title = ", ".join([type.value + "s" for type in types])
def __init__(self):
title = "Packages"
query = query.filter(Package.type.in_(types))
# Get request types
types = request.args.getlist("type")
types = [PackageType.get(tname) for tname in types]
types = [type for type in types if type is not None]
if len(types) > 0:
title = ", ".join([type.value + "s" for type in types])
self.title = title
self.types = types
self.search = request.args.get("q")
search = request.args.get("q")
if search is not None and search.strip() != "":
query = query.filter(Package.title.ilike('%' + search + '%'))
def buildPackageQuery(self):
query = Package.query.filter_by(soft_deleted=False, approved=True)
query = query.order_by(db.desc(Package.score))
if len(self.types) > 0:
query = query.filter(Package.type.in_(self.types))
return query, title
if self.search is not None and self.search.strip() != "":
query = query.filter(Package.title.ilike('%' + self.search + '%'))
query = query.order_by(db.desc(Package.score))
return query
@menu.register_menu(app, ".mods", "Mods", order=11, endpoint_arguments_constructor=lambda: { 'type': 'mod' })
@menu.register_menu(app, ".games", "Games", order=12, endpoint_arguments_constructor=lambda: { 'type': 'game' })
@ -62,7 +73,10 @@ def packages_page():
if shouldReturnJson():
return redirect(url_for("api_packages_page"))
query, title = build_packages_query()
qb = QueryBuilder()
query = qb.buildPackageQuery()
title = qb.title
page = int(request.args.get("page") or 1)
num = min(42, int(request.args.get("n") or 100))
query = query.paginate(page, num, True)
@ -75,8 +89,23 @@ def packages_page():
prev_url = url_for("packages_page", type=type_name, q=search, page=query.prev_num) \
if query.has_prev else None
topics = None
search = request.args.get("q")
if search and not query.has_next:
topics = ForumTopic.query \
.filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
.order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title)) \
.filter(ForumTopic.title.ilike('%' + search + '%'))
if len(qb.types) > 0:
topics = topics.filter(ForumTopic.type.in_(qb.types))
topics = topics.all()
tags = Tag.query.all()
return render_template("packages/list.html", title=title, packages=query.items, \
return render_template("packages/list.html", \
title=title, packages=query.items, topics=topics, \
query=search, tags=tags, type=type_name, \
next_url=next_url, prev_url=prev_url, page=page, page_max=query.pages, packages_count=query.total)