Merge remote-tracking branch 'feature/master'

This commit is contained in:
David Foucher
2016-01-21 00:30:37 +01:00
4 changed files with 61 additions and 4 deletions

View File

@@ -89,6 +89,7 @@ Once you are logged in your username will appear in the top-right corner of the
:``d``: Delete an existing post or comment
:``i``: Display new messages prompt
:``s``: View a list of subscribed subreddits
:``y``: View a list of your saved posts
--------------
Subreddit Mode
@@ -108,6 +109,7 @@ The ``/`` prompt accepts subreddits in the following formats
* ``/r/python+linux`` supports multireddits
* ``/r/front`` will redirect to the front page
* ``/r/me`` will display your submissions
* ``/r/saved`` will display your saved posts/comments
---------------
Submission Mode

View File

@@ -86,7 +86,11 @@ class Content(object):
data = {}
data['object'] = comment
# Saved comments do not have a nested_level
if hasattr(comment, 'nested_level'):
data['level'] = comment.nested_level
else:
data['level'] = None
if isinstance(comment, praw.objects.MoreComments):
data['type'] = 'MoreComments'
@@ -114,6 +118,7 @@ class Content(object):
data['gold'] = comment.gilded > 0
data['permalink'] = permalink
data['stickied'] = stickied
data['saved'] = comment.saved
return data
@@ -156,6 +161,7 @@ class Content(object):
data['stickied'] = sub.stickied
data['saved'] = sub.saved
data['index'] = None # This is filled in later by the method caller
data['saved'] = sub.saved
if sub.url.split('/r/')[-1] == sub.permalink.split('/r/')[-1]:
data['url'] = 'self.{0}'.format(data['subreddit'])
@@ -256,6 +262,12 @@ class SubmissionContent(Content):
submission = reddit.get_submission(url, comment_sort=order)
return cls(submission, loader, indent_size, max_indent_level, order)
def save(self):
"""
Saves a submission to the authenticated user
"""
self._submission.save()
def get(self, index, n_cols=70):
"""
Grab the `i`th submission, with the title field formatted to fit inside
@@ -407,6 +419,15 @@ class SubredditContent(Content):
'new': reddit.get_new,
'controversial': reddit.get_controversial,
}
elif name == 'saved':
dispatch = {
None: reddit.user.get_saved,
'hot': reddit.user.get_saved,
'top': reddit.user.get_saved,
'rising': reddit.user.get_saved,
'new': reddit.user.get_saved,
'controversial': reddit.user.get_saved,
}
else:
subreddit = reddit.get_subreddit(name)
dispatch = {
@@ -439,10 +460,15 @@ class SubredditContent(Content):
except StopIteration:
raise IndexError
else:
# TODO: In order to display saved comment, we need to
# coerce the comment into a submission
try:
data = self.strip_praw_submission(submission)
except:
continue
data['index'] = index
# Add the post number to the beginning of the title
data['title'] = '{0}. {1}'.format(index+1, data['title'])
data['title'] = '{0}. {1}'.format(index+1, data.get('title'))
self._submission_data.append(data)
# Modifies the original dict, faster than copying

View File

@@ -24,7 +24,6 @@ class SubmissionPage(Page):
self.content = SubmissionContent.from_url(reddit, url, term.loader)
else:
self.content = SubmissionContent(submission, term.loader)
self.controller = SubmissionController(self)
# Start at the submission post, which is indexed as -1
self.nav = Navigator(self.content.get, page_index=-1)
@@ -71,6 +70,14 @@ class SubmissionPage(Page):
else:
self.term.flash()
@SubmissionController.register('y')
@logged_in
def save_comment(self):
comment = self.content.get(self.nav.absolute_index).get('object')
if comment:
with self.term.loader('Saving comment'):
comment.save()
@SubmissionController.register('c')
@logged_in
def add_comment(self):
@@ -167,6 +174,10 @@ class SubmissionPage(Page):
text, attr = self.term.stickied
self.term.add_line(win, text, attr=attr)
if data['saved']:
text, attr = 'SAVED', (curses.A_BOLD | Color.BLUE)
self.term.add_line(win, text, attr=attr)
for row, text in enumerate(data['split_body'], start=offset+1):
if row in valid_rows:
self.term.add_line(win, text, row, 1)
@@ -238,4 +249,8 @@ class SubmissionPage(Page):
text, attr = 'NSFW', (curses.A_BOLD | Color.RED)
self.term.add_line(win, text, attr=attr)
if data['saved']:
text, attr = 'SAVED', (curses.A_BOLD | Color.RED)
self.term.add_line(win, text, attr=attr)
win.border()

View File

@@ -145,6 +145,16 @@ class SubredditPage(Page):
self.refresh_content()
@SubredditController.register('y')
@logged_in
def save_submition(self):
data = self.content.get(self.nav.absolute_index)
url = data['permalink']
with self.term.loader('Saving submission'):
page = SubmissionPage(
self.reddit, self.term, self.config, self.oauth, url=url)
page.content.save()
@SubredditController.register('s')
@logged_in
def open_subscriptions(self):
@@ -208,6 +218,10 @@ class SubredditPage(Page):
text, attr = 'NSFW', (curses.A_BOLD | Color.RED)
self.term.add_line(win, text, attr=attr)
if data['saved']:
text, attr = ' SAVED', (curses.A_BOLD | Color.BLUE)
self.term.add_line(win, text, attr=attr)
row = n_title + offset + 2
if row in valid_rows:
text = '{author}'.format(**data)