2021-02-02 18:09:28 +01:00
|
|
|
import datetime
|
2021-02-02 01:07:41 +01:00
|
|
|
|
2021-02-02 18:09:25 +01:00
|
|
|
from app.logic.LogicError import LogicError
|
2021-02-02 01:07:41 +01:00
|
|
|
from app.logic.uploads import upload_file
|
2021-02-02 18:29:03 +01:00
|
|
|
from app.models import User, Package, PackageScreenshot, Permission, NotificationType, db, AuditSeverity
|
|
|
|
from app.utils import addNotification, addAuditLog
|
2021-02-02 01:07:41 +01:00
|
|
|
|
|
|
|
|
2021-02-02 18:29:03 +01:00
|
|
|
def do_create_screenshot(user: User, package: Package, title: str, file, reason: str = None):
|
2021-02-02 18:09:28 +01:00
|
|
|
thirty_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=30)
|
|
|
|
count = package.screenshots.filter(PackageScreenshot.created_at > thirty_minutes_ago).count()
|
|
|
|
if count >= 20:
|
|
|
|
raise LogicError(429, "Too many requests, please wait before trying again")
|
|
|
|
|
2021-02-02 01:07:41 +01:00
|
|
|
uploaded_url, uploaded_path = upload_file(file, "image", "a PNG or JPG image file")
|
|
|
|
|
|
|
|
counter = 1
|
2021-02-02 18:09:28 +01:00
|
|
|
for screenshot in package.screenshots.all():
|
2021-02-02 01:07:41 +01:00
|
|
|
screenshot.order = counter
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
ss = PackageScreenshot()
|
|
|
|
ss.package = package
|
|
|
|
ss.title = title or "Untitled"
|
|
|
|
ss.url = uploaded_url
|
|
|
|
ss.approved = package.checkPerm(user, Permission.APPROVE_SCREENSHOT)
|
|
|
|
ss.order = counter
|
|
|
|
db.session.add(ss)
|
|
|
|
|
2021-02-02 18:29:03 +01:00
|
|
|
if reason is None:
|
|
|
|
msg = "Created screenshot {}".format(ss.title)
|
|
|
|
else:
|
|
|
|
msg = "Created screenshot {} ({})".format(ss.title, reason)
|
|
|
|
|
2021-02-02 01:07:41 +01:00
|
|
|
addNotification(package.maintainers, user, NotificationType.PACKAGE_EDIT, msg, package.getDetailsURL(), package)
|
2021-02-02 18:29:03 +01:00
|
|
|
addAuditLog(AuditSeverity.NORMAL, user, msg, package.getDetailsURL(), package)
|
|
|
|
|
2021-02-02 01:07:41 +01:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return ss
|
2021-02-02 18:09:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def do_order_screenshots(_user: User, package: Package, order: [any]):
|
|
|
|
lookup = {}
|
|
|
|
for screenshot in package.screenshots.all():
|
|
|
|
lookup[screenshot.id] = screenshot
|
|
|
|
|
|
|
|
counter = 1
|
|
|
|
for id in order:
|
|
|
|
try:
|
|
|
|
lookup[int(id)].order = counter
|
|
|
|
counter += 1
|
|
|
|
except KeyError as e:
|
|
|
|
raise LogicError(400, "Unable to find screenshot with id={}".format(id))
|
|
|
|
except ValueError as e:
|
|
|
|
raise LogicError(400, "Invalid number: {}".format(id))
|
|
|
|
|
|
|
|
db.session.commit()
|