예제 #1
0
파일: writers.py 프로젝트: JvGinkel/openode
def retag_question(request, id):
    """retag question view
    """
    question = get_object_or_404(models.Post, pk=id)

    if not question.thread.can_retag(request.user):
        response_data = {
            'message': _(u"PermissionDenied: you can not retag this object."),
            'success': False
        }
        data = simplejson.dumps(response_data)
        return HttpResponse(data, mimetype="application/json")

    try:
        request.user.assert_can_retag_question(question)
        if request.method == 'POST':
            form = forms.RetagQuestionForm(question, request.POST)

            if form.is_valid():
                if form.has_changed():
                    request.user.log(question, const.LOG_ACTION_UPDATE_TAGS)
                    request.user.retag_question(question=question, tags=form.cleaned_data['tags'])

                if request.is_ajax():
                    response_data = {
                        'success': True,
                        # 'new_tags': question.thread.tagnames,
                        'new_tags': render_into_skin_as_string('node/snippets/get_tag_list.html', {"thread": question.thread}, request)
                    }
                    # print response_data

                    if request.user.message_set.count() > 0:
                        #todo: here we will possibly junk messages
                        message = request.user.get_and_delete_messages()[-1]
                        response_data['message'] = message

                    data = simplejson.dumps(response_data)
                    return HttpResponse(data, mimetype="application/json")
                else:
                    return HttpResponseRedirect(question.get_absolute_url())
            elif request.is_ajax():
                response_data = {
                    'message': format_errors(form.errors['tags']),
                    'success': False
                }
                data = simplejson.dumps(response_data)
                return HttpResponse(data, mimetype="application/json")
        else:
            form = forms.RetagQuestionForm(question)

        data = {
            'active_tab': 'questions',
            'question': question,
            'form': form,
        }
        return render_into_skin('thread_retag.html', data, request)
    except exceptions.PermissionDenied, e:
        if request.is_ajax():
            response_data = {
                'message': unicode(e),
                'success': False
            }
            data = simplejson.dumps(response_data)
            return HttpResponse(data, mimetype="application/json")
        else:
            request.user.message_set.create(message=unicode(e))
            return HttpResponseRedirect(question.get_absolute_url())