From 5a0aa636f325373425ab5b25239fe06743611d04 Mon Sep 17 00:00:00 2001
From: rubenwardy
Date: Thu, 10 Dec 2020 22:40:20 +0000
Subject: [PATCH] Add ability to change cover image
Fixes #125
---
app/blueprints/packages/screenshots.py | 16 ++++++++++++-
app/models/packages.py | 5 ++++-
app/templates/packages/screenshots.html | 11 +++++++++
app/templates/packages/view.html | 3 ++-
migrations/versions/8d22def23c8b_.py | 30 +++++++++++++++++++++++++
utils/create_migration.sh | 2 +-
6 files changed, 63 insertions(+), 4 deletions(-)
create mode 100644 migrations/versions/8d22def23c8b_.py
diff --git a/app/blueprints/packages/screenshots.py b/app/blueprints/packages/screenshots.py
index 3126cdc2..9a7b7b24 100644
--- a/app/blueprints/packages/screenshots.py
+++ b/app/blueprints/packages/screenshots.py
@@ -19,6 +19,7 @@ from flask import *
from flask_wtf import FlaskForm
from flask_login import login_required
from wtforms import *
+from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import *
from app.utils import *
@@ -37,6 +38,11 @@ class EditScreenshotForm(FlaskForm):
submit = SubmitField("Save")
+class EditPackageScreenshotsForm(FlaskForm):
+ cover_image = QuerySelectField("Cover Image", [DataRequired()], allow_blank=True, get_pk=lambda a: a.id, get_label=lambda a: a.title)
+ submit = SubmitField("Save")
+
+
@bp.route("/packages///screenshots/", methods=["GET", "POST"])
@login_required
@is_package_page
@@ -47,6 +53,9 @@ def screenshots(package):
if package.screenshots.count() == 0:
return redirect(package.getNewScreenshotURL())
+ form = EditPackageScreenshotsForm(obj=package)
+ form.cover_image.query = package.screenshots
+
if request.method == "POST":
order = request.form.get("order")
if order:
@@ -62,7 +71,11 @@ def screenshots(package):
db.session.commit()
return redirect(package.getDetailsURL())
- return render_template("packages/screenshots.html", package=package)
+ if form.validate_on_submit():
+ form.populate_obj(package)
+ db.session.commit()
+
+ return render_template("packages/screenshots.html", package=package, form=form)
@bp.route("/packages///screenshots/new/", methods=["GET", "POST"])
@@ -99,6 +112,7 @@ def create_screenshot(package):
return render_template("packages/screenshot_new.html", package=package, form=form)
+
@bp.route("/packages///screenshots//edit/", methods=["GET", "POST"])
@login_required
@is_package_page
diff --git a/app/models/packages.py b/app/models/packages.py
index bcca5526..ff2607e6 100644
--- a/app/models/packages.py
+++ b/app/models/packages.py
@@ -308,9 +308,12 @@ class Package(db.Model):
releases = db.relationship("PackageRelease", back_populates="package",
lazy="dynamic", order_by=db.desc("package_release_releaseDate"), cascade="all, delete, delete-orphan")
- screenshots = db.relationship("PackageScreenshot", back_populates="package",
+ screenshots = db.relationship("PackageScreenshot", back_populates="package", foreign_keys="PackageScreenshot.package_id",
lazy="dynamic", order_by=db.asc("package_screenshot_order"), cascade="all, delete, delete-orphan")
+ cover_image_id = db.Column(db.Integer, db.ForeignKey("package_screenshot.id"), nullable=True, default=None)
+ cover_image = db.relationship("PackageScreenshot", uselist=False, foreign_keys=[cover_image_id])
+
maintainers = db.relationship("User", secondary=maintainers)
threads = db.relationship("Thread", back_populates="package", order_by=db.desc("thread_created_at"),
diff --git a/app/templates/packages/screenshots.html b/app/templates/packages/screenshots.html
index f3fa1643..b3a4ba6f 100644
--- a/app/templates/packages/screenshots.html
+++ b/app/templates/packages/screenshots.html
@@ -63,6 +63,17 @@
{{ _("Reordering requires JavaScript.") }}
+
+ {{ _("Cover Image") }}
+
+ {% from "macros/forms.html" import render_submit_field, render_field %}
+
{% endblock %}
{% block scriptextra %}
diff --git a/app/templates/packages/view.html b/app/templates/packages/view.html
index fff52d2a..37a9093d 100644
--- a/app/templates/packages/view.html
+++ b/app/templates/packages/view.html
@@ -27,8 +27,9 @@
{% set release = package.getDownloadRelease() %}
+ {% set cover_image = package.cover_image.url or package.getMainScreenshotURL() %}
diff --git a/migrations/versions/8d22def23c8b_.py b/migrations/versions/8d22def23c8b_.py
new file mode 100644
index 00000000..fe0d9152
--- /dev/null
+++ b/migrations/versions/8d22def23c8b_.py
@@ -0,0 +1,30 @@
+"""empty message
+
+Revision ID: 8d22def23c8b
+Revises: 42b14763c95e
+Create Date: 2020-12-10 22:23:32.291613
+
+"""
+from alembic import op
+import sqlalchemy as sa
+from sqlalchemy.dialects import postgresql
+
+# revision identifiers, used by Alembic.
+revision = '8d22def23c8b'
+down_revision = 'a9c1c08bf956'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.add_column('package', sa.Column('cover_image_id', sa.Integer(), nullable=True))
+ op.create_foreign_key(None, 'package', 'package_screenshot', ['cover_image_id'], ['id'])
+ # ### end Alembic commands ###
+
+
+def downgrade():
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_constraint(None, 'package', type_='foreignkey')
+ op.drop_column('package', 'cover_image_id')
+ # ### end Alembic commands ###
diff --git a/utils/create_migration.sh b/utils/create_migration.sh
index 59aaea01..4c739371 100755
--- a/utils/create_migration.sh
+++ b/utils/create_migration.sh
@@ -2,7 +2,7 @@
# Create a database migration, and copy it back to the host.
-docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db revision"
+docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db migrate"
docker exec -u root contentdb_app_1 sh -c "cp /home/cdb/migrations/versions/* /source/migrations/versions/"
USER=$(whoami)