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

@ -40,7 +40,7 @@ class AnonymousUser:
ssh_pub_key = None ssh_pub_key = None
# Add stubbed relationship backrefs. # Add stubbed relationship backrefs.
package_notifications = StubQuery() notifications = StubQuery()
package_votes = StubQuery() package_votes = StubQuery()
# A nonce attribute, needed for all browser sessions; set in __init__. # A nonce attribute, needed for all browser sessions; set in __init__.

View file

@ -15,7 +15,7 @@ class PackageNotification(Base):
Integer, ForeignKey("Users.ID", ondelete="CASCADE"), Integer, ForeignKey("Users.ID", ondelete="CASCADE"),
nullable=False) nullable=False)
User = relationship( User = relationship(
"User", backref=backref("package_notifications", lazy="dynamic"), "User", backref=backref("notifications", lazy="dynamic"),
foreign_keys=[UserID]) foreign_keys=[UserID])
PackageBaseID = Column( PackageBaseID = Column(
@ -23,7 +23,7 @@ class PackageNotification(Base):
nullable=False) nullable=False)
PackageBase = relationship( PackageBase = relationship(
"PackageBase", "PackageBase",
backref=backref("package_notifications", lazy="dynamic"), backref=backref("notifications", lazy="dynamic"),
foreign_keys=[PackageBaseID]) foreign_keys=[PackageBaseID])
__mapper_args__ = {"primary_key": [UserID, PackageBaseID]} __mapper_args__ = {"primary_key": [UserID, PackageBaseID]}

View file

@ -191,10 +191,26 @@ class User(Base):
).scalar()) ).scalar())
def notified(self, package) -> bool: 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 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()) ).scalar())
def packages(self): def packages(self):

View file

@ -127,9 +127,7 @@ async def make_single_context(request: Request,
context["comments"] = pkgbase.comments context["comments"] = pkgbase.comments
context["is_maintainer"] = (request.user.is_authenticated() context["is_maintainer"] = (request.user.is_authenticated()
and request.user.ID == pkgbase.MaintainerUID) and request.user.ID == pkgbase.MaintainerUID)
context["notified"] = request.user.package_notifications.filter( context["notified"] = request.user.notified(pkgbase)
PackageNotification.PackageBaseID == pkgbase.ID
).scalar()
context["out_of_date"] = bool(pkgbase.OutOfDateTS) context["out_of_date"] = bool(pkgbase.OutOfDateTS)