diff --git a/app/blueprints/admin/admin.py b/app/blueprints/admin/admin.py index 9c5ad568..ff509f97 100644 --- a/app/blueprints/admin/admin.py +++ b/app/blueprints/admin/admin.py @@ -25,7 +25,7 @@ from app.tasks.importtasks import importRepoScreenshot, makeVCSRelease, checkZip from app.tasks.forumtasks import importTopicList, checkAllForumAccounts from flask_wtf import FlaskForm from wtforms import * -from app.utils import loginUser, rank_required, triggerNotif +from app.utils import loginUser, rank_required, addNotification import datetime, os @bp.route("/admin/", methods=["GET", "POST"]) @@ -108,7 +108,7 @@ def admin_page(): makeVCSRelease.apply_async((rel.id, "master"), task_id=rel.task_id) msg = "{}: Release {} created".format(package.title, rel.title) - triggerNotif(package.author, current_user, msg, rel.getEditURL()) + addNotification(package.maintainers, current_user, msg, rel.getEditURL()) db.session.commit() elif action == "cleanuploads": diff --git a/app/blueprints/packages/editrequests.py b/app/blueprints/packages/editrequests.py index ee0d1621..f50a8120 100644 --- a/app/blueprints/packages/editrequests.py +++ b/app/blueprints/packages/editrequests.py @@ -99,8 +99,8 @@ def create_edit_editrequest_page(package, id=None): if wasChangeMade: msg = "{}: Edit request #{} {}" \ .format(package.title, erequest.id, "created" if id is None else "edited") - triggerNotif(package.author, current_user, msg, erequest.getURL()) - triggerNotif(erequest.author, current_user, msg, erequest.getURL()) + addNotification(package.maintainers, current_user, msg, erequest.getURL()) + addNotification(erequest.author, current_user, msg, erequest.getURL()) db.session.commit() return redirect(erequest.getURL()) else: @@ -142,8 +142,8 @@ def approve_editrequest_page(package, id): erequest.applyAll(package) msg = "{}: Edit request #{} merged".format(package.title, erequest.id) - triggerNotif(erequest.author, current_user, msg, erequest.getURL()) - triggerNotif(package.author, current_user, msg, erequest.getURL()) + addNotification(erequest.author, current_user, msg, erequest.getURL()) + addNotification(package.maintainers, current_user, msg, erequest.getURL()) db.session.commit() return redirect(package.getDetailsURL()) @@ -166,8 +166,8 @@ def reject_editrequest_page(package, id): erequest.status = 2 msg = "{}: Edit request #{} rejected".format(package.title, erequest.id) - triggerNotif(erequest.author, current_user, msg, erequest.getURL()) - triggerNotif(package.author, current_user, msg, erequest.getURL()) + addNotification(erequest.author, current_user, msg, erequest.getURL()) + addNotification(package.maintainers, current_user, msg, erequest.getURL()) db.session.commit() return redirect(package.getDetailsURL()) diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py index 4ba2a738..bfa3dd90 100644 --- a/app/blueprints/packages/packages.py +++ b/app/blueprints/packages/packages.py @@ -265,7 +265,7 @@ def create_edit(author=None, name=None): return redirect(url_for("packages.create_edit", author=author, name=name)) else: - triggerNotif(package.author, current_user, + addNotification(package.maintainers, current_user, "{} edited".format(package.title), package.getDetailsURL()) form.populate_obj(package) # copy to row @@ -337,7 +337,7 @@ def approve(package): for s in screenshots: s.approved = True - triggerNotif(package.author, current_user, + addNotification(package.maintainers, current_user, "{} approved".format(package.title), package.getDetailsURL()) db.session.commit() @@ -359,7 +359,7 @@ def remove(package): package.soft_deleted = True url = url_for("users.profile", username=package.author.username) - triggerNotif(package.author, current_user, + addNotification(package.maintainers, current_user, "{} deleted".format(package.title), url) db.session.commit() @@ -373,7 +373,7 @@ def remove(package): package.approved = False - triggerNotif(package.author, current_user, + addNotification(package.maintainers, current_user, "{} unapproved".format(package.title), package.getDetailsURL()) db.session.commit() @@ -408,19 +408,19 @@ def edit_maintainers(package): for user in users: if not user in package.maintainers: - triggerNotif(user, current_user, + addNotification(user, current_user, "Added you as a maintainer of {}".format(package.title), package.getDetailsURL()) for user in package.maintainers: if user != package.author and not user in users: - triggerNotif(user, current_user, + addNotification(user, current_user, "Removed you as a maintainer of {}".format(package.title), package.getDetailsURL()) package.maintainers.clear() package.maintainers.extend(users) package.maintainers.append(package.author) - triggerNotif(package.author, current_user, + addNotification(package.author, current_user, "Edited {} maintainers".format(package.title), package.getDetailsURL()) db.session.commit() @@ -446,7 +446,7 @@ def remove_self_maintainers(package): else: package.maintainers.remove(current_user) - triggerNotif(package.author, current_user, + addNotification(package.author, current_user, "Removed themself as a maintainer of {}".format(package.title), package.getDetailsURL()) db.session.commit() diff --git a/app/blueprints/packages/releases.py b/app/blueprints/packages/releases.py index 12be8f65..44987380 100644 --- a/app/blueprints/packages/releases.py +++ b/app/blueprints/packages/releases.py @@ -93,7 +93,7 @@ def create_release(package): makeVCSRelease.apply_async((rel.id, form["vcsLabel"].data), task_id=rel.task_id) msg = "{}: Release {} created".format(package.title, rel.title) - triggerNotif(package.author, current_user, msg, rel.getEditURL()) + addNotification(package.maintainers, current_user, msg, rel.getEditURL()) db.session.commit() return redirect(url_for("tasks.check", id=rel.task_id, r=rel.getEditURL())) @@ -113,7 +113,7 @@ def create_release(package): checkZipRelease.apply_async((rel.id, uploadedPath), task_id=rel.task_id) msg = "{}: Release {} created".format(package.title, rel.title) - triggerNotif(package.author, current_user, msg, rel.getEditURL()) + addNotification(package.maintainers, current_user, msg, rel.getEditURL()) db.session.commit() return redirect(url_for("tasks.check", id=rel.task_id, r=rel.getEditURL())) diff --git a/app/blueprints/packages/reviews.py b/app/blueprints/packages/reviews.py index 4322e8d4..114afae5 100644 --- a/app/blueprints/packages/reviews.py +++ b/app/blueprints/packages/reviews.py @@ -22,7 +22,7 @@ from flask_wtf import FlaskForm from wtforms import * from wtforms.validators import * from app.models import db, PackageReview, Thread, ThreadReply -from app.utils import is_package_page, triggerNotif +from app.utils import is_package_page, addNotification class ReviewForm(FlaskForm): title = StringField("Title", [InputRequired(), Length(3,100)]) @@ -88,8 +88,7 @@ def review(package): else: notif_msg = "Updated review '{}' on package {}".format(form.title.data, package.title) - for maintainer in package.maintainers: - triggerNotif(maintainer, current_user, notif_msg, url_for("threads.view", id=thread.id)) + addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id)) db.session.commit() diff --git a/app/blueprints/packages/screenshots.py b/app/blueprints/packages/screenshots.py index 2646e292..68a3075a 100644 --- a/app/blueprints/packages/screenshots.py +++ b/app/blueprints/packages/screenshots.py @@ -62,7 +62,7 @@ def create_screenshot(package, id=None): msg = "{}: Screenshot added {}" \ .format(package.title, ss.title) - triggerNotif(package.author, current_user, msg, package.getDetailsURL()) + addNotification(package.maintainers, current_user, msg, package.getDetailsURL()) db.session.commit() return redirect(package.getDetailsURL()) diff --git a/app/blueprints/threads/__init__.py b/app/blueprints/threads/__init__.py index c0b878cc..09b5eb78 100644 --- a/app/blueprints/threads/__init__.py +++ b/app/blueprints/threads/__init__.py @@ -21,7 +21,7 @@ bp = Blueprint("threads", __name__) from flask_user import * from app.models import * -from app.utils import triggerNotif, clearNotifications +from app.utils import addNotification, clearNotifications import datetime @@ -113,10 +113,7 @@ def view(id): msg = "New comment on '{}' on package {}".format(thread.title, thread.package.title) - for user in thread.watchers: - if user != current_user: - triggerNotif(user, current_user, msg, url_for("threads.view", id=thread.id)) - + addNotification(thread.watchers, current_user, msg, url_for("threads.view", id=thread.id)) db.session.commit() return redirect(url_for("threads.view", id=id)) @@ -206,13 +203,12 @@ def new(): notif_msg = None if package is not None: notif_msg = "New thread '{}' on package {}".format(thread.title, package.title) - for maintainer in package.maintainers: - triggerNotif(maintainer, current_user, notif_msg, url_for("threads.view", id=thread.id)) + addNotification(package.maintainers, current_user, notif_msg, url_for("threads.view", id=thread.id)) else: notif_msg = "New thread '{}'".format(thread.title) - for user in User.query.filter(User.rank >= UserRank.EDITOR).all(): - triggerNotif(user, current_user, notif_msg, url_for("threads.view", id=thread.id)) + editors = User.query.filter(User.rank >= UserRank.EDITOR).all() + addNotification(editors, current_user, notif_msg, url_for("threads.view", id=thread.id)) db.session.commit() diff --git a/app/utils.py b/app/utils.py index d6e6eaa6..67b4015e 100644 --- a/app/utils.py +++ b/app/utils.py @@ -188,12 +188,19 @@ def is_package_page(f): return decorated_function -def triggerNotif(owner, causer, title, url): - if owner.rank.atLeast(UserRank.NEW_MEMBER) and owner != causer: - Notification.query.filter_by(user=owner, causer=causer, title=title, url=url).delete() - notif = Notification(owner, causer, title, url) + +def addNotification(target, causer, title, url): + if not isinstance(target, User): + for x in target: + addNotification(x, causer, title, url) + return + + if target.rank.atLeast(UserRank.NEW_MEMBER) and target != causer: + Notification.query.filter_by(user=target, causer=causer, title=title, url=url).delete() + notif = Notification(target, causer, title, url) db.session.add(notif) + def clearNotifications(url): if current_user.is_authenticated: Notification.query.filter_by(user=current_user, url=url).delete()