mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
User: add several utility methods
Added: - User.voted_for(package) - Has a user voted for a particular package? - User.notified(package) - Is a user being notified about a particular package? - User.packages() - Entire collection of Package objects related to User. Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
5bd3a7bbab
commit
af51b5c460
2 changed files with 70 additions and 2 deletions
|
@ -5,13 +5,14 @@ from datetime import datetime
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
from sqlalchemy import Column, ForeignKey, Integer, String, text
|
from sqlalchemy import Column, ForeignKey, Integer, String, or_, text
|
||||||
from sqlalchemy.orm import backref, relationship
|
from sqlalchemy.orm import backref, relationship
|
||||||
|
|
||||||
import aurweb.config
|
import aurweb.config
|
||||||
import aurweb.models.account_type
|
import aurweb.models.account_type
|
||||||
import aurweb.schema
|
import aurweb.schema
|
||||||
|
|
||||||
|
from aurweb import db
|
||||||
from aurweb.models.ban import is_banned
|
from aurweb.models.ban import is_banned
|
||||||
from aurweb.models.declarative import Base
|
from aurweb.models.declarative import Base
|
||||||
|
|
||||||
|
@ -177,6 +178,39 @@ class User(Base):
|
||||||
"""
|
"""
|
||||||
return self == user or self.is_trusted_user() or self.is_developer()
|
return self == user or self.is_trusted_user() or self.is_developer()
|
||||||
|
|
||||||
|
def voted_for(self, package) -> bool:
|
||||||
|
""" Has this User voted for package? """
|
||||||
|
from aurweb.models.package_vote import PackageVote
|
||||||
|
return bool(package.PackageBase.package_votes.filter(
|
||||||
|
PackageVote.UsersID == self.ID
|
||||||
|
).scalar())
|
||||||
|
|
||||||
|
def notified(self, package) -> bool:
|
||||||
|
""" Is this User being notified about package? """
|
||||||
|
from aurweb.models.package_notification import PackageNotification
|
||||||
|
return bool(package.PackageBase.package_notifications.filter(
|
||||||
|
PackageNotification.UserID == self.ID
|
||||||
|
).scalar())
|
||||||
|
|
||||||
|
def packages(self):
|
||||||
|
""" Returns an ORM query to Package objects owned by this user.
|
||||||
|
|
||||||
|
This should really be replaced with an internal ORM join
|
||||||
|
configured for the User model. This has not been done yet
|
||||||
|
due to issues I've been encountering in the process, so
|
||||||
|
sticking with this function until we can properly implement it.
|
||||||
|
|
||||||
|
:return: ORM query of User-packaged or maintained Package objects
|
||||||
|
"""
|
||||||
|
from aurweb.models.package import Package
|
||||||
|
from aurweb.models.package_base import PackageBase
|
||||||
|
return db.query(Package).join(PackageBase).filter(
|
||||||
|
or_(
|
||||||
|
PackageBase.PackagerUID == self.ID,
|
||||||
|
PackageBase.MaintainerUID == self.ID
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<User(ID='%s', AccountType='%s', Username='%s')>" % (
|
return "<User(ID='%s', AccountType='%s', Username='%s')>" % (
|
||||||
self.ID, str(self.AccountType), self.Username)
|
self.ID, str(self.AccountType), self.Username)
|
||||||
|
|
|
@ -12,6 +12,10 @@ import aurweb.config
|
||||||
from aurweb.db import commit, create, query
|
from aurweb.db import commit, create, query
|
||||||
from aurweb.models.account_type import AccountType
|
from aurweb.models.account_type import AccountType
|
||||||
from aurweb.models.ban import Ban
|
from aurweb.models.ban import Ban
|
||||||
|
from aurweb.models.package import Package
|
||||||
|
from aurweb.models.package_base import PackageBase
|
||||||
|
from aurweb.models.package_notification import PackageNotification
|
||||||
|
from aurweb.models.package_vote import PackageVote
|
||||||
from aurweb.models.session import Session
|
from aurweb.models.session import Session
|
||||||
from aurweb.models.ssh_pub_key import SSHPubKey
|
from aurweb.models.ssh_pub_key import SSHPubKey
|
||||||
from aurweb.models.user import User
|
from aurweb.models.user import User
|
||||||
|
@ -25,7 +29,16 @@ account_type = user = None
|
||||||
def setup():
|
def setup():
|
||||||
global account_type, user
|
global account_type, user
|
||||||
|
|
||||||
setup_test_db("Users", "Sessions", "Bans", "SSHPubKeys")
|
setup_test_db(
|
||||||
|
User.__tablename__,
|
||||||
|
Session.__tablename__,
|
||||||
|
Ban.__tablename__,
|
||||||
|
SSHPubKey.__tablename__,
|
||||||
|
Package.__tablename__,
|
||||||
|
PackageBase.__tablename__,
|
||||||
|
PackageVote.__tablename__,
|
||||||
|
PackageNotification.__tablename__
|
||||||
|
)
|
||||||
|
|
||||||
account_type = query(AccountType,
|
account_type = query(AccountType,
|
||||||
AccountType.AccountType == "User").first()
|
AccountType.AccountType == "User").first()
|
||||||
|
@ -249,3 +262,24 @@ def test_user_is_developer():
|
||||||
user.AccountType = dev_type
|
user.AccountType = dev_type
|
||||||
commit()
|
commit()
|
||||||
assert user.is_developer() is True
|
assert user.is_developer() is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_voted_for():
|
||||||
|
now = int(datetime.utcnow().timestamp())
|
||||||
|
pkgbase = create(PackageBase, Name="pkg1", Maintainer=user)
|
||||||
|
pkg = create(Package, PackageBase=pkgbase, Name=pkgbase.Name)
|
||||||
|
create(PackageVote, PackageBase=pkgbase, User=user, VoteTS=now)
|
||||||
|
assert user.voted_for(pkg)
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_notified():
|
||||||
|
pkgbase = create(PackageBase, Name="pkg1", Maintainer=user)
|
||||||
|
pkg = create(Package, PackageBase=pkgbase, Name=pkgbase.Name)
|
||||||
|
create(PackageNotification, PackageBase=pkgbase, User=user)
|
||||||
|
assert user.notified(pkg)
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_packages():
|
||||||
|
pkgbase = create(PackageBase, Name="pkg1", Maintainer=user)
|
||||||
|
pkg = create(Package, PackageBase=pkgbase, Name=pkgbase.Name)
|
||||||
|
assert pkg in user.packages()
|
||||||
|
|
Loading…
Add table
Reference in a new issue