Beispiel #1
0
def read_list(request: BoardDataRequest, board: BoardModel,
              user_id: int) -> (List[CardsList], BaseError):
    query = CardListModel.select()
    list_response = []

    if request.id is not None:
        query = query.where(CardListModel.id == request.id)
    if request.name is not None:
        query = query.where(CardListModel.name == request.name)
    if board is not None:
        query = query.where(CardListModel.board == board)

    if query.count() == 0:
        return None, BaseError(
            code=provider.StorageProviderErrors.LIST_DOES_NOT_EXIST,
            description="List doesn't exist")

    for card_list in query:
        if bool(check_access_to_list(card_list, user_id) & AccessType.READ):
            cards = [card.id for card in card_list.cards]
            list_response += [CardsList(card_list.name, card_list.id, cards)]

    if not list_response:
        return None, BaseError(
            code=provider.StorageProviderErrors.ACCESS_DENIED,
            description="This user can't read this list")
    else:
        return list_response, None
Beispiel #2
0
def read_card(request: CardDataRequest, user_id: int, card_list: CardListModel) -> (List[Card], BaseError):
    card_response = []
    query = CardModel.select()

    if request.id is not None:
        query = query.where(CardModel.id == request.id)
    if request.name is not None:
        query = query.where(CardModel.name == request.name)
    if card_list is not None:
        query = query.where(CardModel.list == card_list)
    if request.board_id is not None:
        query = query.join(CardListModel).join(BoardModel).where(CardModel.list.board == request.board_id)
    if request.tags:
        query = query.switch(CardModel).join(TagCard).join(TagModel).where(TagModel.id == request.tags[0])

    query = query.order_by(-CardModel.priority)

    if query.count() == 0:
        return None, BaseError(code=provider.StorageProviderErrors.CARD_DOES_NOT_EXIST,
                               description="Card doesn't exist")

    for card in query:
        if bool(check_access_to_card(card, user_id) & AccessType.READ):
            card_response += [_create_card_from_orm(card)]

    if not card_response:
        return None, BaseError(code=provider.StorageProviderErrors.ACCESS_DENIED,
                               description="This user can't read this card")
    else:
        return card_response, None
Beispiel #3
0
def write_list(request: BoardDataRequest, board: BoardModel,
               user_id: int) -> (List[CardsList], BaseError):
    try:
        card_list = CardListModel.get(CardListModel.id == request.id)

        if bool(check_access_to_list(card_list, user_id) & AccessType.WRITE):
            card_list.name = request.name
            card_list.save()
            cards = [card_id for card_id in card_list.cards]
            if board is not None:
                card_list.board = board

            return [CardsList(card_list.name, card_list.id, cards)], None
        else:
            return None, BaseError(
                provider.StorageProviderErrors.ACCESS_DENIED,
                "This user can't write to this list")
    except DoesNotExist:
        if bool(check_access_to_board(board, user_id) & AccessType.WRITE):
            card_list = CardListModel.create(name=request.name, board=board)
            CardListUserAccess.create(user_id=user_id, card_list=card_list)
            return [CardsList(card_list.name, card_list.id)], None
        else:
            return None, BaseError(
                provider.StorageProviderErrors.ACCESS_DENIED,
                "This user has not enough rights for "
                "this board")
Beispiel #4
0
def delete_card(request: CardDataRequest, user_id: int) -> (List[Card], BaseError):
    try:
        card = CardModel.get(CardModel.id == request.id)
        access = check_access_to_card(card, user_id)
        if bool(access & AccessType.WRITE):
            _delete_card(card)
        else:
            return None, BaseError(code=provider.StorageProviderErrors.ACCESS_DENIED,
                                   description="This user can't delete this card")
    except DoesNotExist:
        return None, BaseError(code=provider.StorageProviderErrors.LIST_DOES_NOT_EXIST,
                               description="Card doesn't exist")
    return None, None
