Add badges/shields support

This commit is contained in:
rubenwardy 2020-08-02 17:41:06 +01:00
parent ce034fddd4
commit 9a64809542
3 changed files with 48 additions and 0 deletions

@ -33,6 +33,7 @@ from wtforms.validators import *
from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
from sqlalchemy import or_, func from sqlalchemy import or_, func
from sqlalchemy.orm import joinedload, subqueryload from sqlalchemy.orm import joinedload, subqueryload
from urllib.parse import quote as urlescape
from celery import uuid from celery import uuid
@ -182,6 +183,24 @@ def view(package):
threads=threads.all(), has_review=has_review) 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/") @bp.route("/packages/<author>/<name>/download/")
@is_package_page @is_package_page
def download(package): def download(package):

@ -658,6 +658,15 @@ class Package(db.Model):
return url_for("packages.view", return url_for("packages.view",
author=self.author.username, name=self.name) 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): def getEditURL(self):
return url_for("packages.create_edit", return url_for("packages.create_edit",
author=self.author.username, name=self.name) author=self.author.username, name=self.name)

@ -530,5 +530,25 @@
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% 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> </main>
{% endblock %} {% endblock %}