Пример #1
0
 def post(self, request):
     data = json.loads(request.body.decode('utf-8'))
     try:
         if data['content_input']:
             new_cm = Comment()
             new_cm.content = data['content_input']
             new_cm.user_id = request.user.id
             new_cm.post_id = data['post_id']
             new_cm.save()
             return HttpResponse(
                 'bình luận thành công, hãy tiếp tục tương tác nhé')
     except:
         pass
     z = Comment.objects.filter(
         post=data['post_id']).order_by('-created_at')
     comments = []
     for i in z:
         d = model_to_dict(i.user)
         del d['password'], d['cover_image'], d['avatar'], d['email'], d[
             'date_joined']
         out = {
             **model_to_dict(i),
             **d, "created_at":
             i.created_at.strftime("%H:%M:%S ngày %m/%d/%Y")
         }
         comments.append(out)
     return JsonResponse(comments, safe=False)
Пример #2
0
 def post(self, request):
     data = json.loads(request.body.decode('utf-8'))
     try:
         if data['content_input']:
             new_cm = Comment()
             new_cm.content = data['content_input']
             new_cm.user_id = request.user.id
             new_cm.post_id = data['post_id']
             new_cm.save()
             return HttpResponse(
                 'bình luận thành công, hãy tiếp tục tương tác nhé')
     except:
         pass
     database = Database(request.user.id)
     get_comment_post_id = database.get_comment_post_id(data['post_id'])
     return JsonResponse({'result': get_comment_post_id})
Пример #3
0
def addcomment(request, id):
    url = request.META.get('HTTP_REFERER')
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            current_user = request.user
            data = Comment()
            data.user_id = current_user.id
            data.post_id = id
            data.rate = form.cleaned_data['rate']
            data.subject = form.cleaned_data['subject']
            data.comment = form.cleaned_data['comment']
            data.ip = request.META.get('REMOTE_ADDR')
            data.save()
            messages.success(request,
                             "Your comment succesfully send. Thank you")
            return HttpResponseRedirect(url)

    messages.warning(request, "Vups, Someting went wrong. Chechk again")
    return HttpResponseRedirect(url)