From 9a95684c7618e133e955c83e77480ea886ea0bc0 Mon Sep 17 00:00:00 2001 From: Tobin Date: Mon, 6 Apr 2015 11:28:52 -0500 Subject: [PATCH 1/5] added deleting comment or submission --- rtv/page.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rtv/page.py b/rtv/page.py index d55b511..3b846da 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -251,6 +251,29 @@ class BasePage(object): else: show_notification(self.stdscr, ['Welcome {}'.format(username)]) + @BaseController.register('d') + def delete(self): + """ + Delete a submission or comment. + """ + data = self.content.get(self.nav.absolute_index) + if data['author'] == self.reddit.user.name: + prompt = 'Are you sure you want to delete this? (y/n):' + char = self.prompt_input(prompt) + if char == 'y': + 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() + else: + show_notification(self.stdscr, ['Delete cancelled']) + else: + show_notification(self.stdscr, ['You can\'t delete this']) + def logout(self): "Prompt to log out of the user's account." From 6f2dd9f528b8e5b570506659982baeb52e515692 Mon Sep 17 00:00:00 2001 From: Tobin Date: Mon, 6 Apr 2015 11:39:57 -0500 Subject: [PATCH 2/5] edited style to match other functions --- rtv/page.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/rtv/page.py b/rtv/page.py index 3b846da..fa9a412 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -257,22 +257,24 @@ class BasePage(object): Delete a submission or comment. """ data = self.content.get(self.nav.absolute_index) - if data['author'] == self.reddit.user.name: - prompt = 'Are you sure you want to delete this? (y/n):' - char = self.prompt_input(prompt) - if char == 'y': - 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() - else: - show_notification(self.stdscr, ['Delete cancelled']) - else: + if data['author'] != self.reddit.user.name: show_notification(self.stdscr, ['You can\'t delete this']) + 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() def logout(self): "Prompt to log out of the user's account." From e321ac81823313b56d0b36aac1bb2d6b68bdd85f Mon Sep 17 00:00:00 2001 From: Tobin Date: Mon, 6 Apr 2015 12:01:44 -0500 Subject: [PATCH 3/5] added editing submissions and comments --- rtv/docs.py | 7 +++++++ rtv/page.py | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index 0796575..0cf48db 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -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. diff --git a/rtv/page.py b/rtv/page.py index fa9a412..8d8f775 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'] @@ -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." From f31ad72f921f84d31ab664c4e53366af7eef57fd Mon Sep 17 00:00:00 2001 From: Tobin Date: Tue, 7 Apr 2015 01:37:11 -0500 Subject: [PATCH 4/5] catch key error, omit extra notifications --- rtv/page.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/rtv/page.py b/rtv/page.py index 8d8f775..c129f64 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -257,9 +257,15 @@ class BasePage(object): """ Delete 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 delete this']) + 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):' @@ -282,9 +288,15 @@ class BasePage(object): """ 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']) + 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': From 68ae4570dc98d9854a1891e094fd609e1275950c Mon Sep 17 00:00:00 2001 From: Brobin Date: Tue, 7 Apr 2015 22:14:35 -0500 Subject: [PATCH 5/5] fixed submission file to have default content --- rtv/docs.py | 2 +- rtv/page.py | 2 +- rtv/subreddit.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rtv/docs.py b/rtv/docs.py index 0cf48db..a14991a 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -64,7 +64,7 @@ COMMENT_EDIT_FILE = """{content} # Editing your comment """ -SUBMISSION_FILE = """ +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 c129f64..f0b0596 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -302,7 +302,7 @@ class BasePage(object): if data['type'] == 'Submission': subreddit = self.reddit.get_subreddit(self.content.name) sub = str(subreddit).split('/')[2] - sub_file = '{content}' + SUBMISSION_FILE + sub_file = SUBMISSION_FILE content = data['text'] info = sub_file.format(content=content, name=sub) 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()