Esempio n. 1
0
def create(request, comment_id):
    comment = get_object_or_404(Comment.objects.exclude(user=request.user),
                                pk=comment_id)
    form = LikeForm(user=request.user,
                    comment=comment,
                    data=post_data(request))

    if is_post(request) and form.is_valid():
        like = form.save()
        like.comment.increase_likes_count()

        if is_ajax(request):
            return json_response({'url_delete': like.get_delete_url()})

        return safe_redirect(request,
                             'next',
                             comment.get_absolute_url(),
                             method='POST')

    return render(request=request,
                  template_name='spirit/comment/like/create.html',
                  context={
                      'form': form,
                      'comment': comment
                  })
Esempio n. 2
0
def create(request, topic_id):
    if not is_ajax(request):
        return Http404()

    topic = get_object_or_404(Topic, pk=topic_id)
    form = BookmarkForm(user=request.user, topic=topic, data=request.POST)

    if form.is_valid():
        form.save()
        return json_response()

    return Http404()  # TODO: return errors (in json format)
Esempio n. 3
0
def file_upload_ajax(request):
    if not is_ajax(request):
        return Http404()

    form = CommentFileForm(
        user=request.user, data=request.POST, files=request.FILES)

    if form.is_valid():
        file = form.save()
        return json_response({'url': file.url})

    return json_response({'error': dict(form.errors.items())})
Esempio n. 4
0
def index_ajax(request):
    if not is_ajax(request):
        return Http404()

    notifications = (TopicNotification.objects.for_access(
        request.user).order_by("is_read", "-date").with_related_data())
    notifications = notifications[:settings.ST_NOTIFICATIONS_PER_PAGE]
    notifications = [{
        'user': escape(n.comment.user.st.nickname),
        'action': n.action,
        'title': escape(n.topic.title),
        'url': n.get_absolute_url(),
        'is_read': n.is_read
    } for n in notifications]

    return HttpResponse(json.dumps({'n': notifications}),
                        content_type="application/json")
Esempio n. 5
0
def delete(request, pk):
    like = get_object_or_404(CommentLike, pk=pk, user=request.user)

    if is_post(request):
        like.delete()
        like.comment.decrease_likes_count()

        if is_ajax(request):
            url = reverse('spirit:comment:like:create',
                          kwargs={'comment_id': like.comment.pk})
            return json_response({
                'url_create': url,
            })

        return redirect(
            request.POST.get('next', like.comment.get_absolute_url()))

    return render(request=request,
                  template_name='spirit/comment/like/delete.html',
                  context={'like': like})