def update(request): """ 用于增加新 todo 的路由函数 """ form = request.form() TodoAjax.update(form) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo/index')
def add(request): """ 用于增加新 todo 的路由函数 """ u = current_user(request) form = request.form() TodoAjax.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/todo/index')
def add(request): u = current_user(request) form = request.json() log('ajax todo add', form, u) t = TodoAjax.add(form, u.id) message = dict(message='{} added succeed'.format(t.title)) return json_response(message)
def test_data(): SQLModel.init_db() form = dict( username='******', password='******', role=UserRole.normal, ) u, result = User.register(form) Session.add(u.id) form = dict( title='test todo', user_id=u.id, ) t = TodoAjax.new(form) form = dict( content='test_weibo', user_id=u.id, ) w = Weibo.new(form) form = dict( content='test_comment', user_id=u.id, weibo_id=1, ) w = Comment.new(form)
def add(request): # 测试ajax time.sleep(5) u = current_user(request) form = request.json() t = TodoAjax.add(form, u.id) message = dict(message='成功添加 {}'.format(t.title)) return json_response(message)
def index(request): """ todo 首页的路由函数 """ u = current_user(request) todos = TodoAjax.all(user_id=u.id) # 替换模板文件中的标记字符串 return html_response('todo_index.html', todos=todos)
def add(request): """ 用于增加新 todo 的路由函数 """ u = current_user(request) form = request.json() log('ajax todo add', form, u) t = TodoAjax.add(form, u.id) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 message = dict(message='成功添加 {}'.format(t.title)) return json_response(message)
def f(request): log('same_user_required') u = current_user(request) if 'id' in request.query: todo_id = request.query['id'] else: todo_id = request.form()['id'] t = TodoAjax.one(id=int(todo_id)) if t.user_id == u.id: return route_function(request) else: return redirect('/todo/index')
def fake_data(): SQLModel.init_db() Test.new({}) form = dict( username='******', password='******', role=UserRole.normal, ) u, result = User.register(form) Session.add(u.id) form = dict(title='test todo ajax', ) t = TodoAjax.add(form, u.id)
def fake_data(): SQLModel.init_db() Test.new({}) form = dict( username='******', password='******', role=UserRole.normal, ) u, result = User.register(form) Session.add(u.id) form = dict(title='test todo ajax', ) todo = Todo.add(form, u.id) t = TodoAjax.add(form, u.id) form = dict(content='test weibo', ) weibo = Weibo.add(form, u.id) comment = Comment.add(form, u.id)
def all(request): todos = [t.__dict__ for t in TodoAjax.all()] return json_response(todos)
def edit(request): todo_id = int(request.query['id']) t = TodoAjax.one(id=todo_id) return html_response('todo_edit.html', todo=t)
def delete(request): todo_id = int(request.query['id']) TodoAjax.delete(todo_id) return redirect('/todo/index')
def all(request): log('ajax request json', request.body) todos = TodoAjax.all() todos = [t.__dict__ for t in todos] # log('todos all <{}>'.format(todos)) return json_response(todos)
def fake_data(): log('fake data') SQLModel.init_db() Test.new({}) form1 = dict( username='******', password='******', role=UserRole.normal, ) form2 = dict( username='******', password='******', role=UserRole.normal, ) u2, result = User.register(form1) u1, result = User.register(form2) Session.add(u1.id) Session.add(u2.id) form = dict(title='test todo ajax', ) t = TodoAjax.add(form, u1.id) # weibo form1 = dict( content="weibo test user zjt", user_id=u1.id, ) form2 = dict( content="weibo test user abc", user_id=u2.id, ) w1 = Weibo.add(form1, u1.id).id w2 = Weibo.add(form2, u2.id).id w1 = Weibo.add(form1, u2.id).id w2 = Weibo.add(form2, u1.id).id # comment form1 = dict( content="weibo test user zjt", user_id=u1.id, weibo_id=w1, ) form2 = dict( content="weibo test user abc", user_id=u2.id, weibo_id=w1, ) # comment form3 = dict( content="weibo test user zjt", user_id=u1.id, weibo_id=w2, ) form4 = dict( content="weibo test user abc", user_id=u2.id, weibo_id=w2, ) Comment.new(form1) Comment.new(form2) Comment.new(form3) Comment.new(form4) SQLModel.connection.close()