Exemplo n.º 1
0
def add_new_review(request, board_id, card_id):
    if request.method != "PUT":
        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."})
    board = card.board

    put_body = json.loads(request.body)
    if not put_body.get("members"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    reviewers = board.members.filter(id__in=put_body.get("members"))

    description = put_body.get("description", "")

    card.add_review(member, reviewers=reviewers, description=description)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 2
0
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))
Exemplo n.º 3
0
def add_requirement(request, board_id, card_id):
    if request.method != "PUT":
        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."})
    board = card.board

    put_body = json.loads(request.body)
    if not put_body.get("requirement"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    requirement = board.requirements.get(id=put_body.get("requirement"))

    # If the requirement is already in the card, we can't continue
    if card.requirements.filter(id=requirement.id).exists():
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    card.add_requirement(member, requirement)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 4
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))
Exemplo n.º 5
0
def _move_all_list_cards(request, board_id):
    if request.method != "POST":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member
    try:
        board = get_board_or_404(request, board_id)
    except Http404:
        return JsonResponseNotFound({"message": "Board not found"})

    post_params = json.loads(request.body)

    if not post_params.get("source_list") or not post_params.get(
            "destination_list"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    # Check if the lists exists and if they are different
    try:
        source_list = board.active_lists.get(id=post_params.get("source_list"))
        destination_list = board.active_lists.get(
            id=post_params.get("destination_list"))
        if source_list.id == destination_list.id:
            raise AssertionError()
    except (List.DoesNotExist, AssertionError):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    # Move the cards
    source_list.move_cards(member=member, destination_list=destination_list)

    serializer = Serializer(board=board)
    return JsonResponse(serializer.serialize_board())
Exemplo n.º 6
0
def move_to_list(request, board_id, card_id):
    if request.method != "POST":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    post_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."})

    # The new position of the card
    new_position = post_params.get("position", "top")

    # If there is no new_list param, the card is going to be moved in the same list currently is
    if post_params.get("new_list"):
        list_ = card.board.lists.get(id=post_params.get("new_list"))
        card.move(member,
                  destination_list=list_,
                  destination_position=new_position)
    else:
        card.change_order(member, destination_position=new_position)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_board())
Exemplo n.º 7
0
def move_list(request, board_id, list_id):

    if request.method != "POST":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member

    try:
        list_ = get_list_or_404(request, board_id, list_id)
    except Http404:
        return JsonResponseNotFound({"message": "List not found"})

    post_params = json.loads(request.body)

    if not post_params.get("position"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    position = post_params.get("position")
    if position != "top" and position != "bottom" and not re.match(
            r"^\d+", position):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    list_.move(member, position)

    serializer = Serializer(board=list_.board)
    return JsonResponse(serializer.serialize_list(list_))
Exemplo n.º 8
0
def change_labels(request, board_id, card_id):
    if request.method != "POST":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    try:
        card = get_card_or_404(request, board_id, card_id)
    except Http404:
        return JsonResponseNotFound({"message": "Card not found."})
    board = card.board

    post_params = json.loads(request.body)

    if not post_params.get("labels"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    label_ids = post_params.get("labels")
    card.labels.clear()
    for label_id in label_ids:
        if board.labels.filter(id=label_id).exists():
            label = board.labels.get(id=label_id)
            card.labels.add(label)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 9
0
def _add_card(request, board_id):
    if request.method != "PUT":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member

    put_params = json.loads(request.body)

    if not put_params.get("name") or not put_params.get(
            "list") or not put_params.get("position"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    if put_params.get("position") != "top" and put_params.get(
            "position") != "bottom":
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    try:
        list_ = get_list_or_404(request, board_id, put_params.get("list"))
    except Http404:
        return JsonResponseNotFound({"message": "List not found"})

    new_card = list_.add_card(member=member,
                              name=put_params.get("name"),
                              position=put_params.get("position"))

    serializer = Serializer(board=new_card.board)
    return JsonResponse(serializer.serialize_card(new_card))
Exemplo n.º 10
0
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))
Exemplo n.º 11
0
def get_card(request, board_id, card_id):
    if request.method != "GET":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})
    try:
        card = get_card_or_404(request, board_id, card_id)
    except Http404:
        return JsonResponseNotFound({"message": "Card not found."})
    serializer = Serializer(board=card.board)
    card_json = serializer.serialize_card(card)
    return JsonResponse(card_json)
Exemplo n.º 12
0
def get_board(request, board_id):
    if request.method != "GET":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})
    try:
        board = get_user_boards(request.user).get(id=board_id)
    except Board.DoesNotExist:
        return JsonResponseNotFound({"message": "Not found."})

    serializer = Serializer(board=board)
    return JsonResponse(serializer.serialize_board())
Exemplo n.º 13
0
def modify_cards(request, board_id):
    # Create a new card
    if request.method == "PUT":
        return _add_card(request, board_id)

    # Move all cards in a list
    if request.method == "POST":
        return _move_all_list_cards(request, board_id)

    # Otherwise, return HTTP ERROR 405
    return JsonResponseMethodNotAllowed(
        {"message": "HTTP method not allowed."})
