From bb63044c86f47d0f98e60ad75e6d1230c6fc2bcf Mon Sep 17 00:00:00 2001 From: jordan Date: Mon, 27 Dec 2010 17:42:19 +0000 Subject: [PATCH] Adding ability to convert comments to answers. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@631 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/actions/node.py | 16 +++++++++++++ forum/models/user.py | 4 ++++ forum/settings/minrep.py | 4 ++++ forum/skins/default/media/js/osqa.main.js | 6 +++-- .../default/templates/node/comments.html | 13 ++++++++-- .../templates/paginator/sort_tabs.html | 2 +- .../templates/question_list/sort_tabs.html | 2 +- forum/templatetags/node_tags.py | 3 ++- forum/urls.py | 2 ++ forum/views/commands.py | 24 ++++++++++++++++++- 10 files changed, 68 insertions(+), 8 deletions(-) diff --git a/forum/actions/node.py b/forum/actions/node.py index 05d89ec..cd39924 100644 --- a/forum/actions/node.py +++ b/forum/actions/node.py @@ -179,6 +179,22 @@ class AnswerToCommentAction(ActionProxy): 'question': self.describe_node(viewer, self.node.abs_parent), } +class CommentToAnswerAction(ActionProxy): + verb = _("converted") + + def process_data(self, question): + self.node.parent = question + self.node.node_type = "answer" + self.node.last_edited = self + self.node.update_last_activity(self.user, save=True) + + + def describe(self, viewer=None): + return _("%(user)s converted comment on %(question)s into an answer") % { + 'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)), + 'question': self.describe_node(viewer, self.node.abs_parent), + } + class AnswerToQuestionAction(ActionProxy): verb = _("converted to question") diff --git a/forum/models/user.py b/forum/models/user.py index 8a80f03..e3c97fa 100644 --- a/forum/models/user.py +++ b/forum/models/user.py @@ -276,6 +276,10 @@ class User(BaseModel, DjangoUser): def can_delete_comment(self, comment): return self == comment.author or self.reputation >= int(settings.REP_TO_DELETE_COMMENTS) + @true_if_is_super_or_staff + def can_convert_comment_to_answer(self, comment): + return self == comment.author or self.reputation >= int(settings.REP_TO_COMMENTS_TO_ANSWERS) + def can_convert_to_comment(self, answer): return (not answer.marked) and (self.is_superuser or self.is_staff or answer.author == self or self.reputation >= int (settings.REP_TO_CONVERT_TO_COMMENT)) diff --git a/forum/settings/minrep.py b/forum/settings/minrep.py index 8588095..b2ff60d 100644 --- a/forum/settings/minrep.py +++ b/forum/settings/minrep.py @@ -67,6 +67,10 @@ REP_TO_CONVERT_TO_COMMENT = Setting('REP_TO_CONVERT_TO_COMMENT', 2000, MIN_REP_S label = _("Minimum reputation to convert answers to comment"), help_text = _("The minimum reputation an user must have to be allowed to convert an answer into a comment."))) +REP_TO_CONVERT_COMMENTS_TO_ANSWERS = Setting('REP_TO_CONVERT_COMMENTS_TO_ANSWERS', 2000, MIN_REP_SET, dict( +label = _("Minimum reputation to convert comments to answers"), +help_text = _("The minimum reputation an user must have to be allowed to convert comments into an answer."))) + REP_TO_CONVERT_TO_QUESTION = Setting('REP_TO_CONVERT_TO_QUESTION', 2000, MIN_REP_SET, dict( label = _("Minimum reputation to convert answers to questions"), help_text = _("The minimum reputation an user must have to be allowed to convert an answer into a question."))) diff --git a/forum/skins/default/media/js/osqa.main.js b/forum/skins/default/media/js/osqa.main.js index f97f1cd..7571a4b 100644 --- a/forum/skins/default/media/js/osqa.main.js +++ b/forum/skins/default/media/js/osqa.main.js @@ -71,7 +71,7 @@ var response_commands = { }); }, - insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url) { + insert_comment: function(post_id, comment_id, comment, username, profile_url, delete_url, edit_url, convert_url) { var $container = $('#comments-container-' + post_id); var skeleton = $('#new-comment-skeleton-' + post_id).html().toString(); @@ -80,7 +80,8 @@ var response_commands = { .replace(new RegExp('%USERNAME%', 'g'), username) .replace(new RegExp('%PROFILE_URL%', 'g'), profile_url) .replace(new RegExp('%DELETE_URL%', 'g'), delete_url) - .replace(new RegExp('%EDIT_URL%', 'g'), edit_url); + .replace(new RegExp('%EDIT_URL%', 'g'), edit_url) + .replace(new RegExp('%CONVERT_URL%', 'g'), convert_url); $container.append(skeleton); @@ -264,6 +265,7 @@ function process_ajax_response(data, evt, callback) { for (var command in data.commands) { response_commands[command].apply(null, data.commands[command]) + } if (data['message'] != undefined) { diff --git a/forum/skins/default/templates/node/comments.html b/forum/skins/default/templates/node/comments.html index 702d3ad..5fea00c 100644 --- a/forum/skins/default/templates/node/comments.html +++ b/forum/skins/default/templates/node/comments.html @@ -21,6 +21,12 @@ {% endif %} + {% if comment.can_convert %} + {% trans "Convert comment to answer" %} + {% endif %} + + ({% diff_date comment.added_at %}) {{comment.user.decorated_name}} {% if show_gravatar %}{% gravatar comment.user 18 %}{% endif %} @@ -58,11 +64,14 @@
%COMMENT%
-
+
+ {% trans "Convert comment to answer" %} + ({% trans "just now" %}) %USERNAME% {% if user.is_authenticated %} @@ -74,4 +83,4 @@ {% endif %}
- \ No newline at end of file + diff --git a/forum/skins/default/templates/paginator/sort_tabs.html b/forum/skins/default/templates/paginator/sort_tabs.html index bd4cb3c..a0662a9 100644 --- a/forum/skins/default/templates/paginator/sort_tabs.html +++ b/forum/skins/default/templates/paginator/sort_tabs.html @@ -10,4 +10,4 @@ {% endcomment %}
-{% endspaceless %} \ No newline at end of file +{% endspaceless %} diff --git a/forum/skins/default/templates/question_list/sort_tabs.html b/forum/skins/default/templates/question_list/sort_tabs.html index b170549..eff0408 100644 --- a/forum/skins/default/templates/question_list/sort_tabs.html +++ b/forum/skins/default/templates/question_list/sort_tabs.html @@ -5,4 +5,4 @@ {% trans "newest" %} {% trans "hottest" %} {% trans "most voted" %} -
\ No newline at end of file + diff --git a/forum/templatetags/node_tags.py b/forum/templatetags/node_tags.py index bc1fbf4..720f822 100644 --- a/forum/templatetags/node_tags.py +++ b/forum/templatetags/node_tags.py @@ -173,7 +173,8 @@ def comments(post, user): context = { 'can_delete': user.can_delete_comment(c), 'can_like': user.can_like_comment(c), - 'can_edit': user.can_edit_comment(c) + 'can_edit': user.can_edit_comment(c), + 'can_convert': user.can_convert_comment_to_answer(c) } if c in top_scorers or c.is_reply_to(user): diff --git a/forum/urls.py b/forum/urls.py index 718e390..5c5f622 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -81,6 +81,8 @@ urlpatterns += patterns('', url(r'^%s(?P\d+)/' % _('comment/'), app.commands.comment, name='comment'), url(r'^%s(?P\d+)/$' % _('delete_comment/'), app.commands.delete_comment, name="delete_comment"), + url(r'^%s(?P\d+)/$' % _('convert_comment/'), app.commands.convert_comment_to_answer, + name="convert_comment"), url(r'^%s(?P\d+)/$' % _('accept_answer/'), app.commands.accept_answer, name="accept_answer") , url(r'^%s(?P\d+)/$' % _('mark_favorite/'), app.commands.mark_favorite, name="mark_favorite") diff --git a/forum/views/commands.py b/forum/views/commands.py index 4fba05f..fcdd056 100644 --- a/forum/views/commands.py +++ b/forum/views/commands.py @@ -264,7 +264,8 @@ def comment(request, id): 'insert_comment': [ id, comment.id, comment.comment, user.decorated_name, user.get_profile_url(), reverse('delete_comment', kwargs={'id': comment.id}), - reverse('node_markdown', kwargs={'id': comment.id}) + reverse('node_markdown', kwargs={'id': comment.id}), + reverse('convert_comment', kwargs={'id': comment.id}), ] } } @@ -434,6 +435,27 @@ def convert_to_comment(request, id): return RefreshPageCommand() +@decorate.withfn(command) +def convert_comment_to_answer(request, id): + user = request.user + comment = get_object_or_404(Comment, id=id) + parent = comment.parent + + if not parent.question: + question = parent + else: + question = parent.question + + if not user.is_authenticated(): + raise AnonymousNotAllowedException(_("convert comments to answers")) + + if not user.can_convert_comment_to_answer(comment): + raise NotEnoughRepPointsException(_("convert comments to answers")) + + CommentToAnswerAction(user=user, node=comment, ip=request.META['REMOTE_ADDR']).save(data=dict(question=question)) + + return RefreshPageCommand() + @decorate.withfn(command) def convert_to_question(request, id): user = request.user -- 2.39.5