完成待办事件
1. url 的配置
在main.py
代码中:
urls = (
'/', 'index',
'/TodoLists', 'todo.TD',
'/TodoLists/(\w+)', 'todo.TD_Simple',
)
不用修改urls配置,因为todo.TD_Simple
已经含有 PATCH 方法
2. 为todo.TD_Simple
类修改 PATCH 方法
加入对待办事项的状态管理
class TD_Simple: #针对单个待办事件处理类
def PATCH(self, id):
'''
按ID查询修改结果
:param id:
:return:
'''
todo = get_by_id(id)
web.header('Content-Type', 'application/json')
if not todo:
return json.dumps({'error':'没找到这条记录'})
i = web.input()
print i
title = i.get('title', None)
status = i.get('finished', None)
if title :
collection.update({'_id': ObjectId(id)}, {"$set": {'title': title}})
if status:
if status.lower() == 'yes':
finished = True
elif status.lower() == 'no':
finished = False
collection.update({'_id': ObjectId(id)}, {"$set": {'finished': finished}})
if not title and not status:
return json.dumps({'error':'您发起了一个不允许的请求'})
#修改成功
todo = get_by_id(id)
format_data = formatField(todo)
return json.dumps(format_data)
3. postman 修改 finished 状态
4. 完整代码
# coding: utf-8
import web
import json
import datetime
from main import collection
from bson.objectid import ObjectId
def formatField(data):
'''
格式化字段
:param data:
:return:
'''
data['_id'] = str(data['_id'])
data['post_date'] = data['post_date'].strftime('%Y-%m-%d %H:%M:%S')
return data
class TD(object): # 针对待办事件列表 处理类
def POST(self):
'''
新建待办事件
:return:
'''
i = web.input()
web.header('Content-Type', 'application/json')
print i
title = i.get('title', None)
if not title:
return json.dumps({'error': '标题让你吃了吗?'})
post_data = {
'title': title,
'post_date': datetime.datetime.now()
}
collection.insert_one(post_data)
post_data = formatField(post_data)
return json.dumps(post_data)
def GET(self):
results=[]
i = web.input()
print i
page = int(i.get('page', 1))
limit = int(i.get('limit', 10))
skip_num = (page-1)*limit
web.header('Content-Type', 'application/json')
for post_data in collection.find().skip(skip_num).limit(limit):
post_data = formatField(post_data)
results.append(post_data)
return json.dumps(results)
def get_by_id(id):
'''
按ID查询数据库
:param id:
:return:
'''
if len(id) != 24:
return False
s = collection.find_one({'_id':ObjectId(id)})
if not s:
return False
return s
class TD_Simple:
def GET(self,id):
todo = get_by_id(id)
if not todo:
return json.dumps({'error':'没找到这条记录'})
post_data = formatField(todo)
web.header('Content-Type', 'application/json')
return json.dumps(post_data)
def PATCH(self, id):
web.header('Content-Type', 'application/json')
todo = get_by_id(id)
if not todo:
return json.dumps({'error':'没找到这条记录'})
i = web.input()
print i
title = i.get('title', None)
if not title:
return json.dumps({'error':'标题让你吃了吗?'})
status = i.get('finished', 'no')
if status == 'yes':
finished = True
elif status == 'no':
finished = False
else:
return json.dumps({'error':'您发起了一个不允许的请求'})
collection.update({'_id':ObjectId(id)}, {"$set":{'title': title,'finished':finished}})
todo = get_by_id(id)
format_data = formatField(todo)
return json.dumps(format_data)
def DELETE(self,id):
web.header('Content-Type', 'application/json')
if len(id)!=24:
return json.dumps({'error':'id length is error'})
todo = get_by_id(id)
if not todo:
return json.dumps({'error':'没找到这条记录'})
collection.remove({'_id':ObjectId(id)})
return json.dumps({})