示例#1
0
def _ajax_result(request, form, action, comment=None, object_id=None):
    # Based on django-ajaxcomments, BSD licensed.
    # Copyright (c) 2009 Brandon Konkle and individual contributors.
    #
    # This code was extracted out of django-ajaxcomments because
    # django-ajaxcomments is not threadsafe, and it was refactored afterwards.

    success = True
    json_errors = {}

    if form.errors:
        for field_name in form.errors:
            field = form[field_name]
            json_errors[field_name] = _render_errors(field)
        success = False

    json_return = {
        'success': success,
        'action': action,
        'errors': json_errors,
        'object_id': object_id,
        'use_threadedcomments': bool(appsettings.USE_THREADEDCOMMENTS),
    }

    if comment is not None:
        # Render the comment, like {% render_comment comment %} does
        context = get_comment_context_data(comment, action)
        context['request'] = request
        template_name = get_comment_template_name(comment)

        if django.VERSION >= (1, 8):
            comment_html = render_to_string(template_name,
                                            context,
                                            request=request)
        else:
            comment_html = render_to_string(template_name, context)

        json_return.update({
            'html': comment_html,
            'comment_id': comment.id,
            'parent_id': None,
            'is_moderated': not comment.
            is_public,  # is_public flags changes in comment_will_be_posted
        })
        if appsettings.USE_THREADEDCOMMENTS:
            json_return['parent_id'] = comment.parent_id

    json_response = json.dumps(json_return)
    return HttpResponse(json_response, content_type="application/json")
def _ajax_result(request, form, action, comment=None, object_id=None):
    # Based on django-ajaxcomments, BSD licensed.
    # Copyright (c) 2009 Brandon Konkle and individual contributors.
    #
    # This code was extracted out of django-ajaxcomments because
    # django-ajaxcomments is not threadsafe, and it was refactored afterwards.

    success = True
    json_errors = {}

    if form.errors:
        for field_name in form.errors:
            field = form[field_name]
            json_errors[field_name] = _render_errors(field)
        success = False

    json_return = {
        'success': success,
        'action': action,
        'errors': json_errors,
        'object_id': object_id,
        'use_threadedcomments': bool(appsettings.USE_THREADEDCOMMENTS),
    }

    if comment is not None:
        # Render the comment, like {% render_comment comment %} does
        context = get_comment_context_data(comment, action)
        context['request'] = request
        template_name = get_comment_template_name(comment)

        comment_html = render_to_string(template_name, context, request=request)
        json_return.update({
            'html': comment_html,
            'comment_id': comment.id,
            'parent_id': None,
            'is_moderated': not comment.is_public,   # is_public flags changes in comment_will_be_posted
        })
        if appsettings.USE_THREADEDCOMMENTS:
            json_return['parent_id'] = comment.parent_id

    json_response = json.dumps(json_return)
    return HttpResponse(json_response, content_type="application/json")
 def get_template_name(self, *tag_args, **tag_kwargs):
     return get_comment_template_name(comment=tag_args[0])
 def get_template_name(self, *tag_args, **tag_kwargs):
     return get_comment_template_name(comment=tag_args[0])