示例#1
0
def add_attachment(request, board_id, card_id):
    if request.method != "POST":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member
    try:
        board = get_user_boards(request.user).get(id=board_id)
        card = board.cards.get(id=card_id)
    except (Board.DoesNotExist, Card.DoesNotExist) as e:
        return JsonResponseNotFound({"message": "Not found."})

    uploaded_file_content = request.body
    if uploaded_file_content is None:
        return JsonResponseNotFound({"message": "No file sent."})

    with tempfile.TemporaryFile() as uploaded_file:
        uploaded_file.write(uploaded_file_content)
        uploaded_file_name = request.GET.get("uploaded_file_name",
                                             custom_uuid())
        attachment = card.add_new_attachment(member, uploaded_file,
                                             uploaded_file_name)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card_attachment(attachment))
示例#2
0
def delete_attachment(request, board_id, card_id, attachment_id):
    if request.method != "DELETE":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member
    try:
        board = get_user_boards(request.user).get(id=board_id)
        card = board.cards.get(id=card_id)
        attachment = card.attachments.get(id=attachment_id)
    except (Board.DoesNotExist, Card.DoesNotExist,
            CardAttachment.DoesNotExist) as e:
        return JsonResponseNotFound({"message": "Not found."})

    card.delete_attachment(member, attachment)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card_attachment(attachment))