Add packages API

This commit is contained in:
rubenwardy 2018-03-23 17:33:34 +00:00
parent 2a836c1000
commit 269c8c0eb4
4 changed files with 43 additions and 12 deletions

@ -127,6 +127,18 @@ class Package(db.Model):
releases = db.relationship("PackageRelease", backref="package",
lazy="dynamic", order_by=db.desc("package_release_releaseDate"))
def getAsDictionary(self, base_url):
return {
"name": self.name,
"title": self.title,
"author": self.author.display_name,
"shortDesc": self.shortDesc,
"type": self.type.toName(),
"repo": self.repo,
"url": base_url + self.getDownloadURL(),
"screenshots": [ base_url + self.getMainScreenshotURL() ]
}
def getDetailsURL(self):
return url_for("package_page",
type=self.type.toName(),

@ -4,14 +4,12 @@ from flask.ext import menu
from app import app
from app.models import *
from .utils import *
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.validators import *
def isFilenameAllowed(filename, exts):
return "." in filename and \
filename.rsplit(".", 1)[1].lower() in exts
# TODO: the following could be made into one route, except I"m not sure how
# to do the menu
@ -28,11 +26,19 @@ def doPackageList(type):
if search is not None:
query = query.filter(Package.title.contains(search))
return render_template("packages/list.html", title=title, packages=query.all(), query=search)
if shouldReturnJson():
return jsonify([package.getAsDictionary(request.url_root) for package in query.all()])
else:
return render_template("packages/list.html", title=title, packages=query.all(), query=search)
@app.route("/packages/")
def packages_page():
return doPackageList(None)
type = None
typeStr = request.args.get("type")
if typeStr is not None:
type = PackageType[typeStr.upper()]
return doPackageList(type)
@app.route("/mods/")
@menu.register_menu(app, ".mods", "Mods", order=11)
@ -96,9 +102,13 @@ def getReleases(package):
@app.route("/<type>s/<author>/<name>/")
def package_page(type, author, name):
package = getPageByInfo(type, author, name)
releases = getReleases(package)
return render_template("packages/view.html", package=package, releases=releases)
if shouldReturnJson():
return jsonify(package.getAsDictionary(request.url_root))
else:
releases = getReleases(package)
return render_template("packages/view.html", package=package, releases=releases)
@app.route("/<type>s/<author>/<name>/download/")
def package_download_page(type, author, name):
@ -106,10 +116,8 @@ def package_download_page(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:
if "application/zip" in request.accept_mimetypes and \
not "text/html" in request.accept_mimetypes:
return "", 204
else:
flash("No download available.", "error")

9
app/views/utils.py Normal file

@ -0,0 +1,9 @@
from flask import request
def isFilenameAllowed(filename, exts):
return "." in filename and \
filename.rsplit(".", 1)[1].lower() in exts
def shouldReturnJson():
return "application/json" in request.accept_mimetypes and \
not "text/html" in request.accept_mimetypes

@ -8,4 +8,6 @@ SQLALCHEMY_DATABASE_URI = "sqlite:///../db.sqlite"
GITHUB_CLIENT_ID = ""
GITHUB_CLIENT_SECRET = ""
BASE_URL="http://localhost:3000/"
UPLOAD_FOLDER="tmp"