设为首页 加入收藏

TOP

我的第一个python web开发框架(17)——产品管理(二)
2017-12-07 14:22:57 】 浏览:582
Tags:一个 python web 开发 框架 产品管理
是当前变量的中文说明,还有其他参数大家可以打看web_helper.py查看。

  convert_helper.to_int0()这个函数,在前面工具函数第一篇中讲到的函数,它会将接收到的参数字符串转为int类型值,如果这个参数小于0时,会自动使用默认值0代替。

 

sql = 'select count(1) as records from product %(wheres)s' % {'wheres': wheres}

  使用习惯ORM的朋友可能会不太习惯直接写sql语句,本系列第一部分主要面向没有什么基础的朋友,所以尽量不封装各种类和函数,这样大家直接看到内容会更容易理解。第二部分会教大家自己简单封装一个ORM,到时重构后重复代码就会减少一大半。

  上面这行是获取当前查询有多少条记录的sql语句,熟悉python字符串替换的朋友应该会很熟悉,它会将字符串%后面的字典内容替代字符串中对应的键值,如果wheres值为空时,则替换空值,即将%(wheres)s这个键值替换掉。

  python的字符串替换有多种方式,而这里使用字典方式来替换会让代码可读性更高,而且字典中各个值的位置不需要按固定方式摆放,不容易出错。

 

  获取指定id的记录实体

 1 @get('/api/product/<id:int>/')
 2 def callback(id):
 3     """
 4     获取指定记录
 5     """
 6     sql = """select * from product where id = %s""" % (id,)
 7     # 读取记录
 8     result = db_helper.read(sql)
 9     if result:
10         # 直接输出json
11         return web_helper.return_msg(0, '成功', result[0])
12     else:
13         return web_helper.return_msg(-1, "查询失败")

  这段代码比较简单,第6行使用的就是%s替换字符串方式,后面的元组(id,)  好像python3以后元组里不加逗号替换也没有问题,python2是一定要加的。

 

  添加产品与修改产品接口

 1 @post('/api/product/')
 2 def callback():
 3     """
 4     新增记录
 5     """
 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)
14     # 防sql注入攻击处理
15     content = string_helper.filter_str(content, "'")
16     # 防xss攻击处理
17     content = string_helper.clear_xss(content)
18     is_enable = convert_helper.to_int0(web_helper.get_form('is_enable', '是否启用'))
19 
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)
25     # 写入数据库
26     result = db_helper.write(sql, vars)
27     # 判断是否提交成功
28     if result and result[0].get('id'):
29         return web_helper.return_msg(0, '成功')
30     else:
31         return web_helper.return_msg(-1, "提交失败")
32 
33 
34 @put('/api/product/<id:int>/')
35 def callback(id):
36     """
37     修改记录
38     """
39 
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
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇djang-分页 下一篇Django的分页器(paginator)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目