2021-02-02 22:35:29 +01:00
|
|
|
# ContentDB
|
|
|
|
# Copyright (C) 2021 rubenwardy
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2021-02-03 00:58:59 +01:00
|
|
|
import re
|
|
|
|
import validators
|
2021-02-02 22:35:29 +01:00
|
|
|
|
|
|
|
from app.logic.LogicError import LogicError
|
2021-12-20 22:07:12 +01:00
|
|
|
from app.models import User, Package, PackageType, MetaPackage, Tag, ContentWarning, db, Permission, AuditSeverity, \
|
|
|
|
License, UserRank, PackageDevState
|
2021-02-03 00:58:59 +01:00
|
|
|
from app.utils import addAuditLog
|
2021-02-02 22:35:29 +01:00
|
|
|
|
|
|
|
|
2021-02-02 23:34:51 +01:00
|
|
|
def check(cond: bool, msg: str):
|
|
|
|
if not cond:
|
|
|
|
raise LogicError(400, msg)
|
|
|
|
|
|
|
|
|
|
|
|
def get_license(name):
|
|
|
|
if type(name) == License:
|
|
|
|
return name
|
|
|
|
|
|
|
|
license = License.query.filter(License.name.ilike(name)).first()
|
|
|
|
if license is None:
|
|
|
|
raise LogicError(400, "Unknown license: " + name)
|
|
|
|
return license
|
|
|
|
|
|
|
|
|
|
|
|
name_re = re.compile("^[a-z0-9_]+$")
|
|
|
|
|
2021-07-24 01:43:55 +02:00
|
|
|
AnyType = "?"
|
2021-02-02 23:47:46 +01:00
|
|
|
ALLOWED_FIELDS = {
|
2021-07-24 01:43:55 +02:00
|
|
|
"type": AnyType,
|
2021-02-02 23:34:51 +01:00
|
|
|
"title": str,
|
2021-02-02 23:47:46 +01:00
|
|
|
"name": str,
|
2021-02-02 23:34:51 +01:00
|
|
|
"short_description": str,
|
|
|
|
"short_desc": str,
|
2021-12-20 22:07:12 +01:00
|
|
|
"dev_state": AnyType,
|
2021-02-02 23:34:51 +01:00
|
|
|
"tags": list,
|
|
|
|
"content_warnings": list,
|
2021-07-24 01:43:55 +02:00
|
|
|
"license": AnyType,
|
|
|
|
"media_license": AnyType,
|
2021-02-03 01:11:48 +01:00
|
|
|
"long_description": str,
|
2021-02-02 23:47:46 +01:00
|
|
|
"desc": str,
|
2021-02-02 23:34:51 +01:00
|
|
|
"repo": str,
|
|
|
|
"website": str,
|
|
|
|
"issue_tracker": str,
|
|
|
|
"issueTracker": str,
|
|
|
|
"forums": int,
|
|
|
|
}
|
|
|
|
|
2021-02-03 01:11:48 +01:00
|
|
|
ALIASES = {
|
|
|
|
"short_description": "short_desc",
|
|
|
|
"issue_tracker": "issueTracker",
|
|
|
|
"long_description": "desc"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-02 23:34:51 +01:00
|
|
|
def is_int(val):
|
|
|
|
try:
|
|
|
|
int(val)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def validate(data: dict):
|
2021-02-02 23:47:46 +01:00
|
|
|
for key, value in data.items():
|
|
|
|
if value is not None:
|
|
|
|
typ = ALLOWED_FIELDS.get(key)
|
|
|
|
check(typ is not None, key + " is not a known field")
|
2021-07-24 01:43:55 +02:00
|
|
|
if typ != AnyType:
|
2021-02-02 23:47:46 +01:00
|
|
|
check(isinstance(value, typ), key + " must be a " + typ.__name__)
|
2021-02-02 23:34:51 +01:00
|
|
|
|
|
|
|
if "name" in data:
|
|
|
|
name = data["name"]
|
|
|
|
check(isinstance(name, str), "Name must be a string")
|
|
|
|
check(bool(name_re.match(name)),
|
|
|
|
"Name can only contain lower case letters (a-z), digits (0-9), and underscores (_)")
|
|
|
|
|
|
|
|
for key in ["repo", "website", "issue_tracker", "issueTracker"]:
|
|
|
|
value = data.get(key)
|
|
|
|
if value is not None:
|
|
|
|
check(value.startswith("http://") or value.startswith("https://"),
|
|
|
|
key + " must start with http:// or https://")
|
|
|
|
|
|
|
|
check(validators.url(value, public=True), key + " must be a valid URL")
|
|
|
|
|
|
|
|
|
2021-07-24 01:43:55 +02:00
|
|
|
def do_edit_package(user: User, package: Package, was_new: bool, was_web: bool, data: dict,
|
|
|
|
reason: str = None):
|
2021-02-02 23:34:51 +01:00
|
|
|
if not package.checkPerm(user, Permission.EDIT_PACKAGE):
|
|
|
|
raise LogicError(403, "You do not have permission to edit this package")
|
|
|
|
|
2021-02-02 22:35:29 +01:00
|
|
|
if "name" in data and package.name != data["name"] and \
|
|
|
|
not package.checkPerm(user, Permission.CHANGE_NAME):
|
|
|
|
raise LogicError(403, "You do not have permission to change the package name")
|
|
|
|
|
2021-02-03 01:11:48 +01:00
|
|
|
for alias, to in ALIASES.items():
|
2021-02-02 22:35:29 +01:00
|
|
|
if alias in data:
|
|
|
|
data[to] = data[alias]
|
|
|
|
|
2021-02-02 23:34:51 +01:00
|
|
|
validate(data)
|
|
|
|
|
|
|
|
if "type" in data:
|
|
|
|
data["type"] = PackageType.coerce(data["type"])
|
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
if "dev_state" in data:
|
|
|
|
data["dev_state"] = PackageDevState.coerce(data["dev_state"])
|
|
|
|
if data["dev_state"] is None:
|
|
|
|
raise LogicError(400, "dev_state cannot be null")
|
|
|
|
|
2021-02-02 23:34:51 +01:00
|
|
|
if "license" in data:
|
|
|
|
data["license"] = get_license(data["license"])
|
|
|
|
|
|
|
|
if "media_license" in data:
|
|
|
|
data["media_license"] = get_license(data["media_license"])
|
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
for key in ["name", "title", "short_desc", "desc", "type", "dev_state", "license", "media_license",
|
2021-02-02 22:35:29 +01:00
|
|
|
"repo", "website", "issueTracker", "forums"]:
|
|
|
|
if key in data:
|
|
|
|
setattr(package, key, data[key])
|
|
|
|
|
|
|
|
if package.type == PackageType.TXP:
|
|
|
|
package.license = package.media_license
|
|
|
|
|
|
|
|
if was_new and package.type == PackageType.MOD:
|
|
|
|
m = MetaPackage.GetOrCreate(package.name, {})
|
|
|
|
package.provides.append(m)
|
|
|
|
|
2021-02-02 23:34:51 +01:00
|
|
|
if "tags" in data:
|
2021-07-22 19:33:24 +02:00
|
|
|
old_tags = list(package.tags)
|
2021-02-02 23:34:51 +01:00
|
|
|
package.tags.clear()
|
|
|
|
for tag_id in data["tags"]:
|
|
|
|
if is_int(tag_id):
|
2021-07-22 13:13:16 +02:00
|
|
|
tag = Tag.query.get(tag_id)
|
2021-02-02 23:34:51 +01:00
|
|
|
else:
|
|
|
|
tag = Tag.query.filter_by(name=tag_id).first()
|
|
|
|
if tag is None:
|
|
|
|
raise LogicError(400, "Unknown tag: " + tag_id)
|
2021-07-22 13:13:16 +02:00
|
|
|
|
2021-07-24 01:43:55 +02:00
|
|
|
if not was_web and tag.is_protected:
|
|
|
|
break
|
|
|
|
|
2021-07-22 13:13:16 +02:00
|
|
|
if tag.is_protected and tag not in old_tags and not user.rank.atLeast(UserRank.EDITOR):
|
|
|
|
raise LogicError(400, f"Unable to add protected tag {tag.title} to package")
|
|
|
|
|
|
|
|
package.tags.append(tag)
|
2021-02-02 22:35:29 +01:00
|
|
|
|
2021-07-24 01:43:55 +02:00
|
|
|
if not was_web:
|
|
|
|
for tag in old_tags:
|
|
|
|
if tag.is_protected:
|
|
|
|
package.tags.append(tag)
|
|
|
|
|
2021-02-02 22:35:29 +01:00
|
|
|
if "content_warnings" in data:
|
|
|
|
package.content_warnings.clear()
|
2021-02-02 23:34:51 +01:00
|
|
|
for warning_id in data["content_warnings"]:
|
|
|
|
if is_int(warning_id):
|
|
|
|
package.content_warnings.append(ContentWarning.query.get(warning_id))
|
|
|
|
else:
|
|
|
|
warning = ContentWarning.query.filter_by(name=warning_id).first()
|
|
|
|
if warning is None:
|
|
|
|
raise LogicError(400, "Unknown warning: " + warning_id)
|
|
|
|
package.content_warnings.append(warning)
|
2021-02-02 22:35:29 +01:00
|
|
|
|
|
|
|
if not was_new:
|
|
|
|
if reason is None:
|
|
|
|
msg = "Edited {}".format(package.title)
|
|
|
|
else:
|
|
|
|
msg = "Edited {} ({})".format(package.title, reason)
|
|
|
|
|
|
|
|
severity = AuditSeverity.NORMAL if user in package.maintainers else AuditSeverity.EDITOR
|
2021-07-24 05:30:14 +02:00
|
|
|
addAuditLog(severity, user, msg, package.getURL("packages.view"), package)
|
2021-02-02 22:35:29 +01:00
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return package
|