Exemplo n.º 1
0
def api_comment(request, action, comment_id):

    comment_id = int(comment_id)

    if action == 'new':
        # new comment
        if request.method == 'POST':
            message = request.POST['message']
            if not message:
                raise Http404
            message = message[:Comment.MAX_LENGTH]

            if len(message) is 0:
                raise Http404

            comment = Comment()
            comment.user = request.user
            comment.message = message
            comment.bysource = Comment.BY_API
            if comment_id is not 0:
                comment.parent = get_object_or_404(Comment,pk=comment_id)
            comment.save()
            
            response_dict = {
                'id' :  comment.id,
                'status': 'new',
                }

            return HttpResponse(simplejson.dumps(response_dict), 
                                mimetype='application/javascript')

    elif action == 'delete':
        comment = get_object_or_404(Comment,pk=comment_id,user=request.user)

        if len(Comment.objects.filter(parent=comment)) == 0: # root comment
            comment.delete()
            response_dict = {
                'status' :  'remove',
                }
        else:
            comment.message = "This comment has been deleted"
            comment.image0 = None
            comment.save()
            response_dict = {
                'status' :  'update',
                'message' : comment.message,
                }

        return HttpResponse(simplejson.dumps(response_dict), 
                            mimetype='application/javascript')

    raise Http404
Exemplo n.º 2
0
def comment(request, action, comment_id):

    if action == 'new' or action == 'edit':

        # new comment
        if request.method == 'POST':
            message = request.POST['message']
            if not message:
                raise Http404
            message = message[:Comment.MAX_LENGTH]

            if action == 'edit':
                if request.user.is_staff:
                    comment = get_object_or_404(Comment,pk=comment_id)
                else:
                    comment = get_object_or_404(Comment,pk=comment_id,user=request.user)
                comment.message = message
                comment.save()
            else: # new comment
                comment = Comment()
                comment.user = request.user
                comment.message = message
                comment.bysource = Comment.BY_WEB
                comment.parent = get_object_or_404(Comment,pk=comment_id)
                comment.save()

            t = loader.get_template('members/sub_comment.html')
            c = Context({
                    'user' : request.user,
                    'comment': comment,
                    })
            response_dict = {
                'url' : request.user.get_profile().user_image.url,
                'description' : request.user.get_profile().description,
                'fullname' : request.user.get_full_name(),
                'message' : comment.message,
                'id' :  comment.parent.id,
                'status': 'new',
                'render' : t.render(c),
                }
                
            return HttpResponse(simplejson.dumps(response_dict), 
                                mimetype='application/javascript')

    elif action == 'delete':
        comment = get_object_or_404(Comment,pk=comment_id,user=request.user)

        if len(Comment.objects.filter(parent=comment)) == 0: # root comment
            comment.delete()
            response_dict = {
                'status' :  'remove',
                }
        else:
            comment.message = "This comment has been deleted"
            comment.image0 = None
            comment.save()
            response_dict = {
                'status' :  'update',
                'message' : comment.message,
                }

        return HttpResponse(simplejson.dumps(response_dict), 
                                mimetype='application/javascript')

    elif action == 'deleteimg':
        comment = get_object_or_404(Comment,pk=comment_id,user=request.user)
        comment.image0 = None
        comment.save()
        response_dict = {
                'status' :  'OK',
        }
        return HttpResponse(simplejson.dumps(response_dict), 
                                mimetype='application/javascript')

    raise Http404