Browse Source

Remove handling of None in some query methods

The previous setup was a bit unusual and confusing - various functions
that restricted the query somehow were accepting None as an argument and
simply doing nothing if it was passed. This made calling them a little
simpler, but overall didn't make a lot of sense - if you don't want to
apply the restriction, you shouldn't call the function in the first
place.
merge-requests/19/head
Deimos 7 years ago
parent
commit
52db30cdad
  1. 10
      tildes/tildes/models/pagination.py
  2. 13
      tildes/tildes/models/topic/topic_query.py
  3. 20
      tildes/tildes/views/topic.py

10
tildes/tildes/models/pagination.py

@ -74,11 +74,8 @@ class PaginatedQuery(ModelQuery):
"""
return bool(self.before_id)
def after_id36(self, id36: Optional[str]) -> 'PaginatedQuery':
def after_id36(self, id36: str) -> 'PaginatedQuery':
"""Restrict the query to results after an id36 (generative)."""
if not id36:
return self
if self.before_id:
raise ValueError("Can't set both before and after restrictions")
@ -86,11 +83,8 @@ class PaginatedQuery(ModelQuery):
return self
def before_id36(self, id36: Optional[str]) -> 'PaginatedQuery':
def before_id36(self, id36: str) -> 'PaginatedQuery':
"""Restrict the query to results before an id36 (generative)."""
if not id36:
return self
if self.after_id:
raise ValueError("Can't set both before and after restrictions")

13
tildes/tildes/models/topic/topic_query.py

@ -1,6 +1,6 @@
"""Contains the TopicQuery class."""
from typing import Any, Optional, Sequence
from typing import Any, Sequence
from pyramid.request import Request
from sqlalchemy.sql.expression import and_, null
@ -119,21 +119,12 @@ class TopicQuery(PaginatedQuery):
return self.filter(
Topic.group_id.in_(subgroup_subquery)) # type: ignore
def inside_time_period(
self,
period: Optional[SimpleHoursPeriod],
) -> 'TopicQuery':
def inside_time_period(self, period: SimpleHoursPeriod) -> 'TopicQuery':
"""Restrict the topics to inside a time period (generative)."""
if not period:
return self
return self.filter(Topic.created_time > utc_now() - period.timedelta)
def has_tag(self, tag: Ltree) -> 'TopicQuery':
"""Restrict the topics to ones with a specific tag (generative)."""
if not tag:
return self
# casting tag to string really shouldn't be necessary, but some kind of
# strange interaction seems to be happening with the ArrayOfLtree
# class, this will need some investigation

20
tildes/tildes/views/topic.py

@ -122,17 +122,29 @@ def get_group_topics(
if period is missing:
period = default_settings.period
# set up the basic query for topics
query = (
request.query(Topic)
.join_all_relationships()
.inside_groups(groups)
.inside_time_period(period)
.has_tag(tag)
.apply_sort_option(order)
.after_id36(after)
.before_id36(before)
)
# restrict the time period, if not set to "all time"
if period:
query = query.inside_time_period(period)
# restrict to a specific tag, if we're viewing a single one
if tag:
query = query.has_tag(tag)
# apply before/after pagination restrictions if relevant
if before:
query = query.before_id36(before)
if after:
query = query.after_id36(after)
# apply topic tag filters unless they're disabled or viewing a single tag
if not (tag or unfiltered):
# pylint: disable=protected-access

Loading…
Cancel
Save