Move API to dedicated file, and reduce download size

This commit is contained in:
rubenwardy 2018-06-15 22:57:43 +01:00
parent 87af23248e
commit cfa7654efc
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
4 changed files with 113 additions and 58 deletions

View File

@ -376,7 +376,7 @@ class Package(db.Model):
for e in PackagePropertyKey:
setattr(self, e.name, getattr(package, e.name))
def getAsDictionary(self, base_url):
def getAsDictionaryShort(self, base_url):
tnurl = self.getThumbnailURL()
return {
"name": self.name,
@ -384,14 +384,37 @@ class Package(db.Model):
"author": self.author.display_name,
"shortDesc": self.shortDesc,
"type": self.type.toName(),
"license": self.license.name,
"repo": self.repo,
"url": base_url + self.getDownloadURL(),
"release": self.getDownloadRelease().id if self.getDownloadRelease() is not None else None,
"screenshots": [base_url + ss.url for ss in self.screenshots],
"thumbnail": (base_url + tnurl) if tnurl is not None else None
}
def getAsDictionary(self, base_url):
tnurl = self.getThumbnailURL()
return {
"author": self.author.display_name,
"name": self.name,
"title": self.title,
"shortDesc": self.shortDesc,
"desc": self.desc,
"type": self.type.toName(),
"createdAt": self.created_at,
"license": self.license.name,
"mediaLicense": self.media_license.name,
"repo": self.repo,
"website": self.website,
"issueTracker": self.issueTracker,
"forums": self.forums,
"provides": [x.name for x in self.provides],
"thumbnail": (base_url + tnurl) if tnurl is not None else None,
"screenshots": [base_url + ss.url for ss in self.screenshots],
"url": base_url + self.getDownloadURL(),
"release": self.getDownloadRelease().id if self.getDownloadRelease() is not None else None
}
def getThumbnailURL(self):
screenshot = self.screenshots.filter_by(approved=True).first()
return screenshot.getThumbnailURL() if screenshot is not None else None

View File

@ -51,7 +51,8 @@ def home_page():
packages = query.order_by(db.desc(Package.created_at)).limit(15).all()
return render_template("index.html", packages=packages, count=count)
from . import users, githublogin, packages, sass, tasks, admin, notifications, tagseditor, meta, thumbnails, threads
from . import users, githublogin, packages, meta, threads, api
from . import sass, tasks, admin, notifications, tagseditor, thumbnails
@menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' })
@app.route('/<path:path>/')

35
app/views/api.py Normal file
View File

@ -0,0 +1,35 @@
# Content DB
# Copyright (C) 2018 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import *
from flask_user import *
from app import app
from app.models import *
from app.utils import is_package_page
from .packages import build_packages_query
@app.route("/api/packages/")
def api_packages_page():
query = build_packages_query()
pkgs = [package.getAsDictionaryShort(app.config["BASE_URL"]) \
for package in query.all() if package.getDownloadRelease() is not None]
return jsonify(pkgs)
@app.route("/api/packages/<author>/<name>/")
@is_package_page
def api_package_page(package):
return jsonify(package.getAsDictionary(app.config["BASE_URL"]))

View File

@ -30,14 +30,7 @@ from wtforms import *
from wtforms.validators import *
from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField
# TODO: the following could be made into one route, except I"m not sure how
# to do the menu
@menu.register_menu(app, ".mods", "Mods", order=11, endpoint_arguments_constructor=lambda: { 'type': 'mod' })
@menu.register_menu(app, ".games", "Games", order=12, endpoint_arguments_constructor=lambda: { 'type': 'game' })
@menu.register_menu(app, ".txp", "Texture Packs", order=13, endpoint_arguments_constructor=lambda: { 'type': 'txp' })
@app.route("/packages/")
def packages_page():
def build_packages_query():
type_name = request.args.get("type")
type = None
if type_name is not None:
@ -54,11 +47,17 @@ def packages_page():
if search is not None and search.strip() != "":
query = query.filter(Package.title.ilike('%' + search + '%'))
return query
@menu.register_menu(app, ".mods", "Mods", order=11, endpoint_arguments_constructor=lambda: { 'type': 'mod' })
@menu.register_menu(app, ".games", "Games", order=12, endpoint_arguments_constructor=lambda: { 'type': 'game' })
@menu.register_menu(app, ".txp", "Texture Packs", order=13, endpoint_arguments_constructor=lambda: { 'type': 'txp' })
@app.route("/packages/")
def packages_page():
if shouldReturnJson():
pkgs = [package.getAsDictionary(app.config["BASE_URL"]) \
for package in query.all() if package.getDownloadRelease() is not None]
return jsonify(pkgs)
else:
return redirect(url_for("api_packages_page"))
query = build_packages_query()
page = int(request.args.get("page") or 1)
num = min(42, int(request.args.get("n") or 100))
query = query.paginate(page, num, True)
@ -84,9 +83,6 @@ def getReleases(package):
@app.route("/packages/<author>/<name>/")
@is_package_page
def package_page(package):
if shouldReturnJson():
return jsonify(package.getAsDictionary(app.config["BASE_URL"]))
else:
clearNotifications(package.getDetailsURL())
alternatives = None