From c77cf89d1624927371afee3a8c139879fdc290c7 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Thu, 31 Aug 2017 01:15:44 -0400 Subject: [PATCH] Updating sorting options for search results --- rtv/content.py | 25 +++++++++++++++++++------ rtv/docs.py | 5 +++++ rtv/page.py | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index d9a6300..a9f44eb 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -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 diff --git a/rtv/docs.py b/rtv/docs.py index de52f62..9c5ea10 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -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 """ diff --git a/rtv/page.py b/rtv/page.py index 8e7ec62..237dfa8 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -97,7 +97,10 @@ class Page(object): @PageController.register(Command('SORT_HOT')) 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')) def sort_content_top(self): @@ -122,7 +125,27 @@ class Page(object): @PageController.register(Command('SORT_RISING')) 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')) def sort_content_new(self): @@ -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)