예제 #1
0
def like(request):
    post_id = request.POST.get('post_id', False)
    customer_id = request.POST.get('customer_id', False)
    post = Post.objects.get(id=post_id)
    customer = Customer.objects.get(id=customer_id)
    like = Like.objects.filter(post=post, customer=customer_id)
    if like.exists():
        like.delete()
        nlikes = len(Like.objects.filter(post_id=post_id))
        data = nlikes
        notifi2 = Notification.objects.filter(customer_id=customer_id,
                                              post_id=post_id,
                                              status=2)
        notifi2.delete()

    else:
        like2 = Like()
        like2.customer = customer
        like2.post = post
        like2.save()
        nlikes = len(Like.objects.filter(post_id=post_id))
        data = nlikes
        notifi = Notification()
        notifi.customer_id = post.customer.id
        notifi.createtime = timezone.now()
        notifi.customer_user = customer
        notifi.description = customer.fullname + " đã yêu thích bài viết của bạn..."
        notifi.post = post
        notifi.status = 2
        notifi.save()

    return HttpResponse(data)
예제 #2
0
def comments(request, post_id, id_customer):
    post = get_object_or_404(Post, pk=post_id)
    customer = get_object_or_404(Customer, pk=id_customer)
    fullname = customer.fullname
    if request.method == 'POST' and request.POST['text']:
        cmt = request.POST['text']
        if len(cmt) <= 1:
            data = {
                'Erro': "There was an error logging you in. Please Try again"
            }
            return JsonResponse(data)
    if request.method == 'POST' and request.POST['text']:
        comment = Comment()
        comment.createtime = timezone.now()
        comment.updatetime = timezone.now()
        comment.post = post
        comment.text = request.POST['text']
        comment.customer = customer
        comment.save()
        if id_customer != post.customer.id:
            notifi = Notification()
            notifi.customer_id = post.customer.id
            notifi.customer_user = customer
            notifi.createtime = timezone.now()
            notifi.description = customer.fullname + " đã bình luận bài viết của bạn..."
            notifi.post = post
            notifi.status = 1
            notifi.save()
        newcoments = comment.text
        fullname = customer.fullname
        createtime = comment.createtime
        data = {
            'datacmt': newcoments,
            'dataname': fullname,
            'Erro': "",
            'createtime': str(createtime)
        }
        return JsonResponse(data)
    else:
        data = {'Erro': "There was an error logging you in. Please Try again"}
        return JsonResponse(data)