def index(request): ''' todo 首页 ''' headers = { "Content": "text/html", } uname = current_user(request) u = User.find_by(username=uname) if u is None: log("** ERROR ** 未登录 跳转") return redirect('/login') todo_list = Todo.find_all(user_id=u.id) todo_html = ''.join(['<h3>{0}(user_id: {1}): {2} <a href='\ '"/todo/edit?id={0}">编辑</a> <a href='\ '"/todo/delete?id={0}">删除</a></h3>'. format(t.id, t.user_id, t.title, ) for t in todo_list]) if not todo_html: todo_html = '<br><b3>暂无TODO记录!</b3>' body = template('todo_index.html') body = body.replace('{{username}}', u.username) body = body.replace('{{todos}}', todo_html) header = response_with_header(headers) r = header + "\r\n" + body return r.encode(encoding="utf-8")
def index(request): """ todo 首页的路由函数 """ u = current_user(request) ts = Todo.find_all(user_id=u.id) return html_response('todo_index.html', todos=ts)
def index(request): """ todo 首页的路由函数 """ u = current_user(request) todo_list = Todo.find_all(user_id=u.id) # 下面这行生成一个 html 字符串 todo_html = """ <h3> {} : {} <a href="/todo/edit?id={}">编辑</a> <a href="/todo/delete?id={}">删除</a> <div> 创建时间:{}</div> <div> 最后更新时间:{}</div> </h3> """ todo_html = ''.join([ todo_html.format(t.id, t.title, t.id, t.id, formatted_time(t.created_time), formatted_time(t.updated_time)) for t in todo_list ]) # 替换模板文件中的标记字符串 body = template('todo_index.html') body = body.replace('{{todos}}', todo_html) # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中 headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) r = header + '\r\n' + body return r.encode()
def index(request): # todo 首页的路由函数 headers = { 'Content-Type': 'text/html', } uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') todo_list = Todo.find_all( user_id=u.id ) # 查找Todo数据库里 todo类的user_id属性和当前用户相等的Todo实例 即属于此用户的Todo数据 # 返回的是一个list包含符合条件的所有对象实例 # 下面这行生成一个 html 字符串 # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title) # for t in todo_list]) # 上面一行列表推倒的代码相当于下面几行 todos = [] for t in todo_list: edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id) delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id) s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.title, edit_link, delete_link) todos.append(s) todo_html = ''.join(todos) # 替换模板文件中的标记字符串 body = template('todo_index.html') body = body.replace('{{todos}}', todo_html) # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中 header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def index(request): headers = {'Content-Type': 'text/html'} uname = current_user(request) user = User.find_by(username=uname) todo_lists = Todo.find_all(user_id=user.id) log('todo lists: ', todo_lists) todos = [] for todo in todo_lists: edit_link = '<a href="/todo/edit?id={}">Edit</a>'.format(todo.id) delete_link = '<a href="/todo/delete?id={}">Remove</a>'.format(todo.id) created_time = time.strftime('%m/%d %H:%M', time.localtime(todo.create_time)) updated_time = time.strftime('%m/%d %H:%M', time.localtime(todo.update_time)) time_sch = '<sup><small>create by {} update by {}</small></sup>'.format( created_time, updated_time) t = '<h3>{} : {} {} {} </h3>{}'.format(todo.id, todo.title, edit_link, delete_link, time_sch) todos.append(t) todo_html = ''.join(todos) header = response_with_headers(headers, code=200) body = template('todo_index.html') body = body.replace('{{todos}}', todo_html) response = header + '\r\n' + body log('todo response: ', response) return response.encode('utf-8')
def todo_index(request): """ todo 首页函数 """ headers = { 'Content-Type': 'text/html', } # 找到当前登录的用户, 如果没有登录, 就 redirect 到 /login username = current_user(request) u = User.find_by(username=username) todo_list = Todo.find_all(user_id=u.id) # 生成 todo list 的 HTML 字段 todos = [] for i, t in enumerate(todo_list): # 第几个 task 直接用 index 来定位, 不需要新建一个 task_id 来存储 edit_link = f'<a href="/todo/edit?id={t.id}">编辑</a>' delete_link = f'<a href="/todo/delete?id={t.id}">删除</a>' s = f'<h3>{i+1} : {t.title} {edit_link} {delete_link}</h3>' todos.append(s) todo_html = ''.join(todos) body = template('todo_index.html') body = body.replace('{{todos}}', todo_html) header = response_with_headers(headers) response = header + '\r\n' + body return response.encode('utf-8')
def index(request): """ 主页的处理函数, 返回主页的响应 """ u = current_user(request) todo_list = Todo.find_all(user_id=u.id) body = template('todo_index.html', todos=todo_list) return http_response(body)
def all(request): """ 返回当前用户的的所有 todo """ u_id = current_user_id(request) todo_list = Todo.find_all(user_id=u_id) todos = [t.json_parse() for t in todo_list] return json_response(todos)
def index(request): """ todo 首页的路由函数 """ u = current_user(request) todos = Todo.find_all(user_id=u.id) # 替换模板文件中的标记字符串 body = GuaTemplate.render('todo_index.html', todos=todos) return html_response(body)
def index(request): """ todo 首页的路由函数 """ u = current_user(request) todo_list = Todo.find_all(user_id=u.id) # 下面这行生成一个 html 字符串 body = utils_template('todo_index.html', todos=todo_list) return html_response(body)
def todo_index(request): """ todo 首页函数 """ # 找到当前登录的用户, 如果没有登录, 就 redirect 到 /login u = current_u(request) todos = Todo.find_all(user_id=u.id) body = j_template('todo_index.html', todos=todos) return http_response(body)
def index(request): headers = { 'Content-Type': 'text/html', } # 以下代码 是选择 加载所有的todo 还是 某个用户专属的todo u = current_user(request) # todo_list = To_do.all() todo_list = Todo.find_all(user_id=u.id, deleted=False) # 如果删除就不现实出来 body = template('todo_index.html', todos=todo_list) header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def index(request): """ todo 首页的路由函数 """ # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login uname = current_user(request) u = User.find_by(username=uname) if u is None: return redirect('/login') todos = Todo.find_all(user_id=u.id) body = template('todo_index.html', todos=todos) return http_response(body)
def index(): """ 显示该用户所有todo :return: 显示todo页面 """ user = current_user() todo_list = Todo.find_all(user_id=user.id, deleted=False) # 用字典对每个todo进行token和user.id的匹配 # 保证每次调用index函数时清空gg gg.delete_value() # 保证每次调用index函数时都有新的token可用 gg.set_value(user.id) log('from todo', gg.csrf_tokens, gg.token) body = render_template('todo_index.html', todos=todo_list, token=gg.token) return make_response(body)
def index(request): """ 主页的处理函数, 返回主页的响应 """ header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n' session_id = request.cookies.get('user', '') user_id = session.get(session_id) todo_list = Todo.find_all(user_id=user_id) # 判断如果 user_id 为1 ,即用户为管理员用户账号,则展示所有的用户信息,否则展示其他用户对应信息 # 如果未登录状态,则为空 # if user_id == 1: # todo_list = Todo.all() log('index debug', todo_list) body = template('simple_todo_index.html', todos=todo_list) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def index(req: Request, res: Response): """ todo 首页的路由函数 """ try: user_id = int(req.query['userId']) except: return res.json({ 'code': 400, 'msg': '参数错误', }) todos = Todo.find_all(user_id=user_id) res.json({ 'code': 0, 'msg': '', 'data': [t.json() for t in todos], })
def index(request): """ todo 首页的路由函数 """ u = current_user(request) # todos = Todo.all() todos = Todo.find_all(user_id=u.id) # 下面这行生成一个 html 字符串 # 课5作业4,添加了时间的显示 todo_html = """ <h3> 添加时间: {}, 更新时间:{} <br> {} : {} <a href="/todo/edit?id={}">编辑</a> <a href="/todo/delete?id={}">删除</a> </h3> """ # 课5作业4,添加了时间的显示 import datetime def dt(t): return datetime.datetime.fromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S') todo_html = ''.join([ todo_html.format( dt(t.created_time), dt(t.updated_time), t.id, t.title, t.id, t.id ) for t in todos ]) # 替换模板文件中的标记字符串 body = template('todo_index.html') body = body.replace('{{todos}}', todo_html) # 下面 3 行可以改写为一条函数, 还把 headers 也放进函数中 headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) r = header + '\r\n' + body return r.encode()
def todo_add(request): """ 用于增加 todo 的路由函数 """ headers = { 'Content-Type': 'text/html', } username = current_user(request) u = User.find_by(username=username) if request.method == 'POST': form = request.form() t = Todo(form) todos = Todo.find_all(user_id=u.id) Todo.add(t, u.id) # 客户端发送了数据服务器处理完毕以后, 数据被写入数据库 # 重定向到 /todo 页面, 相当于刷新页面, 重新发送 请求到 todo 页面, 然后该页面的路由处理 # 先 post 到了 /todo/add, 然后 302 重定向到 /todo # redirect 保证了 只在 /todo 页面查看数据 return redirect('/todo')
def index(request): """ todo 首页 的路由函数 """ # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login uid = current_user(request) todo_list = Todo.find_all(user_id=uid) todo_html = '' if todo_list is not None: # 下面生成一个 html 字符串 todos = [] for t in todo_list: edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id) delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id) s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.task, edit_link, delete_link) todos.append(s) todo_html = ''.join(todos) # 替换模板文件中的标记字符串 body = template('todo_index.html') body = body.replace('{{ todos }}', todo_html) return http_response(body)
def all(request): u = current_user(request) todos = Todo.find_all(user_id=u.id) todos = [t.json() for t in todos] return json_response(todos)
def index(request): log('routes todo index') u = current_user(request) todos = Todo.find_all(user_id=u.id) return html_response('todo_index.html', todos=todos)
def index(request): u = request.login_user todo_list = Todo.find_all(user_id=u.id) return http_response( render_template('todo_index.html', todo_list=todo_list))
def index(request): u = current_user(request) todo_list = Todo.find_all(user_id=u.id) body = template('todo_index.html', todos=todo_list, user=u) return http_response(body)
def index(): u = session.get('user_name', None) if u is None: return redirect('/') todo_list = Todo.find_all(username = u) return render_template('todo_index.html', todos=todo_list,username = u)