Improve related packages on package page

Fixes #254
This commit is contained in:
rubenwardy 2021-07-25 03:54:08 +01:00
parent fc7739be2c
commit cb5fa4d6e7
4 changed files with 72 additions and 45 deletions

@ -18,8 +18,7 @@
from urllib.parse import quote as urlescape
import flask_menu as menu
from celery import uuid
from flask import render_template, flash
from flask import render_template
from flask_wtf import FlaskForm
from flask_login import login_required
from sqlalchemy import or_, func
@ -30,11 +29,12 @@ from wtforms.validators import *
from app.querybuilder import QueryBuilder
from app.rediscache import has_key, set_key
from app.tasks.importtasks import importRepoScreenshot, checkZipRelease
from app.tasks.importtasks import importRepoScreenshot
from app.utils import *
from . import bp, get_package_tabs
from ...logic.LogicError import LogicError
from ...logic.packages import do_edit_package
from app.models.packages import PackageProvides
@menu.register_menu(bp, ".mods", "Mods", order=11, endpoint_arguments_constructor=lambda: { 'type': 'mod' })
@ -118,26 +118,36 @@ def getReleases(package):
@bp.route("/packages/<author>/<name>/")
@is_package_page
def view(package):
alternatives = None
if package.type == PackageType.MOD:
alternatives = Package.query \
.filter_by(name=package.name, type=PackageType.MOD) \
.filter(Package.id != package.id, Package.state!=PackageState.DELETED) \
.order_by(db.desc(Package.score)) \
.all()
show_similar = not package.approved and (
current_user in package.maintainers or
package.checkPerm(current_user, Permission.APPROVE_NEW))
packages_modnames = None
similar_topics = None
if show_similar and package.type != PackageType.TXP:
packages_modnames = Package.query.filter(Package.id != package.id,
Package.state != PackageState.DELETED) \
.filter(Package.provides.any(PackageProvides.c.metapackage_id.in_([p.id for p in package.provides]))) \
.order_by(db.desc(Package.score)) \
.all()
show_similar_topics = current_user == package.author or \
package.checkPerm(current_user, Permission.APPROVE_NEW)
similar_topics = None if not show_similar_topics else \
ForumTopic.query \
similar_topics = ForumTopic.query \
.filter_by(name=package.name) \
.filter(ForumTopic.topic_id != package.forums) \
.filter(~ db.exists().where(Package.forums==ForumTopic.topic_id)) \
.order_by(db.asc(ForumTopic.name), db.asc(ForumTopic.title)) \
.all()
packages_uses = None
if package.type == PackageType.MOD:
packages_uses = Package.query.filter(
Package.type == PackageType.MOD,
Package.id != package.id,
Package.state != PackageState.DELETED,
Package.dependencies.any(
Dependency.meta_package_id.in_([p.id for p in package.provides]))) \
.order_by(db.desc(Package.score)).limit(6).all()
releases = getReleases(package)
review_thread = package.review_thread
@ -172,8 +182,8 @@ def view(package):
has_review = current_user.is_authenticated and PackageReview.query.filter_by(package=package, author=current_user).count() > 0
return render_template("packages/view.html",
package=package, releases=releases,
alternatives=alternatives, similar_topics=similar_topics,
package=package, releases=releases, packages_uses=packages_uses,
packages_modnames=packages_modnames, similar_topics=similar_topics,
review_thread=review_thread, topic_error=topic_error, topic_error_lvl=topic_error_lvl,
threads=threads.all(), has_review=has_review)

