Пример #1
0
def image(request, image_id):
    # first get the image object
    image = get_object_or_404(Image, pk=image_id)
    
    # handle new comment
    form = CommentForm(request.POST or None, foreign_key=image)
    if form.is_valid():
        comment_object = form.save()
        new_comment = comment_object.text
        
        if request.is_ajax():
            data = json.dumps({'new_comment': escape(new_comment)})
            return HttpResponse(data, mimetype='application/json');
        else:
            return redirect('image', image_id=image_id)
    
    # get image comments
    comments = image.comments.all()
    
    # render image page with comments and form
    return render(request, 'gallery/image.html', {
        'image': image,
        'comments': comments,
        'form': form,
    })
Пример #2
0
def view(request, username, submission_id, template="gallery/view.html"):
    profile = get_object_or_404(Profile, username__iexact=username)
    submission = get_object_or_404(Submission, 
                                   id=submission_id,
                                   profile=profile,
                                   deleted=False,
                                   hidden=False)
    
    comments = Submission_Comment.objects.filter(submission=submission,
                                                 deleted=False)
    
    
    
    
    form = None
    favourite = None
    if request.user.is_authenticated():
        form = CommentForm()
        favourite = Favourite.objects.filter(submission=submission, profile=request.user)[:1]
        if favourite:
            favourite = favourite[0]
    
    if request.POST:
        if form:
            #TODO: check if user can post (privacy/blacklist/etc)
            #TODO: replying to a comment
            form = CommentForm(request.POST)
            if form.is_valid():
                comment = form.save(commit=False)
                comment.profile = request.user
                comment.submission = submission
                #TODO
                #comment.comment = parent_comment
                comment.save()
                #IDEA: redirect with a link to the comment?
                return redirect('gallery:view',
                                username=profile.username,
                                submission_id=submission.id)
    
    return render(request, template ,{'profile':profile,
                                      'submission':submission,
                                      'favourite':favourite,
                                      'form':form,
                                      'comments':comments,
                                      'MIMES_AUDIO':MIMES_AUDIO,
                                      'MIMES_IMAGE':MIMES_IMAGE,
                                      'MIMES_FLASH':MIMES_FLASH})
Пример #3
0
def edit_comment(request, id_comment):
    if request.method == 'POST':
        if request.is_ajax():
            if request.user.is_authenticated():
                form = CommentForm(data=request.POST)
                if form.is_valid():
                    comment = get_object_or_404(CommentForImage, id=id_comment)
                    if comment.author.username == request.user.username:
                        temp_comment = get_normal_comment(request.POST['comment'])
                        if temp_comment:
                            comment.edits = comment.edits + timezone.localtime(comment.updated_at).ctime() + '\n' + comment.text + '\n\n\n\n'
                            comment.updated_at = timezone.now()
                            comment.text = request.POST['comment']
                            comment.save()
                            response_comment = get_comment_item(comment, request.user)
                            return JsonResponse({0: response_comment})
                        else:
                            HttpResponse('take it easy !')
    return HttpResponse(request.META['HTTP_REFERER'])
Пример #4
0
def send_comment(request):
    if request.method == 'POST':
        if request.is_ajax():
            if request.user.is_authenticated():
                form = CommentForm(data=request.POST)
                if form.is_valid():
                    comment = form.save(commit=False)
                    image = get_image(request.POST['href'])
                    comment.imageObject = image
                    comment.author = User.objects.get(username=request.user.get_username())
                    tempComment = get_normal_comment(request.POST['comment'])
                    if tempComment:
                        comment.text = request.POST['comment']
                        comment.save()
                        responseComment = get_comment_item(comment, request.user)  # just text
                        return JsonResponse({'comment': responseComment})
                    else:
                        return HttpResponse('take it easy !')
    return HttpResponse(request.META['HTTP_REFERER'])