Add download button and URL

This commit is contained in:
rubenwardy 2018-03-23 16:20:56 +00:00
parent 570dd519df
commit 5a9fc51ffc
3 changed files with 30 additions and 0 deletions

@ -147,6 +147,18 @@ class Package(db.Model):
type=self.type.toName(),
author=self.author.username, name=self.name)
def getDownloadURL(self):
return url_for("package_download_page",
type=self.type.toName(),
author=self.author.username, name=self.name)
def getDownloadRelease(self):
for rel in self.releases:
if rel.approved:
return rel
return None
def checkPerm(self, user, perm):
if not user.is_authenticated:
return False

@ -39,6 +39,7 @@
</table>
<ul class="buttonset linedbuttonset">
{% if package.getDownloadRelease() %}<li><a href="{{ package.getDownloadURL() }}">Download</a></li>{% endif %}
{% if package.repo %}<li><a href="{{ package.repo }}">View Source</a></li>{% endif %}
{% if package.forums %}<li><a href="https://forum.minetest.net/viewtopic.php?t={{ package.forums }}">Forums</a></li>{% endif %}
{% if package.issueTracker %}<li><a href="{{ package.issueTracker }}">Issue Tracker</a></li>{% endif %}

@ -96,6 +96,23 @@ def package_page(type, author, name):
return render_template("packages/view.html", package=package, releases=releases)
@app.route("/<type>s/<author>/<name>/download/")
def package_download_page(type, author, name):
package = getPageByInfo(type, author, name)
release = package.getDownloadRelease()
if release is None:
wantsJson = "application/zip" in request.accept_mimetypes and \
not "text/html" in request.accept_mimetypes
if wantsJson:
return "", 204
else:
flash("No download available.", "error")
return redirect(package.getDetailsURL())
else:
return redirect(release.url, code=302)
class PackageForm(FlaskForm):
name = StringField("Name", [InputRequired(), Length(1, 20), Regexp("^[a-z0-9_]", 0, "Lower case letters (a-z), digits (0-9), and underscores (_) only")])