From 44a7a70655e9baca5545d7e5debb7965523a32a2 Mon Sep 17 00:00:00 2001 From: hernani Date: Sun, 13 Mar 2011 01:29:36 +0000 Subject: [PATCH] Improved "convert to question" routine, using the question edit form, and the same validation used to edit questions, git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@829 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/actions/node.py | 30 ++++++++++++------- .../templates/node/convert_to_question.html | 9 ++---- .../default/templates/question_edit.html | 4 +-- forum/templatetags/node_tags.py | 2 +- forum/urls.py | 2 +- forum/views/commands.py | 30 ------------------- forum/views/writers.py | 30 ++++++++++++++----- 7 files changed, 48 insertions(+), 59 deletions(-) diff --git a/forum/actions/node.py b/forum/actions/node.py index 530c649..263839a 100644 --- a/forum/actions/node.py +++ b/forum/actions/node.py @@ -196,21 +196,29 @@ class CommentToAnswerAction(ActionProxy): 'question': self.describe_node(viewer, self.node.abs_parent), } -class AnswerToQuestionAction(ActionProxy): +class AnswerToQuestionAction(NodeEditAction): verb = _("converted to question") - def process_data(self, title): + def process_data(self, **data): + revision_data = self.create_revision_data(**data) + revision = self.node.create_revision(self.user, **revision_data) + + original_question = self.node.question + + self.extra = { + 'covert_revision': revision.revision, + 'original_question': original_question + } + self.node.node_type = "question" - self.node.title = title - self.node.active_revision.title = title - self.node.active_revision.save() - self.node.last_edited = self - self.node.update_last_activity(self.user, save=True) + self.node.parent = None + self.node.abs_parent = None - try: - self.node.abs_parent.reset_answer_count_cache() - except AttributeError: - pass + original_question.reset_answer_count_cache() + + def process_action(self): + self.node.last_edited = self + self.node.save() def describe(self, viewer=None): diff --git a/forum/skins/default/templates/node/convert_to_question.html b/forum/skins/default/templates/node/convert_to_question.html index 84ea16b..3656c4b 100644 --- a/forum/skins/default/templates/node/convert_to_question.html +++ b/forum/skins/default/templates/node/convert_to_question.html @@ -1,7 +1,4 @@ +{% extends "question_edit.html" %} {% load i18n %} -
-

{% trans "Title of the new question:" %}

