2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2018-05-17 16:18:20 +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/>.
|
|
|
|
|
|
|
|
|
2019-01-29 04:00:01 +01:00
|
|
|
import enum, datetime
|
|
|
|
|
|
|
|
from app import app, gravatar
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
from flask import Flask, url_for
|
2019-01-29 04:00:01 +01:00
|
|
|
from flask_sqlalchemy import SQLAlchemy, BaseQuery
|
2018-05-23 18:59:24 +02:00
|
|
|
from flask_migrate import Migrate
|
2020-01-19 03:46:07 +01:00
|
|
|
from flask_user import login_required, UserManager, UserMixin
|
2020-01-18 18:54:46 +01:00
|
|
|
from sqlalchemy import func, CheckConstraint
|
2019-01-29 04:00:01 +01:00
|
|
|
from sqlalchemy_searchable import SearchQueryMixin
|
|
|
|
from sqlalchemy_utils.types import TSVectorType
|
|
|
|
from sqlalchemy_searchable import make_searchable
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# Initialise database
|
|
|
|
db = SQLAlchemy(app)
|
2018-05-23 18:59:24 +02:00
|
|
|
migrate = Migrate(app, db)
|
2019-01-29 04:00:01 +01:00
|
|
|
make_searchable(db.metadata)
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleQuery(BaseQuery, SearchQueryMixin):
|
2019-03-13 15:37:20 +01:00
|
|
|
pass
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
|
2018-03-20 04:16:46 +01:00
|
|
|
class UserRank(enum.Enum):
|
2018-05-28 00:53:55 +02:00
|
|
|
BANNED = 0
|
|
|
|
NOT_JOINED = 1
|
|
|
|
NEW_MEMBER = 2
|
|
|
|
MEMBER = 3
|
|
|
|
TRUSTED_MEMBER = 4
|
|
|
|
EDITOR = 5
|
|
|
|
MODERATOR = 6
|
|
|
|
ADMIN = 7
|
2018-03-20 04:16:46 +01:00
|
|
|
|
|
|
|
def atLeast(self, min):
|
|
|
|
return self.value >= min.value
|
|
|
|
|
2018-03-20 04:32:14 +01:00
|
|
|
def getTitle(self):
|
|
|
|
return self.name.replace("_", " ").title()
|
|
|
|
|
2018-03-24 20:37:33 +01:00
|
|
|
def toName(self):
|
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
|
|
|
return [(choice, choice.getTitle()) for choice in cls]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
|
|
|
return item if type(item) == UserRank else UserRank[item]
|
|
|
|
|
2018-03-20 05:11:50 +01:00
|
|
|
|
|
|
|
class Permission(enum.Enum):
|
2018-03-21 17:41:34 +01:00
|
|
|
EDIT_PACKAGE = "EDIT_PACKAGE"
|
|
|
|
APPROVE_CHANGES = "APPROVE_CHANGES"
|
|
|
|
DELETE_PACKAGE = "DELETE_PACKAGE"
|
|
|
|
CHANGE_AUTHOR = "CHANGE_AUTHOR"
|
2019-07-29 21:29:55 +02:00
|
|
|
CHANGE_NAME = "CHANGE_NAME"
|
2018-03-21 17:41:34 +01:00
|
|
|
MAKE_RELEASE = "MAKE_RELEASE"
|
2020-01-19 01:02:33 +01:00
|
|
|
DELETE_RELEASE = "DELETE_RELEASE"
|
2018-05-25 19:57:30 +02:00
|
|
|
ADD_SCREENSHOTS = "ADD_SCREENSHOTS"
|
2020-07-12 03:22:35 +02:00
|
|
|
REIMPORT_META = "REIMPORT_META"
|
2018-05-25 19:57:30 +02:00
|
|
|
APPROVE_SCREENSHOT = "APPROVE_SCREENSHOT"
|
2018-03-21 17:41:34 +01:00
|
|
|
APPROVE_RELEASE = "APPROVE_RELEASE"
|
|
|
|
APPROVE_NEW = "APPROVE_NEW"
|
2020-07-15 20:54:33 +02:00
|
|
|
EDIT_TAGS = "EDIT_TAGS"
|
|
|
|
CREATE_TAG = "CREATE_TAG"
|
2018-03-21 17:41:34 +01:00
|
|
|
CHANGE_RELEASE_URL = "CHANGE_RELEASE_URL"
|
2020-07-11 00:12:15 +02:00
|
|
|
CHANGE_USERNAMES = "CHANGE_USERNAMES"
|
2018-03-24 20:37:33 +01:00
|
|
|
CHANGE_RANK = "CHANGE_RANK"
|
2018-05-14 01:40:34 +02:00
|
|
|
CHANGE_EMAIL = "CHANGE_EMAIL"
|
2018-05-13 16:40:37 +02:00
|
|
|
EDIT_EDITREQUEST = "EDIT_EDITREQUEST"
|
2018-06-11 23:49:25 +02:00
|
|
|
SEE_THREAD = "SEE_THREAD"
|
2018-07-28 16:19:30 +02:00
|
|
|
CREATE_THREAD = "CREATE_THREAD"
|
2020-07-11 02:34:51 +02:00
|
|
|
COMMENT_THREAD = "COMMENT_THREAD"
|
|
|
|
LOCK_THREAD = "LOCK_THREAD"
|
2020-12-04 00:57:58 +01:00
|
|
|
DELETE_THREAD = "DELETE_THREAD"
|
2020-07-11 04:29:33 +02:00
|
|
|
DELETE_REPLY = "DELETE_REPLY"
|
2020-07-11 04:52:56 +02:00
|
|
|
EDIT_REPLY = "EDIT_REPLY"
|
2018-12-25 16:13:30 +01:00
|
|
|
UNAPPROVE_PACKAGE = "UNAPPROVE_PACKAGE"
|
2018-12-25 18:51:29 +01:00
|
|
|
TOPIC_DISCARD = "TOPIC_DISCARD"
|
2019-11-22 15:33:22 +01:00
|
|
|
CREATE_TOKEN = "CREATE_TOKEN"
|
2020-07-08 23:45:24 +02:00
|
|
|
EDIT_MAINTAINERS = "EDIT_MAINTAINERS"
|
2020-02-23 21:12:32 +01:00
|
|
|
CHANGE_PROFILE_URLS = "CHANGE_PROFILE_URLS"
|
2018-03-20 05:11:50 +01:00
|
|
|
|
2018-03-21 20:53:53 +01:00
|
|
|
# Only return true if the permission is valid for *all* contexts
|
|
|
|
# See Package.checkPerm for package-specific contexts
|
2018-03-21 20:03:14 +01:00
|
|
|
def check(self, user):
|
2018-03-21 20:24:45 +01:00
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
2018-03-21 20:53:53 +01:00
|
|
|
if self == Permission.APPROVE_NEW or \
|
2018-07-13 22:28:08 +02:00
|
|
|
self == Permission.APPROVE_CHANGES or \
|
|
|
|
self == Permission.APPROVE_RELEASE or \
|
|
|
|
self == Permission.APPROVE_SCREENSHOT or \
|
2020-07-15 20:54:33 +02:00
|
|
|
self == Permission.EDIT_TAGS or \
|
|
|
|
self == Permission.CREATE_TAG or \
|
2018-07-13 22:28:08 +02:00
|
|
|
self == Permission.SEE_THREAD:
|
2018-03-21 20:03:14 +01:00
|
|
|
return user.rank.atLeast(UserRank.EDITOR)
|
|
|
|
else:
|
|
|
|
raise Exception("Non-global permission checked globally. Use Package.checkPerm or User.checkPerm instead.")
|
|
|
|
|
2020-07-15 20:54:33 +02:00
|
|
|
@staticmethod
|
|
|
|
def checkPerm(user, perm):
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to Permission.check")
|
|
|
|
|
|
|
|
return perm.check(user)
|
|
|
|
|
2020-01-24 19:28:52 +01:00
|
|
|
def display_name_default(context):
|
2020-12-04 00:57:58 +01:00
|
|
|
return context.get_current_parameters()["username"]
|
2020-01-24 19:28:52 +01:00
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
class User(db.Model, UserMixin):
|
2018-12-25 20:28:32 +01:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# User authentication information
|
2018-12-25 20:28:32 +01:00
|
|
|
username = db.Column(db.String(50, collation="NOCASE"), nullable=False, unique=True, index=True)
|
2020-01-19 16:03:38 +01:00
|
|
|
password = db.Column(db.String(255), nullable=False, server_default="")
|
2018-03-21 23:03:37 +01:00
|
|
|
reset_password_token = db.Column(db.String(100), nullable=False, server_default="")
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2018-12-25 20:28:32 +01:00
|
|
|
rank = db.Column(db.Enum(UserRank))
|
2018-03-20 04:16:46 +01:00
|
|
|
|
2018-03-18 19:05:53 +01:00
|
|
|
# Account linking
|
2018-06-03 02:50:14 +02:00
|
|
|
github_username = db.Column(db.String(50, collation="NOCASE"), nullable=True, unique=True)
|
|
|
|
forums_username = db.Column(db.String(50, collation="NOCASE"), nullable=True, unique=True)
|
2018-03-18 19:05:53 +01:00
|
|
|
|
2020-01-25 00:19:06 +01:00
|
|
|
# Access token for webhook setup
|
|
|
|
github_access_token = db.Column(db.String(50), nullable=True, server_default=None)
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
# User email information
|
2018-12-25 20:28:32 +01:00
|
|
|
email = db.Column(db.String(255), nullable=True, unique=True)
|
2020-01-19 03:46:07 +01:00
|
|
|
email_confirmed_at = db.Column(db.DateTime())
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# User information
|
2018-12-25 20:28:32 +01:00
|
|
|
profile_pic = db.Column(db.String(255), nullable=True, server_default=None)
|
|
|
|
active = db.Column("is_active", db.Boolean, nullable=False, server_default="0")
|
2020-01-24 19:28:52 +01:00
|
|
|
display_name = db.Column(db.String(100), nullable=False, default=display_name_default)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2019-07-02 01:45:04 +02:00
|
|
|
# Links
|
|
|
|
website_url = db.Column(db.String(255), nullable=True, default=None)
|
|
|
|
donate_url = db.Column(db.String(255), nullable=True, default=None)
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
# Content
|
2020-09-16 19:16:41 +02:00
|
|
|
notifications = db.relationship("Notification", primaryjoin="User.id==Notification.user_id", \
|
|
|
|
order_by="Notification.created_at")
|
2018-05-13 18:55:28 +02:00
|
|
|
|
2020-04-21 21:35:05 +02:00
|
|
|
packages = db.relationship("Package", backref=db.backref("author", lazy="joined"), lazy="dynamic")
|
2018-05-13 18:55:28 +02:00
|
|
|
requests = db.relationship("EditRequest", backref="author", lazy="dynamic")
|
2018-06-11 23:49:25 +02:00
|
|
|
threads = db.relationship("Thread", backref="author", lazy="dynamic")
|
2019-11-22 15:33:22 +01:00
|
|
|
tokens = db.relationship("APIToken", backref="owner", lazy="dynamic")
|
2018-06-11 23:49:25 +02:00
|
|
|
replies = db.relationship("ThreadReply", backref="author", lazy="dynamic")
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2020-01-24 19:28:52 +01:00
|
|
|
def __init__(self, username=None, active=False, email=None, password=""):
|
2018-03-18 18:43:30 +01:00
|
|
|
self.username = username
|
2020-01-19 03:46:07 +01:00
|
|
|
self.email_confirmed_at = datetime.datetime.now() - datetime.timedelta(days=6000)
|
2018-03-18 19:05:53 +01:00
|
|
|
self.display_name = username
|
2018-05-29 19:27:33 +02:00
|
|
|
self.active = active
|
|
|
|
self.email = email
|
|
|
|
self.password = password
|
2018-03-20 05:03:13 +01:00
|
|
|
self.rank = UserRank.NOT_JOINED
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2020-01-19 20:48:41 +01:00
|
|
|
def hasPassword(self):
|
|
|
|
return self.password != ""
|
|
|
|
|
2018-05-18 00:26:36 +02:00
|
|
|
def canAccessTodoList(self):
|
|
|
|
return Permission.APPROVE_NEW.check(self) or \
|
|
|
|
Permission.APPROVE_RELEASE.check(self) or \
|
|
|
|
Permission.APPROVE_CHANGES.check(self)
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
def isClaimed(self):
|
2018-04-03 15:56:43 +02:00
|
|
|
return self.rank.atLeast(UserRank.NEW_MEMBER)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2018-12-25 20:28:32 +01:00
|
|
|
def getProfilePicURL(self):
|
|
|
|
if self.profile_pic:
|
|
|
|
return self.profile_pic
|
|
|
|
else:
|
|
|
|
return gravatar(self.email or "")
|
|
|
|
|
2018-03-24 20:37:33 +01:00
|
|
|
def checkPerm(self, user, perm):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to User.checkPerm()")
|
|
|
|
|
|
|
|
# Members can edit their own packages, and editors can edit any packages
|
2018-04-03 15:56:43 +02:00
|
|
|
if perm == Permission.CHANGE_AUTHOR:
|
|
|
|
return user.rank.atLeast(UserRank.EDITOR)
|
2020-07-10 23:59:41 +02:00
|
|
|
elif perm == Permission.CHANGE_RANK or perm == Permission.CHANGE_USERNAMES:
|
2018-03-24 20:37:33 +01:00
|
|
|
return user.rank.atLeast(UserRank.MODERATOR)
|
2020-02-23 21:12:32 +01:00
|
|
|
elif perm == Permission.CHANGE_EMAIL or perm == Permission.CHANGE_PROFILE_URLS:
|
2020-07-13 01:34:01 +02:00
|
|
|
return user == self or user.rank.atLeast(UserRank.ADMIN)
|
2019-11-22 15:33:22 +01:00
|
|
|
elif perm == Permission.CREATE_TOKEN:
|
|
|
|
if user == self:
|
|
|
|
return user.rank.atLeast(UserRank.MEMBER)
|
|
|
|
else:
|
|
|
|
return user.rank.atLeast(UserRank.MODERATOR) and user.rank.atLeast(self.rank)
|
2018-03-24 20:37:33 +01:00
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to users".format(perm.name))
|
|
|
|
|
2019-01-28 20:01:37 +01:00
|
|
|
def canCommentRL(self):
|
2020-08-18 19:10:42 +02:00
|
|
|
factor = 1
|
|
|
|
if self.rank.atLeast(UserRank.ADMIN):
|
|
|
|
return True
|
|
|
|
elif self.rank.atLeast(UserRank.TRUSTED_MEMBER):
|
|
|
|
factor *= 2
|
|
|
|
|
2020-07-09 05:10:09 +02:00
|
|
|
one_min_ago = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)
|
|
|
|
if ThreadReply.query.filter_by(author=self) \
|
2020-08-18 19:10:42 +02:00
|
|
|
.filter(ThreadReply.created_at > one_min_ago).count() >= 3 * factor:
|
2020-07-09 05:10:09 +02:00
|
|
|
return False
|
|
|
|
|
2019-01-28 20:01:37 +01:00
|
|
|
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
|
2020-07-09 05:10:09 +02:00
|
|
|
if ThreadReply.query.filter_by(author=self) \
|
2020-08-18 19:10:42 +02:00
|
|
|
.filter(ThreadReply.created_at > hour_ago).count() >= 20 * factor:
|
2020-07-09 05:10:09 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2019-01-28 20:01:37 +01:00
|
|
|
|
|
|
|
def canOpenThreadRL(self):
|
2020-08-18 19:10:42 +02:00
|
|
|
factor = 1
|
|
|
|
if self.rank.atLeast(UserRank.ADMIN):
|
|
|
|
return True
|
|
|
|
elif self.rank.atLeast(UserRank.TRUSTED_MEMBER):
|
|
|
|
factor *= 5
|
|
|
|
|
2019-01-28 20:01:37 +01:00
|
|
|
hour_ago = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
|
|
|
|
return Thread.query.filter_by(author=self) \
|
2020-08-18 19:10:42 +02:00
|
|
|
.filter(Thread.created_at > hour_ago).count() < 2 * factor
|
2019-01-28 20:01:37 +01:00
|
|
|
|
2020-01-19 20:48:41 +01:00
|
|
|
def __eq__(self, other):
|
2020-04-11 18:56:35 +02:00
|
|
|
if other is None:
|
|
|
|
return False
|
|
|
|
|
2020-01-19 20:48:41 +01:00
|
|
|
if not self.is_authenticated or not other.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
assert self.id > 0
|
|
|
|
return self.id == other.id
|
|
|
|
|
2018-05-14 01:40:34 +02:00
|
|
|
class UserEmailVerification(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
|
|
|
|
email = db.Column(db.String(100))
|
|
|
|
token = db.Column(db.String(32))
|
|
|
|
user = db.relationship("User", foreign_keys=[user_id])
|
2018-05-13 18:55:28 +02:00
|
|
|
|
|
|
|
class Notification(db.Model):
|
2020-07-11 01:53:03 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2018-05-13 18:55:28 +02:00
|
|
|
|
2020-07-11 01:53:03 +02:00
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
user = db.relationship("User", foreign_keys=[user_id])
|
2018-05-13 18:55:28 +02:00
|
|
|
|
2020-07-11 01:53:03 +02:00
|
|
|
causer_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
causer = db.relationship("User", foreign_keys=[causer_id])
|
|
|
|
|
|
|
|
title = db.Column(db.String(100), nullable=False)
|
|
|
|
url = db.Column(db.String(200), nullable=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
|
|
|
|
|
|
|
created_at = db.Column(db.DateTime, nullable=True, default=datetime.datetime.utcnow)
|
|
|
|
|
|
|
|
def __init__(self, user, causer, title, url, package=None):
|
|
|
|
if len(title) > 100:
|
2019-08-31 19:38:15 +02:00
|
|
|
title = title[:99] + "…"
|
|
|
|
|
2020-07-11 01:53:03 +02:00
|
|
|
self.user = user
|
|
|
|
self.causer = causer
|
|
|
|
self.title = title
|
|
|
|
self.url = url
|
|
|
|
self.package = package
|
2018-05-13 18:55:28 +02: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)
|
2018-04-03 15:13:11 +02:00
|
|
|
|
2018-05-29 22:24:50 +02:00
|
|
|
def __init__(self, v, is_foss=True):
|
2018-04-03 15:13:11 +02:00
|
|
|
self.name = v
|
2018-05-29 22:24:50 +02:00
|
|
|
self.is_foss = is_foss
|
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"
|
|
|
|
|
2018-03-20 04:32:14 +01:00
|
|
|
def toName(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
|
|
|
|
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):
|
|
|
|
return [(choice, choice.value) for choice in cls]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2018-03-20 05:11:50 +01:00
|
|
|
return item if type(item) == PackageType else PackageType[item]
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-23 20:08:55 +01:00
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
class PackageState(enum.Enum):
|
|
|
|
WIP = "Work in Progress"
|
|
|
|
CHANGES_NEEDED = "Changes Needed"
|
|
|
|
READY_FOR_REVIEW = "Ready for Review"
|
|
|
|
APPROVED = "Approved"
|
|
|
|
DELETED = "Deleted"
|
|
|
|
|
|
|
|
def toName(self):
|
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def verb(self):
|
|
|
|
if self == self.READY_FOR_REVIEW:
|
|
|
|
return "Submit for Review"
|
|
|
|
elif self == self.APPROVED:
|
|
|
|
return "Approve"
|
|
|
|
elif self == self.DELETED:
|
|
|
|
return "Delete"
|
|
|
|
else:
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@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):
|
|
|
|
return item if type(item) == PackageState else PackageState[item]
|
|
|
|
|
|
|
|
|
|
|
|
PACKAGE_STATE_FLOW = {
|
|
|
|
PackageState.WIP: set([ PackageState.READY_FOR_REVIEW ]),
|
|
|
|
PackageState.CHANGES_NEEDED: set([ PackageState.READY_FOR_REVIEW ]),
|
|
|
|
PackageState.READY_FOR_REVIEW: set([ PackageState.WIP, PackageState.CHANGES_NEEDED, PackageState.APPROVED ]),
|
|
|
|
PackageState.APPROVED: set([ PackageState.CHANGES_NEEDED ]),
|
|
|
|
PackageState.DELETED: set([ PackageState.READY_FOR_REVIEW ]),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 20:08:55 +01:00
|
|
|
class PackagePropertyKey(enum.Enum):
|
2018-05-29 21:51:42 +02:00
|
|
|
name = "Name"
|
|
|
|
title = "Title"
|
2019-01-29 04:00:01 +01:00
|
|
|
short_desc = "Short Description"
|
2018-05-29 21:51:42 +02:00
|
|
|
desc = "Description"
|
|
|
|
type = "Type"
|
|
|
|
license = "License"
|
|
|
|
media_license = "Media License"
|
|
|
|
tags = "Tags"
|
|
|
|
provides = "Provides"
|
|
|
|
repo = "Repository"
|
|
|
|
website = "Website"
|
|
|
|
issueTracker = "Issue Tracker"
|
|
|
|
forums = "Forum Topic ID"
|
2018-03-23 20:08:55 +01:00
|
|
|
|
2018-03-26 16:17:39 +02:00
|
|
|
def convert(self, value):
|
|
|
|
if self == PackagePropertyKey.tags:
|
2018-05-21 23:20:28 +02:00
|
|
|
return ",".join([t.title for t in value])
|
2018-05-27 17:37:54 +02:00
|
|
|
elif self == PackagePropertyKey.provides:
|
|
|
|
return ",".join([t.name for t in value])
|
2018-03-26 16:17:39 +02:00
|
|
|
else:
|
|
|
|
return str(value)
|
|
|
|
|
2018-05-27 17:37:54 +02:00
|
|
|
provides = 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 00:57:58 +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-07-12 18:56:06 +02:00
|
|
|
Tags = db.Table("tags",
|
2020-12-04 00:57:58 +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-07-17 21:48:51 +02:00
|
|
|
ContentWarnings = db.Table("content_warnings",
|
2020-12-04 00:57:58 +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-07-08 23:45:24 +02:00
|
|
|
maintainers = db.Table("maintainers",
|
2020-12-04 00:57:58 +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
|
|
|
)
|
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
class Dependency(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
depender_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
|
|
|
meta_package_id = db.Column(db.Integer, db.ForeignKey("meta_package.id"), nullable=True)
|
|
|
|
optional = db.Column(db.Boolean, nullable=False, default=False)
|
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!")
|
|
|
|
|
2020-01-21 23:59:44 +01:00
|
|
|
def getName(self):
|
|
|
|
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
|
|
|
|
def SpecToList(depender, spec, cache={}):
|
|
|
|
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
|
|
|
|
2018-03-19 19:25:13 +01:00
|
|
|
class Package(db.Model):
|
2019-01-29 04:00:01 +01:00
|
|
|
query_class = ArticleQuery
|
|
|
|
|
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
|
2018-03-21 23:03:37 +01:00
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.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)
|
2018-03-20 01:44:47 +01:00
|
|
|
type = db.Column(db.Enum(PackageType))
|
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
|
|
|
|
2020-01-18 18:54:46 +01:00
|
|
|
name_valid = db.CheckConstraint("name ~* '^[a-z0-9_]+$'")
|
|
|
|
|
2020-01-19 00:20:49 +01:00
|
|
|
search_vector = db.Column(TSVectorType("name", "title", "short_desc", "desc", \
|
|
|
|
weights={ "name": "A", "title": "B", "short_desc": "C", "desc": "D" }))
|
2019-01-29 04:00:01 +01:00
|
|
|
|
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
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
state = db.Column(db.Enum(PackageState), default=PackageState.WIP)
|
|
|
|
|
|
|
|
@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)
|
|
|
|
review_thread = db.relationship("Thread", foreign_keys=[review_thread_id])
|
|
|
|
|
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)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2020-01-21 23:48:03 +01:00
|
|
|
provides = db.relationship("MetaPackage", \
|
2020-04-21 21:35:05 +02:00
|
|
|
secondary=provides, lazy="select", order_by=db.asc("name"), \
|
2019-09-03 01:42:51 +02:00
|
|
|
backref=db.backref("packages", lazy="dynamic", order_by=db.desc("score")))
|
2018-05-21 23:20:28 +02:00
|
|
|
|
2018-05-27 22:31:11 +02:00
|
|
|
dependencies = db.relationship("Dependency", backref="depender", lazy="dynamic", foreign_keys=[Dependency.depender_id])
|
2018-03-26 12:28:34 +02:00
|
|
|
|
2020-07-12 18:56:06 +02:00
|
|
|
tags = db.relationship("Tag", secondary=Tags, lazy="select",
|
2018-05-27 17:37:54 +02:00
|
|
|
backref=db.backref("packages", lazy=True))
|
2018-03-26 12:28:34 +02:00
|
|
|
|
2020-07-17 21:48:51 +02:00
|
|
|
content_warnings = db.relationship("ContentWarning", secondary=ContentWarnings, lazy="select",
|
|
|
|
backref=db.backref("packages", lazy=True))
|
|
|
|
|
2018-03-21 23:03:37 +01:00
|
|
|
releases = db.relationship("PackageRelease", backref="package",
|
|
|
|
lazy="dynamic", order_by=db.desc("package_release_releaseDate"))
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2018-03-26 11:52:25 +02:00
|
|
|
screenshots = db.relationship("PackageScreenshot", backref="package",
|
2019-01-08 22:35:46 +01:00
|
|
|
lazy="dynamic", order_by=db.asc("package_screenshot_id"))
|
2018-03-23 20:08:55 +01:00
|
|
|
|
|
|
|
requests = db.relationship("EditRequest", backref="package",
|
|
|
|
lazy="dynamic")
|
|
|
|
|
2020-07-08 23:45:24 +02:00
|
|
|
maintainers = db.relationship("User", secondary=maintainers, lazy="subquery")
|
|
|
|
|
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)
|
|
|
|
|
2018-05-25 18:12:34 +02:00
|
|
|
for e in PackagePropertyKey:
|
|
|
|
setattr(self, e.name, getattr(package, e.name))
|
|
|
|
|
2020-06-05 05:29:52 +02:00
|
|
|
def getId(self):
|
|
|
|
return "{}/{}".format(self.author.username, self.name)
|
|
|
|
|
2019-11-21 23:16:35 +01:00
|
|
|
def getIsFOSS(self):
|
|
|
|
return self.license.is_foss and self.media_license.is_foss
|
|
|
|
|
2020-01-25 00:19:06 +01:00
|
|
|
def getIsOnGitHub(self):
|
|
|
|
if self.repo is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
url = urlparse(self.repo)
|
|
|
|
return url.netloc == "github.com"
|
|
|
|
|
|
|
|
def getGitHubFullName(self):
|
|
|
|
if self.repo is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
url = urlparse(self.repo)
|
|
|
|
if url.netloc != "github.com":
|
|
|
|
return None
|
|
|
|
|
|
|
|
import re
|
|
|
|
m = re.search(r"^\/([^\/]+)\/([^\/]+)\/?$", url.path)
|
|
|
|
if m is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
user = m.group(1)
|
|
|
|
repo = m.group(2).replace(".git", "")
|
|
|
|
|
|
|
|
return (user,repo)
|
|
|
|
|
2020-01-21 23:59:44 +01:00
|
|
|
def getSortedDependencies(self, is_hard=None):
|
|
|
|
query = self.dependencies
|
|
|
|
if is_hard is not None:
|
|
|
|
query = query.filter_by(optional=not is_hard)
|
|
|
|
|
|
|
|
deps = query.all()
|
|
|
|
deps.sort(key = lambda x: x.getName())
|
|
|
|
return deps
|
|
|
|
|
|
|
|
def getSortedHardDependencies(self):
|
|
|
|
return self.getSortedDependencies(True)
|
|
|
|
|
|
|
|
def getSortedOptionalDependencies(self):
|
|
|
|
return self.getSortedDependencies(False)
|
|
|
|
|
2019-09-03 01:42:51 +02:00
|
|
|
def getAsDictionaryKey(self):
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"author": self.author.display_name,
|
|
|
|
"type": self.type.toName(),
|
|
|
|
}
|
|
|
|
|
2020-06-03 18:17:21 +02:00
|
|
|
def getAsDictionaryShort(self, base_url, version=None):
|
2018-12-21 22:22:15 +01:00
|
|
|
tnurl = self.getThumbnailURL(1)
|
2020-06-03 18:17:21 +02:00
|
|
|
release = self.getDownloadRelease(version=version)
|
2018-03-23 18:33:34 +01:00
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"title": self.title,
|
2020-05-27 18:27:41 +02:00
|
|
|
"author": self.author.username,
|
2019-01-29 04:00:01 +01:00
|
|
|
"short_description": self.short_desc,
|
2018-03-23 18:33:34 +01:00
|
|
|
"type": self.type.toName(),
|
2019-03-13 15:37:20 +01:00
|
|
|
"release": release and release.id,
|
2020-01-19 20:09:04 +01:00
|
|
|
"thumbnail": (base_url + tnurl) if tnurl is not None else None
|
2018-06-15 23:57:43 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:17:21 +02:00
|
|
|
def getAsDictionary(self, base_url, version=None):
|
2018-12-21 22:22:15 +01:00
|
|
|
tnurl = self.getThumbnailURL(1)
|
2020-06-03 18:17:21 +02:00
|
|
|
release = self.getDownloadRelease(version=version)
|
2018-06-15 23:57:43 +02:00
|
|
|
return {
|
2020-05-27 18:27:41 +02:00
|
|
|
"author": self.author.username,
|
2018-06-15 23:57:43 +02:00
|
|
|
"name": self.name,
|
|
|
|
"title": self.title,
|
2019-01-29 04:00:01 +01:00
|
|
|
"short_description": self.short_desc,
|
2018-06-15 23:57:43 +02:00
|
|
|
"desc": self.desc,
|
|
|
|
"type": self.type.toName(),
|
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,
|
|
|
|
|
|
|
|
"provides": [x.name for x in self.provides],
|
|
|
|
"thumbnail": (base_url + tnurl) if tnurl is not None else None,
|
2018-05-29 17:19:17 +02:00
|
|
|
"screenshots": [base_url + ss.url for ss in self.screenshots],
|
2018-06-15 23:57:43 +02:00
|
|
|
|
|
|
|
"url": base_url + self.getDownloadURL(),
|
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,
|
|
|
|
"downloads": self.downloads
|
2018-03-23 18:33:34 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 22:22:15 +01:00
|
|
|
def getThumbnailURL(self, level=2):
|
2019-01-08 22:35:46 +01:00
|
|
|
screenshot = self.screenshots.filter_by(approved=True).order_by(db.asc(PackageScreenshot.id)).first()
|
2018-12-21 22:22:15 +01:00
|
|
|
return screenshot.getThumbnailURL(level) if screenshot is not None else None
|
2018-05-29 17:19:17 +02:00
|
|
|
|
2020-04-24 00:49:46 +02:00
|
|
|
def getMainScreenshotURL(self, absolute=False):
|
2019-01-08 22:35:46 +01:00
|
|
|
screenshot = self.screenshots.filter_by(approved=True).order_by(db.asc(PackageScreenshot.id)).first()
|
2020-04-24 00:49:46 +02:00
|
|
|
if screenshot is None:
|
|
|
|
return None
|
2018-05-29 17:19:17 +02:00
|
|
|
|
2020-04-24 00:49:46 +02:00
|
|
|
if absolute:
|
|
|
|
from app.utils import abs_url
|
|
|
|
return abs_url(screenshot.url)
|
|
|
|
else:
|
|
|
|
return screenshot.url
|
|
|
|
|
|
|
|
def getDetailsURL(self, absolute=False):
|
|
|
|
if absolute:
|
|
|
|
from app.utils import abs_url_for
|
|
|
|
return abs_url_for("packages.view",
|
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
else:
|
|
|
|
return url_for("packages.view",
|
|
|
|
author=self.author.username, name=self.name)
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2020-08-02 18:41:06 +02:00
|
|
|
def getShieldURL(self, type):
|
|
|
|
from app.utils import abs_url_for
|
|
|
|
return abs_url_for("packages.shield",
|
|
|
|
author=self.author.username, name=self.name, type=type)
|
|
|
|
|
|
|
|
def makeShield(self, type):
|
|
|
|
return "[![ContentDB]({})]({})" \
|
|
|
|
.format(self.getShieldURL(type), self.getDetailsURL(True))
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
def getEditURL(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.create_edit",
|
2018-03-20 03:58:31 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
def getSetStateURL(self, state):
|
|
|
|
if type(state) == str:
|
|
|
|
state = PackageState[perm]
|
|
|
|
elif type(state) != PackageState:
|
|
|
|
raise Exception("Unknown state given to Package.canMoveToState()")
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-12-25 16:13:30 +01:00
|
|
|
def getRemoveURL(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.remove",
|
2018-05-25 19:28:24 +02:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2018-03-26 11:52:25 +02:00
|
|
|
def getNewScreenshotURL(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.create_screenshot",
|
2018-03-26 11:52:25 +02:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2018-03-20 19:40:19 +01:00
|
|
|
def getCreateReleaseURL(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.create_release",
|
2018-03-20 19:40:19 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2018-03-24 03:49:30 +01:00
|
|
|
def getCreateEditRequestURL(self):
|
2018-05-13 16:40:37 +02:00
|
|
|
return url_for("create_edit_editrequest_page",
|
2018-03-24 03:49:30 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2019-01-28 22:49:29 +01:00
|
|
|
def getBulkReleaseURL(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.bulk_change_release",
|
2019-01-28 22:49:29 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2018-03-23 17:20:56 +01:00
|
|
|
def getDownloadURL(self):
|
2019-11-16 00:51:42 +01:00
|
|
|
return url_for("packages.download",
|
2018-03-23 17:20:56 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2020-07-08 23:45:24 +02:00
|
|
|
def getEditMaintainersURL(self):
|
|
|
|
return url_for("packages.edit_maintainers",
|
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2020-07-09 00:42:30 +02:00
|
|
|
def getRemoveSelfMaintainerURL(self):
|
|
|
|
return url_for("packages.remove_self_maintainers",
|
|
|
|
author=self.author.username, name=self.name)
|
2020-07-10 20:01:58 +02:00
|
|
|
|
2020-07-12 03:22:35 +02:00
|
|
|
def getUpdateFromReleaseURL(self):
|
|
|
|
return url_for("packages.update_from_release",
|
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
2020-07-10 20:01:58 +02:00
|
|
|
def getReviewURL(self):
|
|
|
|
return url_for('packages.review',
|
|
|
|
author=self.author.username, name=self.name)
|
2020-07-09 00:42:30 +02:00
|
|
|
|
2020-06-03 18:17:21 +02:00
|
|
|
def getDownloadRelease(self, version=None):
|
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-06-03 18:17:21 +02:00
|
|
|
((rel.min_rel is None or rel.min_rel_id <= version.id) and \
|
|
|
|
(rel.max_rel is None or rel.max_rel_id >= version.id))):
|
2018-03-23 17:20:56 +01:00
|
|
|
return rel
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
def checkPerm(self, user, perm):
|
2018-03-20 04:45:01 +01:00
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
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:
|
|
|
|
raise Exception("Unknown permission given to Package.checkPerm()")
|
2018-03-20 04:16:46 +01:00
|
|
|
|
|
|
|
isOwner = user == self.author
|
2020-07-08 23:45:24 +02:00
|
|
|
isMaintainer = isOwner or user.rank.atLeast(UserRank.EDITOR) or user in self.maintainers
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2019-01-28 20:01:37 +01:00
|
|
|
if perm == Permission.CREATE_THREAD:
|
|
|
|
return user.rank.atLeast(UserRank.MEMBER)
|
|
|
|
|
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:
|
|
|
|
return isMaintainer
|
2018-03-20 19:20:30 +01:00
|
|
|
|
2020-08-18 17:14:47 +02:00
|
|
|
elif perm == Permission.EDIT_PACKAGE or \
|
2020-07-12 03:22:35 +02:00
|
|
|
perm == Permission.APPROVE_CHANGES or perm == Permission.APPROVE_RELEASE:
|
2020-07-08 23:45:24 +02:00
|
|
|
return isMaintainer and user.rank.atLeast(UserRank.MEMBER if self.approved else UserRank.NEW_MEMBER)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2019-07-29 21:29:55 +02:00
|
|
|
# Anyone can change the package name when not approved, but only editors when approved
|
|
|
|
elif perm == Permission.CHANGE_NAME:
|
|
|
|
return not self.approved or user.rank.atLeast(UserRank.EDITOR)
|
|
|
|
|
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:
|
2018-03-20 04:16:46 +01:00
|
|
|
return user.rank.atLeast(UserRank.EDITOR)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
2020-04-21 18:06:27 +02:00
|
|
|
elif perm == Permission.APPROVE_SCREENSHOT:
|
2020-07-08 23:45:24 +02:00
|
|
|
return isMaintainer and user.rank.atLeast(UserRank.TRUSTED_MEMBER if self.approved else UserRank.NEW_MEMBER)
|
|
|
|
|
|
|
|
elif perm == Permission.EDIT_MAINTAINERS:
|
|
|
|
return isOwner or user.rank.atLeast(UserRank.MODERATOR)
|
2018-05-28 00:53:55 +02:00
|
|
|
|
2020-07-10 21:13:48 +02:00
|
|
|
elif perm == Permission.UNAPPROVE_PACKAGE or perm == Permission.DELETE_PACKAGE:
|
2020-09-16 19:05:37 +02:00
|
|
|
return user.rank.atLeast(UserRank.MEMBER if isOwner else UserRank.EDITOR)
|
2020-07-10 21:13:48 +02:00
|
|
|
|
|
|
|
elif perm == Permission.CHANGE_RELEASE_URL:
|
2018-03-20 04:45:01 +01:00
|
|
|
return user.rank.atLeast(UserRank.MODERATOR)
|
|
|
|
|
2020-08-18 17:14:47 +02:00
|
|
|
elif perm == Permission.REIMPORT_META:
|
|
|
|
return user.rank.atLeast(UserRank.ADMIN)
|
|
|
|
|
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
|
|
|
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
def canMoveToState(self, user, state):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(state) == str:
|
|
|
|
state = PackageState[perm]
|
|
|
|
elif type(state) != PackageState:
|
|
|
|
raise Exception("Unknown state given to Package.canMoveToState()")
|
|
|
|
|
|
|
|
if state not in PACKAGE_STATE_FLOW[self.state]:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if state == PackageState.READY_FOR_REVIEW or state == PackageState.APPROVED:
|
|
|
|
requiredPerm = Permission.APPROVE_NEW if state == PackageState.APPROVED else Permission.EDIT_PACKAGE
|
|
|
|
|
|
|
|
if not self.checkPerm(user, requiredPerm):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if state == PackageState.APPROVED and \
|
|
|
|
("Other" in self.license.name or "Other" in self.media_license.name):
|
|
|
|
return False
|
|
|
|
|
|
|
|
needsScreenshot = \
|
|
|
|
(self.type == self.type.GAME or self.type == self.type.TXP) and \
|
|
|
|
self.screenshots.count() == 0
|
|
|
|
return self.releases.count() > 0 and not needsScreenshot
|
|
|
|
|
|
|
|
elif state == PackageState.CHANGES_NEEDED:
|
|
|
|
return self.checkPerm(user, Permission.APPROVE_NEW)
|
|
|
|
|
|
|
|
elif state == PackageState.WIP:
|
2020-09-16 23:10:22 +02:00
|
|
|
return self.checkPerm(user, Permission.EDIT_PACKAGE) and \
|
|
|
|
(user in self.maintainers or user.rank.atLeast(UserRank.ADMIN))
|
2020-09-16 18:51:03 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def getNextStates(self, user):
|
|
|
|
states = []
|
|
|
|
|
|
|
|
for state in PackageState:
|
|
|
|
if self.canMoveToState(user, state):
|
|
|
|
states.append(state)
|
|
|
|
|
|
|
|
return states
|
|
|
|
|
|
|
|
|
2020-07-09 05:32:13 +02:00
|
|
|
def getScoreDict(self):
|
|
|
|
return {
|
|
|
|
"author": self.author.username,
|
|
|
|
"name": self.name,
|
|
|
|
"score": self.score,
|
|
|
|
"score_downloads": self.score_downloads,
|
|
|
|
"score_reviews": self.score - self.score_downloads,
|
|
|
|
"downloads": self.downloads
|
|
|
|
}
|
2020-07-09 02:26:01 +02:00
|
|
|
|
|
|
|
def recalcScore(self):
|
2020-07-09 05:32:13 +02:00
|
|
|
review_scores = [ 100 * r.asSign() for r in self.reviews ]
|
|
|
|
self.score = self.score_downloads + sum(review_scores)
|
2020-07-09 02:26:01 +02:00
|
|
|
|
2019-11-21 23:16:35 +01:00
|
|
|
|
2018-05-27 17:37:54 +02:00
|
|
|
class MetaPackage(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
name = db.Column(db.String(100), unique=True, nullable=False)
|
2018-05-27 22:31:11 +02:00
|
|
|
dependencies = db.relationship("Dependency", backref="meta_package", lazy="dynamic")
|
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
|
|
|
|
def GetOrCreate(name, cache={}):
|
|
|
|
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
|
|
|
|
def SpecToList(spec, cache={}):
|
|
|
|
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)
|
|
|
|
|
|
|
|
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(" ", "_"))
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
def getActual(self):
|
|
|
|
return None if self.name == "None" else self
|
|
|
|
|
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:
|
2020-06-04 21:40:56 +02:00
|
|
|
return MinetestRelease.query.filter_by(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"))
|
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)
|
2018-05-23 20:24:10 +02:00
|
|
|
url = db.Column(db.String(200), nullable=False)
|
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
|
|
|
|
2020-01-24 21:21:40 +01:00
|
|
|
def getAsDictionary(self):
|
|
|
|
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,
|
|
|
|
"min_protocol": self.min_rel and self.min_rel.protocol,
|
|
|
|
"max_protocol": self.max_rel and self.max_rel.protocol
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:41:34 +01:00
|
|
|
def getEditURL(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)
|
|
|
|
|
2020-01-19 01:02:33 +01:00
|
|
|
def getDeleteURL(self):
|
|
|
|
return url_for("packages.delete_release",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2018-07-28 19:33:36 +02:00
|
|
|
def getDownloadURL(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
|
|
|
|
2019-03-29 21:47:48 +01:00
|
|
|
def approve(self, user):
|
2020-05-31 17:06:04 +02:00
|
|
|
if not self.package.checkPerm(user, Permission.APPROVE_RELEASE):
|
2019-03-29 21:47:48 +01:00
|
|
|
return False
|
|
|
|
|
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
|
|
|
|
return True
|
|
|
|
|
2020-01-19 01:02:33 +01:00
|
|
|
def checkPerm(self, user, perm):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to PackageRelease.checkPerm()")
|
|
|
|
|
|
|
|
isOwner = user == self.package.author
|
|
|
|
|
|
|
|
if perm == Permission.DELETE_RELEASE:
|
|
|
|
if user.rank.atLeast(UserRank.ADMIN):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if not (isOwner or user.rank.atLeast(UserRank.EDITOR)):
|
|
|
|
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
|
|
|
|
|
|
|
|
count = PackageRelease.query \
|
|
|
|
.filter_by(package_id=self.package_id) \
|
|
|
|
.filter(PackageRelease.id > self.id) \
|
|
|
|
.count()
|
|
|
|
|
|
|
|
return count > 0
|
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to releases".format(perm.name))
|
|
|
|
|
2018-07-28 17:05:09 +02:00
|
|
|
|
2020-01-19 16:03:38 +01:00
|
|
|
# class PackageReview(db.Model):
|
|
|
|
# id = db.Column(db.Integer, primary_key=True)
|
|
|
|
# package_id = db.Column(db.Integer, db.ForeignKey("package.id"))
|
|
|
|
# thread_id = db.Column(db.Integer, db.ForeignKey("thread.id"), nullable=False)
|
|
|
|
# recommend = db.Column(db.Boolean, nullable=False, default=True)
|
2018-07-28 17:05:09 +02:00
|
|
|
|
|
|
|
|
2018-03-26 11:52:25 +02:00
|
|
|
class PackageScreenshot(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"))
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
def getEditURL(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
|
|
|
|
2018-12-21 22:22:15 +01:00
|
|
|
def getThumbnailURL(self, level=2):
|
|
|
|
return self.url.replace("/uploads/", ("/thumbnails/{:d}/").format(level))
|
|
|
|
|
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
class APIToken(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
access_token = db.Column(db.String(34), unique=True)
|
2020-01-24 20:26:00 +01:00
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
name = db.Column(db.String(100), nullable=False)
|
|
|
|
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
2020-01-24 20:26:00 +01:00
|
|
|
# owner is created using backref
|
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
|
|
|
|
2020-01-24 20:26:00 +01:00
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
def canOperateOnPackage(self, package):
|
2020-01-24 21:21:40 +01:00
|
|
|
if self.package and self.package != package:
|
2020-01-24 20:26:00 +01:00
|
|
|
return False
|
|
|
|
|
2020-01-24 21:21:40 +01:00
|
|
|
return package.author == self.owner
|
2019-11-22 15:33:22 +01:00
|
|
|
|
2018-03-26 11:52:25 +02:00
|
|
|
|
2018-03-23 20:08:55 +01:00
|
|
|
class EditRequest(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"))
|
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
|
|
|
|
|
2018-03-23 20:25:48 +01:00
|
|
|
title = db.Column(db.String(100), nullable=False)
|
2018-03-23 20:08:55 +01:00
|
|
|
desc = db.Column(db.String(1000), nullable=True)
|
|
|
|
|
2018-05-13 16:40:37 +02:00
|
|
|
# 0 - open
|
|
|
|
# 1 - merged
|
|
|
|
# 2 - rejected
|
2018-03-23 20:49:57 +01:00
|
|
|
status = db.Column(db.Integer, nullable=False, default=0)
|
2018-03-23 20:08:55 +01:00
|
|
|
|
|
|
|
changes = db.relationship("EditRequestChange", backref="request",
|
|
|
|
lazy="dynamic")
|
|
|
|
|
2018-03-23 20:25:48 +01:00
|
|
|
def getURL(self):
|
|
|
|
return url_for("view_editrequest_page",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2018-03-23 20:49:57 +01:00
|
|
|
def getApproveURL(self):
|
|
|
|
return url_for("approve_editrequest_page",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
|
|
|
def getRejectURL(self):
|
|
|
|
return url_for("reject_editrequest_page",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2018-05-13 16:40:37 +02:00
|
|
|
def getEditURL(self):
|
|
|
|
return url_for("create_edit_editrequest_page",
|
|
|
|
author=self.package.author.username,
|
|
|
|
name=self.package.name,
|
|
|
|
id=self.id)
|
|
|
|
|
2018-03-23 20:08:55 +01:00
|
|
|
def applyAll(self, package):
|
|
|
|
for change in self.changes:
|
|
|
|
change.apply(package)
|
|
|
|
|
2018-03-23 20:25:48 +01:00
|
|
|
|
2018-05-13 16:40:37 +02:00
|
|
|
def checkPerm(self, user, perm):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to EditRequest.checkPerm()")
|
|
|
|
|
|
|
|
isOwner = user == self.author
|
|
|
|
|
|
|
|
# Members can edit their own packages, and editors can edit any packages
|
|
|
|
if perm == Permission.EDIT_EDITREQUEST:
|
|
|
|
return isOwner or user.rank.atLeast(UserRank.EDITOR)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to packages".format(perm.name))
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-23 20:25:48 +01:00
|
|
|
|
2018-03-23 20:08:55 +01:00
|
|
|
class EditRequestChange(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
request_id = db.Column(db.Integer, db.ForeignKey("edit_request.id"))
|
|
|
|
key = db.Column(db.Enum(PackagePropertyKey), nullable=False)
|
|
|
|
|
|
|
|
# TODO: make diff instead
|
2018-03-23 20:49:57 +01:00
|
|
|
oldValue = db.Column(db.Text, nullable=True)
|
|
|
|
newValue = db.Column(db.Text, nullable=True)
|
2018-03-23 20:08:55 +01:00
|
|
|
|
|
|
|
def apply(self, package):
|
2018-03-26 16:17:39 +02:00
|
|
|
if self.key == PackagePropertyKey.tags:
|
|
|
|
package.tags.clear()
|
|
|
|
for tagTitle in self.newValue.split(","):
|
|
|
|
tag = Tag.query.filter_by(title=tagTitle.strip()).first()
|
|
|
|
package.tags.append(tag)
|
2018-05-25 18:41:16 +02:00
|
|
|
|
2018-03-26 16:17:39 +02:00
|
|
|
else:
|
|
|
|
setattr(package, self.key.name, self.newValue)
|
2018-03-23 20:08:55 +01:00
|
|
|
|
2018-06-02 19:22:57 +02:00
|
|
|
|
2018-06-12 00:38:03 +02:00
|
|
|
watchers = db.Table("watchers",
|
2020-12-04 00:57:58 +01:00
|
|
|
db.Column("user_id", db.Integer, db.ForeignKey("user.id"), primary_key=True),
|
|
|
|
db.Column("thread_id", db.Integer, db.ForeignKey("thread.id"), primary_key=True)
|
2018-06-12 00:38:03 +02:00
|
|
|
)
|
|
|
|
|
2018-06-11 23:49:25 +02:00
|
|
|
class Thread(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
|
|
|
|
2020-07-11 02:34:51 +02:00
|
|
|
review_id = db.Column(db.Integer, db.ForeignKey("package_review.id"), nullable=True)
|
2020-12-04 00:57:58 +01:00
|
|
|
review = db.relationship("PackageReview", foreign_keys=[review_id], cascade="all, delete")
|
2020-07-09 05:10:09 +02:00
|
|
|
|
2018-06-11 23:49:25 +02:00
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
title = db.Column(db.String(100), nullable=False)
|
2020-07-11 02:34:51 +02:00
|
|
|
private = db.Column(db.Boolean, server_default="0", nullable=False)
|
|
|
|
|
|
|
|
locked = db.Column(db.Boolean, server_default="0", nullable=False)
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2019-01-28 20:01:37 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2020-07-13 00:53:20 +02:00
|
|
|
replies = db.relationship("ThreadReply", backref="thread", lazy="dynamic", \
|
2020-12-04 00:57:58 +01:00
|
|
|
order_by=db.asc("thread_reply_id"), cascade="all, delete")
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2018-06-12 00:38:03 +02:00
|
|
|
watchers = db.relationship("User", secondary=watchers, lazy="subquery", \
|
2020-07-13 00:53:20 +02:00
|
|
|
backref=db.backref("watching", lazy=True))
|
2018-06-12 00:38:03 +02:00
|
|
|
|
2020-07-10 20:26:37 +02:00
|
|
|
def getViewURL(self):
|
|
|
|
return url_for("threads.view", id=self.id)
|
2018-07-28 16:30:59 +02:00
|
|
|
|
|
|
|
def getSubscribeURL(self):
|
2020-07-10 20:26:37 +02:00
|
|
|
return url_for("threads.subscribe", id=self.id)
|
2018-07-28 16:30:59 +02:00
|
|
|
|
|
|
|
def getUnsubscribeURL(self):
|
2020-07-10 20:26:37 +02:00
|
|
|
return url_for("threads.unsubscribe", id=self.id)
|
2018-07-28 16:30:59 +02:00
|
|
|
|
2018-06-11 23:49:25 +02:00
|
|
|
def checkPerm(self, user, perm):
|
|
|
|
if not user.is_authenticated:
|
2020-07-11 04:35:14 +02:00
|
|
|
return perm == Permission.SEE_THREAD and not self.private
|
2018-06-11 23:49:25 +02:00
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to Thread.checkPerm()")
|
|
|
|
|
2020-07-08 23:45:24 +02:00
|
|
|
isMaintainer = user == self.author or (self.package is not None and self.package.author == user)
|
|
|
|
if self.package:
|
|
|
|
isMaintainer = isMaintainer or user in self.package.maintainers
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2020-07-11 02:34:51 +02:00
|
|
|
canSee = not self.private or isMaintainer or user.rank.atLeast(UserRank.EDITOR)
|
|
|
|
|
2018-06-11 23:49:25 +02:00
|
|
|
if perm == Permission.SEE_THREAD:
|
2020-07-11 02:34:51 +02:00
|
|
|
return canSee
|
|
|
|
|
|
|
|
elif perm == Permission.COMMENT_THREAD:
|
|
|
|
return canSee and (not self.locked or user.rank.atLeast(UserRank.MODERATOR))
|
|
|
|
|
2020-12-04 00:57:58 +01:00
|
|
|
elif perm == Permission.LOCK_THREAD or perm == Permission.DELETE_THREAD:
|
2020-07-11 02:34:51 +02:00
|
|
|
return user.rank.atLeast(UserRank.MODERATOR)
|
2018-06-11 23:49:25 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to threads".format(perm.name))
|
|
|
|
|
|
|
|
class ThreadReply(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
thread_id = db.Column(db.Integer, db.ForeignKey("thread.id"), nullable=False)
|
2020-07-15 17:01:33 +02:00
|
|
|
comment = db.Column(db.String(2000), nullable=False)
|
2018-06-11 23:49:25 +02:00
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
2019-01-28 20:01:37 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2020-07-11 04:35:14 +02:00
|
|
|
def checkPerm(self, user, perm):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to ThreadReply.checkPerm()")
|
|
|
|
|
2020-07-11 04:52:56 +02:00
|
|
|
if perm == Permission.EDIT_REPLY:
|
2020-07-11 05:26:50 +02:00
|
|
|
return user == self.author and user.rank.atLeast(UserRank.MEMBER) and not self.thread.locked
|
2020-07-11 04:52:56 +02:00
|
|
|
|
|
|
|
elif perm == Permission.DELETE_REPLY:
|
2020-07-11 04:35:14 +02:00
|
|
|
return user.rank.atLeast(UserRank.MODERATOR) and self.thread.replies[0] != self
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to threads".format(perm.name))
|
|
|
|
|
2018-06-11 23:49:25 +02:00
|
|
|
|
2020-07-09 05:10:09 +02:00
|
|
|
class PackageReview(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id], backref=db.backref("reviews", lazy=True))
|
|
|
|
|
2020-07-09 06:22:58 +02:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
|
|
|
|
2020-07-09 05:10:09 +02:00
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
author = db.relationship("User", foreign_keys=[author_id], backref=db.backref("reviews", lazy=True))
|
|
|
|
|
|
|
|
recommends = db.Column(db.Boolean, nullable=False)
|
|
|
|
|
|
|
|
thread = db.relationship("Thread", uselist=False, back_populates="review")
|
|
|
|
|
2020-07-09 05:32:13 +02:00
|
|
|
def asSign(self):
|
|
|
|
return 1 if self.recommends else -1
|
|
|
|
|
2020-07-09 05:10:09 +02:00
|
|
|
def getEditURL(self):
|
2020-07-10 20:26:37 +02:00
|
|
|
return self.package.getReviewURL()
|
|
|
|
|
|
|
|
def getDeleteURL(self):
|
|
|
|
return url_for("packages.delete_review",
|
2020-07-09 05:10:09 +02:00
|
|
|
author=self.package.author.username,
|
2020-07-10 20:26:37 +02:00
|
|
|
name=self.package.name)
|
2020-07-09 05:10:09 +02:00
|
|
|
|
|
|
|
|
2020-07-11 03:32:17 +02:00
|
|
|
class AuditSeverity(enum.Enum):
|
|
|
|
NORMAL = 0 # Normal user changes
|
|
|
|
EDITOR = 1 # Editor changes
|
|
|
|
MODERATION = 2 # Destructive / moderator changes
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return self.name.replace("_", " ").title()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
|
|
|
return [(choice, choice.getTitle()) for choice in cls]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
|
|
|
return item if type(item) == AuditSeverity else AuditSeverity[item]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AuditLogEntry(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
|
|
|
|
|
|
|
causer_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
causer = db.relationship("User", foreign_keys=[causer_id])
|
|
|
|
|
|
|
|
severity = db.Column(db.Enum(AuditSeverity), nullable=False)
|
|
|
|
|
|
|
|
title = db.Column(db.String(100), nullable=False)
|
|
|
|
url = db.Column(db.String(200), nullable=True)
|
|
|
|
|
|
|
|
package_id = db.Column(db.Integer, db.ForeignKey("package.id"), nullable=True)
|
|
|
|
package = db.relationship("Package", foreign_keys=[package_id])
|
|
|
|
|
2020-07-11 04:29:33 +02:00
|
|
|
description = db.Column(db.Text, nullable=True, default=None)
|
|
|
|
|
|
|
|
def __init__(self, causer, severity, title, url, package=None, description=None):
|
2020-07-11 03:32:17 +02:00
|
|
|
if len(title) > 100:
|
|
|
|
title = title[:99] + "…"
|
|
|
|
|
|
|
|
self.causer = causer
|
|
|
|
self.severity = severity
|
|
|
|
self.title = title
|
|
|
|
self.url = url
|
|
|
|
self.package = package
|
2020-07-11 04:29:33 +02:00
|
|
|
self.description = description
|
2020-07-11 03:32:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-09 05:10:09 +02:00
|
|
|
|
2018-06-06 00:45:15 +02:00
|
|
|
REPO_BLACKLIST = [".zip", "mediafire.com", "dropbox.com", "weebly.com", \
|
|
|
|
"minetest.net", "dropboxusercontent.com", "4shared.com", \
|
|
|
|
"digitalaudioconcepts.com", "hg.intevation.org", "www.wtfpl.net", \
|
|
|
|
"imageshack.com", "imgur.com"]
|
2018-06-02 19:22:57 +02:00
|
|
|
|
2018-07-04 01:14:37 +02:00
|
|
|
class ForumTopic(db.Model):
|
2018-06-02 19:22:57 +02:00
|
|
|
topic_id = db.Column(db.Integer, primary_key=True, autoincrement=False)
|
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
|
|
|
author = db.relationship("User")
|
|
|
|
|
2018-07-07 00:15:56 +02:00
|
|
|
wip = db.Column(db.Boolean, server_default="0")
|
2018-12-24 00:49:49 +01:00
|
|
|
discarded = db.Column(db.Boolean, server_default="0")
|
2018-07-07 00:15:56 +02:00
|
|
|
|
2018-07-04 01:14:37 +02:00
|
|
|
type = db.Column(db.Enum(PackageType), nullable=False)
|
2018-06-02 19:22:57 +02:00
|
|
|
title = db.Column(db.String(200), nullable=False)
|
|
|
|
name = db.Column(db.String(30), nullable=True)
|
2018-06-02 19:32:07 +02:00
|
|
|
link = db.Column(db.String(200), nullable=True)
|
2018-06-02 19:22:57 +02:00
|
|
|
|
2018-07-04 01:14:37 +02:00
|
|
|
posts = db.Column(db.Integer, nullable=False)
|
|
|
|
views = db.Column(db.Integer, nullable=False)
|
|
|
|
|
2019-01-28 20:01:37 +01:00
|
|
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
2018-06-02 19:22:57 +02:00
|
|
|
|
2018-06-06 00:45:15 +02:00
|
|
|
def getRepoURL(self):
|
2018-07-04 01:14:37 +02:00
|
|
|
if self.link is None:
|
|
|
|
return None
|
|
|
|
|
2018-06-06 00:45:15 +02:00
|
|
|
for item in REPO_BLACKLIST:
|
|
|
|
if item in self.link:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return self.link.replace("repo.or.cz/w/", "repo.or.cz/")
|
|
|
|
|
2018-07-07 00:41:44 +02:00
|
|
|
def getAsDictionary(self):
|
|
|
|
return {
|
|
|
|
"author": self.author.username,
|
|
|
|
"name": self.name,
|
|
|
|
"type": self.type.toName(),
|
|
|
|
"title": self.title,
|
|
|
|
"id": self.topic_id,
|
|
|
|
"link": self.link,
|
|
|
|
"posts": self.posts,
|
|
|
|
"views": self.views,
|
|
|
|
"is_wip": self.wip,
|
2018-12-24 00:49:49 +01:00
|
|
|
"discarded": self.discarded,
|
2018-07-07 00:41:44 +02:00
|
|
|
"created_at": self.created_at.isoformat(),
|
|
|
|
}
|
|
|
|
|
2018-12-25 18:51:29 +01:00
|
|
|
def checkPerm(self, user, perm):
|
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to ForumTopic.checkPerm()")
|
|
|
|
|
|
|
|
if perm == Permission.TOPIC_DISCARD:
|
|
|
|
return self.author == user or user.rank.atLeast(UserRank.EDITOR)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise Exception("Permission {} is not related to topics".format(perm.name))
|
|
|
|
|
2018-06-02 19:22:57 +02:00
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
# Setup Flask-User
|
2020-01-19 03:46:07 +01:00
|
|
|
user_manager = UserManager(app, db, User)
|
2020-04-21 21:35:05 +02:00
|
|
|
|
|
|
|
if app.config.get("LOG_SQL"):
|
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
|