Skip to content

Commit

Permalink
server: post category filter
Browse files Browse the repository at this point in the history
  • Loading branch information
neobooru committed Jul 5, 2023
1 parent c2fdc2d commit 4806bbe
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/szurubooru/search/configs/post_search_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ def _pool_filter(
)(query, criterion, negated)


def _category_filter(
query: SaQuery, criterion: Optional[criteria.BaseCriterion], negated: bool
) -> SaQuery:
assert criterion

# Step 1. find the id for the category
q1 = db.session.query(model.TagCategory.tag_category_id).filter(
model.TagCategory.name == criterion.value
)

# Step 2. find the tags with that category
q2 = db.session.query(model.Tag.tag_id).filter(
model.Tag.category_id.in_(q1)
)

# Step 3. find all posts that have at least one of those tags
q3 = db.session.query(model.PostTag.post_id).filter(
model.PostTag.tag_id.in_(q2)
)

# Step 4. profit
expr = model.Post.post_id.in_(q3)
if negated:
expr = ~expr

return query.filter(expr)


class PostSearchConfig(BaseSearchConfig):
def __init__(self) -> None:
self.user = None # type: Optional[model.User]
Expand Down Expand Up @@ -349,6 +377,7 @@ def named_filters(self) -> Dict[str, Filter]:
),
),
(["pool"], _pool_filter),
(["category"], _category_filter),
]
)

Expand Down

0 comments on commit 4806bbe

Please sign in to comment.