2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-05-25 18:28:32 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2018-05-25 18:28:32 +02:00
|
|
|
# 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
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2018-05-25 18:28:32 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-05-25 18:28:32 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
from flask import *
|
2022-01-07 22:55:33 +01:00
|
|
|
from flask_babel import gettext, lazy_gettext
|
2018-05-25 18:28:32 +02:00
|
|
|
from flask_wtf import FlaskForm
|
2020-12-04 23:05:10 +01:00
|
|
|
from flask_login import login_required
|
2018-05-25 18:28:32 +02:00
|
|
|
from wtforms import *
|
2022-01-27 19:21:47 +01:00
|
|
|
from wtforms_sqlalchemy.fields import QuerySelectField
|
2018-05-25 18:28:32 +02:00
|
|
|
from wtforms.validators import *
|
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
from app.utils import *
|
2021-05-04 04:15:26 +02:00
|
|
|
from . import bp, get_package_tabs
|
2021-02-02 01:07:41 +01:00
|
|
|
from app.logic.LogicError import LogicError
|
2021-02-02 18:09:25 +01:00
|
|
|
from app.logic.screenshots import do_create_screenshot, do_order_screenshots
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2018-05-25 18:28:32 +02:00
|
|
|
|
|
|
|
class CreateScreenshotForm(FlaskForm):
|
2022-01-07 22:55:33 +01:00
|
|
|
title = StringField(lazy_gettext("Title/Caption"), [Optional(), Length(-1, 100)])
|
2022-08-09 18:51:17 +02:00
|
|
|
file_upload = FileField(lazy_gettext("File Upload"), [InputRequired()])
|
2022-01-07 22:55:33 +01:00
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
2018-05-25 18:28:32 +02:00
|
|
|
|
2018-05-25 19:57:30 +02:00
|
|
|
|
|
|
|
class EditScreenshotForm(FlaskForm):
|
2022-01-07 22:55:33 +01:00
|
|
|
title = StringField(lazy_gettext("Title/Caption"), [Optional(), Length(-1, 100)])
|
|
|
|
approved = BooleanField(lazy_gettext("Is Approved"))
|
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
2018-05-25 19:57:30 +02:00
|
|
|
|
2020-12-06 04:35:25 +01:00
|
|
|
|
2020-12-10 23:40:20 +01:00
|
|
|
class EditPackageScreenshotsForm(FlaskForm):
|
2022-01-07 22:55:33 +01:00
|
|
|
cover_image = QuerySelectField(lazy_gettext("Cover Image"), [DataRequired()], allow_blank=True, get_pk=lambda a: a.id, get_label=lambda a: a.title)
|
|
|
|
submit = SubmitField(lazy_gettext("Save"))
|
2020-12-10 23:40:20 +01:00
|
|
|
|
|
|
|
|
2020-12-06 04:35:25 +01:00
|
|
|
@bp.route("/packages/<author>/<name>/screenshots/", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def screenshots(package):
|
|
|
|
if not package.checkPerm(current_user, Permission.ADD_SCREENSHOTS):
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.view"))
|
2020-12-06 04:35:25 +01:00
|
|
|
|
2020-12-06 05:07:51 +01:00
|
|
|
if package.screenshots.count() == 0:
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.create_screenshot"))
|
2020-12-06 05:07:51 +01:00
|
|
|
|
2020-12-10 23:40:20 +01:00
|
|
|
form = EditPackageScreenshotsForm(obj=package)
|
|
|
|
form.cover_image.query = package.screenshots
|
|
|
|
|
2020-12-06 04:35:25 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
order = request.form.get("order")
|
|
|
|
if order:
|
2021-02-02 18:09:25 +01:00
|
|
|
try:
|
|
|
|
do_order_screenshots(current_user, package, order.split(","))
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.view"))
|
2021-02-02 18:09:25 +01:00
|
|
|
except LogicError as e:
|
|
|
|
flash(e.message, "danger")
|
2020-12-06 04:35:25 +01:00
|
|
|
|
2020-12-10 23:40:20 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
form.populate_obj(package)
|
|
|
|
db.session.commit()
|
|
|
|
|
2021-05-04 04:15:26 +02:00
|
|
|
return render_template("packages/screenshots.html", package=package, form=form,
|
|
|
|
tabs=get_package_tabs(current_user, package), current_tab="screenshots")
|
2020-12-06 04:35:25 +01:00
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@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):
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.view"))
|
2018-05-25 18:28:32 +02:00
|
|
|
|
|
|
|
# Initial form class from post data and default data
|
|
|
|
form = CreateScreenshotForm()
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2021-02-02 01:07:41 +01:00
|
|
|
try:
|
2022-08-09 18:51:17 +02:00
|
|
|
do_create_screenshot(current_user, package, form.title.data, form.file_upload.data, False)
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.screenshots"))
|
2021-02-02 01:07:41 +01:00
|
|
|
except LogicError as e:
|
|
|
|
flash(e.message, "danger")
|
2018-05-25 18:28:32 +02:00
|
|
|
|
|
|
|
return render_template("packages/screenshot_new.html", package=package, form=form)
|
2018-05-25 19:57:30 +02:00
|
|
|
|
2020-12-10 23:40:20 +01:00
|
|
|
|
2019-11-16 00:51:42 +01: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
|
2019-11-16 00:51:42 +01:00
|
|
|
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):
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.screenshots"))
|
2018-05-25 19:57:30 +02:00
|
|
|
|
|
|
|
# Initial form class from post data and default data
|
2020-12-06 05:30:47 +01:00
|
|
|
form = EditScreenshotForm(obj=screenshot)
|
2020-12-05 00:07:19 +01:00
|
|
|
if form.validate_on_submit():
|
2020-12-06 05:30:47 +01:00
|
|
|
wasApproved = screenshot.approved
|
2018-05-25 19:57:30 +02:00
|
|
|
|
2020-12-06 05:30:47 +01:00
|
|
|
if canEdit:
|
|
|
|
screenshot.title = form["title"].data or "Untitled"
|
2018-05-25 19:57:30 +02:00
|
|
|
|
2020-12-06 05:30:47 +01:00
|
|
|
if canApprove:
|
|
|
|
screenshot.approved = form["approved"].data
|
|
|
|
else:
|
|
|
|
screenshot.approved = wasApproved
|
2018-05-25 19:57:30 +02:00
|
|
|
|
|
|
|
db.session.commit()
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.screenshots"))
|
2018-05-25 19:57:30 +02:00
|
|
|
|
|
|
|
return render_template("packages/screenshot_edit.html", package=package, screenshot=screenshot, form=form)
|
2020-12-06 05:30:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/packages/<author>/<name>/screenshots/<id>/delete/", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@is_package_page
|
|
|
|
def delete_screenshot(package, id):
|
|
|
|
screenshot = PackageScreenshot.query.get(id)
|
|
|
|
if screenshot is None or screenshot.package != package:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if not package.checkPerm(current_user, Permission.ADD_SCREENSHOTS):
|
2022-01-07 22:46:16 +01:00
|
|
|
flash(gettext("Permission denied"), "danger")
|
2020-12-06 05:30:47 +01:00
|
|
|
return redirect(url_for("homepage.home"))
|
|
|
|
|
2020-12-11 00:07:13 +01:00
|
|
|
if package.cover_image == screenshot:
|
|
|
|
package.cover_image = None
|
|
|
|
db.session.merge(package)
|
|
|
|
|
2020-12-06 05:30:47 +01:00
|
|
|
db.session.delete(screenshot)
|
|
|
|
db.session.commit()
|
|
|
|
|
2021-07-24 05:30:14 +02:00
|
|
|
return redirect(package.getURL("packages.screenshots"))
|