Beispiel #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)
Beispiel #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))
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
0
    def post(self):
        #POST/ CREATE the product
        product_records = request.json['products']
        product = Product(product_records['name'], product_records['price'])
        category_list = product_records['category_id']
        for c in category_list:
            category = Category.find_by_id(c)
            product.categories.append(category)
        product.save_to_db()

        return {'message' : 'Product Created Successfully'}
Beispiel #6
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))
Beispiel #7
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))
Beispiel #8
0
 def put(self, id):
     '''Update the Category by ID'''
     self._validate()
     data = CategoryResource.parser.parse_args()
     category = Category.find_by_id(id)
     if not category:
         return {
             'message':
             'Category with the ID: {} does not exist. To update please create the Category first'
             .format(id)
         }
     else:
         category.name = data['name']
         category.save_to_db()
         return category.json()
Beispiel #9
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))
Beispiel #10
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))
Beispiel #11
0
 def get(self, id):
     '''Get Category by ID'''
     category = Category.find_by_id(id)
     if category:
         return category.json()
     return {'message': 'category NOT found'}, 404