mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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:
parent
4abbf9a917
commit
f849e8b696
4 changed files with 23 additions and 9 deletions
|
@ -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__.
|
||||
|
|
|
@ -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]}
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue