From e0549be5fd14e91a3c5ee4dc90f18a28c6d49f0b Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 7 Aug 2018 16:30:54 -0600 Subject: [PATCH] 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. --- tildes/tildes/lib/datetime.py | 6 +++++- tildes/tildes/models/topic/topic_query.py | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tildes/tildes/lib/datetime.py b/tildes/tildes/lib/datetime.py index 5c966d3..3752c91 100644 --- a/tildes/tildes/lib/datetime.py +++ b/tildes/tildes/lib/datetime.py @@ -18,7 +18,11 @@ class SimpleHoursPeriod: raise ValueError('Period must be at least 1 hour.') self.hours = hours - self.timedelta = timedelta(hours=hours) + + try: + self.timedelta = timedelta(hours=hours) + except OverflowError: + raise ValueError('Time period is too large') @classmethod def from_short_form(cls, short_form: str) -> 'SimpleHoursPeriod': diff --git a/tildes/tildes/models/topic/topic_query.py b/tildes/tildes/models/topic/topic_query.py index 9816d40..150c0b1 100644 --- a/tildes/tildes/models/topic/topic_query.py +++ b/tildes/tildes/models/topic/topic_query.py @@ -121,7 +121,15 @@ class TopicQuery(PaginatedQuery): def inside_time_period(self, period: SimpleHoursPeriod) -> 'TopicQuery': """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': """Restrict the topics to ones with a specific tag (generative)."""