@ -143,7 +143,7 @@ class PackagePropertyKey(enum.Enum):
return str(value)
provides = db.Table("provides",
PackageProvides = db.Table("provides",
db.Column("package_id", db.Integer, db.ForeignKey("package.id"), primary_key=True),
db.Column("metapackage_id", db.Integer, db.ForeignKey("meta_package.id"), primary_key=True)
)
@ -296,7 +296,7 @@ class Package(db.Model):
issueTracker = db.Column(db.String(200), nullable=True)
forums = db.Column(db.Integer, nullable=True)
provides = db.relationship("MetaPackage", secondary=provides, order_by=db.asc("name"), back_populates="packages")
provides = db.relationship("MetaPackage", secondary=PackageProvides, order_by=db.asc("name"), back_populates="packages")
dependencies = db.relationship("Dependency", back_populates="depender", lazy="dynamic", foreign_keys=[Dependency.depender_id])
@ -615,7 +615,7 @@ class MetaPackage(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
dependencies = db.relationship("Dependency", back_populates="meta_package", lazy="dynamic")
packages = db.relationship("Package", lazy="dynamic", back_populates="provides", secondary=provides)
packages = db.relationship("Package", lazy="dynamic", back_populates="provides", secondary=PackageProvides)
mp_name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$'")

@ -1,4 +1,4 @@
{% macro render_banners(package, current_user, topic_error, topic_error_lvl, similar_topics) -%}
{% macro render_banners(package, current_user, topic_error, topic_error_lvl, show_modname_warning) -%}
<div class="row mb-4">
<span class="col">
@ -82,13 +82,10 @@
</div>
{% endif %}
{% if similar_topics %}
{% if show_modname_warning %}
<div class="alert alert-warning">
Please make sure that this package has the right to
the name '{{ package.name }}'.
See the
<a href="/policy_and_guidance/">Inclusion Policy</a>
for more info.
Please make sure that this package has the right to all the technical names it provides.
See the <a href="/policy_and_guidance/">Inclusion Policy</a> for more info.
</div>
{% endif %}

@ -185,7 +185,7 @@
<section class="my-4 pb-3" style="border-bottom: 1px solid rgba(0,0,0,0.5);">
<div class="container">
{% from "macros/package_approval.html" import render_banners %}
{{ render_banners(package, current_user, topic_error, topic_error_lvl, similar_topics) }}
{{ render_banners(package, current_user, topic_error, topic_error_lvl, similar_topics or packages_modnames) }}
{% if review_thread and review_thread.checkPerm(current_user, "SEE_THREAD") %}
<h2>{% if review_thread.private %}&#x1f512;{% endif %} {{ review_thread.title }}</h2>
@ -265,26 +265,46 @@
{% endif %}
{{ render_reviews(package.reviews, current_user) }}
{% if alternatives %}
<h2>Related</h2>
{% if packages_uses %}
<h2>Used By</h2>
{% from "macros/packagegridtile.html" import render_pkggrid %}
{{ render_pkggrid(alternatives) }}
{{ render_pkggrid(packages_uses) }}
{% endif %}
{% if similar_topics %}
<h2>Similar Forum Topics</h2>
<ul>
{% for t in similar_topics %}
<li>
[{{ t.type.value }}]
<a href="https://forum.minetest.net/viewtopic.php?t={{ t.topic_id }}">
{{ t.title }} by {{ t.author.display_name }}
</a>
{% if t.wip %}[WIP]{% endif %}
</li>
{% endfor %}
</ul>
{% if packages_modnames or similar_topics %}
<h2>Modname uniqueness</h2>
{% if packages_modnames %}
<h3>Packages sharing provided mods</h3>
<p class="text-muted">
This package contains modnames that are present in the following packages:
</p>
<ul>
{% for pkg in packages_modnames %}
<li>
<a href="{{ pkg.getURL('packages.view') }}">
{{ _("%(title)s by %(author)s", title=pkg.title, author=pkg.author.display_name) }}
</a>
[{{ pkg.type.value }}]
</li>
{% endfor %}
</ul>
{% endif %}
{% if similar_topics %}
<h3>Similar Forum Topics</h3>
<ul>
{% for t in similar_topics %}
<li>
[{{ t.type.value }}]
<a href="https://forum.minetest.net/viewtopic.php?t={{ t.topic_id }}">
{{ t.title }} by {{ t.author.display_name }}
</a>
{% if t.wip %}[WIP]{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>