2018-03-19 19:08:41 +01:00
|
|
|
from flask import *
|
|
|
|
from flask_user import *
|
|
|
|
from flask.ext import menu
|
|
|
|
from app import app
|
|
|
|
from app.models import *
|
|
|
|
|
2018-03-23 18:33:34 +01:00
|
|
|
from .utils import *
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import *
|
2018-03-21 18:42:43 +01:00
|
|
|
from wtforms.validators import *
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-23 18:05:07 +01:00
|
|
|
|
|
|
|
# TODO: the following could be made into one route, except I"m not sure how
|
2018-03-20 02:19:19 +01:00
|
|
|
# to do the menu
|
|
|
|
|
2018-03-21 21:02:53 +01:00
|
|
|
def doPackageList(type):
|
2018-03-21 21:10:11 +01:00
|
|
|
title = "Packages"
|
|
|
|
query = Package.query
|
2018-03-21 21:02:53 +01:00
|
|
|
|
2018-03-21 21:10:11 +01:00
|
|
|
if type is not None:
|
|
|
|
title = type.value + "s"
|
|
|
|
query = query.filter_by(type=type, approved=True)
|
2018-03-21 21:02:53 +01:00
|
|
|
|
2018-03-21 21:10:11 +01:00
|
|
|
search = request.args.get("q")
|
|
|
|
if search is not None:
|
|
|
|
query = query.filter(Package.title.contains(search))
|
|
|
|
|
2018-03-23 18:33:34 +01:00
|
|
|
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)
|
|
|
|
|
2018-03-21 21:10:11 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/packages/")
|
2018-03-21 21:10:11 +01:00
|
|
|
def packages_page():
|
2018-03-23 18:33:34 +01:00
|
|
|
type = None
|
|
|
|
typeStr = request.args.get("type")
|
|
|
|
if typeStr is not None:
|
|
|
|
type = PackageType[typeStr.upper()]
|
|
|
|
return doPackageList(type)
|
2018-03-21 21:02:53 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/mods/")
|
|
|
|
@menu.register_menu(app, ".mods", "Mods", order=11)
|
2018-03-19 19:08:41 +01:00
|
|
|
def mods_page():
|
2018-03-21 21:02:53 +01:00
|
|
|
return doPackageList(PackageType.MOD)
|
2018-03-19 19:08:41 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/games/")
|
|
|
|
@menu.register_menu(app, ".games", "Games", order=12)
|
2018-03-20 01:44:47 +01:00
|
|
|
def games_page():
|
2018-03-21 21:02:53 +01:00
|
|
|
return doPackageList(PackageType.GAME)
|
2018-03-20 01:44:47 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/texturepacks/")
|
|
|
|
@menu.register_menu(app, ".txp", "Texture Packs", order=13)
|
2018-03-20 01:44:47 +01:00
|
|
|
def txp_page():
|
2018-03-21 21:02:53 +01:00
|
|
|
return doPackageList(PackageType.TXP)
|
2018-03-20 01:44:47 +01:00
|
|
|
|
2018-03-21 20:24:45 +01:00
|
|
|
def canSeeWorkQueue():
|
2018-03-21 20:53:53 +01:00
|
|
|
return Permission.APPROVE_NEW.check(current_user) or \
|
|
|
|
Permission.APPROVE_RELEASE.check(current_user) or \
|
|
|
|
Permission.APPROVE_CHANGES.check(current_user)
|
2018-03-21 20:24:45 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@menu.register_menu(app, ".todo", "Work Queue", order=20, visible_when=canSeeWorkQueue)
|
2018-03-21 20:24:45 +01:00
|
|
|
@app.route("/todo/")
|
|
|
|
@login_required
|
|
|
|
def todo_page():
|
2018-03-21 20:53:53 +01:00
|
|
|
canApproveNew = Permission.APPROVE_NEW.check(current_user)
|
|
|
|
canApproveRel = Permission.APPROVE_RELEASE.check(current_user)
|
|
|
|
|
|
|
|
packages = None
|
|
|
|
if canApproveNew:
|
|
|
|
packages = Package.query.filter_by(approved=False).all()
|
|
|
|
|
|
|
|
releases = None
|
|
|
|
if canApproveRel:
|
|
|
|
releases = PackageRelease.query.filter_by(approved=False).all()
|
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
return render_template("todo.html", title="Reports and Work Queue",
|
2018-03-21 20:53:53 +01:00
|
|
|
approve_new=packages, releases=releases,
|
|
|
|
canApproveNew=canApproveNew, canApproveRel=canApproveRel)
|
2018-03-21 20:24:45 +01:00
|
|
|
|
2018-03-20 02:19:19 +01:00
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
def getPageByInfo(type, author, name):
|
2018-03-20 01:58:44 +01:00
|
|
|
user = User.query.filter_by(username=author).first()
|
|
|
|
if user is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
package = Package.query.filter_by(name=name, author_id=user.id,
|
2018-03-20 05:11:50 +01:00
|
|
|
type=PackageType[type.upper()]).first()
|
2018-03-19 19:08:41 +01:00
|
|
|
if package is None:
|
|
|
|
abort(404)
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
return package
|
|
|
|
|
2018-03-20 19:20:30 +01:00
|
|
|
def getReleases(package):
|
|
|
|
if package.checkPerm(current_user, Permission.MAKE_RELEASE):
|
|
|
|
return package.releases
|
|
|
|
else:
|
|
|
|
return [rel for rel in package.releases if rel.approved]
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
|
|
|
|
@app.route("/<type>s/<author>/<name>/")
|
|
|
|
def package_page(type, author, name):
|
|
|
|
package = getPageByInfo(type, author, name)
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2018-03-23 18:33:34 +01:00
|
|
|
if shouldReturnJson():
|
|
|
|
return jsonify(package.getAsDictionary(request.url_root))
|
|
|
|
else:
|
|
|
|
releases = getReleases(package)
|
|
|
|
return render_template("packages/view.html", package=package, releases=releases)
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-23 17:20:56 +01:00
|
|
|
@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:
|
2018-03-23 18:33:34 +01:00
|
|
|
if "application/zip" in request.accept_mimetypes and \
|
|
|
|
not "text/html" in request.accept_mimetypes:
|
2018-03-23 17:20:56 +01:00
|
|
|
return "", 204
|
|
|
|
else:
|
|
|
|
flash("No download available.", "error")
|
|
|
|
return redirect(package.getDetailsURL())
|
|
|
|
else:
|
|
|
|
return redirect(release.url, code=302)
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
|
|
|
|
class PackageForm(FlaskForm):
|
2018-03-23 18:05:07 +01:00
|
|
|
name = StringField("Name", [InputRequired(), Length(1, 20), Regexp("^[a-z0-9_]", 0, "Lower case letters (a-z), digits (0-9), and underscores (_) only")])
|
|
|
|
title = StringField("Title", [InputRequired(), Length(3, 50)])
|
|
|
|
shortDesc = StringField("Short Description", [InputRequired(), Length(1,200)])
|
|
|
|
desc = TextAreaField("Long Description", [Optional(), Length(0,10000)])
|
|
|
|
type = SelectField("Type", [InputRequired()], choices=PackageType.choices(), coerce=PackageType.coerce, default=PackageType.MOD)
|
|
|
|
repo = StringField("Repo URL", [Optional(), URL()])
|
|
|
|
website = StringField("Website URL", [Optional(), URL()])
|
2018-03-21 18:42:43 +01:00
|
|
|
issueTracker = StringField("Issue Tracker URL", [Optional(), URL()])
|
2018-03-23 18:05:07 +01:00
|
|
|
forums = IntegerField("Forum Topic ID", [InputRequired(), NumberRange(0,999999)])
|
|
|
|
submit = SubmitField("Save")
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@menu.register_menu(app, ".new", "Create", order=21, visible_when=lambda: current_user.is_authenticated)
|
|
|
|
@app.route("/new/", methods=["GET", "POST"])
|
|
|
|
@app.route("/<type>s/<author>/<name>/edit/", methods=["GET", "POST"])
|
2018-03-20 03:58:31 +01:00
|
|
|
@login_required
|
2018-03-21 18:03:14 +01:00
|
|
|
def create_edit_package_page(type=None, author=None, name=None):
|
|
|
|
package = None
|
|
|
|
form = None
|
|
|
|
if type is None:
|
|
|
|
form = PackageForm(formdata=request.form)
|
|
|
|
else:
|
|
|
|
package = getPageByInfo(type, author, name)
|
|
|
|
if not package.checkPerm(current_user, Permission.EDIT_PACKAGE):
|
|
|
|
return redirect(package.getDetailsURL())
|
|
|
|
|
|
|
|
form = PackageForm(formdata=request.form, obj=package)
|
2018-03-20 03:58:31 +01:00
|
|
|
|
|
|
|
# Initial form class from post data and default data
|
|
|
|
if request.method == "POST" and form.validate():
|
|
|
|
# Successfully submitted!
|
2018-03-21 18:03:14 +01:00
|
|
|
if not package:
|
|
|
|
package = Package()
|
|
|
|
package.author = current_user
|
2018-03-21 20:03:14 +01:00
|
|
|
# package.approved = package.checkPerm(current_user, Permission.APPROVE_NEW)
|
2018-03-21 18:03:14 +01:00
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
form.populate_obj(package) # copy to row
|
|
|
|
db.session.commit() # save
|
|
|
|
return redirect(package.getDetailsURL()) # redirect
|
|
|
|
|
2018-03-23 15:07:13 +01:00
|
|
|
return render_template("packages/create_edit.html", package=package, form=form)
|
2018-03-20 19:40:19 +01:00
|
|
|
|
2018-03-21 20:03:14 +01:00
|
|
|
@app.route("/<type>s/<author>/<name>/approve/")
|
|
|
|
@login_required
|
|
|
|
def approve_package_page(type=None, author=None, name=None):
|
|
|
|
package = getPageByInfo(type, author, name)
|
|
|
|
|
|
|
|
if not package.checkPerm(current_user, Permission.APPROVE_NEW):
|
|
|
|
flash("You don't have permission to do that.", "error")
|
|
|
|
|
|
|
|
elif package.approved:
|
|
|
|
flash("Package has already been approved", "error")
|
|
|
|
|
|
|
|
else:
|
|
|
|
package.approved = True
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
return redirect(package.getDetailsURL())
|
2018-03-20 19:40:19 +01:00
|
|
|
|
2018-03-21 17:41:34 +01:00
|
|
|
class CreatePackageReleaseForm(FlaskForm):
|
2018-03-23 18:05:07 +01:00
|
|
|
name = StringField("Name")
|
|
|
|
title = StringField("Title")
|
|
|
|
uploadOpt = RadioField ("File", choices=[("vcs", "From VCS Commit or Branch"), ("upload", "File Upload")])
|
|
|
|
vcsLabel = StringField("VCS Commit or Branch", default="master")
|
2018-03-20 19:40:19 +01:00
|
|
|
fileUpload = FileField("File Upload")
|
2018-03-23 18:05:07 +01:00
|
|
|
submit = SubmitField("Save")
|
2018-03-20 19:40:19 +01:00
|
|
|
|
2018-03-21 17:41:34 +01:00
|
|
|
class EditPackageReleaseForm(FlaskForm):
|
2018-03-23 18:05:07 +01:00
|
|
|
name = StringField("Name")
|
|
|
|
title = StringField("Title")
|
|
|
|
url = StringField("URL", [URL])
|
|
|
|
approved = BooleanField("Is Approved")
|
|
|
|
submit = SubmitField("Save")
|
2018-03-21 17:41:34 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/<type>s/<author>/<name>/releases/new/", methods=["GET", "POST"])
|
2018-03-20 19:40:19 +01:00
|
|
|
@login_required
|
|
|
|
def create_release_page(type, author, name):
|
|
|
|
package = getPageByInfo(type, author, name)
|
|
|
|
if not package.checkPerm(current_user, Permission.MAKE_RELEASE):
|
|
|
|
return redirect(package.getDetailsURL())
|
|
|
|
|
|
|
|
# Initial form class from post data and default data
|
2018-03-23 18:05:07 +01:00
|
|
|
form = CreatePackageReleaseForm()
|
2018-03-20 19:40:19 +01:00
|
|
|
if request.method == "POST" and form.validate():
|
2018-03-23 18:05:07 +01:00
|
|
|
for key, value in request.files.items() :
|
|
|
|
print (key, value)
|
2018-03-20 19:40:19 +01:00
|
|
|
if form["uploadOpt"].data == "vcs":
|
|
|
|
rel = PackageRelease()
|
|
|
|
rel.package = package
|
|
|
|
rel.title = form["title"].data
|
|
|
|
rel.url = form["vcsLabel"].data
|
|
|
|
# TODO: get URL to commit from branch name
|
|
|
|
db.session.commit()
|
2018-03-23 18:05:07 +01:00
|
|
|
return redirect(package.getDetailsURL())
|
2018-03-20 19:40:19 +01:00
|
|
|
else:
|
2018-03-23 18:05:07 +01:00
|
|
|
file = form.fileUpload.data
|
|
|
|
if not file or file.filename == "":
|
|
|
|
flash("No selected file", "error")
|
|
|
|
elif not isFilenameAllowed(file.filename, ["zip"]):
|
|
|
|
flash("Please select a zip file", "error")
|
|
|
|
else:
|
|
|
|
import random, string, os
|
|
|
|
filename = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(10)) + ".zip"
|
|
|
|
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
|
|
|
|
|
|
|
rel = PackageRelease()
|
|
|
|
rel.package = package
|
|
|
|
rel.title = form["title"].data
|
|
|
|
rel.url = "/uploads/" + filename
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(package.getDetailsURL())
|
2018-03-20 19:40:19 +01:00
|
|
|
|
2018-03-23 15:07:13 +01:00
|
|
|
return render_template("packages/release_new.html", package=package, form=form)
|
2018-03-21 17:41:34 +01:00
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
@app.route("/<type>s/<author>/<name>/releases/<id>/", methods=["GET", "POST"])
|
2018-03-21 17:41:34 +01:00
|
|
|
@login_required
|
|
|
|
def edit_release_page(type, author, name, id):
|
|
|
|
user = User.query.filter_by(username=author).first()
|
|
|
|
if user is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
release = PackageRelease.query.filter_by(id=id).first()
|
|
|
|
if release is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
package = release.package
|
|
|
|
if package.name != name or package.type != PackageType[type.upper()]:
|
|
|
|
abort(404)
|
|
|
|
|
2018-03-23 18:05:07 +01:00
|
|
|
canEdit = package.checkPerm(current_user, Permission.MAKE_RELEASE)
|
2018-03-21 17:41:34 +01:00
|
|
|
canApprove = package.checkPerm(current_user, Permission.APPROVE_RELEASE)
|
|
|
|
if not (canEdit or canApprove):
|
|
|
|
return redirect(package.getDetailsURL())
|
|
|
|
|
|
|
|
# Initial form class from post data and default data
|
|
|
|
form = EditPackageReleaseForm(formdata=request.form, obj=release)
|
|
|
|
if request.method == "POST" and form.validate():
|
|
|
|
wasApproved = release.approved
|
|
|
|
if canEdit:
|
|
|
|
release.title = form["title"].data
|
|
|
|
|
|
|
|
if package.checkPerm(current_user, Permission.CHANGE_RELEASE_URL):
|
|
|
|
release.url = form["url"].data
|
|
|
|
|
|
|
|
if canApprove:
|
|
|
|
release.approved = form["approved"].data
|
|
|
|
else:
|
|
|
|
release.approved = wasApproved
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(package.getDetailsURL())
|
|
|
|
|
2018-03-23 15:07:13 +01:00
|
|
|
return render_template("packages/release_edit.html", package=package, release=release, form=form)
|