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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-27 13:49:33 -07:00
parent 4abbf9a917
commit f849e8b696
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
4 changed files with 23 additions and 9 deletions

View file

@ -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):