Beispiel #1
0
def ajax_comment(request,  content_type,  object_id, comment_class = 'standard', parent = None):
    """
    Handles ajax submission of comments.
    
    Attempts to render django_comments/standard_comment_list.html after
    the comment has been added.
    
    @param comment_class - used in template for layout and css purposes.
    @param parent - parent comment.
    @param content_type - content type of object which comment belongs to.
    @param object_id - id of parent object.
    """
    user = request.user
    
    CommentForm = forms.build_comment_form(comment_class)
    
    if user and request.POST:
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.cleaned_data.get('comment', '')
            target_model = ContentType.objects.get(id = content_type)
            target_object = target_model.model_class().objects.get(id = object_id)
            models.Comment.objects.add_comment(user, target_object, comment, parent)    
                 
    return shortcuts.render_to_response(
            'django_comments/standard_comment_list.html', 
            {'node':target_object, 
             'node_content_type':content_type,  
             'object_id':object_id,  
             'comments':models.Comment.objects.comments_for_object(target_object),  
             'comment_class':comment_class}, 
            context_instance = RequestContext(request), 
    )
Beispiel #2
0
 def render(self,  context):
     context = copy_context(context)
     content_type = resolve_variable(self.content_type, context, self.content_type)
     model_name = resolve_variable(self.model_name,  context, self.model_name)
     node_id = resolve_variable(self.node_id,  context, self.node_id)
     comment_class = resolve_variable(self.comment_class, context, self.comment_class)
     
     content_type_object = ContentType.objects.get(id = content_type)
     node = content_type_object.model_class().objects.get(id = node_id)
     all_comments = models.Comment.objects.comments_for_object(node, 'highest_rated')
     additional_count = all_comments.count() - self.count
     CommentForm = forms.build_comment_form(comment_class)
     comment_form = CommentForm()
     
     template = render_to_string('django_comments/standard_comments.html',   
                                     {'comment_target_node':node,
                                      'comments':all_comments[0:self.count],
                                      'comment_target_content_type':content_type_object.id,
                                      'comment_target_model':model_name,
                                      'comment_form':comment_form,
                                      'comment_class':comment_class,
                                      'additional_count':additional_count},  
                                      context
                                     )
     return template