From f849e8b696416933282185df2d7581890e748f5a Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 27 Sep 2021 13:49:33 -0700 Subject: [PATCH] change(FastAPI): allow User.notified to accept a Package OR PackageBase In addition, shorten the `package_notifications` relationship to `notifications`. Signed-off-by: Kevin Morris --- aurweb/auth.py | 2 +- aurweb/models/package_notification.py | 4 ++-- aurweb/models/user.py | 22 +++++++++++++++++++--- aurweb/routers/packages.py | 4 +--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/aurweb/auth.py b/aurweb/auth.py index 2e6674b0..19c3a276 100644 --- a/aurweb/auth.py +++ b/aurweb/auth.py @@ -40,7 +40,7 @@ class AnonymousUser: ssh_pub_key = None # Add stubbed relationship backrefs. - package_notifications = StubQuery() + notifications = StubQuery() package_votes = StubQuery() # A nonce attribute, needed for all browser sessions; set in __init__. diff --git a/aurweb/models/package_notification.py b/aurweb/models/package_notification.py index ab23a212..803c0496 100644 --- a/aurweb/models/package_notification.py +++ b/aurweb/models/package_notification.py @@ -15,7 +15,7 @@ class PackageNotification(Base): Integer, ForeignKey("Users.ID", ondelete="CASCADE"), nullable=False) User = relationship( - "User", backref=backref("package_notifications", lazy="dynamic"), + "User", backref=backref("notifications", lazy="dynamic"), foreign_keys=[UserID]) PackageBaseID = Column( @@ -23,7 +23,7 @@ class PackageNotification(Base): nullable=False) PackageBase = relationship( "PackageBase", - backref=backref("package_notifications", lazy="dynamic"), + backref=backref("notifications", lazy="dynamic"), foreign_keys=[PackageBaseID]) __mapper_args__ = {"primary_key": [UserID, PackageBaseID]} diff --git a/aurweb/models/user.py b/aurweb/models/user.py index 28aa613e..5f848304 100644 --- a/aurweb/models/user.py +++ b/aurweb/models/user.py @@ -191,10 +191,26 @@ class User(Base): ).scalar()) def notified(self, package) -> bool: - """ Is this User being notified about package? """ + """ Is this User being notified about package (or package base)? + + :param package: Package or PackageBase instance + :return: Boolean indicating state of package notification + in relation to this User + """ + from aurweb.models.package import Package + from aurweb.models.package_base import PackageBase from aurweb.models.package_notification import PackageNotification - return bool(package.PackageBase.package_notifications.filter( - PackageNotification.UserID == self.ID + + query = None + if isinstance(package, Package): + query = package.PackageBase.notifications + elif isinstance(package, PackageBase): + query = package.notifications + + # Run an exists() query where a pkgbase-related + # PackageNotification exists for self (a user). + return bool(db.query( + query.filter(PackageNotification.UserID == self.ID).exists() ).scalar()) def packages(self): diff --git a/aurweb/routers/packages.py b/aurweb/routers/packages.py index 72cd8c99..aa20e5fa 100644 --- a/aurweb/routers/packages.py +++ b/aurweb/routers/packages.py @@ -127,9 +127,7 @@ async def make_single_context(request: Request, context["comments"] = pkgbase.comments context["is_maintainer"] = (request.user.is_authenticated() and request.user.ID == pkgbase.MaintainerUID) - context["notified"] = request.user.package_notifications.filter( - PackageNotification.PackageBaseID == pkgbase.ID - ).scalar() + context["notified"] = request.user.notified(pkgbase) context["out_of_date"] = bool(pkgbase.OutOfDateTS)