예제 #1
0
    def test_pagination(self, client):
        user1 = User(username='******', email='*****@*****.**', password='******')
        user2 = User(username='******', email='*****@*****.**', password='******')
        db.session.add(user1)
        db.session.add(user2)
        db.session.commit()

        # test ?order_by= worked
        resp = pagination(User, 1, 10, order_by=[''])
        assert resp['total'] == 2
        assert resp['page_size'] == 10
        assert resp['page'] == 1
        assert [i.id for i in resp['items']] == [1, 2]

        # test order_by: str
        resp = pagination(User, 1, 10, order_by='role')
        assert [i.id for i in resp['items']] == [1, 2]

        resp = pagination(User, 1, 10, order_by=['role', '-id'])
        assert [i.id for i in resp['items']] == [2, 1]

        resp = pagination(User, 1, 10, order_by=['role', 'username'])
        assert [i.id for i in resp['items']] == [1, 2]

        with pytest.raises(Exception, match='first arg obj must be model.'):
            pagination('User', 1, 10, order_by='role')

        msg = r'columns .*roles.* not exist in {} model'.format(User)
        with pytest.raises(Exception, match=msg):
            pagination(User, 1, 10, order_by='roles')
예제 #2
0
def list_attribute(page, page_size, order_by):
    # qexp = Category.query.filter(Category.category_level.in_(range(type)))
    # qexp = Category.query.filter(Category.category_level==type-1)
    qexp = Attribute.query
    paged_ret = pagination(Attribute,
                           page,
                           page_size,
                           order_by,
                           query_exp=qexp)
    return jsonify(schemas.paged_attribute_schemas.dump(paged_ret))
예제 #3
0
def category_attributes(pk, page, page_size, order_by, attribute_sel=None):
    category = Category.query.filter_by(id=pk).one()
    if not category:
        return ValidationErrorResult(message='ID 为{pk} 分类不存在')
    qexp = category.attributes.filter(Attribute.attribute_sel == attribute_sel)
    # qexp = Category.query.filter(Category.category_level.in_(range(type)))
    paged_ret = pagination(Attribute,
                           page,
                           page_size,
                           order_by,
                           query_exp=qexp)
    return jsonify(schemas.paged_attribute_schemas.dump(paged_ret))
예제 #4
0
def list(page, page_size, order_by, keyword):
    qexp = Good.query
    if keyword:
        qexp = qexp.filter(Good.good_name.like(f'%{keyword}%'))
    paged_ret = pagination(Good, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_good_schemas.dump(paged_ret))
예제 #5
0
def list_category(page, page_size, order_by, type=3):
    # qexp = Category.query.filter(Category.category_level.in_(range(type)))
    qexp = Category.query.filter(Category.category_level == type - 1)
    paged_ret = pagination(Category, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_category_schemas.dump(paged_ret))
예제 #6
0
def list_aside(page, page_size, order_by):
    qexp = Menu.query.filter_by(level=0)
    paged_ret = pagination(Menu, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_menu_schemas.dump(paged_ret))
예제 #7
0
def list(page, page_size, order_by):
    qexp = Menu.query
    paged_ret = pagination(Menu, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_menu_schemas.dump(paged_ret))
예제 #8
0
def list(page, page_size, order_by, keyword):
    qexp = Order.query
    if keyword:
        qexp = qexp.filter(Order.order_number.like(f'%{keyword}%'))
    paged_ret = pagination(Order, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_order_schemas.dump(paged_ret))
예제 #9
0
def list(page, page_size, order_by):
    qexp = User.s_query()
    paged_ret = pagination(User, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_user_schemas.dump(paged_ret))
예제 #10
0
def list(page, page_size, order_by):
    qexp = Role.query
    paged_ret = pagination(Role, page, page_size, order_by, query_exp=qexp)
    return jsonify(schemas.paged_role_schemas.dump(paged_ret))