一、添加代码
在api文件夹下面新建一个product.py文件,加入以下获取产品列表接口代码
5 | from bottle import get, put, post, delete |
6 | from common import web_helper, db_helper, convert_helper, json_helper, string_helper |
16 | product_class_id = convert_helper.to_int0(web_helper.get_query( 'product_class_id' , '' , is_check_null=False)) |
17 | if product_class_id > 0: |
18 | wheres = 'where product_class_id=' + str(product_class_id) |
20 | page_number = convert_helper.to_int1(web_helper.get_query( 'page' , '' , is_check_null=False)) |
22 | page_size = convert_helper.to_int0(web_helper.get_query( 'rows' , '' , is_check_null=False)) |
24 | sidx = web_helper.get_query( 'sidx' , '' , is_check_null=False) |
26 | sord = web_helper.get_query( 'sord' , '' , is_check_null=False) |
30 | order_by = sidx + ' ' + sord |
32 | ############################################################# |
33 | # 初始化输出格式(前端使用jqgrid列表,需要指定输出格式) |
40 | ############################################################# |
42 | sql = 'select count(1) as records from product %(wheres)s' % { 'wheres' : wheres} |
43 | result = db_helper.read(sql) |
44 | # 如果查询失败或不存在指定条件记录,则直接返回初始值 |
45 | if not result or result[0][ 'records' ] == 0: |
48 | data[ 'records' ] = result[0].get( 'records' , 0) |
50 | ############################################################# |
53 | if page_size is None or page_size <= 0: |
56 | if data[ 'records' ] % page_size == 0: |
57 | page_total = data[ 'records' ] |
59 | page_total = data[ 'records' ] |
61 | data[ 'total' ] = page_total |
64 | if page_number < 1 or page_number > page_total: |
65 | page_number = page_total |
67 | data[ 'page' ] = page_number |
70 | record_number = (page_number - 1) * page_size |
72 | paging = ' limit ' + str(page_size) + ' offset ' + str(record_number) |
76 | ############################################################# |
79 | sql = "select * from product %(wheres)s order by %(orderby)s %(paging)s" % \ |
80 | { 'wheres' : wheres, 'orderby' : order_by, 'paging' : paging} |
82 | result = db_helper.read(sql) |
89 | return web_helper.return_raise(json.dumps(data, cls=json_helper.CJsonEncoder)) |
91 | return web_helper.return_msg(-1, "查询失败" ) |
二、获取指定id的记录实体
1 | @get( '/api/product/<id:int>/' ) |
6 | sql = "" "select * from product where id = %s" "" % (id,) |
8 | result = db_helper.read(sql) |
11 | return web_helper.return_msg(0, '成功' , result[0]) |
13 | return web_helper.return_msg(-1, "查询失败" ) |
三、添加产品与修改产品接口
6 | name = web_helper.get_form( 'name' , '产品名称' ) |
7 | code = web_helper.get_form( 'code' , '产品编码' ) |
8 | product_class_id = convert_helper.to_int0(web_helper.get_form( 'product_class_id' , '产品分类' )) |
9 | standard = web_helper.get_form( 'standard' , '产品规格' ) |
10 | quality_guarantee_period = web_helper.get_form( 'quality_guarantee_period' , '保质期' ) |
11 | place_of_origin = web_helper.get_form( 'place_of_origin' , '产地' ) |
12 | front_cover_img = web_helper.get_form( 'front_cover_img' , '封面图片' ) |
13 | content = web_helper.get_form( 'content' , '产品描述' , is_check_special_char=False) |
15 | content = string_helper.filter_str(content, "'" ) |
17 | content = string_helper.clear_xss(content) |
18 | is_enable = convert_helper.to_int0(web_helper.get_form( 'is_enable' , '是否启用' )) |
20 | # 添加记录(使用returning这个函数能返回指定的字段值,这里要求返回新添加记录的自增id值) |
21 | sql = "" "insert into product (name, code, product_class_id, standard, quality_guarantee_period, |
22 | place_of_origin, front_cover_img, content, is_enable) |
23 | values (%s, %s, %s, %s, %s, %s, %s, %s, %s) returning id "" " |
24 | vars = (name, code, product_class_id, standard, quality_guarantee_period, place_of_origin, front_cover_img, content, is_enable) |
26 | result = db_helper.write(sql, vars) |
28 | if result and result[0].get( 'id' ): |
29 | return web_helper.return_msg(0, '成功' ) |
31 | return web_helper.return_msg(-1, "提交失败" ) |
34 | @put( '/api/product/<id:int>/' ) |
40 | name = web_helper.get_form( 'name' , '产品名称' ) |
41 | code = web_helper.get_form( 'code' , '产品编码' ) |
42 | product_class_id = convert_helper.to_int0(web_helper.get_form( 'product_class_id' , '产品分类' )) |
43 | standard = web_helper.get_form( 'standard' , '产品规格' ) |
44 | quality_guarantee_period = web_helper.get_form( 'quality_guarantee_period' , '保质期' ) |
45 | place_of_origin = web_helper.get_form( 'place_of_origin' , '产地' ) |
46 | front_cover_img = web_helper.get_form( 'front_cover_img' , '封面图片' ) |
47 | content = web_helper.get_form( 'content' , '产品描述' , is_check_special_char=False) |
49 | content = string_helper.filter_str(content, "'" ) |
51 | content = string_helper.clear_xss(content) |
52 | is_enable = convert_helper.to_int0(web_helper.get_form( 'is_enable' , '是否启用' )) |
57 | set name=%s, code=%s, product_class_id=%s, standard=%s, quality_guarantee_period=%s, |
58 | place_of_origin=%s, front_cover_img=%s, content=%s, is_enable=%s |
59 | where id=%s returning id "" " |
60 | vars = (name, code, product_class_id, standard, quality_guarantee_period, place_of_origin, front_cover_img, content, |
63 | result = db_helper.write(sql, vars) |
65 | if result and result[0].get( 'id' ): |
66 | return web_helper.return_msg(0, '成功' ) |
68 | return web_helper.return_msg(-1, "提交失败" ) |
四、删除记录接口
1 | @ delete ( '/api/product/<id:int>/' ) |
7 | sql = "" "delete from product where id=%s returning id" "" |
10 | result = db_helper.write(sql, vars) |
13 | return web_helper.return_msg(0, '成功' ) |
15 | return web_helper.return_msg(-1, "删除失败" ) |
五、修改product_class_edit.html文件
六、 测试代码