Beispiel #5
0
    def execute(self, request: namedtuple) -> (namedtuple, BaseError):
        if type(request.request_type) is not RequestType:
            return None, BaseError(
                code=StorageProviderErrors.REQUEST_TYPE_NOT_SPECIFIED,
                description='Request type is not specified')

        handler = self.handler_map[type(request)]

        if handler is None:
            return None, BaseError(
                code=StorageProviderErrors.INVALID_REQUEST,
                description='This request cannot be handled by this provider')

        return handler(request)
Beispiel #6
0
def read_plan(user_id: int, card: CardModel) -> (Plan, BaseError):
    if bool(check_access_to_card(card, user_id) & AccessType.READ):
        try:
            plan_model = PlanModel.get(PlanModel.card == card)
            return Plan(datetime.timedelta(seconds=plan_model.interval),
                        card.id, plan_model.last_created_at,
                        plan_model.id), None
        except DoesNotExist:
            return None, BaseError(
                code=provider.StorageProviderErrors.PLAN_DOES_NOT_EXIST,
                description="There is no plan for this task")
    else:
        return None, BaseError(
            code=provider.StorageProviderErrors.ACCESS_DENIED,
            description="This user can't read this card")
Beispiel #7
0
def delete_plan(user_id: int, card: CardModel) -> (None, BaseError):
    if bool(check_access_to_card(card, user_id) & AccessType.WRITE):
        count = PlanModel.delete().where(PlanModel.card == card).execute()
        if count == 0:
            return None, BaseError(
                code=provider.StorageProviderErrors.PLAN_DOES_NOT_EXIST,
                description="There is no plan for this task")
    return None, None
Beispiel #8
0
def delete_list(request: BoardDataRequest,
                user_id: int) -> (List[CardsList], BaseError):
    try:
        card_list = CardListModel.get(CardListModel.id == request.id)
        access = check_access_to_list(card_list, user_id)
        if bool(access & AccessType.WRITE):
            CardListUserAccess.delete().where(
                CardListUserAccess.card_list == card_list).execute()
            _delete_list(card_list)
        else:
            return None, BaseError(
                code=provider.StorageProviderErrors.ACCESS_DENIED,
                description="This user can't delete this list")
    except DoesNotExist:
        return None, BaseError(
            code=provider.StorageProviderErrors.LIST_DOES_NOT_EXIST,
            description="List doesn't exist")
    return None, None
Beispiel #9
0
def process_card_call(request: CardDataRequest) -> (namedtuple, BaseError):
    try:
        card_list = CardListModel.get(CardListModel.id == request.list_id) if request.list_id is not None else None
        user_id = request.request_user_id
        card_response, error = METHOD_MAP[request.request_type](request, user_id, card_list)

        return provider.CardDataResponse(cards=card_response, request_id=request.request_id), error
    except DoesNotExist:
        return None, BaseError(code=provider.StorageProviderErrors.LIST_DOES_NOT_EXIST,
                               description="List or board doesn't exist")
Beispiel #10
0
def delete_tag(request: TagDataRequest) -> (List[TagModel], BaseError):
    try:
        tag_model = TagModel.get((TagModel.id == request.id)
                                 | (TagModel.name == request.name))
        TagCard.delete().where(TagCard.tag == tag_model).execute()
        tag_model.delete_instance()
    except DoesNotExist:
        return None, BaseError(
            code=provider.StorageProviderErrors.TAG_DOES_NOT_EXIST,
            description="Tag doesn't exist")
    return None, None
Beispiel #11
0
def delete_board(request: BoardDataRequest,
                 user_id: int) -> (List[Board], BaseError):
    try:
        board = BoardModel.get(BoardModel.id == request.id)
        access = check_access_to_board(board, user_id)
        if bool(access & AccessType.WRITE):
            BoardUserAccess.delete().where(
                BoardUserAccess.board == board).execute()
            for card_list in board.card_lists:
                list_processor._delete_list(card_list)
            board.delete_instance()
        else:
            return None, BaseError(
                code=StorageProviderErrors.ACCESS_DENIED,
                description="This user can't delete this board")
    except DoesNotExist:
        return None, BaseError(code=StorageProviderErrors.BOARD_DOES_NOT_EXIST,
                               description="Board doesn't exist")

    return None, None
