diff --git a/rtv/docs.py b/rtv/docs.py index 0796575..a14991a 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -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. # diff --git a/rtv/page.py b/rtv/page.py index d55b511..f0b0596 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -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." diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 557974e..03ee2a3 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -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()