Verified Commit c149afb1 authored by Kevin Morris's avatar Kevin Morris
Browse files

Merge remote-tracking branch 'fosskers/colin/prework-reformatting'

parents d7cb04b9 3aa8d523
Pipeline #16534 passed with stages
in 2 minutes and 54 seconds
...@@ -12,7 +12,7 @@ from aurweb.models.package_vote import PackageVote ...@@ -12,7 +12,7 @@ from aurweb.models.package_vote import PackageVote
class PackageSearch: class PackageSearch:
""" A Package search query builder. """ """A Package search query builder."""
# A constant mapping of short to full name sort orderings. # A constant mapping of short to full name sort orderings.
FULL_SORT_ORDER = {"d": "desc", "a": "asc"} FULL_SORT_ORDER = {"d": "desc", "a": "asc"}
...@@ -24,14 +24,18 @@ class PackageSearch: ...@@ -24,14 +24,18 @@ class PackageSearch:
if self.user: if self.user:
self.query = self.query.join( self.query = self.query.join(
PackageVote, PackageVote,
and_(PackageVote.PackageBaseID == PackageBase.ID, and_(
PackageVote.UsersID == self.user.ID), PackageVote.PackageBaseID == PackageBase.ID,
isouter=True PackageVote.UsersID == self.user.ID,
),
isouter=True,
).join( ).join(
PackageNotification, PackageNotification,
and_(PackageNotification.PackageBaseID == PackageBase.ID, and_(
PackageNotification.UserID == self.user.ID), PackageNotification.PackageBaseID == PackageBase.ID,
isouter=True PackageNotification.UserID == self.user.ID,
),
isouter=True,
) )
self.ordering = "d" self.ordering = "d"
...@@ -47,7 +51,7 @@ class PackageSearch: ...@@ -47,7 +51,7 @@ class PackageSearch:
"m": self._search_by_maintainer, "m": self._search_by_maintainer,
"c": self._search_by_comaintainer, "c": self._search_by_comaintainer,
"M": self._search_by_co_or_maintainer, "M": self._search_by_co_or_maintainer,
"s": self._search_by_submitter "s": self._search_by_submitter,
} }
# Setup SB (Sort By) callbacks. # Setup SB (Sort By) callbacks.
...@@ -58,7 +62,7 @@ class PackageSearch: ...@@ -58,7 +62,7 @@ class PackageSearch:
"w": self._sort_by_voted, "w": self._sort_by_voted,
"o": self._sort_by_notify, "o": self._sort_by_notify,
"m": self._sort_by_maintainer, "m": self._sort_by_maintainer,
"l": self._sort_by_last_modified "l": self._sort_by_last_modified,
} }
self._joined_user = False self._joined_user = False
...@@ -66,12 +70,10 @@ class PackageSearch: ...@@ -66,12 +70,10 @@ class PackageSearch:
self._joined_comaint = False self._joined_comaint = False
def _join_user(self, outer: bool = True) -> orm.Query: def _join_user(self, outer: bool = True) -> orm.Query:
""" Centralized joining of a package base's maintainer. """ """Centralized joining of a package base's maintainer."""
if not self._joined_user: if not self._joined_user:
self.query = self.query.join( self.query = self.query.join(
User, User, User.ID == PackageBase.MaintainerUID, isouter=outer
User.ID == PackageBase.MaintainerUID,
isouter=outer
) )
self._joined_user = True self._joined_user = True
return self.query return self.query
...@@ -87,7 +89,7 @@ class PackageSearch: ...@@ -87,7 +89,7 @@ class PackageSearch:
self.query = self.query.join( self.query = self.query.join(
PackageComaintainer, PackageComaintainer,
PackageComaintainer.PackageBaseID == PackageBase.ID, PackageComaintainer.PackageBaseID == PackageBase.ID,
isouter=isouter isouter=isouter,
) )
self._joined_comaint = True self._joined_comaint = True
return self.query return self.query
...@@ -95,8 +97,10 @@ class PackageSearch: ...@@ -95,8 +97,10 @@ class PackageSearch:
def _search_by_namedesc(self, keywords: str) -> orm.Query: def _search_by_namedesc(self, keywords: str) -> orm.Query:
self._join_user() self._join_user()
self.query = self.query.filter( self.query = self.query.filter(
or_(Package.Name.like(f"%{keywords}%"), or_(
Package.Description.like(f"%{keywords}%")) Package.Name.like(f"%{keywords}%"),
Package.Description.like(f"%{keywords}%"),
)
) )
return self return self
...@@ -132,8 +136,7 @@ class PackageSearch: ...@@ -132,8 +136,7 @@ class PackageSearch:
self._join_user() self._join_user()
if keywords: if keywords:
self.query = self.query.filter( self.query = self.query.filter(
and_(User.Username == keywords, and_(User.Username == keywords, User.ID == PackageBase.MaintainerUID)
User.ID == PackageBase.MaintainerUID)
) )
else: else:
self.query = self.query.filter(PackageBase.MaintainerUID.is_(None)) self.query = self.query.filter(PackageBase.MaintainerUID.is_(None))
...@@ -197,8 +200,7 @@ class PackageSearch: ...@@ -197,8 +200,7 @@ class PackageSearch:
# in terms of performance. We should improve this; there's no # in terms of performance. We should improve this; there's no
# reason it should take _longer_. # reason it should take _longer_.
column = getattr( column = getattr(
case([(models.PackageVote.UsersID == self.user.ID, 1)], else_=0), case([(models.PackageVote.UsersID == self.user.ID, 1)], else_=0), order
order
) )
name = getattr(models.Package.Name, order) name = getattr(models.Package.Name, order)
self.query = self.query.order_by(column(), name()) self.query = self.query.order_by(column(), name())
...@@ -209,9 +211,8 @@ class PackageSearch: ...@@ -209,9 +211,8 @@ class PackageSearch:
# in terms of performance. We should improve this; there's no # in terms of performance. We should improve this; there's no
# reason it should take _longer_. # reason it should take _longer_.
column = getattr( column = getattr(
case([(models.PackageNotification.UserID == self.user.ID, 1)], case([(models.PackageNotification.UserID == self.user.ID, 1)], else_=0),
else_=0), order,
order
) )
name = getattr(models.Package.Name, order) name = getattr(models.Package.Name, order)
self.query = self.query.order_by(column(), name()) self.query = self.query.order_by(column(), name())
...@@ -239,16 +240,16 @@ class PackageSearch: ...@@ -239,16 +240,16 @@ class PackageSearch:
return callback(ordering) return callback(ordering)
def count(self) -> int: def count(self) -> int:
""" Return internal query's count. """ """Return internal query's count."""
return self.query.count() return self.query.count()
def results(self) -> orm.Query: def results(self) -> orm.Query:
""" Return internal query. """ """Return internal query."""
return self.query return self.query
class RPCSearch(PackageSearch): class RPCSearch(PackageSearch):
""" A PackageSearch-derived RPC package search query builder. """A PackageSearch-derived RPC package search query builder.
With RPC search, we need a subset of PackageSearch's handlers, With RPC search, we need a subset of PackageSearch's handlers,
with a few additional handlers added. So, within the RPCSearch with a few additional handlers added. So, within the RPCSearch
...@@ -270,52 +271,60 @@ class RPCSearch(PackageSearch): ...@@ -270,52 +271,60 @@ class RPCSearch(PackageSearch):
# We keep: "nd", "n" and "m". We also overlay four new by params # We keep: "nd", "n" and "m". We also overlay four new by params
# on top: "depends", "makedepends", "optdepends" and "checkdepends". # on top: "depends", "makedepends", "optdepends" and "checkdepends".
self.search_by_cb = { self.search_by_cb = {
k: v for k, v in self.search_by_cb.items() k: v
for k, v in self.search_by_cb.items()
if k not in RPCSearch.keys_removed if k not in RPCSearch.keys_removed
} }
self.search_by_cb.update({ self.search_by_cb.update(
"depends": self._search_by_depends, {
"makedepends": self._search_by_makedepends, "depends": self._search_by_depends,
"optdepends": self._search_by_optdepends, "makedepends": self._search_by_makedepends,
"checkdepends": self._search_by_checkdepends "optdepends": self._search_by_optdepends,
}) "checkdepends": self._search_by_checkdepends,
}
)
# We always want an optional Maintainer in the RPC. # We always want an optional Maintainer in the RPC.
self._join_user() self._join_user()
def _join_depends(self, dep_type_id: int) -> orm.Query: def _join_depends(self, dep_type_id: int) -> orm.Query:
""" Join Package with PackageDependency and filter results """Join Package with PackageDependency and filter results
based on `dep_type_id`. based on `dep_type_id`.
:param dep_type_id: DependencyType ID :param dep_type_id: DependencyType ID
:returns: PackageDependency-joined orm.Query :returns: PackageDependency-joined orm.Query
""" """
self.query = self.query.join(models.PackageDependency).filter( self.query = self.query.join(models.PackageDependency).filter(
models.PackageDependency.DepTypeID == dep_type_id) models.PackageDependency.DepTypeID == dep_type_id
)
return self.query return self.query
def _search_by_depends(self, keywords: str) -> "RPCSearch": def _search_by_depends(self, keywords: str) -> "RPCSearch":
self.query = self._join_depends(DEPENDS_ID).filter( self.query = self._join_depends(DEPENDS_ID).filter(
models.PackageDependency.DepName == keywords) models.PackageDependency.DepName == keywords
)
return self return self
def _search_by_makedepends(self, keywords: str) -> "RPCSearch": def _search_by_makedepends(self, keywords: str) -> "RPCSearch":
self.query = self._join_depends(MAKEDEPENDS_ID).filter( self.query = self._join_depends(MAKEDEPENDS_ID).filter(
models.PackageDependency.DepName == keywords) models.PackageDependency.DepName == keywords
)
return self return self
def _search_by_optdepends(self, keywords: str) -> "RPCSearch": def _search_by_optdepends(self, keywords: str) -> "RPCSearch":
self.query = self._join_depends(OPTDEPENDS_ID).filter( self.query = self._join_depends(OPTDEPENDS_ID).filter(
models.PackageDependency.DepName == keywords) models.PackageDependency.DepName == keywords
)
return self return self
def _search_by_checkdepends(self, keywords: str) -> "RPCSearch": def _search_by_checkdepends(self, keywords: str) -> "RPCSearch":
self.query = self._join_depends(CHECKDEPENDS_ID).filter( self.query = self._join_depends(CHECKDEPENDS_ID).filter(
models.PackageDependency.DepName == keywords) models.PackageDependency.DepName == keywords
)
return self return self
def search_by(self, by: str, keywords: str) -> "RPCSearch": def search_by(self, by: str, keywords: str) -> "RPCSearch":
""" Override inherited search_by. In this override, we reduce the """Override inherited search_by. In this override, we reduce the
scope of what we handle within this function. We do not set `by` scope of what we handle within this function. We do not set `by`
to a default of "nd" in the RPC, as the RPC returns an error when to a default of "nd" in the RPC, as the RPC returns an error when
incorrect `by` fields are specified. incorrect `by` fields are specified.
...@@ -329,6 +338,4 @@ class RPCSearch(PackageSearch): ...@@ -329,6 +338,4 @@ class RPCSearch(PackageSearch):
return result return result
def results(self) -> orm.Query: def results(self) -> orm.Query:
return self.query.filter( return self.query.filter(models.PackageBase.PackagerUID.isnot(None))
models.PackageBase.PackagerUID.isnot(None)
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment