def update(request): """ 用于增加新 todo 的路由函数 """ form = request.form() log('todo update', form, form['id'], type(form['id'])) todo_id = int(form['id']) Todo.update(todo_id, title=form['title']) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo/index')
def add(request): """ 用于增加新 todo 的路由函数 """ form = request.form() u = current_user(request) Todo.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo/index')
def index(request): """ todo 首页的路由函数 """ u = current_user(request) todos = Todo.all(user_id=u.id) return html_response('todo_index.html', todos=todos)
def edit(request): """ todo 首页的路由函数 """ # 替换模板文件中的标记字符串 todo_id = int(request.query['id']) t = Todo.one(id=todo_id) return html_response('todo_edit.html', todo_id=todo_id, todo_title=t.title)
def f(request): log('same_user_required', route_function) u = current_user(request) if request.method == 'GET': todo_id = int(request.query['id']) elif request.method == 'POST': todo_id = int(request.form()['id']) else: raise ValueError('不支持的请求方法', request.method) t = Todo.one(id=todo_id) if t.user_id == u.id: return route_function(request) else: return redirect('/todo')
def delete(request): todo_id = int(request.query['id']) Todo.delete(todo_id) return redirect('/todo/index')
def test(): connection = pymysql.connect( host='localhost', user='******', password='******'.format(secret.database_password), # db='web8', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) with connection.cursor() as cursor: cursor.execute('DROP DATABASE IF EXISTS`web`') cursor.execute('CREATE DATABASE `web` CHARACTER SET utf8mb4') cursor.execute('USE `web`') cursor.execute(User.sql_create) cursor.execute(Session.sql_create) cursor.execute(Todo.sql_create) cursor.execute(Weibo.sql_create) cursor.execute(Comment.sql_create) connection.commit() connection.close() form = dict( username='******', password='******', ) User.register_user(form) u, result = User.login_user(form) assert u is not None, result form = dict( username='******', password='******', ) User.register_user(form) session_id = random_string() form = dict( session_id=session_id, user_id=u.id, ) Session.new(form) s: Session = Session.one(session_id=session_id) assert s.session_id == session_id form = dict( title='test todo', user_id=u.id, ) t = Todo.add(form, u.id) assert t.title == 'test todo' form = dict( content='test weibo', user_id=u.id, ) w = Weibo.add(form, u.id) assert w.content == 'test weibo' form = dict( content='test comment', user_id=u.id, ) c = Comment.add(form, u.id, w.id) assert c.content == 'test comment'