Browse Source

Prevent OverflowErrors from long time periods

A couple of different locations in the code were causing crashes if the
user specified ridiculously long time periods (via topic listings).

This should prevent it, or at least give a better error when it happens.
merge-requests/25/head
Deimos 6 years ago
parent
commit
e0549be5fd
  1. 6
      tildes/tildes/lib/datetime.py
  2. 10
      tildes/tildes/models/topic/topic_query.py

6
tildes/tildes/lib/datetime.py

@ -18,7 +18,11 @@ class SimpleHoursPeriod:
raise ValueError('Period must be at least 1 hour.') raise ValueError('Period must be at least 1 hour.')
self.hours = hours self.hours = hours
self.timedelta = timedelta(hours=hours)
try:
self.timedelta = timedelta(hours=hours)
except OverflowError:
raise ValueError('Time period is too large')
@classmethod @classmethod
def from_short_form(cls, short_form: str) -> 'SimpleHoursPeriod': def from_short_form(cls, short_form: str) -> 'SimpleHoursPeriod':

10
tildes/tildes/models/topic/topic_query.py

@ -121,7 +121,15 @@ class TopicQuery(PaginatedQuery):
def inside_time_period(self, period: SimpleHoursPeriod) -> 'TopicQuery': def inside_time_period(self, period: SimpleHoursPeriod) -> 'TopicQuery':
"""Restrict the topics to inside a time period (generative).""" """Restrict the topics to inside a time period (generative)."""
return self.filter(Topic.created_time > utc_now() - period.timedelta)
# if the time period is too long, this will crash by creating a
# datetime outside the valid range - catch that and just don't filter
# by time period at all if the range is that large
try:
start_time = utc_now() - period.timedelta
except OverflowError:
return self
return self.filter(Topic.created_time > start_time)
def has_tag(self, tag: Ltree) -> 'TopicQuery': def has_tag(self, tag: Ltree) -> 'TopicQuery':
"""Restrict the topics to ones with a specific tag (generative).""" """Restrict the topics to ones with a specific tag (generative)."""

Loading…
Cancel
Save