- - -
- +{% block title %}{% trans "Convert to question" %}{% endblock %} +{% block edittype %}{% trans "Convert to question" %}{% endblock %} diff --git a/forum/skins/default/templates/question_edit.html b/forum/skins/default/templates/question_edit.html index 9d7a6cd..7b41cac 100644 --- a/forum/skins/default/templates/question_edit.html +++ b/forum/skins/default/templates/question_edit.html @@ -88,11 +88,11 @@ {% block content %}
- {% trans "Edit question" %} [{% trans "back" %}] + {% block edittype %}{% trans "Edit question" %}{% endblock %} [{% trans "back" %}]
-
+
{% if revision_form.revision.errors %}{{ revision_form.revision.errors.as_ul }}{% endif %}
diff --git a/forum/templatetags/node_tags.py b/forum/templatetags/node_tags.py index 544d918..6680f59 100644 --- a/forum/templatetags/node_tags.py +++ b/forum/templatetags/node_tags.py @@ -151,7 +151,7 @@ def post_controls(post, user): if post.node_type == "answer" and user.can_convert_to_question(post): menu.append(post_control(_('convert to question'), reverse('convert_to_question', kwargs={'id': post.id}), - command=True, withprompt=True)) + command=False, confirm=True)) if user.is_superuser or user.is_staff: plain_text = strip_tags(post.html) diff --git a/forum/urls.py b/forum/urls.py index fc64e28..564eafd 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -97,7 +97,7 @@ urlpatterns += patterns('', url(r'^%s(?P\d+)/' % _('node_markdown/'), app.commands.node_markdown, name='node_markdown'), url(r'^%s(?P\d+)/' % _('convert/'), app.commands.convert_to_comment, name='convert_to_comment'), - url(r'^%s(?P\d+)/' % _('convert_to_question/'), app.commands.convert_to_question, + url(r'^%s(?P\d+)/' % _('convert_to_question/'), app.writers.convert_to_question, name='convert_to_question'), url(r'^%s(?P\d+)/' % _('wikify/'), app.commands.wikify, name='wikify'), diff --git a/forum/views/commands.py b/forum/views/commands.py index 11818ec..a447ffe 100644 --- a/forum/views/commands.py +++ b/forum/views/commands.py @@ -456,36 +456,6 @@ def convert_comment_to_answer(request, id): return RefreshPageCommand() -@decorate.withfn(command) -def convert_to_question(request, id): - user = request.user - answer = get_object_or_404(Answer, id=id) - question = answer.question - - if not request.POST: - description = lambda a: _("Answer by %(uname)s: %(snippet)s...") % {'uname': a.author.username, - 'snippet': a.summary[:10]} - nodes = [(question.id, _("Question"))] - [nodes.append((a.id, description(a))) for a in - question.answers.filter_state(deleted=False).exclude(id=answer.id)] - - return render_to_response('node/convert_to_question.html', {'answer': answer}) - - if not user.is_authenticated(): - raise AnonymousNotAllowedException(_("convert answers to questions")) - - if not user.can_convert_to_question(answer): - raise NotEnoughRepPointsException(_("convert answers to questions")) - - try: - title = request.POST.get('title', None) - except: - raise CommandException(_("You haven't specified the title of the new question")) - - AnswerToQuestionAction(user=user, node=answer, ip=request.META['REMOTE_ADDR']).save(data=dict(title=title)) - - return RefreshPageCommand() - @decorate.withfn(command) def subscribe(request, id, user=None): if user: diff --git a/forum/views/writers.py b/forum/views/writers.py index a5c5d70..5064757 100644 --- a/forum/views/writers.py +++ b/forum/views/writers.py @@ -12,11 +12,12 @@ from django.utils.translation import ugettext as _ from django.core.urlresolvers import reverse from django.core.exceptions import PermissionDenied -from forum.actions import AskAction, AnswerAction, ReviseAction, RollbackAction, RetagAction +from forum.actions import AskAction, AnswerAction, ReviseAction, RollbackAction, RetagAction, AnswerToQuestionAction from forum.forms import * from forum.models import * from forum.forms import get_next_url from forum.utils import html +from forum.http_responses import HttpResponseUnauthorized from vars import PENDING_SUBMISSION_SESSION_ATTR @@ -111,6 +112,16 @@ def ask(request): 'tab' : 'ask' }, context_instance=RequestContext(request)) +def convert_to_question(request, id): + user = request.user + answer = get_object_or_404(Answer, id=id) + + if not user.can_convert_to_question(answer): + return HttpResponseUnauthorized(request) + + return _edit_question(request, answer, template='node/convert_to_question.html', summary=_("Converted to question"), + action_class=AnswerToQuestionAction, allow_rollback=False, url_getter=lambda a: Question.objects.get(id=a.id).get_absolute_url()) + def edit_question(request, id): question = get_object_or_404(Question, id=id) if question.nis.deleted and not request.user.can_view_deleted_post(question): @@ -138,7 +149,7 @@ def _retag_question(request, question): #'tags' : _get_tags_cache_json(), }, context_instance=RequestContext(request)) -def _edit_question(request, question): +def _edit_question(request, question, template='question_edit.html', summary='', action_class=ReviseAction, allow_rollback=True, url_getter=lambda q: q.get_absolute_url()): if request.method == 'POST': revision_form = RevisionForm(question, data=request.POST) revision_form.is_valid() @@ -151,7 +162,7 @@ def _edit_question(request, question): if not 'select_revision' in request.POST and form.is_valid(): if form.has_changed(): - action = ReviseAction(user=request.user, node=question, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data) + action = action_class(user=request.user, node=question, ip=request.META['REMOTE_ADDR']).save(data=form.cleaned_data) if settings.WIKI_ON: if request.POST.get('wiki', False) and not question.nis.wiki: @@ -160,20 +171,23 @@ def _edit_question(request, question): question.nstate.wiki = None else: if not revision == question.active_revision: - RollbackAction(user=request.user, node=question).save(data=dict(activate=revision)) + if allow_rollback: + RollbackAction(user=request.user, node=question).save(data=dict(activate=revision)) + else: + pass - return HttpResponseRedirect(question.get_absolute_url()) + return HttpResponseRedirect(url_getter(question)) else: revision_form = RevisionForm(question) - form = EditQuestionForm(question, request.user) + form = EditQuestionForm(question, request.user, initial={'summary': summary}) - return render_to_response('question_edit.html', { + return render_to_response(template, { 'question': question, 'revision_form': revision_form, 'form' : form, - #'tags' : _get_tags_cache_json() }, context_instance=RequestContext(request)) + def edit_answer(request, id): answer = get_object_or_404(Answer, id=id) if answer.deleted and not request.user.can_view_deleted_post(answer): -- 2.39.5