contentdb/app/blueprints/packages/screenshots.py

107 lines
3.5 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2018-05-25 18:28:32 +02:00
# 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_wtf import FlaskForm
from flask_login import login_required
2018-05-25 18:28:32 +02:00
from wtforms import *
from wtforms.validators import *
2020-12-04 03:23:04 +01:00
from app.utils import *
from . import bp
2018-05-25 18:28:32 +02:00
class CreateScreenshotForm(FlaskForm):
title = StringField("Title/Caption", [Optional(), Length(-1, 100)])
2018-05-25 18:28:32 +02:00
fileUpload = FileField("File Upload", [InputRequired()])
submit = SubmitField("Save")
2018-05-25 19:57:30 +02:00
class EditScreenshotForm(FlaskForm):
title = StringField("Title/Caption", [Optional(), Length(-1, 100)])
2018-05-25 19:57:30 +02:00
approved = BooleanField("Is Approved")
delete = BooleanField("Delete")
submit = SubmitField("Save")
@bp.route("/packages/<author>/<name>/screenshots/new/", methods=["GET", "POST"])
2018-05-25 18:28:32 +02:00
@login_required
@is_package_page
2020-12-04 03:23:04 +01:00
def create_screenshot(package):
2018-05-25 19:57:30 +02:00
if not package.checkPerm(current_user, Permission.ADD_SCREENSHOTS):
2018-05-25 18:28:32 +02:00
return redirect(package.getDetailsURL())
# Initial form class from post data and default data
form = CreateScreenshotForm()
2020-12-05 00:07:19 +01:00
if form.validate_on_submit():
2020-01-19 02:37:15 +01:00
uploadedUrl, uploadedPath = doFileUpload(form.fileUpload.data, "image",
2018-05-25 18:28:32 +02:00
"a PNG or JPG image file")
2020-01-19 02:37:15 +01:00
if uploadedUrl is not None:
2018-05-25 18:28:32 +02:00
ss = PackageScreenshot()
ss.package = package
ss.title = form["title"].data or "Untitled"
2020-01-19 02:37:15 +01:00
ss.url = uploadedUrl
ss.approved = package.checkPerm(current_user, Permission.APPROVE_SCREENSHOT)
2018-05-25 18:28:32 +02:00
db.session.add(ss)
msg = "Screenshot added {}" \
.format(ss.title)
2020-12-05 04:44:34 +01:00
addNotification(package.maintainers, current_user, NotificationType.PACKAGE_EDIT, msg, package.getDetailsURL(), package)
2018-05-25 18:28:32 +02:00
db.session.commit()
return redirect(package.getDetailsURL())
return render_template("packages/screenshot_new.html", package=package, form=form)
2018-05-25 19:57:30 +02:00
@bp.route("/packages/<author>/<name>/screenshots/<id>/edit/", methods=["GET", "POST"])
2018-05-25 19:57:30 +02:00
@login_required
@is_package_page
def edit_screenshot(package, id):
2018-05-25 19:57:30 +02:00
screenshot = PackageScreenshot.query.get(id)
if screenshot is None or screenshot.package != package:
abort(404)
canEdit = package.checkPerm(current_user, Permission.ADD_SCREENSHOTS)
canApprove = package.checkPerm(current_user, Permission.APPROVE_SCREENSHOT)
if not (canEdit or canApprove):
return redirect(package.getDetailsURL())
# Initial form class from post data and default data
form = EditScreenshotForm(formdata=request.form, obj=screenshot)
if request.method == "GET":
# HACK: fix bug in wtforms
form.approved.data = screenshot.approved
2020-12-05 00:07:19 +01:00
if form.validate_on_submit():
2018-05-25 19:57:30 +02:00
if canEdit and form["delete"].data:
PackageScreenshot.query.filter_by(id=id).delete()
else:
wasApproved = screenshot.approved
if canEdit:
2018-07-28 15:13:03 +02:00
screenshot.title = form["title"].data or "Untitled"
2018-05-25 19:57:30 +02:00
if canApprove:
screenshot.approved = form["approved"].data
else:
screenshot.approved = wasApproved
db.session.commit()
return redirect(package.getDetailsURL())
return render_template("packages/screenshot_edit.html", package=package, screenshot=screenshot, form=form)