Beispiel #12
0
def write_board(request: BoardDataRequest,
                user_id: int) -> (List[Board], BaseError):
    try:
        board = BoardModel.get(BoardModel.id == request.id)

        if bool(check_access_to_board(board, user_id) & AccessType.WRITE):
            board.name = request.name
            board.save()
            lists = [list_id for list_id in board.card_lists]
            return [Board(board.name, board.id, lists)], None
        else:
            return None, BaseError(StorageProviderErrors.ACCESS_DENIED,
                                   "This user can't write to this board")
    except DoesNotExist:
        return [_create_board_with_defaults(request.name, user_id)], None
Beispiel #13
0
def read_board(request: BoardDataRequest,
               user_id: int) -> (List[Board], BaseError):
    query = BoardModel.select()
    board_response = []

    if request.id is not None:
        query = query.where(BoardModel.id == request.id)
    if request.name is not None:
        query = query.where(BoardModel.name == request.name)

    if query.count() == 0:
        return None, BaseError(code=StorageProviderErrors.BOARD_DOES_NOT_EXIST,
                               description="Board doesn't exist")

    for board in query:
        if bool(check_access_to_board(board, user_id) & AccessType.READ):
            lists = [card_list.id for card_list in board.card_lists]
            board_response += [Board(board.name, board.id, lists)]

    if not board_response:
        return None, BaseError(code=StorageProviderErrors.ACCESS_DENIED,
                               description="This user can't read this board")
    else:
        return board_response, None
Beispiel #14
0
def process_list_call(
        request: BoardDataRequest) -> (provider.ListDataResponse, BaseError):
    try:
        board = BoardModel.get(BoardModel.id == request.board_id
                               ) if request.board_id is not None else None
        user_id = request.request_user_id
        list_response, error = METHOD_MAP[request.request_type](request,
                                                                user_id, board)

        return provider.ListDataResponse(lists=list_response,
                                         request_id=request.request_id), error
    except DoesNotExist:
        return None, BaseError(
            code=provider.StorageProviderErrors.BOARD_DOES_NOT_EXIST,
            description="Board doesn't exist")
Beispiel #15
0
def read_tag(request: TagDataRequest) -> (List[TagModel], BaseError):
    if request.id is None and request.name is None:
        tag_models = TagModel.select()
        return [
            Tag(tag_model.name, tag_model.id, tag_model.color)
            for tag_model in tag_models
        ], None
    else:
        try:
            tag_model = TagModel.get((TagModel.id == request.id)
                                     | (TagModel.name == request.name))
            return [Tag(tag_model.name, tag_model.id, tag_model.color)], None
        except DoesNotExist:
            return None, BaseError(
                code=provider.StorageProviderErrors.TAG_DOES_NOT_EXIST,
                description="Tag doesn't exist")
Beispiel #16
0
def process_plan_call(request: PlanDataRequest) -> (namedtuple, BaseError):
    if type(request) == PlanTriggerRequest:
        return create_cards_by_plans()

    try:
        card = CardModel.get(CardModel.id == request.card_id)
        user_id = request.request_user_id
        plan_response, error = METHOD_MAP[request.request_type](request,
                                                                user_id, card)

        return provider.PlanDataResponse(plan=plan_response,
                                         request_id=request.request_id), error
    except DoesNotExist:
        return None, BaseError(
            code=provider.StorageProviderErrors.CARD_DOES_NOT_EXIST,
            description="Card doesn't exist")
Beispiel #17
0
    def _database_call(request: namedtuple) -> (List[User], BaseError):
        if request.request_type == RequestType.WRITE:
            user = User.create(id=request.id, username=request.name)
            return [create_user_from_orm(user)], None
        elif request.request_type == RequestType.READ:
            query = User.select()

            if request.id is not None or request.name is not None:
                query = query.where((User.id == request.id)
                                    | (User.username == request.name))

            if query.count() == 0:
                return None, BaseError(
                    code=UserProviderErrorCodes.USER_DOES_NOT_EXIST,
                    description="User doesn't "
                    "exist")
            return [create_user_from_orm(orm_user) for orm_user in query], None
        elif request.request_type == RequestType.DELETE:
            User.delete().where(User.id == request.id).execute()
