2023-06-19 20:32:36 +02:00
|
|
|
# ContentDB
|
|
|
|
# Copyright (C) 2018-23 rubenwardy
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-11-23 02:05:43 +01:00
|
|
|
from flask import Blueprint, render_template, redirect
|
2023-08-20 23:25:18 +02:00
|
|
|
from sqlalchemy import and_
|
2019-11-16 00:51:42 +01:00
|
|
|
|
2023-08-20 23:25:18 +02:00
|
|
|
from app.models import Package, PackageReview, Thread, User, PackageState, db, PackageType, PackageRelease, Tags, Tag, \
|
2023-11-18 12:25:22 +01:00
|
|
|
Collection, License
|
2023-06-19 20:32:36 +02:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
bp = Blueprint("homepage", __name__)
|
|
|
|
|
2023-11-18 12:25:22 +01:00
|
|
|
from sqlalchemy.orm import joinedload, subqueryload, load_only, noload
|
2019-11-16 00:51:42 +01:00
|
|
|
from sqlalchemy.sql.expression import func
|
2021-11-23 01:58:53 +01:00
|
|
|
|
|
|
|
|
2023-08-25 22:31:19 +02:00
|
|
|
PKGS_PER_ROW = 4
|
2023-08-22 20:58:43 +02:00
|
|
|
|
2024-01-05 00:09:03 +01:00
|
|
|
# GAMEJAM_BANNER = "https://jam.minetest.net/img/banner.png"
|
|
|
|
#
|
|
|
|
# class GameJam:
|
|
|
|
# cover_image = type("", (), dict(url=GAMEJAM_BANNER))()
|
|
|
|
# tags = []
|
|
|
|
#
|
|
|
|
# def get_cover_image_url(self):
|
|
|
|
# return GAMEJAM_BANNER
|
|
|
|
#
|
|
|
|
# def get_url(self, _name):
|
|
|
|
# return "/gamejam/"
|
|
|
|
#
|
|
|
|
# title = "Minetest Game Jam 2023: \"Unexpected\""
|
|
|
|
# author = None
|
|
|
|
#
|
|
|
|
# short_desc = "The game jam has finished! It's now up to the community to play and rate the games."
|
|
|
|
# type = type("", (), dict(value="Competition"))()
|
|
|
|
# content_warnings = []
|
|
|
|
# reviews = []
|
2023-12-02 14:11:57 +01:00
|
|
|
|
2023-08-22 20:58:43 +02:00
|
|
|
|
2022-10-25 18:24:43 +02:00
|
|
|
@bp.route("/gamejam/")
|
|
|
|
def gamejam():
|
2023-12-02 14:11:57 +01:00
|
|
|
return redirect("https://jam.minetest.net/")
|
2022-10-25 18:24:43 +02:00
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/")
|
2019-11-21 20:38:26 +01:00
|
|
|
def home():
|
2022-11-18 21:29:52 +01:00
|
|
|
def package_load(query):
|
2020-12-04 03:23:04 +01:00
|
|
|
return query.options(
|
2023-11-18 12:25:22 +01:00
|
|
|
load_only(Package.name, Package.title, Package.short_desc, Package.state, raiseload=True),
|
2022-11-18 21:29:52 +01:00
|
|
|
subqueryload(Package.main_screenshot),
|
2023-11-18 12:25:22 +01:00
|
|
|
joinedload(Package.author).load_only(User.username, User.display_name, raiseload=True),
|
|
|
|
joinedload(Package.license).load_only(License.name, License.is_foss, raiseload=True),
|
|
|
|
joinedload(Package.media_license).load_only(License.name, License.is_foss, raiseload=True))
|
|
|
|
|
|
|
|
def package_spotlight_load(query):
|
|
|
|
return query.options(
|
|
|
|
load_only(Package.name, Package.title, Package.type, Package.short_desc, Package.state, Package.cover_image_id, raiseload=True),
|
|
|
|
subqueryload(Package.main_screenshot),
|
|
|
|
joinedload(Package.tags),
|
|
|
|
joinedload(Package.content_warnings),
|
|
|
|
joinedload(Package.author).load_only(User.username, User.display_name, raiseload=True),
|
2022-11-18 21:29:52 +01:00
|
|
|
subqueryload(Package.cover_image),
|
2023-11-18 12:25:22 +01:00
|
|
|
joinedload(Package.license).load_only(License.name, License.is_foss, raiseload=True),
|
|
|
|
joinedload(Package.media_license).load_only(License.name, License.is_foss, raiseload=True))
|
2020-04-21 21:35:05 +02:00
|
|
|
|
2022-11-18 22:15:46 +01:00
|
|
|
def review_load(query):
|
|
|
|
return query.options(
|
2023-11-18 12:25:22 +01:00
|
|
|
load_only(PackageReview.id, PackageReview.rating, PackageReview.created_at, raiseload=True),
|
|
|
|
joinedload(PackageReview.author).load_only(User.username, User.rank, User.email, User.display_name, User.profile_pic, User.is_active, raiseload=True),
|
|
|
|
joinedload(PackageReview.votes),
|
|
|
|
joinedload(PackageReview.thread).load_only(Thread.title, Thread.replies_count, raiseload=True).subqueryload(Thread.first_reply),
|
|
|
|
joinedload(PackageReview.package)
|
|
|
|
.load_only(Package.title, Package.name, raiseload=True)
|
|
|
|
.joinedload(Package.author).load_only(User.username, User.display_name, raiseload=True))
|
2022-11-18 22:15:46 +01:00
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
query = Package.query.filter_by(state=PackageState.APPROVED)
|
2023-11-18 12:25:22 +01:00
|
|
|
count = db.session.query(Package.id).filter(Package.state == PackageState.APPROVED).count()
|
|
|
|
|
|
|
|
spotlight_pkgs = package_spotlight_load(query.filter(
|
|
|
|
Package.collections.any(and_(Collection.name == "spotlight", Collection.author.has(username="ContentDB"))))
|
|
|
|
.order_by(func.random())).limit(6).all()
|
2024-01-05 00:09:03 +01:00
|
|
|
# spotlight_pkgs.insert(0, GameJam())
|
2023-11-18 12:25:22 +01:00
|
|
|
|
|
|
|
new = package_load(query).order_by(db.desc(Package.approved_at)).limit(PKGS_PER_ROW).all() # 0.06
|
|
|
|
pop_mod = package_load(query).filter_by(type=PackageType.MOD).order_by(db.desc(Package.score)).limit(2*PKGS_PER_ROW).all()
|
|
|
|
pop_gam = package_load(query).filter_by(type=PackageType.GAME).order_by(db.desc(Package.score)).limit(2*PKGS_PER_ROW).all()
|
|
|
|
pop_txp = package_load(query).filter_by(type=PackageType.TXP).order_by(db.desc(Package.score)).limit(2*PKGS_PER_ROW).all()
|
|
|
|
|
|
|
|
high_reviewed = package_load(query.order_by(db.desc(Package.score - Package.score_downloads))
|
|
|
|
.filter(Package.reviews.any()).limit(PKGS_PER_ROW)).all()
|
2020-07-18 02:22:52 +02:00
|
|
|
|
2023-11-17 23:53:26 +01:00
|
|
|
recent_releases_query = (
|
|
|
|
db.session.query(
|
|
|
|
Package.id,
|
|
|
|
func.max(PackageRelease.releaseDate).label("max_created_at")
|
|
|
|
)
|
|
|
|
.join(PackageRelease, Package.releases)
|
|
|
|
.group_by(Package.id)
|
|
|
|
.order_by(db.desc("max_created_at"))
|
2023-11-18 11:59:47 +01:00
|
|
|
.limit(3*PKGS_PER_ROW)
|
2023-11-17 23:53:26 +01:00
|
|
|
.subquery())
|
2023-11-18 11:59:47 +01:00
|
|
|
|
2023-11-17 23:53:26 +01:00
|
|
|
updated = (
|
2023-11-18 11:59:47 +01:00
|
|
|
package_load(db.session.query(Package)
|
|
|
|
.select_from(recent_releases_query)
|
|
|
|
.join(Package, Package.id == recent_releases_query.c.id)
|
|
|
|
.filter(Package.state == PackageState.APPROVED)
|
|
|
|
.limit(PKGS_PER_ROW))
|
|
|
|
.all())
|
2020-07-18 02:22:52 +02:00
|
|
|
|
2023-04-15 21:06:24 +02:00
|
|
|
reviews = review_load(PackageReview.query.filter(PackageReview.rating > 3)
|
2023-04-15 03:37:58 +02:00
|
|
|
.order_by(db.desc(PackageReview.created_at))).limit(5).all()
|
2020-07-18 02:22:52 +02:00
|
|
|
|
2020-07-09 02:11:50 +02:00
|
|
|
downloads_result = db.session.query(func.sum(Package.downloads)).one_or_none()
|
2020-01-19 16:03:38 +01:00
|
|
|
downloads = 0 if not downloads_result or not downloads_result[0] else downloads_result[0]
|
2020-07-18 02:22:52 +02:00
|
|
|
|
2020-07-18 03:48:22 +02:00
|
|
|
tags = db.session.query(func.count(Tags.c.tag_id), Tag) \
|
2023-05-13 16:40:03 +02:00
|
|
|
.select_from(Tag).outerjoin(Tags).join(Package).filter(Package.state == PackageState.APPROVED)\
|
|
|
|
.group_by(Tag.id).order_by(db.asc(Tag.title)).all()
|
2020-07-18 03:48:22 +02:00
|
|
|
|
2023-06-01 18:55:00 +02:00
|
|
|
return render_template("index.html", count=count, downloads=downloads, tags=tags, spotlight_pkgs=spotlight_pkgs,
|
2023-06-19 20:32:36 +02:00
|
|
|
new=new, updated=updated, pop_mod=pop_mod, pop_txp=pop_txp, pop_gam=pop_gam, high_reviewed=high_reviewed,
|
|
|
|
reviews=reviews)
|