Exemplo n.º 14
0
def change(request, board_id, card_id):
    if request.method != "PUT":
        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."})

    put_params = json.loads(request.body)

    if put_params.get("name"):
        card.change_attribute(member,
                              attribute="name",
                              value=put_params.get("name"))

    elif put_params.get("description"):
        card.change_attribute(member,
                              attribute="description",
                              value=put_params.get("name"))

    elif put_params.get("is_closed") is not None:
        card.change_attribute(member,
                              attribute="is_closed",
                              value=put_params.get("name"))

    elif put_params.get("due_datetime"):
        due_datetime_str = put_params.get("due_datetime")
        due_datetime = dateutil.parser.parse(due_datetime_str)
        card.change_attribute(member,
                              attribute="due_datetime",
                              value=due_datetime)

    elif "due_datetime" in put_params and put_params.get(
            "due_datetime") is None:
        card.change_attribute(member, attribute="due_datetime", value=None)

    elif "value" in put_params:
        card_value = put_params.get("value")
        if card_value == "":
            card_value = None
        card.change_value(member=member, value=card_value)

    else:
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 15
0
def get_members(request):

    if request.method != "GET":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    current_member = request.user.member
    members = current_member.viewable_members

    serializer = Serializer()

    response_json = []
    for member in members:
        response_json.append(serializer.serialize_member(member))

    return JsonResponse(response_json, safe=False)
Exemplo n.º 16
0
def remove_requirement(request, board_id, card_id, requirement_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)
        requirement = card.requirements.get(id=requirement_id)
    except (Board.DoesNotExist, Card.DoesNotExist) as e:
        return JsonResponseNotFound({"message": "Not found."})

    card.remove_requirement(member, requirement)
    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 17
0
def add_se_time(request, board_id, card_id):
    if 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."})

    post_params = json.loads(request.body)
    spent_time = post_params.get("spent_time")
    estimated_time = post_params.get("estimated_time")
    date = post_params.get("date")
    description = post_params.get("description", card.name)

    if spent_time != "":
        try:
            spent_time = float(str(spent_time).replace(",", "."))
        except ValueError:
            return JsonResponseNotFound({"message": "Not found."})
    else:
        spent_time = None

    if estimated_time != "":
        try:
            estimated_time = float(str(estimated_time).replace(",", "."))
        except ValueError:
            return JsonResponseNotFound({"message": "Not found."})
    else:
        estimated_time = None

    if spent_time is None and estimated_time is None:
        return JsonResponseNotFound({"message": "Not found."})

    # Optional days ago parameter
    days_ago = None
    matches = re.match(r"^\-(?P<days_ago>\d+)$", date)
    if matches:
        days_ago = int(matches.group("days_ago"))

    card.add_spent_estimated_time(member, spent_time, estimated_time, days_ago,
                                  description)

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 18
0
def remove_member(request, board_id, member_id):
    if request.method != "DELETE":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member
    try:
        board = get_board_or_404(request, board_id)
    except Http404:
        return JsonResponseNotFound({"message": "Board not found"})

    try:
        member_to_remove = board.members.get(id=member_id)
        board.remove_member(member=member, member_to_remove=member_to_remove)
    except Member.DoesNotExist:
        return JsonResponseNotFound({"message": "Member nor found."})

    serializer = Serializer(board=board)
    return JsonResponse(serializer.serialize_member(member_to_remove))
Exemplo n.º 19
0
def get_boards(request):
    if request.method != "GET":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})
    try:
        boards = get_user_boards(request.user)
    except Board.DoesNotExist:
        return JsonResponseNotFound({"message": "Not found."})

    response_json = []
    for board in boards:
        board_json = {
            "id": board.id,
            "uuid": board.uuid,
            "name": board.name,
            "description": board.description,
            "lists": []
        }
        response_json.append(board_json)

    return JsonResponse(response_json, safe=False)
Exemplo n.º 20
0
def add_blocking_card(request, board_id, card_id):
    if request.method != "PUT":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member
    put_body = json.loads(request.body)
    if not put_body.get("blocking_card"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

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

    card.add_blocking_card(member, blocking_card)
    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))
Exemplo n.º 21
0
def add_member(request, board_id):
    if request.method != "PUT":
        return JsonResponseMethodNotAllowed(
            {"message": "HTTP method not allowed."})

    member = request.user.member
    put_body = json.loads(request.body)

    if not put_body.get("member"):
        return HttpResponseBadRequest()

    member_id = put_body.get("member")

    try:
        board = get_board_or_404(request, board_id)
    except Http404:
        return JsonResponseNotFound({"message": "Board not found"})

    serializer = Serializer(board=board)

    if board.members.filter(id=member_id).exists():
        return JsonResponse(
            serializer.serialize_member(board.members.get(id=member_id)))

    member_type = put_body.get("member_type")
    if not member_type or not member_type in ("admin", "normal", "guest"):
        return JsonResponseNotFound({"message": "Member type not found"})

    try:
        new_member = Member.objects.get(id=member_id)
        board.add_member(member=member, member_to_add=new_member)
        member_role, member_role_created = MemberRole.objects.get_or_create(
            board=board, type=member_type)
        member_role.members.add(new_member)
    except Member.DoesNotExist:
        return JsonResponseNotFound({"message": "Not found."})

    return JsonResponse(serializer.serialize_member(new_member))
Exemplo n.º 22
0
def change_members(request, board_id, card_id):
    if 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."})
    board = card.board

    post_params = json.loads(request.body)

    if not post_params.get("members"):
        return JsonResponseBadRequest(
            {"message": "Bad request: some parameters are missing."})

    member_ids = post_params.get("members")

    card.update_members(member, board.members.filter(id__in=member_ids))

    serializer = Serializer(board=card.board)
    return JsonResponse(serializer.serialize_card(card))