Updating sorting options for search results

This commit is contained in:
Michael Lazar
2017-08-31 01:15:44 -04:00
parent 061e8064f6
commit c77cf89d16
3 changed files with 56 additions and 9 deletions

View File

@@ -478,7 +478,7 @@ class SubredditContent(Content):
query (text): Content to search for on the given subreddit or
user's page.
"""
# TODO: refactor this into smaller methods
# TODO: This desperately needs to be refactored
# Strip leading, trailing, and redundant backslashes
parts = [seg for seg in name.strip(' /').split('/') if seg]
@@ -521,13 +521,26 @@ class SubredditContent(Content):
else:
period = None
if order not in ['hot', 'top', 'rising', 'new', 'controversial', None]:
raise InvalidSubreddit('Invalid order `%s`' % order)
if query:
# The allowed order for sorting search results
if order not in ['relevance', 'top', 'comments', 'new', None]:
raise InvalidSubreddit('Invalid order `%s`' % order)
else:
if order not in ['hot', 'top', 'rising', 'new', 'controversial', None]:
raise InvalidSubreddit('Invalid order `%s`' % order)
if period not in ['all', 'day', 'hour', 'month', 'week', 'year', None]:
raise InvalidSubreddit('Invalid period `%s`' % period)
if period and order not in ['top', 'controversial']:
raise InvalidSubreddit('`%s` order does not allow sorting by'
' period' % order)
if query:
# The allowed order for sorting search results
if period and order not in ['top', 'comments']:
raise InvalidSubreddit('`%s` order does not allow sorting by'
' period' % order)
else:
if period and order not in ['top', 'controversial']:
raise InvalidSubreddit('`%s` order does not allow sorting by'
' period' % order)
# On some objects, praw doesn't allow you to pass arguments for the
# order and period. Instead you need to call special helper functions