def modify_comment(request, board_id, card_id, comment_id): if request.method != "DELETE" and request.method != "POST": return JsonResponseMethodNotAllowed( {"message": "HTTP method not allowed."}) member = request.user.member try: card = get_card_or_404(request, board_id, card_id) except Http404: return JsonResponseNotFound({"message": "Card not found."}) try: comment = card.comments.get(id=comment_id) except CardComment.DoesNotExist as e: return JsonResponseNotFound({"message": "Not found."}) if request.method == "DELETE": comment = _delete_comment(member, card, comment) elif request.method == "POST": post_params = json.loads(request.body) new_comment_content = post_params.get("content") if not new_comment_content: return JsonResponseBadRequest( {"message": "Bad request: some parameters are missing."}) comment = _edit_comment(member, card, comment, new_comment_content) else: return JsonResponseBadRequest( {"message": "Bad request: some parameters are missing."}) serializer = Serializer(board=card.board) return JsonResponse(serializer.serialize_card_comment(comment))
def add_new_comment(request, board_id, card_id): if request.method != "PUT": return JsonResponseMethodNotAllowed( {"message": "HTTP method not allowed."}) put_params = json.loads(request.body) member = request.user.member try: card = get_card_or_404(request, board_id, card_id) except Http404: return JsonResponseNotFound({"message": "Card not found."}) # Getting the comment content comment_content = put_params.get("content") # If the comment is empty, fail if not comment_content: return JsonResponseBadRequest( {"message": "Bad request: some parameters are missing."}) # Otherwise, add the comment new_comment = card.add_comment(member, comment_content) serializer = Serializer(board=card.board) return JsonResponse(serializer.serialize_card_comment(new_comment))