def post_comment(request, post_id): if not request.user.is_authenticated: return redirect('login') if request.method == "POST" and request.POST.get('comment') != '': post = get_object_or_404(Post, pk=post_id) myUser = CustomUser.objects.get(user=request.user) com = Comment() com.post = post com.user = myUser com.date_pub = datetime.datetime.now() com.text = request.POST.get('comment') com.save() return redirect('post_index')
def comment(request): try: if request.method == 'POST': try: # Get Object user = User.objects.get(pk=request.data['user_id']) post = Post.objects.get(pk=request.data['post_id']) # Create Comment Object comment = Comment(text=request.data['text'], user=user, post=post) # Create Message Object if user.id != post.user_id: message = Message( text=f"{user.first_name} Commented on your post.", post=post, send_by=user, message_to=User.objects.get(pk=post.user_id)) message.save() # Save Object comment.save() # Response return JsonResponse({ "statusCode": 201, "statusText": "Created", "message": "Comment Posted!", "error": False }) except ObjectDoesNotExist: return JsonResponse({ "statusCode": 404, "statusText": "Not Found", "message": "Post Or User Not Exist", "error": True }) elif request.method == 'PUT': try: comment = Comment.objects.get(pk=request.data['comment_id']) comment.text = request.data['text'] comment.save() return JsonResponse({ "statusCode": 200, "statusText": "Success", "message": "Success", "error": False }) except ObjectDoesNotExist: return JsonResponse({ "statusCode": 404, "statusText": "Not Found", "message": "Comment Not Exist", "error": True }) except: return JsonResponse({ "statusCode": 500, "statusText": "Internal Server", "message": "Internal Server", "error": True })