Merge branch 'edit_delete' of https://github.com/Brobin/rtv into Brobin-edit_delete

This commit is contained in:
Michael Lazar
2015-04-10 16:24:25 -07:00
3 changed files with 88 additions and 4 deletions

View File

@@ -57,7 +57,14 @@ COMMENT_FILE = """
{content}
"""
SUBMISSION_FILE = """
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 = """{content}
# 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']
@@ -251,6 +252,82 @@ class BasePage(object):
else:
show_notification(self.stdscr, ['Welcome {}'.format(username)])
@BaseController.register('d')
def delete(self):
"""
Delete a submission or comment.
"""
if not self.reddit.is_logged_in():
show_notification(self.stdscr, ['Login to delete'])
return
try:
data = self.content.get(self.nav.absolute_index)
if data['author'] != self.reddit.user.name:
return
except KeyError:
return
prompt = 'Are you sure you want to delete this? (y/n):'
char = self.prompt_input(prompt)
if char != 'y':
show_notification(self.stdscr, ['Delete cancelled'])
return
try:
data['object'].delete()
show_notification(self.stdscr, ['Deleted'])
except praw.errors.APIException as e:
show_notification(self.stdscr, [e.message])
else:
time.sleep(0.5)
self.refresh_content()
@BaseController.register('e')
def edit(self):
"""
Edit a submission or comment.
"""
if not self.reddit.is_logged_in():
show_notification(self.stdscr, ['Login to edit'])
return
try:
data = self.content.get(self.nav.absolute_index)
if data['author'] != self.reddit.user.name:
return
except KeyError:
return
if data['type'] == 'Submission':
subreddit = self.reddit.get_subreddit(self.content.name)
sub = str(subreddit).split('/')[2]
sub_file = 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."

View File

@@ -126,7 +126,7 @@ class SubredditPage(BasePage):
return
# Open the submission window
submission_info = SUBMISSION_FILE.format(name=sub)
submission_info = SUBMISSION_FILE.format(name=sub, content='')
curses.endwin()
submission_text = open_editor(submission_info)
curses.doupdate()