mirror of
https://github.com/minetest/contentdb.git
synced 2024-12-23 06:22:24 +01:00
Add badges/shields support
This commit is contained in:
parent
ce034fddd4
commit
9a64809542
@ -33,6 +33,7 @@ from wtforms.validators import *
|
||||
from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
|
||||
from sqlalchemy import or_, func
|
||||
from sqlalchemy.orm import joinedload, subqueryload
|
||||
from urllib.parse import quote as urlescape
|
||||
|
||||
from celery import uuid
|
||||
|
||||
@ -182,6 +183,24 @@ def view(package):
|
||||
threads=threads.all(), has_review=has_review)
|
||||
|
||||
|
||||
@bp.route("/packages/<author>/<name>/shields/<type>/")
|
||||
@is_package_page
|
||||
def shield(package, type):
|
||||
if type == "title":
|
||||
url = "https://img.shields.io/badge/ContentDB-{}-{}" \
|
||||
.format(urlescape(package.title), urlescape("#375a7f"))
|
||||
elif type == "downloads":
|
||||
#api_url = abs_url_for("api.package", author=package.author.username, name=package.name)
|
||||
api_url = "https://content.minetest.net" + url_for("api.package", author=package.author.username, name=package.name)
|
||||
url = "https://img.shields.io/badge/dynamic/json?color={}&label=ContentDB&query=downloads&suffix=+downloads&url={}" \
|
||||
.format(urlescape("#375a7f"), urlescape(api_url))
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
return redirect(url)
|
||||
|
||||
|
||||
|
||||
@bp.route("/packages/<author>/<name>/download/")
|
||||
@is_package_page
|
||||
def download(package):
|
||||
|
@ -658,6 +658,15 @@ class Package(db.Model):
|
||||
return url_for("packages.view",
|
||||
author=self.author.username, name=self.name)
|
||||
|
||||
def getShieldURL(self, type):
|
||||
from app.utils import abs_url_for
|
||||
return abs_url_for("packages.shield",
|
||||
author=self.author.username, name=self.name, type=type)
|
||||
|
||||
def makeShield(self, type):
|
||||
return "[![ContentDB]({})]({})" \
|
||||
.format(self.getShieldURL(type), self.getDetailsURL(True))
|
||||
|
||||
def getEditURL(self):
|
||||
return url_for("packages.create_edit",
|
||||
author=self.author.username, name=self.name)
|
||||
|
@ -530,5 +530,25 @@
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% if package.checkPerm(current_user, "EDIT_PACKAGE") %}
|
||||
<h3>Badges</h3>
|
||||
|
||||
<p>
|
||||
{{ package.makeShield("title") | markdown }}
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<pre><code>{{ package.makeShield("title") }}</code></pre>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
{{ package.makeShield("downloads") | markdown }}
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<pre><code>{{ package.makeShield("downloads") }}</code></pre>
|
||||
</p>
|
||||
{% endif %}
|
||||
</main>
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user