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

View File

@@ -83,6 +83,11 @@ BANNER = """
[1]hot [2]top [3]rising [4]new [5]controversial [1]hot [2]top [3]rising [4]new [5]controversial
""" """
BANNER_SEARCH = """
[1]relevance [2]top [3]comments [4]new
"""
FOOTER_SUBREDDIT = """ FOOTER_SUBREDDIT = """
[?]Help [q]Quit [l]Comments [/]Prompt [u]Login [o]Open [c]Post [a/z]Vote [?]Help [q]Quit [l]Comments [/]Prompt [u]Login [o]Open [c]Post [a/z]Vote
""" """

View File

@@ -97,7 +97,10 @@ class Page(object):
@PageController.register(Command('SORT_HOT')) @PageController.register(Command('SORT_HOT'))
def sort_content_hot(self): def sort_content_hot(self):
self.refresh_content(order='hot') if self.content.query:
self.refresh_content(order='relevance')
else:
self.refresh_content(order='hot')
@PageController.register(Command('SORT_TOP')) @PageController.register(Command('SORT_TOP'))
def sort_content_top(self): def sort_content_top(self):
@@ -122,7 +125,27 @@ class Page(object):
@PageController.register(Command('SORT_RISING')) @PageController.register(Command('SORT_RISING'))
def sort_content_rising(self): def sort_content_rising(self):
self.refresh_content(order='rising')
if self.content.query:
choices = {
'\n': 'comments',
'1': 'comments-hour',
'2': 'comments-day',
'3': 'comments-week',
'4': 'comments-month',
'5': 'comments-year',
'6': 'comments-all'}
message = docs.TIME_ORDER_MENU.strip().splitlines()
ch = self.term.show_notification(message)
ch = six.unichr(ch)
if ch not in choices:
self.term.show_notification('Invalid option')
return
self.refresh_content(order=choices[ch])
else:
self.refresh_content(order='rising')
@PageController.register(Command('SORT_NEW')) @PageController.register(Command('SORT_NEW'))
def sort_content_new(self): def sort_content_new(self):
@@ -131,6 +154,10 @@ class Page(object):
@PageController.register(Command('SORT_CONTROVERSIAL')) @PageController.register(Command('SORT_CONTROVERSIAL'))
def sort_content_controversial(self): def sort_content_controversial(self):
if self.content.query:
self.term.flash()
return
choices = { choices = {
'\n': 'controversial', '\n': 'controversial',
'1': 'controversial-hour', '1': 'controversial-hour',
@@ -458,7 +485,9 @@ class Page(object):
ch, attr = str(' '), curses.A_BOLD | Color.YELLOW ch, attr = str(' '), curses.A_BOLD | Color.YELLOW
window.bkgd(ch, attr) window.bkgd(ch, attr)
items = docs.BANNER.strip().split(' ') banner = docs.BANNER_SEARCH if self.content.query else docs.BANNER
items = banner.strip().split(' ')
distance = (n_cols - sum(len(t) for t in items) - 1) / (len(items) - 1) distance = (n_cols - sum(len(t) for t in items) - 1) / (len(items) - 1)
spacing = max(1, int(distance)) * ' ' spacing = max(1, int(distance)) * ' '
text = spacing.join(items) text = spacing.join(items)