2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-05-17 16:18:20 +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-17 16:18:20 +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-17 16:18:20 +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-17 16:18:20 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
import datetime
|
|
|
|
import enum
|
2023-12-15 17:33:34 +01:00
|
|
|
import os
|
2019-01-29 04:00:01 +01:00
|
|
|
|
2024-02-25 19:24:30 +01:00
|
|
|
import typing
|
2020-12-04 03:23:04 +01:00
|
|
|
from flask import url_for
|
2024-02-25 19:43:27 +01:00
|
|
|
from flask_babel import lazy_gettext, get_locale, gettext
|
2024-05-24 02:16:34 +02:00
|
|
|
from flask_sqlalchemy.query import Query
|
2023-11-18 12:25:22 +01:00
|
|
|
from sqlalchemy import or_, func
|
|
|
|
from sqlalchemy.dialects.postgresql import insert
|
2020-12-10 12:37:15 +01:00
|
|
|
from sqlalchemy_searchable import SearchQueryMixin
|
2019-01-29 04:00:01 +01:00
|
|
|
from sqlalchemy_utils.types import TSVectorType
|
2020-12-05 21:36:09 +01:00
|
|
|
|
2023-11-18 12:25:22 +01:00
|
|
|
from app import app
|
2020-12-10 12:37:15 +01:00
|
|
|
from . import db
|
|
|
|
from .users import Permission, UserRank, User
|
2019-01-29 04:00:01 +01:00
|
|
|
|
|
|
|
|
2024-05-24 02:16:34 +02:00
|
|
|
class PackageQuery(Query, SearchQueryMixin):
|
2019-03-13 15:37:20 +01:00
|
|
|
pass
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
|
2018-04-03 15:13:11 +02:00
|
|
|
class License(db.Model):
|
2018-05-29 22:24:50 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(50), nullable=False, unique=True)
|
|
|
|
is_foss = db.Column(db.Boolean, nullable=False, default=True)
|
2021-07-31 22:03:45 +02:00
|
|
|
url = db.Column(db.String(128), nullable=True, default=None)
|
2018-04-03 15:13:11 +02:00
|
|
|
|
2021-07-31 22:03:45 +02:00
|
|
|
def __init__(self, v: str, is_foss: bool = True, url: str = None):
|
2018-04-03 15:13:11 +02:00
|
|
|
self.name = v
|
2018-05-29 22:24:50 +02:00
|
|
|
self.is_foss = is_foss
|
2021-07-31 22:03:45 +02:00
|
|
|
self.url = url
|
2018-04-03 15:13:11 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2018-03-20 01:44:47 +01:00
|
|
|
class PackageType(enum.Enum):
|
|
|
|
MOD = "Mod"
|
|
|
|
GAME = "Game"
|
|
|
|
TXP = "Texture Pack"
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def to_name(self):
|
2018-03-20 05:11:50 +01:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2018-03-20 01:58:44 +01:00
|
|
|
|
2022-01-14 19:25:28 +01:00
|
|
|
@property
|
|
|
|
def text(self):
|
|
|
|
if self == PackageType.MOD:
|
|
|
|
return lazy_gettext("Mod")
|
|
|
|
elif self == PackageType.GAME:
|
|
|
|
return lazy_gettext("Game")
|
|
|
|
elif self == PackageType.TXP:
|
|
|
|
return lazy_gettext("Texture Pack")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def plural(self):
|
|
|
|
if self == PackageType.MOD:
|
|
|
|
return lazy_gettext("Mods")
|
|
|
|
elif self == PackageType.GAME:
|
|
|
|
return lazy_gettext("Games")
|
|
|
|
elif self == PackageType.TXP:
|
|
|
|
return lazy_gettext("Texture Packs")
|
|
|
|
|
2018-07-28 19:12:22 +02:00
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
try:
|
|
|
|
return PackageType[name.upper()]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
2022-01-14 19:25:28 +01:00
|
|
|
return [(choice, choice.text) for choice in cls]
|
2018-03-20 03:58:31 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2021-02-28 17:04:31 +01:00
|
|
|
return item if type(item) == PackageType else PackageType[item.upper()]
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-23 20:08:55 +01:00
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
class PackageDevState(enum.Enum):
|
|
|
|
WIP = "Work in Progress"
|
|
|
|
BETA = "Beta"
|
|
|
|
ACTIVELY_DEVELOPED = "Actively Developed"
|
|
|
|
MAINTENANCE_ONLY = "Maintenance Only"
|
|
|
|
AS_IS = "As-Is"
|
|
|
|
DEPRECATED = "Deprecated"
|
|
|
|
LOOKING_FOR_MAINTAINER = "Looking for Maintainer"
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def to_name(self):
|
2021-12-20 22:07:12 +01:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def get_desc(self):
|
|
|
|
if self == PackageDevState.WIP:
|
|
|
|
return "Under active development, and may break worlds/things without warning"
|
|
|
|
elif self == PackageDevState.BETA:
|
|
|
|
return "Fully playable, but with some breakages/changes expected"
|
|
|
|
elif self == PackageDevState.MAINTENANCE_ONLY:
|
|
|
|
return "Finished, with bug fixes being made as needed"
|
|
|
|
elif self == PackageDevState.AS_IS:
|
|
|
|
return "Finished, the maintainer doesn't intend to continue working on it or provide support"
|
|
|
|
elif self == PackageDevState.DEPRECATED:
|
|
|
|
return "The maintainer doesn't recommend this package. See the description for more info"
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
try:
|
|
|
|
return PackageDevState[name.upper()]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls, with_none):
|
|
|
|
def build_label(choice):
|
|
|
|
desc = choice.get_desc()
|
|
|
|
if desc is None:
|
|
|
|
return choice.value
|
|
|
|
else:
|
|
|
|
return f"{choice.value}: {desc}"
|
|
|
|
|
|
|
|
ret = [(choice, build_label(choice)) for choice in cls]
|
|
|
|
|
|
|
|
if with_none:
|
2022-01-04 14:08:53 +01:00
|
|
|
ret.insert(0, (None, ""))
|
2021-12-20 22:07:12 +01:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2022-01-04 14:08:53 +01:00
|
|
|
if item is None or (isinstance(item, str) and item.upper() == "NONE"):
|
2021-12-20 22:07:12 +01:00
|
|
|
return None
|
|
|
|
return item if type(item) == PackageDevState else PackageDevState[item.upper()]
|
|
|
|
|
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
class PackageState(enum.Enum):
|
2021-01-25 17:58:58 +01:00
|
|
|
WIP = "Draft"
|
2020-09-16 18:51:03 +02:00
|
|
|
CHANGES_NEEDED = "Changes Needed"
|
|
|
|
READY_FOR_REVIEW = "Ready for Review"
|
2021-01-25 17:58:58 +01:00
|
|
|
APPROVED = "Approved"
|
2020-09-16 18:51:03 +02:00
|
|
|
DELETED = "Deleted"
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def to_name(self):
|
2020-09-16 18:51:03 +02:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def verb(self):
|
|
|
|
if self == self.READY_FOR_REVIEW:
|
2022-01-20 02:18:10 +01:00
|
|
|
return lazy_gettext("Submit for Approval")
|
2020-09-16 18:51:03 +02:00
|
|
|
elif self == self.APPROVED:
|
2022-01-20 02:18:10 +01:00
|
|
|
return lazy_gettext("Approve")
|
2020-09-16 18:51:03 +02:00
|
|
|
elif self == self.DELETED:
|
2022-01-20 02:18:10 +01:00
|
|
|
return lazy_gettext("Delete")
|
2020-09-16 18:51:03 +02:00
|
|
|
else:
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2021-08-12 19:57:25 +02:00
|
|
|
@property
|
|
|
|
def color(self):
|
|
|
|
if self == self.WIP:
|
|
|
|
return "warning"
|
|
|
|
elif self == self.CHANGES_NEEDED:
|
|
|
|
return "danger"
|
|
|
|
elif self == self.READY_FOR_REVIEW:
|
|
|
|
return "success"
|
|
|
|
elif self == self.APPROVED:
|
|
|
|
return "info"
|
|
|
|
else:
|
|
|
|
return "danger"
|
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
try:
|
|
|
|
return PackageState[name.upper()]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
|
|
|
return [(choice, choice.value) for choice in cls]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2021-02-28 17:04:31 +01:00
|
|
|
return item if type(item) == PackageState else PackageState[item.upper()]
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
|
2021-07-25 04:54:08 +02:00
|
|
|
PackageProvides = db.Table("provides",
|
2018-05-21 23:20:28 +02:00
|
|
|
db.Column("package_id", db.Integer, db.ForeignKey("package.id"), primary_key=True),
|
2020-12-04 03:23:04 +01:00
|
|
|
db.Column("metapackage_id", db.Integer, db.ForeignKey("meta_package.id"), primary_key=True)
|
2018-05-21 23:20:28 +02:00
|
|
|
)
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
|
2020-07-12 18:56:06 +02:00
|
|
|
Tags = db.Table("tags",
|
2020-12-04 03:23:04 +01:00
|
|
|
db.Column("tag_id", db.Integer, db.ForeignKey("tag.id"), primary_key=True),
|
|
|
|
db.Column("package_id", db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
2018-05-21 23:20:28 +02:00
|
|
|
)
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
ContentWarnings = db.Table("content_warnings",
|
2020-12-04 03:23:04 +01:00
|
|
|
db.Column("content_warning_id", db.Integer, db.ForeignKey("content_warning.id"), primary_key=True),
|
|
|
|
db.Column("package_id", db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
2020-07-17 21:48:51 +02:00
|
|
|
)
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
|
2020-07-08 23:45:24 +02:00
|
|
|
maintainers = db.Table("maintainers",
|
2020-12-04 03:23:04 +01:00
|
|
|
db.Column("user_id", db.Integer, db.ForeignKey("user.id"), primary_key=True),
|
|
|
|
db.Column("package_id", db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
2020-07-08 23:45:24 +02:00
|
|
|
)
|
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
class Dependency(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-12-09 19:35:11 +01:00
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
depender_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
2020-12-09 19:35:11 +01:00
|
|
|
depender = db.relationship("Package", foreign_keys=[depender_id])
|
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
2020-12-09 19:35:11 +01:00
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
meta_package_id = db.Column(db.Integer, db.ForeignKey("meta_package.id"), nullable=True)
|
2020-12-09 19:35:11 +01:00
|
|
|
meta_package = db.relationship("MetaPackage", foreign_keys=[meta_package_id])
|
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
optional = db.Column(db.Boolean, nullable=False, default=False)
|
2020-12-09 19:35:11 +01:00
|
|
|
|
2020-01-18 18:54:46 +01:00
|
|
|
__table_args__ = (db.UniqueConstraint("depender_id", "package_id", "meta_package_id", name="_dependency_uc"), )
|
2018-05-27 22:31:11 +02:00
|
|
|
|
2020-07-12 03:22:35 +02:00
|
|
|
def __init__(self, depender=None, package=None, meta=None, optional=False):
|
2018-05-27 22:31:11 +02:00
|
|
|
if depender is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.depender = depender
|
2020-07-12 03:22:35 +02:00
|
|
|
self.optional = optional
|
2018-05-27 22:31:11 +02:00
|
|
|
|
|
|
|
packageProvided = package is not None
|
|
|
|
metaProvided = meta is not None
|
|
|
|
|
|
|
|
if packageProvided and not metaProvided:
|
|
|
|
self.package = package
|
|
|
|
elif metaProvided and not packageProvided:
|
|
|
|
self.meta_package = meta
|
|
|
|
else:
|
|
|
|
raise Exception("Either meta or package must be given, but not both!")
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def get_name(self):
|
2020-01-21 23:59:44 +01:00
|
|
|
if self.meta_package:
|
|
|
|
return self.meta_package.name
|
|
|
|
elif self.package:
|
|
|
|
return self.package.name
|
|
|
|
else:
|
|
|
|
assert False
|
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
def __str__(self):
|
|
|
|
if self.package is not None:
|
|
|
|
return self.package.author.username + "/" + self.package.name
|
|
|
|
elif self.meta_package is not None:
|
|
|
|
return self.meta_package.name
|
|
|
|
else:
|
|
|
|
raise Exception("Meta and package are both none!")
|
|
|
|
|
|
|
|
@staticmethod
|
2020-12-04 03:23:04 +01:00
|
|
|
def SpecToList(depender, spec, cache):
|
2018-05-27 22:31:11 +02:00
|
|
|
retval = []
|
|
|
|
arr = spec.split(",")
|
|
|
|
|
|
|
|
import re
|
|
|
|
pattern1 = re.compile("^([a-z0-9_]+)$")
|
|
|
|
pattern2 = re.compile("^([A-Za-z0-9_]+)/([a-z0-9_]+)$")
|
|
|
|
|
|
|
|
for x in arr:
|
|
|
|
x = x.strip()
|
|
|
|
if x == "":
|
|
|
|
continue
|
|
|
|
|
|
|
|
if pattern1.match(x):
|
|
|
|
meta = MetaPackage.GetOrCreate(x, cache)
|
|
|
|
retval.append(Dependency(depender, meta=meta))
|
|
|
|
else:
|
|
|
|
m = pattern2.match(x)
|
|
|
|
username = m.group(1)
|
|
|
|
name = m.group(2)
|
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user is None:
|
|
|
|
raise Exception("Unable to find user " + username)
|
|
|
|
|
|
|
|
package = Package.query.filter_by(author=user, name=name).first()
|
|
|
|
if package is None:
|
|
|
|
raise Exception("Unable to find package " + name + " by " + username)
|
|
|
|
|
|
|
|
retval.append(Dependency(depender, package=package))
|
|
|
|
|
|
|
|
return retval
|
|
|
|
|
2018-05-21 23:20:28 +02:00
|
|
|
|
2022-02-01 21:53:59 +01:00
|
|
|
class PackageGameSupport(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=False)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
|
|
|
|
|
|
|
game_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=False)
|
|
|
|
game = db.relationship("Package", foreign_keys=[game_id])
|
|
|
|
|
|
|
|
supports = db.Column(db.Boolean, nullable=False, default=True)
|
2022-02-02 02:11:44 +01:00
|
|
|
confidence = db.Column(db.Integer, nullable=False, default=1)
|
2022-02-01 21:53:59 +01:00
|
|
|
|
|
|
|
__table_args__ = (db.UniqueConstraint("game_id", "package_id", name="_package_game_support_uc"),)
|
|
|
|
|
|
|
|
|
2018-03-19 19:25:13 +01:00
|
|
|
class Package(db.Model):
|
2020-12-09 19:35:11 +01:00
|
|
|
query_class = PackageQuery
|
2019-01-29 04:00:01 +01:00
|
|
|
|
2018-03-20 01:44:47 +01:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# Basic details
|
2020-12-10 17:49:37 +01:00
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
2020-12-09 19:35:11 +01:00
|
|
|
author = db.relationship("User", back_populates="packages", foreign_keys=[author_id])
|
|
|
|
|
2020-01-18 18:54:46 +01:00
|
|
|
name = db.Column(db.Unicode(100), nullable=False)
|
2019-01-29 04:00:01 +01:00
|
|
|
title = db.Column(db.Unicode(100), nullable=False)
|
|
|
|
short_desc = db.Column(db.Unicode(200), nullable=False)
|
|
|
|
desc = db.Column(db.UnicodeText, nullable=True)
|
2020-12-10 17:49:37 +01:00
|
|
|
type = db.Column(db.Enum(PackageType), nullable=False)
|
2019-01-28 20:01:37 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2020-07-18 02:22:52 +02:00
|
|
|
approved_at = db.Column(db.DateTime, nullable=True, default=None)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2022-06-25 04:44:25 +02:00
|
|
|
name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$' AND name != '_game'")
|
2020-01-18 18:54:46 +01:00
|
|
|
|
2020-12-04 03:23:04 +01:00
|
|
|
search_vector = db.Column(TSVectorType("name", "title", "short_desc", "desc",
|
2022-09-14 23:12:43 +02:00
|
|
|
weights={ "name": "A", "title": "B", "short_desc": "C" }))
|
2019-01-29 04:00:01 +01:00
|
|
|
|
2023-10-02 01:32:20 +02:00
|
|
|
__table_args__ = (db.UniqueConstraint("author_id", "name", name="_package_uc"),)
|
|
|
|
|
2018-05-29 21:17:18 +02:00
|
|
|
license_id = db.Column(db.Integer, db.ForeignKey("license.id"), nullable=False, default=1)
|
|
|
|
license = db.relationship("License", foreign_keys=[license_id])
|
|
|
|
media_license_id = db.Column(db.Integer, db.ForeignKey("license.id"), nullable=False, default=1)
|
|
|
|
media_license = db.relationship("License", foreign_keys=[media_license_id])
|
2018-04-03 15:13:11 +02:00
|
|
|
|
2023-05-12 00:08:01 +02:00
|
|
|
ck_license_txp = db.CheckConstraint("type != 'TXP' OR license_id = media_license_id")
|
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
state = db.Column(db.Enum(PackageState), nullable=False, default=PackageState.WIP)
|
|
|
|
dev_state = db.Column(db.Enum(PackageDevState), nullable=True, default=None)
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def approved(self):
|
|
|
|
return self.state == PackageState.APPROVED
|
2018-03-21 20:03:14 +01:00
|
|
|
|
2020-07-09 02:26:01 +02:00
|
|
|
score = db.Column(db.Float, nullable=False, default=0)
|
|
|
|
score_downloads = db.Column(db.Float, nullable=False, default=0)
|
2020-07-09 02:11:50 +02:00
|
|
|
downloads = db.Column(db.Integer, nullable=False, default=0)
|
2018-07-04 02:05:32 +02:00
|
|
|
|
2018-06-11 23:49:25 +02:00
|
|
|
review_thread_id = db.Column(db.Integer, db.ForeignKey("thread.id"), nullable=True, default=None)
|
2022-06-01 18:29:56 +02:00
|
|
|
review_thread = db.relationship("Thread", uselist=False, foreign_keys=[review_thread_id],
|
|
|
|
back_populates="is_review_thread", post_update=True)
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2023-06-18 22:11:17 +02:00
|
|
|
# Supports all games by default, may have unsupported games
|
|
|
|
supports_all_games = db.Column(db.Boolean, nullable=False, default=False)
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
# Downloads
|
2018-03-20 01:44:47 +01:00
|
|
|
repo = db.Column(db.String(200), nullable=True)
|
|
|
|
website = db.Column(db.String(200), nullable=True)
|
2018-03-18 18:43:30 +01:00
|
|
|
issueTracker = db.Column(db.String(200), nullable=True)
|
2018-05-23 20:40:37 +02:00
|
|
|
forums = db.Column(db.Integer, nullable=True)
|
2022-01-25 21:48:37 +01:00
|
|
|
video_url = db.Column(db.String(200), nullable=True, default=None)
|
2023-03-05 19:13:07 +01:00
|
|
|
donate_url = db.Column(db.String(200), nullable=True, default=None)
|
2024-02-25 16:31:17 +01:00
|
|
|
translation_url = db.Column(db.String(200), nullable=True)
|
2023-03-05 19:13:07 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def donate_url_actual(self):
|
|
|
|
return self.donate_url or self.author.donate_url
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2022-06-25 03:27:51 +02:00
|
|
|
enable_game_support_detection = db.Column(db.Boolean, nullable=False, default=True)
|
|
|
|
|
2024-02-25 16:31:17 +01:00
|
|
|
translations = db.relationship("PackageTranslation", back_populates="package",
|
|
|
|
lazy="dynamic", order_by=db.asc("package_translation_language_id"),
|
|
|
|
cascade="all, delete, delete-orphan")
|
|
|
|
|
2021-07-25 04:54:08 +02:00
|
|
|
provides = db.relationship("MetaPackage", secondary=PackageProvides, order_by=db.asc("name"), back_populates="packages")
|
2018-05-21 23:20:28 +02:00
|
|
|
|
2020-12-09 19:35:11 +01:00
|
|
|
dependencies = db.relationship("Dependency", back_populates="depender", lazy="dynamic", foreign_keys=[Dependency.depender_id])
|
2018-03-26 12:28:34 +02:00
|
|
|
|
2022-02-01 21:53:59 +01:00
|
|
|
supported_games = db.relationship("PackageGameSupport", back_populates="package", lazy="dynamic",
|
2023-11-13 18:44:40 +01:00
|
|
|
foreign_keys=[PackageGameSupport.package_id], cascade="all, delete, delete-orphan")
|
2022-02-01 21:53:59 +01:00
|
|
|
|
|
|
|
game_supported_mods = db.relationship("PackageGameSupport", back_populates="game", lazy="dynamic",
|
2023-11-13 18:44:40 +01:00
|
|
|
foreign_keys=[PackageGameSupport.game_id], cascade="all, delete, delete-orphan")
|
2022-02-01 21:53:59 +01:00
|
|
|
|
2020-12-10 18:46:10 +01:00
|
|
|
tags = db.relationship("Tag", secondary=Tags, back_populates="packages")
|
2018-03-26 12:28:34 +02:00
|
|
|
|
2020-12-10 18:46:10 +01:00
|
|
|
content_warnings = db.relationship("ContentWarning", secondary=ContentWarnings, back_populates="packages")
|
2020-07-17 21:48:51 +02:00
|
|
|
|
2020-12-09 19:35:11 +01:00
|
|
|
releases = db.relationship("PackageRelease", back_populates="package",
|
2020-12-10 12:25:52 +01:00
|
|
|
lazy="dynamic", order_by=db.desc("package_release_releaseDate"), cascade="all, delete, delete-orphan")
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2020-12-10 23:40:20 +01:00
|
|
|
screenshots = db.relationship("PackageScreenshot", back_populates="package", foreign_keys="PackageScreenshot.package_id",
|
2020-12-10 12:25:52 +01:00
|
|
|
lazy="dynamic", order_by=db.asc("package_screenshot_order"), cascade="all, delete, delete-orphan")
|
2018-03-23 20:08:55 +01:00
|
|
|
|
2021-01-30 20:32:04 +01:00
|
|
|
main_screenshot = db.relationship("PackageScreenshot", uselist=False, foreign_keys="PackageScreenshot.package_id",
|
2022-01-27 19:43:10 +01:00
|
|
|
lazy=True, order_by=db.asc("package_screenshot_order"), viewonly=True,
|
2021-01-30 20:32:04 +01:00
|
|
|
primaryjoin="and_(Package.id==PackageScreenshot.package_id, PackageScreenshot.approved)")
|
|
|
|
|
2020-12-10 23:40:20 +01:00
|
|
|
cover_image_id = db.Column(db.Integer, db.ForeignKey("package_screenshot.id"), nullable=True, default=None)
|
2024-06-01 16:35:10 +02:00
|
|
|
cover_image = db.relationship("PackageScreenshot", uselist=False, foreign_keys=[cover_image_id], post_update=True)
|
2020-12-10 23:40:20 +01:00
|
|
|
|
2020-12-10 18:46:10 +01:00
|
|
|
maintainers = db.relationship("User", secondary=maintainers)
|
2020-07-08 23:45:24 +02:00
|
|
|
|
2020-12-10 12:25:52 +01:00
|
|
|
threads = db.relationship("Thread", back_populates="package", order_by=db.desc("thread_created_at"),
|
2020-12-15 20:05:29 +01:00
|
|
|
foreign_keys="Thread.package_id", cascade="all, delete, delete-orphan", lazy="dynamic")
|
2020-12-10 12:25:52 +01:00
|
|
|
|
2023-11-18 12:25:22 +01:00
|
|
|
reviews = db.relationship("PackageReview", back_populates="package", lazy="dynamic",
|
|
|
|
order_by=[db.desc("package_review_score"), db.desc("package_review_created_at")],
|
2020-12-10 12:25:52 +01:00
|
|
|
cascade="all, delete, delete-orphan")
|
|
|
|
|
2021-02-08 01:46:39 +01:00
|
|
|
audit_log_entries = db.relationship("AuditLogEntry", foreign_keys="AuditLogEntry.package_id",
|
|
|
|
lazy="dynamic", back_populates="package", order_by=db.desc("audit_log_entry_created_at"))
|
2020-12-10 12:25:52 +01:00
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
notifications = db.relationship("Notification", foreign_keys="Notification.package_id",
|
|
|
|
back_populates="package", cascade="all, delete, delete-orphan")
|
|
|
|
|
2020-12-10 12:25:52 +01:00
|
|
|
tokens = db.relationship("APIToken", foreign_keys="APIToken.package_id", back_populates="package",
|
2020-12-10 18:46:10 +01:00
|
|
|
cascade="all, delete, delete-orphan")
|
2020-12-09 19:35:11 +01:00
|
|
|
|
2020-12-15 20:05:29 +01:00
|
|
|
update_config = db.relationship("PackageUpdateConfig", uselist=False, back_populates="package",
|
|
|
|
cascade="all, delete, delete-orphan")
|
|
|
|
|
2021-07-24 03:30:43 +02:00
|
|
|
aliases = db.relationship("PackageAlias", foreign_keys="PackageAlias.package_id",
|
|
|
|
back_populates="package", cascade="all, delete, delete-orphan")
|
|
|
|
|
2022-11-06 11:32:46 +01:00
|
|
|
daily_stats = db.relationship("PackageDailyStats", foreign_keys="PackageDailyStats.package_id",
|
|
|
|
back_populates="package", cascade="all, delete, delete-orphan", lazy="dynamic")
|
|
|
|
|
2018-05-25 18:12:34 +02:00
|
|
|
def __init__(self, package=None):
|
|
|
|
if package is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.author_id = package.author_id
|
|
|
|
self.created_at = package.created_at
|
2020-09-16 18:51:03 +02:00
|
|
|
self.state = package.state
|
2018-05-25 18:12:34 +02:00
|
|
|
|
2020-07-11 17:56:36 +02:00
|
|
|
self.maintainers.append(self.author)
|
|
|
|
|
2022-02-01 22:22:28 +01:00
|
|
|
@classmethod
|
|
|
|
def get_by_key(cls, key):
|
|
|
|
parts = key.split("/")
|
|
|
|
if len(parts) != 2:
|
|
|
|
return None
|
|
|
|
|
2023-08-19 01:33:04 +02:00
|
|
|
name = parts[1]
|
|
|
|
if name.endswith("_game"):
|
|
|
|
name = name[:-5]
|
|
|
|
|
2024-03-23 17:17:26 +01:00
|
|
|
return Package.query.filter(or_(Package.name == name, Package.name == name + "_game"),
|
2023-08-19 01:33:04 +02:00
|
|
|
Package.author.has(username=parts[0])).first()
|
2022-02-01 22:22:28 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_id(self):
|
2020-06-05 05:29:52 +02:00
|
|
|
return "{}/{}".format(self.author.username, self.name)
|
|
|
|
|
2024-03-23 17:17:26 +01:00
|
|
|
@property
|
|
|
|
def normalised_name(self):
|
|
|
|
name = self.name
|
|
|
|
if name.endswith("_game"):
|
|
|
|
name = name[:-5]
|
|
|
|
return name
|
|
|
|
|
2024-02-25 19:48:42 +01:00
|
|
|
def get_translated(self, lang=None, load_desc=True):
|
2024-02-25 19:24:30 +01:00
|
|
|
if lang is None:
|
|
|
|
locale = get_locale()
|
|
|
|
if locale:
|
|
|
|
lang = locale.language
|
|
|
|
else:
|
|
|
|
lang = "en"
|
|
|
|
|
2024-02-25 19:43:27 +01:00
|
|
|
translation: typing.Optional[PackageTranslation] = None
|
|
|
|
if lang != "en":
|
|
|
|
translation = self.translations.filter_by(language_id=lang).first()
|
|
|
|
|
2024-02-25 19:24:30 +01:00
|
|
|
if translation is None:
|
|
|
|
return {
|
|
|
|
"title": self.title,
|
|
|
|
"short_desc": self.short_desc,
|
2024-02-25 19:48:42 +01:00
|
|
|
"desc": self.desc if load_desc else None,
|
2024-02-25 19:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"title": translation.title or self.title,
|
|
|
|
"short_desc": translation.short_desc or self.short_desc,
|
2024-02-25 19:48:42 +01:00
|
|
|
"desc": (translation.desc or self.desc) if load_desc else None,
|
2024-02-25 19:24:30 +01:00
|
|
|
}
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_sorted_dependencies(self, is_hard=None):
|
2020-01-21 23:59:44 +01:00
|
|
|
query = self.dependencies
|
|
|
|
if is_hard is not None:
|
|
|
|
query = query.filter_by(optional=not is_hard)
|
|
|
|
|
|
|
|
deps = query.all()
|
2023-06-19 22:27:49 +02:00
|
|
|
deps.sort(key=lambda x: x.get_name())
|
2020-01-21 23:59:44 +01:00
|
|
|
return deps
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_sorted_hard_dependencies(self):
|
|
|
|
return self.get_sorted_dependencies(True)
|
2020-01-21 23:59:44 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_sorted_optional_dependencies(self):
|
|
|
|
return self.get_sorted_dependencies(False)
|
2020-01-21 23:59:44 +01:00
|
|
|
|
2024-03-27 20:03:48 +01:00
|
|
|
def get_sorted_game_support(self) -> list[PackageGameSupport]:
|
2023-06-18 22:11:17 +02:00
|
|
|
query = self.supported_games.filter(PackageGameSupport.game.has(state=PackageState.APPROVED))
|
2022-06-25 01:32:32 +02:00
|
|
|
|
|
|
|
supported = query.all()
|
2022-06-25 00:55:44 +02:00
|
|
|
supported.sort(key=lambda x: -(x.game.score + 100000*x.confidence))
|
2022-02-01 21:53:59 +01:00
|
|
|
return supported
|
|
|
|
|
2024-03-27 20:03:48 +01:00
|
|
|
def get_sorted_game_support_pair(self) -> list[list[PackageGameSupport]]:
|
2023-06-18 22:11:17 +02:00
|
|
|
supported = self.get_sorted_game_support()
|
|
|
|
return [
|
|
|
|
[x for x in supported if x.supports],
|
|
|
|
[x for x in supported if not x.supports],
|
|
|
|
]
|
|
|
|
|
|
|
|
def has_game_support_confirmed(self):
|
|
|
|
return self.supports_all_games or \
|
|
|
|
self.supported_games.filter(PackageGameSupport.confidence > 1).count() > 0
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_key_dict(self):
|
2019-09-03 01:42:51 +02:00
|
|
|
return {
|
|
|
|
"name": self.name,
|
2022-01-12 21:50:01 +01:00
|
|
|
"author": self.author.username,
|
2023-06-18 22:56:19 +02:00
|
|
|
"type": self.type.to_name(),
|
2019-09-03 01:42:51 +02:00
|
|
|
}
|
|
|
|
|
2024-02-25 19:43:27 +01:00
|
|
|
def as_short_dict(self, base_url, version=None, release_id=None, no_load=False, lang="en"):
|
2023-11-10 19:57:49 +01:00
|
|
|
tnurl = self.get_thumb_url(1, format="png")
|
2021-01-30 20:32:04 +01:00
|
|
|
|
2021-01-30 20:49:15 +01:00
|
|
|
if release_id is None and no_load == False:
|
2023-06-18 22:56:19 +02:00
|
|
|
release = self.get_download_release(version=version)
|
2021-01-30 20:32:04 +01:00
|
|
|
release_id = release and release.id
|
|
|
|
|
2024-02-25 19:43:27 +01:00
|
|
|
meta = self.get_translated(lang, load_desc=False)
|
|
|
|
short_desc = meta["short_desc"]
|
2021-12-23 12:56:03 +01:00
|
|
|
if self.dev_state == PackageDevState.WIP:
|
2024-02-25 19:43:27 +01:00
|
|
|
short_desc = gettext("Work in Progress") + ". " + self.short_desc
|
2021-12-23 12:56:03 +01:00
|
|
|
|
2021-07-24 03:30:43 +02:00
|
|
|
ret = {
|
2018-03-23 18:33:34 +01:00
|
|
|
"name": self.name,
|
2024-02-25 19:43:27 +01:00
|
|
|
"title": meta["title"],
|
2020-05-27 18:27:41 +02:00
|
|
|
"author": self.author.username,
|
2021-12-23 12:56:03 +01:00
|
|
|
"short_description": short_desc,
|
2023-06-18 22:56:19 +02:00
|
|
|
"type": self.type.to_name(),
|
2021-01-30 20:32:04 +01:00
|
|
|
"release": release_id,
|
2021-07-24 03:30:43 +02:00
|
|
|
"thumbnail": (base_url + tnurl) if tnurl is not None else None,
|
2023-06-18 22:56:19 +02:00
|
|
|
"aliases": [alias.as_dict() for alias in self.aliases],
|
2018-06-15 23:57:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-24 03:30:43 +02:00
|
|
|
if not ret["aliases"]:
|
|
|
|
del ret["aliases"]
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2024-04-01 18:43:16 +02:00
|
|
|
def as_dict(self, base_url, version=None, lang="en", screenshots_dict=False):
|
2023-11-10 19:57:49 +01:00
|
|
|
tnurl = self.get_thumb_url(1, format="png")
|
2023-06-18 22:56:19 +02:00
|
|
|
release = self.get_download_release(version=version)
|
2024-02-25 19:43:27 +01:00
|
|
|
meta = self.get_translated(lang)
|
|
|
|
|
2024-04-01 18:43:16 +02:00
|
|
|
if screenshots_dict:
|
|
|
|
screenshots = [ss.as_short_dict(base_url) for ss in self.screenshots]
|
|
|
|
else:
|
|
|
|
screenshots = [base_url + ss.url for ss in self.screenshots]
|
|
|
|
|
2018-06-15 23:57:43 +02:00
|
|
|
return {
|
2020-05-27 18:27:41 +02:00
|
|
|
"author": self.author.username,
|
2021-02-02 23:47:46 +01:00
|
|
|
"maintainers": [x.username for x in self.maintainers],
|
|
|
|
|
|
|
|
"state": self.state.name,
|
2021-12-23 12:58:39 +01:00
|
|
|
"dev_state": self.dev_state.name if self.dev_state else None,
|
2021-02-02 23:47:46 +01:00
|
|
|
|
2018-06-15 23:57:43 +02:00
|
|
|
"name": self.name,
|
2024-02-25 19:43:27 +01:00
|
|
|
"title": meta["title"],
|
|
|
|
"short_description": meta["short_desc"],
|
|
|
|
"long_description": meta["desc"],
|
2023-06-18 22:56:19 +02:00
|
|
|
"type": self.type.to_name(),
|
2020-01-24 21:21:40 +01:00
|
|
|
"created_at": self.created_at.isoformat(),
|
2018-06-15 23:57:43 +02:00
|
|
|
|
2018-04-03 15:13:11 +02:00
|
|
|
"license": self.license.name,
|
2018-11-25 14:21:24 +01:00
|
|
|
"media_license": self.media_license.name,
|
2018-06-15 23:57:43 +02:00
|
|
|
|
2018-03-23 18:33:34 +01:00
|
|
|
"repo": self.repo,
|
2018-06-15 23:57:43 +02:00
|
|
|
"website": self.website,
|
2018-11-25 14:21:24 +01:00
|
|
|
"issue_tracker": self.issueTracker,
|
2018-06-15 23:57:43 +02:00
|
|
|
"forums": self.forums,
|
2022-01-25 21:48:37 +01:00
|
|
|
"video_url": self.video_url,
|
2023-03-05 19:13:07 +01:00
|
|
|
"donate_url": self.donate_url_actual,
|
2024-02-25 19:05:29 +01:00
|
|
|
"translation_url": self.translation_url,
|
2018-06-15 23:57:43 +02:00
|
|
|
|
2023-05-12 01:45:27 +02:00
|
|
|
"tags": sorted([x.name for x in self.tags]),
|
|
|
|
"content_warnings": sorted([x.name for x in self.content_warnings]),
|
2021-02-02 23:47:46 +01:00
|
|
|
|
2023-05-12 01:45:27 +02:00
|
|
|
"provides": sorted([x.name for x in self.provides]),
|
2018-06-15 23:57:43 +02:00
|
|
|
"thumbnail": (base_url + tnurl) if tnurl is not None else None,
|
2024-04-01 18:43:16 +02:00
|
|
|
"screenshots": screenshots,
|
2018-06-15 23:57:43 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
"url": base_url + self.get_url("packages.download"),
|
2019-03-13 15:37:20 +01:00
|
|
|
"release": release and release.id,
|
2018-07-04 02:05:32 +02:00
|
|
|
|
2020-08-02 18:10:47 +02:00
|
|
|
"score": round(self.score * 10) / 10,
|
2022-02-02 02:11:44 +01:00
|
|
|
"downloads": self.downloads,
|
|
|
|
|
|
|
|
"game_support": [
|
|
|
|
{
|
|
|
|
"supports": support.supports,
|
|
|
|
"confidence": support.confidence,
|
2023-06-18 22:56:19 +02:00
|
|
|
"game": support.game.as_short_dict(base_url, version)
|
2022-02-02 02:11:44 +01:00
|
|
|
} for support in self.supported_games.all()
|
|
|
|
]
|
2018-03-23 18:33:34 +01:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:57:49 +01:00
|
|
|
def get_thumb_or_placeholder(self, level=2, format="webp"):
|
|
|
|
return self.get_thumb_url(level, False, format) or "/static/placeholder.png"
|
2021-01-29 21:21:17 +01:00
|
|
|
|
2023-11-10 19:57:49 +01:00
|
|
|
def get_thumb_url(self, level=2, abs=False, format="webp"):
|
2021-01-30 20:32:04 +01:00
|
|
|
screenshot = self.main_screenshot
|
2023-11-10 19:57:49 +01:00
|
|
|
url = screenshot.get_thumb_url(level, format) if screenshot is not None else None
|
2022-10-25 02:57:38 +02:00
|
|
|
if abs:
|
2020-04-24 00:49:46 +02:00
|
|
|
from app.utils import abs_url
|
2022-10-25 02:57:38 +02:00
|
|
|
return abs_url(url)
|
2020-04-24 00:49:46 +02:00
|
|
|
else:
|
2022-10-25 02:57:38 +02:00
|
|
|
return url
|
|
|
|
|
2023-11-10 19:57:49 +01:00
|
|
|
def get_cover_image_url(self, format="webp"):
|
2022-10-25 02:57:38 +02:00
|
|
|
screenshot = self.cover_image or self.main_screenshot
|
2023-11-10 19:57:49 +01:00
|
|
|
return screenshot and screenshot.get_thumb_url(4, format)
|
2020-04-24 00:49:46 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_url(self, endpoint, absolute=False, **kwargs):
|
2020-04-24 00:49:46 +02:00
|
|
|
if absolute:
|
|
|
|
from app.utils import abs_url_for
|
2021-07-24 05:30:14 +02:00
|
|
|
return abs_url_for(endpoint, author=self.author.username, name=self.name, **kwargs)
|
2020-04-24 00:49:46 +02:00
|
|
|
else:
|
2021-07-24 05:30:14 +02:00
|
|
|
return url_for(endpoint, author=self.author.username, name=self.name, **kwargs)
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_shield_url(self, type):
|
2020-08-02 18:41:06 +02:00
|
|
|
from app.utils import abs_url_for
|
|
|
|
return abs_url_for("packages.shield",
|
|
|
|
author=self.author.username, name=self.name, type=type)
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def make_shield(self, type):
|
2020-08-02 18:41:06 +02:00
|
|
|
return "[![ContentDB]({})]({})" \
|
2023-06-18 22:56:19 +02:00
|
|
|
.format(self.get_shield_url(type), self.get_url("packages.view", True))
|
2021-05-04 04:15:26 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_set_state_url(self, state):
|
2020-09-16 18:51:03 +02:00
|
|
|
if type(state) == str:
|
2020-12-04 23:05:10 +01:00
|
|
|
state = PackageState[state]
|
2020-09-16 18:51:03 +02:00
|
|
|
elif type(state) != PackageState:
|
2024-03-23 17:17:26 +01:00
|
|
|
raise Exception("Unknown state given to Package.get_set_state_url()")
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
return url_for("packages.move_to_state",
|
|
|
|
author=self.author.username, name=self.name, state=state.name.lower())
|
2018-03-21 20:03:14 +01:00
|
|
|
|
2024-04-01 18:32:12 +02:00
|
|
|
def get_download_release(self, version=None) -> typing.Optional["PackageRelease"]:
|
2018-03-23 17:20:56 +01:00
|
|
|
for rel in self.releases:
|
2019-03-13 15:37:20 +01:00
|
|
|
if rel.approved and (version is None or
|
2020-12-04 03:23:04 +01:00
|
|
|
((rel.min_rel is None or rel.min_rel_id <= version.id) and
|
2023-06-18 22:56:19 +02:00
|
|
|
(rel.max_rel is None or rel.max_rel_id >= version.id))):
|
2018-03-23 17:20:56 +01:00
|
|
|
return rel
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def check_perm(self, user, perm):
|
2018-03-20 04:16:46 +01:00
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
2018-03-20 04:54:38 +01:00
|
|
|
elif type(perm) != Permission:
|
2023-06-18 22:56:19 +02:00
|
|
|
raise Exception("Unknown permission given to Package.check_perm()")
|
2018-03-20 04:16:46 +01:00
|
|
|
|
2023-08-16 02:00:51 +02:00
|
|
|
if perm == Permission.VIEW_PACKAGE:
|
|
|
|
return self.state == PackageState.APPROVED or self.check_perm(user, Permission.EDIT_PACKAGE)
|
|
|
|
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
is_owner = user == self.author
|
2023-06-19 22:27:49 +02:00
|
|
|
is_maintainer = is_owner or user.rank.at_least(UserRank.EDITOR) or user in self.maintainers
|
|
|
|
is_approver = user.rank.at_least(UserRank.APPROVER)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2022-02-01 15:53:51 +01:00
|
|
|
if perm == Permission.CREATE_THREAD:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.NEW_MEMBER)
|
2019-01-28 20:01:37 +01:00
|
|
|
|
2018-03-20 04:45:01 +01:00
|
|
|
# Members can edit their own packages, and editors can edit any packages
|
2020-07-08 23:45:24 +02:00
|
|
|
elif perm == Permission.MAKE_RELEASE or perm == Permission.ADD_SCREENSHOTS:
|
2023-06-18 22:56:19 +02:00
|
|
|
return is_maintainer
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2021-08-16 19:57:05 +02:00
|
|
|
elif perm == Permission.EDIT_PACKAGE:
|
2023-06-19 22:27:49 +02:00
|
|
|
return is_maintainer and user.rank.at_least(UserRank.NEW_MEMBER)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2021-08-16 19:57:05 +02:00
|
|
|
elif perm == Permission.APPROVE_RELEASE:
|
2023-06-19 22:27:49 +02:00
|
|
|
return (is_maintainer or is_approver) and user.rank.at_least(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER)
|
2021-08-16 19:57:05 +02:00
|
|
|
|
2023-10-15 23:13:35 +02:00
|
|
|
# Anyone can change the package name when not approved
|
2019-07-29 21:29:55 +02:00
|
|
|
elif perm == Permission.CHANGE_NAME:
|
2023-10-15 23:13:35 +02:00
|
|
|
return not self.approved
|
2019-07-29 21:29:55 +02:00
|
|
|
|
2018-07-28 15:25:51 +02:00
|
|
|
# Editors can change authors and approve new packages
|
|
|
|
elif perm == Permission.APPROVE_NEW or perm == Permission.CHANGE_AUTHOR:
|
2023-06-18 22:56:19 +02:00
|
|
|
return is_approver
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2020-04-21 18:06:27 +02:00
|
|
|
elif perm == Permission.APPROVE_SCREENSHOT:
|
2023-06-18 22:56:19 +02:00
|
|
|
return (is_maintainer or is_approver) and \
|
2023-06-19 22:27:49 +02:00
|
|
|
user.rank.at_least(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER)
|
2020-07-08 23:45:24 +02:00
|
|
|
|
2021-08-16 19:57:05 +02:00
|
|
|
elif perm == Permission.EDIT_MAINTAINERS or perm == Permission.DELETE_PACKAGE:
|
2023-06-19 22:27:49 +02:00
|
|
|
return is_owner or user.rank.at_least(UserRank.EDITOR)
|
2020-07-10 21:13:48 +02:00
|
|
|
|
2021-08-16 19:57:05 +02:00
|
|
|
elif perm == Permission.UNAPPROVE_PACKAGE:
|
2023-06-19 22:27:49 +02:00
|
|
|
return is_owner or user.rank.at_least(UserRank.APPROVER)
|
2021-08-16 19:57:05 +02:00
|
|
|
|
2020-07-10 21:13:48 +02:00
|
|
|
elif perm == Permission.CHANGE_RELEASE_URL:
|
2023-06-19 22:27:49 +02:00
|
|
|
return user.rank.at_least(UserRank.MODERATOR)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2018-03-20 04:16:46 +01:00
|
|
|
else:
|
2018-03-20 04:54:38 +01:00
|
|
|
raise Exception("Permission {} is not related to packages".format(perm.name))
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_missing_hard_dependencies_query(self):
|
2020-12-04 01:59:09 +01:00
|
|
|
return MetaPackage.query \
|
|
|
|
.filter(~ MetaPackage.packages.any(state=PackageState.APPROVED)) \
|
|
|
|
.filter(MetaPackage.dependencies.any(optional=False, depender=self)) \
|
|
|
|
.order_by(db.asc(MetaPackage.name))
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_missing_hard_dependencies(self):
|
|
|
|
return [mp.name for mp in self.get_missing_hard_dependencies_query().all()]
|
2020-09-16 18:51:03 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_next_states(self, user):
|
2024-03-23 17:17:26 +01:00
|
|
|
from app.logic.package_approval import can_move_to_state
|
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
states = []
|
|
|
|
|
|
|
|
for state in PackageState:
|
2024-03-23 17:17:26 +01:00
|
|
|
if can_move_to_state(self, user, state):
|
2020-09-16 18:51:03 +02:00
|
|
|
states.append(state)
|
|
|
|
|
|
|
|
return states
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_score_dict(self):
|
2024-06-01 01:06:25 +02:00
|
|
|
reviews = self.get_review_summary()
|
2020-07-09 05:32:13 +02:00
|
|
|
return {
|
|
|
|
"author": self.author.username,
|
|
|
|
"name": self.name,
|
|
|
|
"score": self.score,
|
|
|
|
"score_downloads": self.score_downloads,
|
|
|
|
"score_reviews": self.score - self.score_downloads,
|
2024-06-01 01:06:25 +02:00
|
|
|
"downloads": self.downloads,
|
|
|
|
"reviews": {
|
|
|
|
"positive": reviews[0],
|
|
|
|
"neutral": reviews[1],
|
|
|
|
"negative": reviews[2],
|
|
|
|
},
|
2020-07-09 05:32:13 +02:00
|
|
|
}
|
2020-07-09 02:26:01 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def recalculate_score(self):
|
|
|
|
review_scores = [ 100 * r.as_weight() for r in self.reviews ]
|
2020-07-09 05:32:13 +02:00
|
|
|
self.score = self.score_downloads + sum(review_scores)
|
2020-07-09 02:26:01 +02:00
|
|
|
|
2023-06-19 23:33:45 +02:00
|
|
|
def get_conf_file_name(self):
|
|
|
|
if self.type == PackageType.MOD:
|
|
|
|
return "mod.conf"
|
|
|
|
elif self.type == PackageType.TXP:
|
|
|
|
return "texture_pack.conf"
|
|
|
|
elif self.type == PackageType.GAME:
|
|
|
|
return "game.conf"
|
|
|
|
|
2023-11-18 12:25:22 +01:00
|
|
|
def get_review_summary(self):
|
|
|
|
from app.models import PackageReview
|
|
|
|
rows = (db.session.query(PackageReview.rating, func.count(PackageReview.id))
|
|
|
|
.select_from(PackageReview)
|
|
|
|
.where(PackageReview.package_id == self.id)
|
|
|
|
.group_by(PackageReview.rating)
|
|
|
|
.all())
|
|
|
|
|
|
|
|
negative = 0
|
|
|
|
neutral = 0
|
|
|
|
positive = 0
|
|
|
|
for rating, count in rows:
|
|
|
|
if rating > 3:
|
|
|
|
positive += count
|
|
|
|
elif rating == 3:
|
|
|
|
neutral += count
|
|
|
|
else:
|
|
|
|
negative += count
|
|
|
|
|
|
|
|
return [positive, neutral, negative]
|
|
|
|
|
2019-11-21 23:16:35 +01:00
|
|
|
|
2024-02-25 16:31:17 +01:00
|
|
|
class Language(db.Model):
|
|
|
|
id = db.Column(db.String(10), primary_key=True)
|
|
|
|
title = db.Column(db.String(100), unique=True, nullable=False)
|
|
|
|
|
2024-02-26 03:09:00 +01:00
|
|
|
packages = db.relationship("Package", secondary="package_translation", lazy="dynamic")
|
|
|
|
|
2024-02-26 02:50:15 +01:00
|
|
|
@property
|
|
|
|
def has_contentdb_translation(self):
|
|
|
|
return self.id in app.config["LANGUAGES"].keys()
|
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
return {
|
|
|
|
"id": self.id,
|
|
|
|
"title": self.title,
|
|
|
|
"has_contentdb_translation": self.has_contentdb_translation,
|
|
|
|
}
|
|
|
|
|
2024-02-25 16:31:17 +01:00
|
|
|
|
|
|
|
class PackageTranslation(db.Model):
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
|
|
|
package = db.relationship("Package", back_populates="translations", foreign_keys=[package_id])
|
|
|
|
|
|
|
|
language_id = db.Column(db.String(10), db.ForeignKey("language.id"), primary_key=True)
|
|
|
|
language = db.relationship("Language", foreign_keys=[language_id])
|
|
|
|
|
|
|
|
title = db.Column(db.Unicode(100), nullable=True)
|
|
|
|
short_desc = db.Column(db.Unicode(200), nullable=True)
|
|
|
|
desc = db.Column(db.UnicodeText, nullable=True)
|
|
|
|
|
|
|
|
|
2018-05-27 17:37:54 +02:00
|
|
|
class MetaPackage(db.Model):
|
2023-06-18 22:56:19 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(100), unique=True, nullable=False)
|
2020-12-09 19:35:11 +01:00
|
|
|
dependencies = db.relationship("Dependency", back_populates="meta_package", lazy="dynamic")
|
2023-06-18 22:56:19 +02:00
|
|
|
packages = db.relationship("Package", lazy="dynamic", back_populates="provides", secondary=PackageProvides)
|
2018-05-27 17:37:54 +02:00
|
|
|
|
2020-07-12 03:43:48 +02:00
|
|
|
mp_name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$'")
|
|
|
|
|
2018-05-27 17:37:54 +02:00
|
|
|
def __init__(self, name=None):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-05-27 18:58:09 +02:00
|
|
|
@staticmethod
|
|
|
|
def ListToSpec(list):
|
|
|
|
return ",".join([str(x) for x in list])
|
|
|
|
|
2018-05-27 21:15:35 +02:00
|
|
|
@staticmethod
|
2020-12-04 03:23:04 +01:00
|
|
|
def GetOrCreate(name, cache):
|
2018-05-27 21:15:35 +02:00
|
|
|
mp = cache.get(name)
|
|
|
|
if mp is None:
|
|
|
|
mp = MetaPackage.query.filter_by(name=name).first()
|
|
|
|
|
|
|
|
if mp is None:
|
|
|
|
mp = MetaPackage(name)
|
|
|
|
db.session.add(mp)
|
|
|
|
|
|
|
|
cache[name] = mp
|
|
|
|
return mp
|
|
|
|
|
2018-05-27 18:58:09 +02:00
|
|
|
@staticmethod
|
2020-12-04 03:23:04 +01:00
|
|
|
def SpecToList(spec, cache):
|
2018-05-27 18:58:09 +02:00
|
|
|
retval = []
|
|
|
|
arr = spec.split(",")
|
|
|
|
|
|
|
|
import re
|
|
|
|
pattern = re.compile("^([a-z0-9_]+)$")
|
|
|
|
|
|
|
|
for x in arr:
|
|
|
|
x = x.strip()
|
|
|
|
if x == "":
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not pattern.match(x):
|
|
|
|
continue
|
|
|
|
|
2018-05-27 21:15:35 +02:00
|
|
|
retval.append(MetaPackage.GetOrCreate(x, cache))
|
2018-05-27 18:58:09 +02:00
|
|
|
|
|
|
|
return retval
|
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
|
|
|
|
class ContentWarning(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(100), unique=True, nullable=False)
|
|
|
|
title = db.Column(db.String(100), nullable=False)
|
|
|
|
description = db.Column(db.String(500), nullable=False)
|
|
|
|
|
2020-12-09 19:35:11 +01:00
|
|
|
packages = db.relationship("Package", back_populates="content_warnings", secondary=ContentWarnings)
|
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
def __init__(self, title, description=""):
|
|
|
|
self.title = title
|
|
|
|
self.description = description
|
|
|
|
|
|
|
|
import re
|
|
|
|
regex = re.compile("[^a-z_]")
|
|
|
|
self.name = regex.sub("", self.title.lower().replace(" ", "_"))
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_dict(self):
|
2021-02-03 00:58:59 +01:00
|
|
|
description = self.description if self.description != "" else None
|
|
|
|
return { "name": self.name, "title": self.title, "description": description }
|
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
|
2018-03-26 12:28:34 +02:00
|
|
|
class Tag(db.Model):
|
2020-07-15 20:06:00 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2018-03-26 13:42:35 +02:00
|
|
|
name = db.Column(db.String(100), unique=True, nullable=False)
|
2018-03-26 12:28:34 +02:00
|
|
|
title = db.Column(db.String(100), nullable=False)
|
2020-07-17 23:08:34 +02:00
|
|
|
description = db.Column(db.String(500), nullable=True, default=None)
|
2020-07-15 20:06:00 +02:00
|
|
|
backgroundColor = db.Column(db.String(6), nullable=False)
|
|
|
|
textColor = db.Column(db.String(6), nullable=False)
|
|
|
|
views = db.Column(db.Integer, nullable=False, default=0)
|
2018-03-26 12:28:34 +02:00
|
|
|
|
2020-12-09 19:35:11 +01:00
|
|
|
packages = db.relationship("Package", back_populates="tags", secondary=Tags)
|
|
|
|
|
2018-03-26 12:28:34 +02:00
|
|
|
def __init__(self, title, backgroundColor="000000", textColor="ffffff"):
|
|
|
|
self.title = title
|
|
|
|
self.backgroundColor = backgroundColor
|
|
|
|
self.textColor = textColor
|
|
|
|
|
|
|
|
import re
|
2018-05-21 23:20:28 +02:00
|
|
|
regex = re.compile("[^a-z_]")
|
2018-03-26 13:42:35 +02:00
|
|
|
self.name = regex.sub("", self.title.lower().replace(" ", "_"))
|
2018-03-26 12:28:34 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_dict(self):
|
2021-01-16 01:34:09 +01:00
|
|
|
description = self.description if self.description != "" else None
|
2022-01-29 20:26:55 +01:00
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"title": self.title,
|
|
|
|
"description": description,
|
|
|
|
"views": self.views,
|
|
|
|
}
|
2021-01-16 01:34:09 +01:00
|
|
|
|
2019-01-28 21:48:07 +01:00
|
|
|
|
|
|
|
class MinetestRelease(db.Model):
|
2019-01-28 22:31:08 +01:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(100), unique=True, nullable=False)
|
|
|
|
protocol = db.Column(db.Integer, nullable=False, default=0)
|
2019-01-28 21:48:07 +01:00
|
|
|
|
2020-01-19 20:09:04 +01:00
|
|
|
def __init__(self, name=None, protocol=0):
|
2019-01-28 21:48:07 +01:00
|
|
|
self.name = name
|
2020-01-19 20:09:04 +01:00
|
|
|
self.protocol = protocol
|
2019-01-28 21:48:07 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_actual(self):
|
2019-01-28 21:48:07 +01:00
|
|
|
return None if self.name == "None" else self
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_dict(self):
|
2021-02-02 18:09:28 +01:00
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"protocol_version": self.protocol,
|
|
|
|
"is_dev": "-dev" in self.name,
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:17:21 +02:00
|
|
|
@classmethod
|
|
|
|
def get(cls, version, protocol_num):
|
|
|
|
if version:
|
2020-06-03 18:46:59 +02:00
|
|
|
parts = version.strip().split(".")
|
2020-06-03 18:17:21 +02:00
|
|
|
if len(parts) >= 2:
|
|
|
|
major_minor = parts[0] + "." + parts[1]
|
2020-06-04 21:40:56 +02:00
|
|
|
query = MinetestRelease.query.filter(MinetestRelease.name.like("{}%".format(major_minor)))
|
2020-06-03 18:17:21 +02:00
|
|
|
if protocol_num:
|
|
|
|
query = query.filter_by(protocol=protocol_num)
|
|
|
|
|
|
|
|
release = query.one_or_none()
|
|
|
|
if release:
|
|
|
|
return release
|
|
|
|
|
|
|
|
if protocol_num:
|
2021-05-03 18:59:23 +02:00
|
|
|
# Find the closest matching release
|
2021-05-03 19:27:20 +02:00
|
|
|
return MinetestRelease.query.order_by(db.desc(MinetestRelease.protocol),
|
|
|
|
db.desc(MinetestRelease.id)) \
|
2021-05-03 18:59:23 +02:00
|
|
|
.filter(MinetestRelease.protocol <= protocol_num).first()
|
2020-06-03 18:17:21 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2019-01-28 21:48:07 +01:00
|
|
|
|
2018-03-20 19:20:30 +01:00
|
|
|
class PackageRelease(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"))
|
2020-12-09 19:35:11 +01:00
|
|
|
package = db.relationship("Package", back_populates="releases", foreign_keys=[package_id])
|
|
|
|
|
2018-03-20 19:20:30 +01:00
|
|
|
title = db.Column(db.String(100), nullable=False)
|
2019-01-28 22:31:08 +01:00
|
|
|
releaseDate = db.Column(db.DateTime, nullable=False)
|
2020-12-15 20:05:29 +01:00
|
|
|
url = db.Column(db.String(200), nullable=False, default="")
|
2018-03-20 19:20:30 +01:00
|
|
|
approved = db.Column(db.Boolean, nullable=False, default=False)
|
2018-05-23 19:19:29 +02:00
|
|
|
task_id = db.Column(db.String(37), nullable=True)
|
2018-07-28 15:48:03 +02:00
|
|
|
commit_hash = db.Column(db.String(41), nullable=True, default=None)
|
2019-01-29 01:43:55 +01:00
|
|
|
downloads = db.Column(db.Integer, nullable=False, default=0)
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2019-01-28 21:48:07 +01:00
|
|
|
min_rel_id = db.Column(db.Integer, db.ForeignKey("minetest_release.id"), nullable=True, server_default=None)
|
|
|
|
min_rel = db.relationship("MinetestRelease", foreign_keys=[min_rel_id])
|
|
|
|
|
|
|
|
max_rel_id = db.Column(db.Integer, db.ForeignKey("minetest_release.id"), nullable=True, server_default=None)
|
|
|
|
max_rel = db.relationship("MinetestRelease", foreign_keys=[max_rel_id])
|
|
|
|
|
2020-01-19 00:10:11 +01:00
|
|
|
# If the release is approved, then the task_id must be null and the url must be present
|
|
|
|
CK_approval_valid = db.CheckConstraint("not approved OR (task_id IS NULL AND (url = '') IS NOT FALSE)")
|
2018-03-21 17:41:34 +01:00
|
|
|
|
2022-01-26 03:51:40 +01:00
|
|
|
@property
|
|
|
|
def file_path(self):
|
|
|
|
return self.url.replace("/uploads/", app.config["UPLOAD_DIR"])
|
|
|
|
|
2023-12-15 17:33:34 +01:00
|
|
|
@property
|
|
|
|
def file_size_bytes(self):
|
|
|
|
path = self.file_path
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
file_stats = os.stat(path)
|
|
|
|
return file_stats.st_size
|
|
|
|
|
|
|
|
@property
|
|
|
|
def file_size(self):
|
|
|
|
size = self.file_size_bytes / 1024
|
|
|
|
if size > 1024:
|
|
|
|
return f"{round(size / 1024, 1)} MB"
|
|
|
|
else:
|
|
|
|
return f"{round(size)} KB"
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_dict(self):
|
2020-01-24 21:21:40 +01:00
|
|
|
return {
|
|
|
|
"id": self.id,
|
|
|
|
"title": self.title,
|
|
|
|
"url": self.url if self.url != "" else None,
|
|
|
|
"release_date": self.releaseDate.isoformat(),
|
|
|
|
"commit": self.commit_hash,
|
|
|
|
"downloads": self.downloads,
|
2023-06-18 22:56:19 +02:00
|
|
|
"min_minetest_version": self.min_rel and self.min_rel.as_dict(),
|
|
|
|
"max_minetest_version": self.max_rel and self.max_rel.as_dict(),
|
2023-12-15 17:33:34 +01:00
|
|
|
"size": self.file_size_bytes,
|
2020-01-24 21:21:40 +01:00
|
|
|
}
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_long_dict(self):
|
2021-03-05 13:55:21 +01:00
|
|
|
return {
|
|
|
|
"id": self.id,
|
|
|
|
"title": self.title,
|
|
|
|
"url": self.url if self.url != "" else None,
|
|
|
|
"release_date": self.releaseDate.isoformat(),
|
|
|
|
"commit": self.commit_hash,
|
|
|
|
"downloads": self.downloads,
|
2023-06-18 22:56:19 +02:00
|
|
|
"min_minetest_version": self.min_rel and self.min_rel.as_dict(),
|
|
|
|
"max_minetest_version": self.max_rel and self.max_rel.as_dict(),
|
2023-12-15 17:33:34 +01:00
|
|
|
"package": self.package.as_key_dict(),
|
|
|
|
"size": self.file_size_bytes,
|
2021-03-05 13:55:21 +01:00
|
|
|
}
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_edit_url(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.edit_release",
|
2018-03-21 17:41:34 +01:00
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_delete_url(self):
|
2020-01-19 01:02:33 +01:00
|
|
|
return url_for("packages.delete_release",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_download_url(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.download_release",
|
2018-07-28 19:33:36 +02:00
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2018-03-20 19:20:30 +01:00
|
|
|
def __init__(self):
|
2019-01-28 20:01:37 +01:00
|
|
|
self.releaseDate = datetime.datetime.now()
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_download_filename(self):
|
2021-05-04 23:06:31 +02:00
|
|
|
return f"{self.package.name}_{self.id}.zip"
|
|
|
|
|
2019-03-29 21:47:48 +01:00
|
|
|
def approve(self, user):
|
2023-06-18 22:56:19 +02:00
|
|
|
if not self.check_perm(user, Permission.APPROVE_RELEASE):
|
2019-03-29 21:47:48 +01:00
|
|
|
return False
|
|
|
|
|
2021-01-30 02:19:43 +01:00
|
|
|
if self.approved:
|
|
|
|
return True
|
|
|
|
|
2020-01-18 02:38:00 +01:00
|
|
|
assert self.task_id is None and self.url is not None and self.url != ""
|
2019-03-29 21:47:48 +01:00
|
|
|
|
|
|
|
self.approved = True
|
2020-12-15 22:44:29 +01:00
|
|
|
|
|
|
|
if self.package.update_config:
|
2021-01-29 23:20:44 +01:00
|
|
|
self.package.update_config.outdated_at = None
|
2020-12-15 23:40:49 +01:00
|
|
|
self.package.update_config.last_commit = self.commit_hash
|
2020-12-15 22:44:29 +01:00
|
|
|
|
2019-03-29 21:47:48 +01:00
|
|
|
return True
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def check_perm(self, user, perm):
|
2024-01-10 01:07:51 +01:00
|
|
|
if not hasattr(user, "rank") or user.is_banned:
|
2020-01-19 01:02:33 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
2023-06-18 22:56:19 +02:00
|
|
|
raise Exception("Unknown permission given to PackageRelease.check_perm()")
|
2020-01-19 01:02:33 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
is_maintainer = user == self.package.author or user in self.package.maintainers
|
2020-01-19 01:02:33 +01:00
|
|
|
|
|
|
|
if perm == Permission.DELETE_RELEASE:
|
2023-06-19 22:27:49 +02:00
|
|
|
if user.rank.at_least(UserRank.ADMIN):
|
2020-01-19 01:02:33 +01:00
|
|
|
return True
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
if not (is_maintainer or user.rank.at_least(UserRank.EDITOR)):
|
2020-01-19 01:02:33 +01:00
|
|
|
return False
|
|
|
|
|
2020-01-19 02:23:56 +01:00
|
|
|
if not self.package.approved or self.task_id is not None:
|
2020-01-19 01:02:33 +01:00
|
|
|
return True
|
|
|
|
|
2021-03-07 16:00:36 +01:00
|
|
|
count = self.package.releases \
|
2020-01-19 01:02:33 +01:00
|
|
|
.filter(PackageRelease.id > self.id) \
|
|
|
|
.count()
|
|
|
|
|
|
|
|
return count > 0
|
2021-03-07 16:00:36 +01:00
|
|
|
elif perm == Permission.APPROVE_RELEASE:
|
2023-12-29 12:36:25 +01:00
|
|
|
return is_maintainer or user.rank.at_least(UserRank.APPROVER)
|
2020-01-19 01:02:33 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to releases".format(perm.name))
|
|
|
|
|
2018-07-28 17:05:09 +02:00
|
|
|
|
2018-03-26 11:52:25 +02:00
|
|
|
class PackageScreenshot(db.Model):
|
2022-01-26 03:51:40 +01:00
|
|
|
HARD_MIN_SIZE = (920, 517)
|
|
|
|
SOFT_MIN_SIZE = (1280, 720)
|
|
|
|
|
2018-03-26 11:52:25 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2020-12-09 19:35:11 +01:00
|
|
|
|
2020-12-10 17:49:37 +01:00
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=False)
|
2020-12-09 19:35:11 +01:00
|
|
|
package = db.relationship("Package", back_populates="screenshots", foreign_keys=[package_id])
|
|
|
|
|
2020-12-06 04:35:25 +01:00
|
|
|
order = db.Column(db.Integer, nullable=False, default=0)
|
2018-03-26 11:52:25 +02:00
|
|
|
title = db.Column(db.String(100), nullable=False)
|
|
|
|
url = db.Column(db.String(100), nullable=False)
|
2018-05-25 19:57:30 +02:00
|
|
|
approved = db.Column(db.Boolean, nullable=False, default=False)
|
2021-02-02 18:29:03 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2018-05-25 19:57:30 +02:00
|
|
|
|
2022-01-26 03:51:40 +01:00
|
|
|
width = db.Column(db.Integer, nullable=False)
|
|
|
|
height = db.Column(db.Integer, nullable=False)
|
|
|
|
|
|
|
|
def is_very_small(self):
|
|
|
|
return self.width < 720 or self.height < 405
|
|
|
|
|
|
|
|
def is_too_small(self):
|
|
|
|
return self.width < PackageScreenshot.HARD_MIN_SIZE[0] or self.height < PackageScreenshot.HARD_MIN_SIZE[1]
|
|
|
|
|
|
|
|
def is_low_res(self):
|
|
|
|
return self.width < PackageScreenshot.SOFT_MIN_SIZE[0] or self.height < PackageScreenshot.SOFT_MIN_SIZE[1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def file_path(self):
|
|
|
|
return self.url.replace("/uploads/", app.config["UPLOAD_DIR"])
|
|
|
|
|
2023-12-29 11:28:00 +01:00
|
|
|
@property
|
|
|
|
def file_size_bytes(self):
|
|
|
|
path = self.file_path
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
file_stats = os.stat(path)
|
|
|
|
return file_stats.st_size
|
|
|
|
|
|
|
|
@property
|
|
|
|
def file_size(self):
|
|
|
|
size = self.file_size_bytes / 1024
|
|
|
|
if size > 1024:
|
|
|
|
return f"{round(size / 1024, 1)} MB"
|
|
|
|
else:
|
|
|
|
return f"{round(size)} KB"
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_edit_url(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.edit_screenshot",
|
2018-05-25 19:57:30 +02:00
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
2018-03-26 11:52:25 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_delete_url(self):
|
2020-12-06 05:30:47 +01:00
|
|
|
return url_for("packages.delete_screenshot",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2023-11-10 19:57:49 +01:00
|
|
|
def get_thumb_url(self, level=2, format="webp"):
|
|
|
|
url = self.url.replace("/uploads/", "/thumbnails/{:d}/".format(level))
|
|
|
|
if format is not None:
|
|
|
|
start = url[:url.rfind(".")]
|
|
|
|
url = f"{start}.{format}"
|
|
|
|
return url
|
2020-12-15 20:05:29 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_dict(self, base_url=""):
|
2021-02-02 18:09:21 +01:00
|
|
|
return {
|
|
|
|
"id": self.id,
|
2021-02-02 18:09:25 +01:00
|
|
|
"order": self.order,
|
2021-02-02 18:09:21 +01:00
|
|
|
"title": self.title,
|
2021-02-02 18:09:25 +01:00
|
|
|
"url": base_url + self.url,
|
2022-02-03 18:02:01 +01:00
|
|
|
"width": self.width,
|
|
|
|
"height": self.height,
|
2021-02-02 18:09:21 +01:00
|
|
|
"approved": self.approved,
|
2021-02-02 18:29:03 +01:00
|
|
|
"created_at": self.created_at.isoformat(),
|
2022-02-02 02:20:11 +01:00
|
|
|
"is_cover_image": self.package.cover_image == self,
|
2021-02-02 18:09:21 +01:00
|
|
|
}
|
|
|
|
|
2024-04-01 18:43:16 +02:00
|
|
|
def as_short_dict(self, base_url=""):
|
|
|
|
return {
|
|
|
|
"title": self.title,
|
|
|
|
"url": base_url + self.url,
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:05:29 +01:00
|
|
|
|
|
|
|
class PackageUpdateTrigger(enum.Enum):
|
|
|
|
COMMIT = "New Commit"
|
|
|
|
TAG = "New Tag"
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def to_name(self):
|
2020-12-15 20:05:29 +01:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, name):
|
|
|
|
try:
|
|
|
|
return PackageUpdateTrigger[name.upper()]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
|
|
|
return [(choice, choice.value) for choice in cls]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2021-02-28 17:04:31 +01:00
|
|
|
return item if type(item) == PackageUpdateTrigger else PackageUpdateTrigger[item.upper()]
|
2020-12-15 20:05:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PackageUpdateConfig(db.Model):
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
|
|
|
package = db.relationship("Package", back_populates="update_config", foreign_keys=[package_id])
|
|
|
|
|
|
|
|
last_commit = db.Column(db.String(41), nullable=True, default=None)
|
2021-01-29 23:20:44 +01:00
|
|
|
last_tag = db.Column(db.String(41), nullable=True, default=None)
|
2020-12-15 20:05:29 +01:00
|
|
|
|
2021-01-29 23:20:44 +01:00
|
|
|
# Set to now when an outdated notification is sent. Set to None when a release is created
|
|
|
|
outdated_at = db.Column(db.DateTime, nullable=True, default=None)
|
2020-12-15 22:44:29 +01:00
|
|
|
|
2020-12-15 20:05:29 +01:00
|
|
|
trigger = db.Column(db.Enum(PackageUpdateTrigger), nullable=False, default=PackageUpdateTrigger.COMMIT)
|
2020-12-15 23:00:46 +01:00
|
|
|
ref = db.Column(db.String(41), nullable=True, default=None)
|
|
|
|
|
2020-12-15 20:05:29 +01:00
|
|
|
make_release = db.Column(db.Boolean, nullable=False, default=False)
|
2021-01-29 23:20:44 +01:00
|
|
|
|
2021-01-30 00:18:37 +01:00
|
|
|
# Was this made using Add Update Configs in Admin?
|
|
|
|
auto_created = db.Column(db.Boolean, nullable=False, default=False)
|
|
|
|
|
2021-01-29 23:20:44 +01:00
|
|
|
def set_outdated(self):
|
|
|
|
self.outdated_at = datetime.datetime.utcnow()
|
2021-01-30 17:48:11 +01:00
|
|
|
|
|
|
|
def get_message(self):
|
|
|
|
if self.trigger == PackageUpdateTrigger.COMMIT:
|
|
|
|
msg = "New commit {} found on the Git repo.".format(self.last_commit[0:5])
|
|
|
|
|
2021-01-30 17:50:32 +01:00
|
|
|
last_release = self.package.releases.first()
|
|
|
|
if last_release and last_release.commit_hash:
|
|
|
|
msg += " The last release was commit {}".format(last_release.commit_hash[0:5])
|
2021-01-30 17:48:11 +01:00
|
|
|
|
2021-01-30 17:50:32 +01:00
|
|
|
return msg
|
2021-01-30 17:48:11 +01:00
|
|
|
|
2021-01-30 17:50:32 +01:00
|
|
|
else:
|
|
|
|
return "New tag {} found on the Git repo.".format(self.last_tag)
|
2021-01-30 17:48:11 +01:00
|
|
|
|
2021-01-30 23:58:55 +01:00
|
|
|
def get_title(self):
|
|
|
|
return self.last_tag or self.outdated_at.strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
def get_ref(self):
|
|
|
|
return self.last_tag or self.last_commit
|
|
|
|
|
2021-01-30 17:48:11 +01:00
|
|
|
def get_create_release_url(self):
|
2023-06-18 22:56:19 +02:00
|
|
|
return self.package.get_url("packages.create_release", title=self.get_title(), ref=self.get_ref())
|
2021-07-24 03:30:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PackageAlias(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=False)
|
|
|
|
package = db.relationship("Package", back_populates="aliases", foreign_keys=[package_id])
|
|
|
|
|
|
|
|
author = db.Column(db.String(50), nullable=False)
|
|
|
|
name = db.Column(db.String(100), nullable=False)
|
|
|
|
|
|
|
|
def __init__(self, author="", name=""):
|
|
|
|
self.author = author
|
|
|
|
self.name = name
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def get_edit_url(self):
|
2021-07-24 03:30:43 +02:00
|
|
|
return url_for("packages.alias_create_edit", author=self.package.author.username,
|
|
|
|
name=self.package.name, alias_id=self.id)
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
def as_dict(self):
|
2021-07-24 03:30:43 +02:00
|
|
|
return f"{self.author}/{self.name}"
|
2022-11-05 23:39:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PackageDailyStats(db.Model):
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), primary_key=True)
|
2022-11-06 11:32:46 +01:00
|
|
|
package = db.relationship("Package", back_populates="daily_stats", foreign_keys=[package_id])
|
2022-11-05 23:39:33 +01:00
|
|
|
date = db.Column(db.Date, primary_key=True)
|
|
|
|
|
|
|
|
platform_minetest = db.Column(db.Integer, nullable=False, default=0)
|
|
|
|
platform_other = db.Column(db.Integer, nullable=False, default=0)
|
|
|
|
|
|
|
|
reason_new = db.Column(db.Integer, nullable=False, default=0)
|
|
|
|
reason_dependency = db.Column(db.Integer, nullable=False, default=0)
|
|
|
|
reason_update = db.Column(db.Integer, nullable=False, default=0)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def update(package: Package, is_minetest: bool, reason: str):
|
2022-11-06 20:12:43 +01:00
|
|
|
date = datetime.datetime.utcnow().date()
|
|
|
|
|
2022-11-05 23:39:33 +01:00
|
|
|
to_update = dict()
|
|
|
|
kwargs = {
|
|
|
|
"package_id": package.id, "date": date
|
|
|
|
}
|
|
|
|
|
|
|
|
field_platform = "platform_minetest" if is_minetest else "platform_other"
|
|
|
|
to_update[field_platform] = getattr(PackageDailyStats, field_platform) + 1
|
|
|
|
kwargs[field_platform] = 1
|
|
|
|
|
|
|
|
field_reason = None
|
|
|
|
if reason == "new":
|
|
|
|
field_reason = "reason_new"
|
2022-11-06 00:22:21 +01:00
|
|
|
elif reason == "dependency":
|
2022-11-05 23:39:33 +01:00
|
|
|
field_reason = "reason_dependency"
|
|
|
|
elif reason == "update":
|
2022-11-06 00:22:21 +01:00
|
|
|
field_reason = "reason_update"
|
2022-11-05 23:39:33 +01:00
|
|
|
|
|
|
|
if field_reason:
|
|
|
|
to_update[field_reason] = getattr(PackageDailyStats, field_reason) + 1
|
|
|
|
kwargs[field_reason] = 1
|
|
|
|
|
|
|
|
stmt = insert(PackageDailyStats).values(**kwargs)
|
|
|
|
stmt = stmt.on_conflict_do_update(
|
|
|
|
index_elements=[PackageDailyStats.package_id, PackageDailyStats.date],
|
|
|
|
set_=to_update
|
|
|
|
)
|
|
|
|
|
|
|
|
conn = db.session.connection()
|
|
|
|
conn.execute(stmt)
|