From 69a2c07396f94ec0099c24089dd452b30cc06052 Mon Sep 17 00:00:00 2001 From: Jacob Mason Date: Wed, 4 Aug 2010 16:06:10 -0500 Subject: [PATCH] added delete_comment method --- sphinx/websupport/__init__.py | 19 +++++++++++++-- sphinx/websupport/errors.py | 7 +++++- tests/test_websupport.py | 46 ++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/sphinx/websupport/__init__.py b/sphinx/websupport/__init__.py index b5cea0212..03b9c8e8b 100644 --- a/sphinx/websupport/__init__.py +++ b/sphinx/websupport/__init__.py @@ -85,7 +85,7 @@ class WebSupport(object): """Build the documentation. Places the data into the `outdir` directory. Use it like this:: - support = WebSupport(srcdir, outdir, search) + support = WebSupport(srcdir, outdir, search='xapian') support.build() This will read reStructured text files from `srcdir`. Then it @@ -109,7 +109,7 @@ class WebSupport(object): """Load and return a document from a pickle. The document will be a dict object which can be used to render a template:: - support = WebSupport(outdir=outdir) + support = WebSupport(datadir=datadir) support.get_document('index') In most cases `docname` will be taken from the request path and @@ -193,6 +193,21 @@ class WebSupport(object): """ return self.storage.get_data(node_id, username, moderator) + def delete_comment(self, comment_id, username='', moderator=False): + """Delete a comment. Doesn't actually delete the comment, but + instead replaces the username and text files with "[deleted]" so + as not to leave any comments orphaned. + + If `moderator` is True, the comment will always be deleted. If + `moderator` is False, the comment will only be deleted if the + `username` matches the `username` on the comment. + + :param comment_id: the id of the comment to delete. + :param username: the username requesting the deletion. + :param moderator: whether the requestor is a moderator. + """ + self.storage.delete_comment(comment_id, username, moderator) + def add_comment(self, text, node_id='', parent_id='', displayed=True, username=None, rating=0, time=None, proposal=None, moderator=False): diff --git a/sphinx/websupport/errors.py b/sphinx/websupport/errors.py index b1c47915e..fbb75a93d 100644 --- a/sphinx/websupport/errors.py +++ b/sphinx/websupport/errors.py @@ -9,7 +9,8 @@ :license: BSD, see LICENSE for details. """ -__all__ = ['DocumentNotFoundError', 'SrcdirNotSpecifiedError'] +__all__ = ['DocumentNotFoundError', 'SrcdirNotSpecifiedError', + 'UserNotAuthorizedError'] class DocumentNotFoundError(Exception): pass @@ -17,3 +18,7 @@ class DocumentNotFoundError(Exception): class SrcdirNotSpecifiedError(Exception): pass + + +class UserNotAuthorizedError(Exception): + pass diff --git a/tests/test_websupport.py b/tests/test_websupport.py index bfa07226f..ca64ec2d1 100644 --- a/tests/test_websupport.py +++ b/tests/test_websupport.py @@ -79,11 +79,13 @@ def test_comments(support): # Create a displayed comment and a non displayed comment. comment = support.add_comment('First test comment', - node_id=str(first_node.id)) + node_id=str(first_node.id), + username='user_one') support.add_comment('Hidden comment', node_id=str(first_node.id), displayed=False) # Add a displayed and not displayed child to the displayed comment. - support.add_comment('Child test comment', parent_id=str(comment['id'])) + support.add_comment('Child test comment', parent_id=str(comment['id']), + username='user_one') support.add_comment('Hidden child test comment', parent_id=str(comment['id']), displayed=False) # Add a comment to another node to make sure it isn't returned later. @@ -144,8 +146,7 @@ def test_voting(support): @with_support() def test_proposals(support): session = Session() - nodes = session.query(Node).all() - node = nodes[0] + node = session.query(Node).first() data = support.get_data(str(node.id)) @@ -156,6 +157,43 @@ def test_proposals(support): node_id=str(node.id), proposal=proposal) + +@with_support() +def test_user_delete_comments(support): + def get_comment(): + session = Session() + node = session.query(Node).first() + session.close() + return support.get_data(str(node.id))['comments'][0] + + comment = get_comment() + assert comment['username'] == 'user_one' + # Make sure other normal users can't delete someone elses comments. + raises(UserNotAuthorizedError, support.delete_comment, + comment['id'], username='user_two') + # Now delete the comment using the correct username. + support.delete_comment(comment['id'], username='user_one') + comment = get_comment() + assert comment['username'] == '[deleted]' + assert comment['text'] == '[deleted]' + + +@with_support() +def test_moderator_delete_comments(support): + def get_comment(): + session = Session() + node = session.query(Node).first() + session.close() + return support.get_data(str(node.id), moderator=True)['comments'][1] + + comment = get_comment() + support.delete_comment(comment['id'], username='user_two', + moderator=True) + comment = get_comment() + assert comment['username'] == '[deleted]' + assert comment['text'] == '[deleted]' + + def test_differ(): differ = CombinedHtmlDiff() source = 'Lorem ipsum dolor sit amet,\nconsectetur adipisicing elit,\n' \