Esempio n. 1
0
def create_item(data, user, category):
    """Create a new item in the given category.

    category_id is not needed as it was resolved by a decorator as category
    :param data: The data of the item
    :param user: User instance of the authenticated user
    :param category: Category from which the item is being created
    :return: A newly created Item
    """

    # Check if the category already has an item with the same name
    duplicated_item = category.items.filter_by(name=data.get('name')).one_or_none()

    if duplicated_item:
        raise DuplicatedItemError()

    # Proceed to create new item in category
    new_item = ItemModel(**data)
    new_item.user_id = user.id
    new_item.category_id = category.id

    # Save to DB
    new_item.save()

    return jsonify(
        ItemSchema().dump(new_item)
    )
Esempio n. 2
0
def create_item(body_params):
    """
    Create an item
    :param body_params:
    :bodyparam title: Title of the item
    :bodyparam description: Description of the item
    :bodyparam category_id: Identifier of the category to which this item will belong

    :raise ValidationError 400: if form is messed up
    :raise DuplicatedEntity 400: If try to create an existed object.
    :raise BadRequest 400: if the body mimetype is not JSON
    :raise Unauthorized 401: If not login
    :raise NotFound 404: If category_id is not valid
    :return: the created item
    """
    if CategoryModel.find_by_id(body_params['category_id']) is None:
        raise NotFound(error_message='Category with this id doesn\'t exist.')
    if ItemModel.query.filter_by(title=body_params['title']).first():
        raise DuplicatedEntity(error_message='Item with this title exists.')

    body_params['creator_id'] = get_jwt_identity()
    item = ItemModel(**body_params)
    item.save()

    return create_data_response(item_schema.dump(item))
Esempio n. 3
0
def create_item(title, description, cat_id):
    """
    Create user and save to database
    :param title: A unique string length >=4
    :param description: A string length >=4
    :param cat_id: category id
    :return: category object
    """
    item = ItemModel(title=title, description=description, category_id=cat_id)
    item.save()

    return item