mirror of
https://github.com/minetest/contentdb.git
synced 2025-01-08 22:17:34 +01:00
parent
4b7bf9d778
commit
c19f93e36d
@ -171,6 +171,8 @@ class Package(db.Model):
|
||||
releases = db.relationship("PackageRelease", backref="package",
|
||||
lazy="dynamic", order_by=db.desc("package_release_releaseDate"))
|
||||
|
||||
screenshots = db.relationship("PackageScreenshot", backref="package",
|
||||
lazy="dynamic")
|
||||
|
||||
requests = db.relationship("EditRequest", backref="package",
|
||||
lazy="dynamic")
|
||||
@ -184,7 +186,7 @@ class Package(db.Model):
|
||||
"type": self.type.toName(),
|
||||
"repo": self.repo,
|
||||
"url": base_url + self.getDownloadURL(),
|
||||
"screenshots": [ base_url + self.getMainScreenshotURL() ]
|
||||
"screenshots": [base_url + ss.url for ss in self.screenshots]
|
||||
}
|
||||
|
||||
def getDetailsURL(self):
|
||||
@ -202,6 +204,13 @@ class Package(db.Model):
|
||||
type=self.type.toName(),
|
||||
author=self.author.username, name=self.name)
|
||||
|
||||
|
||||
|
||||
def getNewScreenshotURL(self):
|
||||
return url_for("create_screenshot_page",
|
||||
type=self.type.toName(),
|
||||
author=self.author.username, name=self.name)
|
||||
|
||||
def getCreateReleaseURL(self):
|
||||
return url_for("create_release_page",
|
||||
type=self.type.toName(),
|
||||
@ -218,7 +227,7 @@ class Package(db.Model):
|
||||
author=self.author.username, name=self.name)
|
||||
|
||||
def getMainScreenshotURL(self):
|
||||
return "/static/screenshot.png"
|
||||
return self.screenshots[0].url if len(self.screenshots) > 0 else None
|
||||
|
||||
def getDownloadRelease(self):
|
||||
for rel in self.releases:
|
||||
@ -277,6 +286,15 @@ class PackageRelease(db.Model):
|
||||
def __init__(self):
|
||||
self.releaseDate = datetime.now()
|
||||
|
||||
class PackageScreenshot(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
package_id = db.Column(db.Integer, db.ForeignKey("package.id"))
|
||||
title = db.Column(db.String(100), nullable=False)
|
||||
url = db.Column(db.String(100), nullable=False)
|
||||
|
||||
def getThumbnailURL(self):
|
||||
return self.url # TODO
|
||||
|
||||
class EditRequest(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
|
||||
|
@ -10,6 +10,28 @@ h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.screenshot_list, .screenshot_list li, .screenshot_list li a {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.screenshot_list li {
|
||||
display: inline-block;
|
||||
margin: 5px;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.screenshot_list li a {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.screenshot_list img {
|
||||
width: 164px;
|
||||
height: 164px;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
|
||||
h2, h3 {
|
||||
margin: 5px 0;
|
||||
|
17
app/templates/packages/screenshot_new.html
Normal file
17
app/templates/packages/screenshot_new.html
Normal file
@ -0,0 +1,17 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}
|
||||
Add a screenshot | {{ package.title }}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% from "macros/forms.html" import render_field, render_submit_field %}
|
||||
<form method="POST" action="" enctype="multipart/form-data">
|
||||
{{ form.hidden_tag() }}
|
||||
|
||||
{{ render_field(form.title) }}
|
||||
{{ render_field(form.fileUpload) }}
|
||||
{{ render_submit_field(form.submit) }}
|
||||
<input type="submit" value="Go">
|
||||
</form>
|
||||
{% endblock %}
|
@ -18,6 +18,16 @@
|
||||
|
||||
<h1>{{ package.title }} by {{ package.author.display_name }}</h1>
|
||||
|
||||
<ul class="screenshot_list">
|
||||
{% for ss in package.screenshots %}
|
||||
<li>
|
||||
<a href="{{ ss.url }}">
|
||||
<img src="{{ ss.getThumbnailURL() }}" alt="{{ ss.title }}" />
|
||||
</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<aside class="asideright box box_grey">
|
||||
<table>
|
||||
<tr>
|
||||
@ -46,6 +56,7 @@
|
||||
{% if package.website %}<li><a href="{{ package.website }}">Website</a></li>{% endif %}
|
||||
{% if package.checkPerm(current_user, "EDIT_PACKAGE") %}
|
||||
<li><a href="{{ package.getEditURL() }}">Edit</a></li>
|
||||
<li><a href="{{ package.getNewScreenshotURL() }}">Add screenshot</a></li>
|
||||
{% elif current_user.is_authenticated %}
|
||||
<li><a href="{{ package.getCreateEditRequestURL() }}">Suggest Change</a></li>
|
||||
{% endif %}
|
||||
|
@ -187,6 +187,34 @@ def approve_package_page(type=None, author=None, name=None):
|
||||
|
||||
return redirect(package.getDetailsURL())
|
||||
|
||||
class CreateScreenshotForm(FlaskForm):
|
||||
title = StringField("Title/Caption", [Optional()])
|
||||
fileUpload = FileField("File Upload", [InputRequired()])
|
||||
submit = SubmitField("Save")
|
||||
|
||||
@app.route("/<type>s/<author>/<name>/screenshots/new/", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def create_screenshot_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
|
||||
form = CreateScreenshotForm()
|
||||
if request.method == "POST" and form.validate():
|
||||
uploadedPath = doFileUpload(form.fileUpload.data, ["png", "jpg", "jpeg"],
|
||||
"a PNG or JPG image file")
|
||||
if uploadedPath is not None:
|
||||
ss = PackageScreenshot()
|
||||
ss.package = package
|
||||
ss.title = form["title"].data
|
||||
ss.url = uploadedPath
|
||||
db.session.add(ss)
|
||||
db.session.commit()
|
||||
return redirect(package.getDetailsURL())
|
||||
|
||||
return render_template("packages/screenshot_new.html", package=package, form=form)
|
||||
|
||||
|
||||
class EditRequestForm(PackageForm):
|
||||
edit_title = StringField("Edit Title", [InputRequired(), Length(1, 100)])
|
||||
@ -333,20 +361,13 @@ def create_release_page(type, author, name):
|
||||
db.session.commit()
|
||||
return redirect(package.getDetailsURL())
|
||||
else:
|
||||
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))
|
||||
|
||||
uploadedPath = doFileUpload(form.fileUpload.data, ["zip"], "a zip file")
|
||||
if uploadedPath is not None:
|
||||
rel = PackageRelease()
|
||||
rel.package = package
|
||||
rel.title = form["title"].data
|
||||
rel.url = "/uploads/" + filename
|
||||
rel.url = uploadedPath
|
||||
db.session.add(rel)
|
||||
db.session.commit()
|
||||
return redirect(package.getDetailsURL())
|
||||
|
||||
|
@ -1,9 +1,31 @@
|
||||
from flask import request
|
||||
from flask import request, flash
|
||||
from app import app
|
||||
import random, string, os
|
||||
|
||||
def getExtension(filename):
|
||||
return filename.rsplit(".", 1)[1].lower() if "." in filename else None
|
||||
|
||||
def isFilenameAllowed(filename, exts):
|
||||
return "." in filename and \
|
||||
filename.rsplit(".", 1)[1].lower() in exts
|
||||
return getExtension(filename) in exts
|
||||
|
||||
def shouldReturnJson():
|
||||
return "application/json" in request.accept_mimetypes and \
|
||||
not "text/html" in request.accept_mimetypes
|
||||
|
||||
def randomString(n):
|
||||
return ''.join(random.choice(string.ascii_lowercase + \
|
||||
string.ascii_uppercase + string.digits) for _ in range(n))
|
||||
|
||||
def doFileUpload(file, allowedExtensions, fileTypeName):
|
||||
if not file or file is None or file.filename == "":
|
||||
flash("No selected file", "error")
|
||||
return None
|
||||
|
||||
ext = getExtension(file.filename)
|
||||
if ext is None or not ext in allowedExtensions:
|
||||
flash("Please upload load " + fileTypeName, "error")
|
||||
return None
|
||||
|
||||
filename = randomString(10) + "." + ext
|
||||
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
|
||||
return "/uploads/" + filename
|
||||
|
Loading…
Reference in New Issue
Block a user