Beispiel #18
0
def write_plan(request: PlanDataRequest, user_id: int,
               card: CardModel) -> (Plan, BaseError):
    if bool(check_access_to_card(card, user_id) & AccessType.WRITE):
        try:
            plan_model = PlanModel.get(PlanModel.card == card)
            if request.interval is not None:
                plan_model.interval = request.interval.total_seconds()
            if request.last_created is not None:
                plan_model.last_created_at = request.last_created
            plan_model.save()
        except DoesNotExist:
            plan_model = PlanModel.create(
                card=card,
                interval=request.interval.total_seconds(),
                last_created_at=request.last_created)
        return Plan(datetime.timedelta(seconds=plan_model.interval), card.id,
                    plan_model.last_created_at, plan_model.id), None
    else:
        return None, BaseError(
            code=provider.StorageProviderErrors.ACCESS_DENIED,
            description="This user can't write to this card")
Beispiel #19
0
def write_card(request: CardDataRequest, user_id: int, card_list: CardListModel) -> (List[Card], BaseError):
    try:
        card = CardModel.get(CardModel.id == request.id)

        if bool(check_access_to_card(card, user_id) & AccessType.WRITE):
            card.name = request.name
            card.description = request.description
            card.expiration_date = request.expiration_date
            card.priority = request.priority if request.priority is not None else Priority.MEDIUM
            card.assignee_id = request.assignee
            if card_list is not None:
                card.list = card_list

            actual_children_quarry = ParentChild.select().where(ParentChild.parent == card)
            for parent_child in actual_children_quarry:
                if parent_child.child.id not in request.children:
                    parent_child.delete_instance()

            if request.children is not None:
                potential_children_quarry = CardModel.select().where(CardModel.id.in_(request.children))
                for iter_card in potential_children_quarry:
                    if bool(check_access_to_card(iter_card, user_id) & AccessType.READ) and not (iter_card == card):
                        try:
                            ParentChild.get(ParentChild.parent == card, ParentChild.child == iter_card)
                        except DoesNotExist:
                            ParentChild.create(parent=card, child=iter_card)

            for tag_card in TagCard.select().where(TagCard.card == card):
                if tag_card.tag.id not in request.tags:
                    tag_card.delete_instance()

            if request.tags is not None:
                for tag in TagModel.select().where(TagModel.id.in_(request.tags)):
                        TagCard.get_or_create(tag=tag, card=card)

            card.save()
            return [_create_card_from_orm(card)], None
        else:
            return None, BaseError(provider.StorageProviderErrors.ACCESS_DENIED, "This user can't write to this card")
    except DoesNotExist:
        if bool(check_access_to_list(card_list, user_id) & AccessType.WRITE):
            card = CardModel.create(name=request.name,
                                    description=request.description,
                                    expiration_date=request.expiration_date,
                                    priority=Priority.MEDIUM if request.priority is None else request.priority,
                                    assignee_id=request.assignee,
                                    list=card_list,
                                    user_id=user_id)

            if request.tags is not None:
                for tag in TagModel.select().where(TagModel.id.in_(request.tags)):
                    TagCard.create(tag=tag, card=card)

            if request.children is not None:
                potential_children_quarry = CardModel.select().where(CardModel.id.in_(request.children))
                for iter_card in potential_children_quarry:
                    if bool(check_access_to_card(iter_card, user_id) & AccessType.READ) and not (iter_card == card):
                        ParentChild.create(parent=card, child=iter_card)

            return [_create_card_from_orm(card)], None
        else:
            return None, BaseError(provider.StorageProviderErrors.ACCESS_DENIED, "This user has not enough rights for "
                                                                                 "this list")