Example #1
0
def get_items_in_category(category_id):
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    items = category.items

    page = request.args.get('page')
    if page:
        # Check if the page 'value' is integer
        try:
            page = int(page)
        except ValueError:
            raise WrongPageNumberTypeError()

        # Get index of last page, items
        number_of_items = items.count()
        items_per_page = app.config['ITEMS_PER_PAGE']
        number_of_page = math.ceil(float(number_of_items) / items_per_page)
        if page < 1 or page > number_of_page:
            raise PageNotFoundError()
        offset = (page - 1) * items_per_page
        items = category.items.offset(offset).limit(items_per_page)
        items = ItemSchema().dump(items, many=True).data
        item_pagination = PaginationSchema().load({
            'last_page': number_of_page,
            'current_page': page,
            'items': items
        }).data
        return message(data=item_pagination)
    items = ItemSchema().dump(items, many=True).data
    return message(data=items)
Example #2
0
def update_item_in_category(new_item, category_id, item_id):
    # Check existences of category and item
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    item = category.items.filter_by(id=item_id).first()
    if not item:
        raise ItemNotFoundError(item_id)

    # Check permission
    if item.user.id != get_jwt_identity():
        raise ForbiddenError()

    # Save title of item for notification
    old_title = item.title

    # Check existences of item title
    title = new_item.title
    old_item = Item.find_by_title(title)
    if old_item and old_item.id != item_id:
        raise DuplicateValueError('item', 'title', title)

    # Update final result
    item.update_from_copy(new_item)
    item.save_to_db()
    return message('Item "{}" was updated.'.format(old_title))
Example #3
0
def create_user(user):
    old_user = User.find_by_username(user.username)
    if old_user:
        raise DuplicateValueError('user', 'username', user.username)
    user.save_to_db()
    return message('Account with username "{}" was created.'.format(
        user.username))
Example #4
0
def get_category(category_id):
    # Check existence of category
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    category = CategorySchema().dump(category).data
    return message(data=category)
Example #5
0
def get_item_in_category(category_id, item_id):
    # Check existences of category and item
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    item = category.items.filter_by(id=item_id).first()
    if not item:
        raise ItemNotFoundError(item_id)
    item = ItemSchema().dump(item).data
    return message(data=item)
Example #6
0
def create_category(category):
    # Fill necessary field
    category.user_id = get_jwt_identity()

    # Check existence of category name
    name = category.name
    old_category = Category.find_by_name(name)
    if old_category:
        raise DuplicateValueError('category', 'name', name)

    category.save_to_db()
    return message('Category "{}" was created.'.format(category.name))
Example #7
0
def login(user):
    # Because the password is hashed in post_load, this function have to achieve original information
    user_in_db = User.find_by_username(user.username)
    if not user_in_db:
        raise InvalidCredentialsError()
    password = user.password

    valid = verify_hash(password, user_in_db.password_hash)
    if not valid:
        raise InvalidCredentialsError()

    access_token = create_access_token(identity=user_in_db.id)
    return message(data={'access_token': access_token})
Example #8
0
def delete_category(category_id):
    # Check existence of category
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)

    # Check permission
    if category.user.id != get_jwt_identity():
        raise ForbiddenError()

    name = category.name
    category.delete_from_db()
    return message('Category "{}" was deleted'.format(name))
Example #9
0
def delete_item_in_category(category_id, item_id):
    # Check existences of category and item
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    item = category.items.filter_by(id=item_id).first()
    if not item:
        raise ItemNotFoundError(item_id)

    # Check permission
    if item.user.id != get_jwt_identity():
        raise ForbiddenError()

    item.delete_from_db()
    return message('Item "{}" was deleted.'.format(item.title))
Example #10
0
def create_item_in_category(item, category_id):
    # Check existences of category
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)

    # Fill necessary fields
    item.user_id = get_jwt_identity()
    item.category_id = category_id

    # Check existences of item title
    title = item.title
    old_item = Item.find_by_title(title)
    if old_item:
        raise DuplicateValueError('item', 'title', title)

    item.save_to_db()
    return message('Item "{}" was created.'.format(item.title))
Example #11
0
def update_category(new_category, category_id):
    # Check existence of category
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)

    # Check permission
    if category.user.id != get_jwt_identity():
        raise ForbiddenError()

    # Save category name for notification
    new_name = new_category.name

    # Check existence of category name
    old_category = Category.find_by_name(new_name)
    if old_category and old_category.id != category_id:
        raise DuplicateValueError('category', 'name', new_name)

    # Update final result
    category.update_from_copy(new_category)
    category.save_to_db()

    return message('Category "{}" was updated.'.format(new_name))
Example #12
0
def get_categories():
    categories = Category.query.all()
    categories = CategorySchema().dump(categories, many=True).data
    return message(data=categories)