added editing submissions and comments

This commit is contained in:
Tobin
2015-04-06 12:01:44 -05:00
parent 6f2dd9f528
commit e321ac8182
2 changed files with 49 additions and 2 deletions

View File

@@ -57,6 +57,13 @@ COMMENT_FILE = """
{content}
"""
COMMENT_EDIT_FILE = """{content}
# Please enter a comment. Lines starting with '#' will be ignored,
# and an empty message aborts the comment.
#
# Editing your comment
"""
SUBMISSION_FILE = """
# Please enter your submission. Lines starting with '#' will be ignored,
# and an empty field aborts the submission.

View File

@@ -1,12 +1,13 @@
import curses
import time
import six
import sys
import praw.errors
from .helpers import clean
from .helpers import clean, open_editor
from .curses_helpers import Color, show_notification, show_help, text_input
from .docs import AGENT
from .docs import AGENT, COMMENT_EDIT_FILE, SUBMISSION_FILE
__all__ = ['Navigator']
@@ -276,6 +277,45 @@ class BasePage(object):
time.sleep(0.5)
self.refresh_content()
@BaseController.register('e')
def edit(self):
"""
Edit a submission or comment.
"""
data = self.content.get(self.nav.absolute_index)
if data['author'] != self.reddit.user.name:
show_notification(self.stdscr, ['You can\'t edit this'])
return
if data['type'] == 'Submission':
subreddit = self.reddit.get_subreddit(self.content.name)
sub = str(subreddit).split('/')[2]
sub_file = '{content}' + SUBMISSION_FILE
content = data['text']
info = sub_file.format(content=content, name=sub)
elif data['type'] == 'Comment':
content = data['body']
info = COMMENT_EDIT_FILE.format(content=content)
else:
curses.flash()
return
curses.endwin()
text = open_editor(info)
curses.doupdate()
if text == content:
return
try:
data['object'].edit(text)
except praw.errors.APIException as e:
show_notification(self.stdscr, [e.message])
else:
time.sleep(0.5)
self.refresh_content()
def logout(self):
"Prompt to log out of the user's account."