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,10 +521,23 @@ class SubredditContent(Content):
else:
period = None
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 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)

View File

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

View File

@@ -97,6 +97,9 @@ class Page(object):
@PageController.register(Command('SORT_HOT'))
def sort_content_hot(self):
if self.content.query:
self.refresh_content(order='relevance')
else:
self.refresh_content(order='hot')
@PageController.register(Command('SORT_TOP'))
@@ -122,6 +125,26 @@ class Page(object):
@PageController.register(Command('SORT_RISING'))
def sort_content_rising(self):
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'))
@@ -131,6 +154,10 @@ class Page(object):
@PageController.register(Command('SORT_CONTROVERSIAL'))
def sort_content_controversial(self):
if self.content.query:
self.term.flash()
return
choices = {
'\n': 'controversial',
'1': 'controversial-hour',
@@ -458,7 +485,9 @@ class Page(object):
ch, attr = str(' '), curses.A_BOLD | Color.YELLOW
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)
spacing = max(1, int(distance)) * ' '
text = spacing.join(items)