def route_login(request): headers = { 'Content-Type': 'text/html', } log('login, headers', request.headers, request.method) username = current_user(request) if request.method == 'POST': form = request.form() u = Users(form) if u.validate_login(): session_id = random_str() session[session_id] = u.username headers['Set-Cookie'] = 'user={}'.format(session_id) result = "登录成功" else: result = '没登录' else: result = '' log('result', result) body = template("login.html", result=result, username=username) header = response_with_headers(headers) r = header + '\r\n' + body log('body', body) return r.encode(encoding='utf-8')
def route_login(request): headers = { 'Content-Type': 'text/html', # 'Set-Cookie': 'user=gua; height=169', } username = current_user(request) if request.method == 'POST': # log('login, self.cookies', request.cookies) form = request.form() # log('login form', form) usr = User(form) # log('login usr', usr) if usr.validate_login(): # log('验证通过') # session 使 cookie 不能被轻易伪造 session_id = random_str() session[session_id] = usr.username headers['Set-Cookie'] = 'user={}'.format(session_id) result = '登录成功' # return redirect('/weibo?user_id={}'.format(str(usr.id))) else: result = '用户名或密码错误' else: result = '' body = template('login.html', result=result, username=username) # Set-Cookie 是在登录成功之后发送,所以在响应前生成 header header = response_with_header(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } # username = current_user(request) # if username == '游客': # # 没登录 不让看 重定向到 / # return redirect('/login') # else: header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) def weibo_tag(weibo): return '<p>{} from {}@{} <a href="/weibo/delete?id={}">删除</a></p>'.format( weibo.content, user.username, weibo.created_time, weibo.id, ) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_login(request): """ 登录页面的路由函数 """ headers = { 'Content-Type': 'text/html', } username = current_user(request) if request.method == 'POST': form = request.form() u = User(form) if u.validate_login(): session_id = random_str() session[session_id] = u.username headers['Set-Cookie'] = 'user={}'.format(session_id) result = '登录成功' # return redirect('/weibo/new') else: result = '用户名或者密码错误' else: result = '' body = template('login.html', result=result, username=username) header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_login(request): headers = { 'Content-Type': 'text/html', } log('login, cookies ({})'.format(request.cookies)) username = current_user(request) if request.method == 'POST': form = request.form() u = User(form) if u.validate_login(): session_id = random_str() session[session_id] = u.username headers['Set-Cookie'] = 'user={}'.format(session_id) result = '登陆成功' username = u.username else: result = '用户名或密码错误' else: result = '' body = template('login.html', result=result, username=username, ) header = response_with_headers(headers) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_blog(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) body = template('starbuzz/blog.html') r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user.id) # 任一 user 访问任一 index current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) # format 函数的字典用法 # 注意 u.id 是 current_user # user.username 是博主 w = { 'id': weibo.id, 'user_id': u.id, 'content': weibo.content, 'username': user.username, 'time': weibo.created_time, 'comments': comments, } # 手动处理 weibos 这个 list # 把每个 weibo 以 <p> 的形式展现在页面 return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a></p> <button class="weibo-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="weibo-comment-form weibo-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) # 用 join() 返回 str weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_index(request): """ PATH '/' """ header = 'HTTP/1.X 200 OK\r\nContent-Type: text/html\r\n' username = current_user(request) body = template('index.html', username=username) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_new(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) user = User.find_by(username=username) body = template('weibo_new.html') r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_index(request): """ 主页的处理函数, 返回主页的响应 """ header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' username = current_user(request) body = template('index.html', username=username) # body = body.replace('{{username}}', username) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) w = { "id": weibo.id, "user_id": u.id, "content": weibo.content, "username": user.username, "time": weibo.created_time, "comments": comments, } log('comments debug', comment_list) return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a> <button class="gua-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="gua-comment-form gua-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_profile(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) user = Users.find(username) body = template('profile.html', id=user.id, username=user.name, note=user.note) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_edit(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) weibo_id = request.query.get('id', -1) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) if w is None: return error(request) # 生成一个 edit 页面 body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_register(request): header = "HTTP/1.x 200 VERY OK\r\nContent-Type: text/html\r\n" if request.method == 'POST': form = request.form() u = Users(form) if u.validate_register(): u.save() result = "注册成功<br> <pre>{}</pre>".format(Users.all()) else: result = "用户名或者密码长度必须大于2" else: result = '' body = template("register.html", result=result) r = header + "\r\n" + body return r.encode(encoding='utf-8')
def route_weibo_new(request): """ 发表新微博的页面 在 form 里面写上微博内容 post 的 action 则是 /weibo/add 这个路由 """ headers = { 'Content-Type': 'text/html', } # username = current_user(request) header = response_with_header(headers) # user = User.find_by(username=username) body = template('weibo_new.html') r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_register(request): header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' if request.method == 'POST': form = request.form() u = User(form) if u.validate_register(): u.save() result = '注册成功<br> <pre>{}</pre>'.format(User.all()) else: result = '用户名或密码长度必须大于2' else: result = '' body = template('register.html', result=result) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) todos = Todo.all() def todo_tag(t): status = t.status() return '<p>{} {}@{} 状态: {} <a href="/todo/complete?id={}">完成</a></p>'.format( t.id, t.content, t.created_time, status, t.id) todo_html = '\n <br>\n '.join([todo_tag(w) for w in todos]) body = template('todo_index.html', todos=todo_html) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_register(request): header = 'HTTP/1.X 200 OK\r\nContent-Type: text/html\r\n' if request.method == 'POST': form = request.form() usr = User(form) if usr.validate_register(): # log('DEBUG-form-usr', usr) usr.save() result = 'True<br/> <pre>{}</pre>'.format(User.all()) else: result = 'Username and Password must longer than 3 words.' else: result = '' body = template('register.html', result=result) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_message(request): headers = { 'Content-Type': 'text/html', } log('本次请求的method', request.method) username = current_user(request) log('username', username) header = response_with_headers(headers) if request.method == "POST": form = request.form() message = Message(form) log('post', form) message_list.append(message) msgs = '<br>'.join(str(m) for m in message_list) body = template('html_basic.html', messages=msgs) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_message(request): """ 消息页面的路由函数 """ headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) if request.method == 'POST': form = request.form() msg = Message(form) message_list.append(msg) # 应该在这里保存 message_list msgs = '<br>'.join([str(m) for m in message_list]) body = template('html_basic.html', messages=msgs) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_edit(request): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) # 这个 query.get 是在 weibo_index路由 里面放上去的 # 用来指定要修改那一条微博 weibo_id = request.query.get('id', -1) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) if w is None: return error(request) # 生成一个 edit 页面 body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_profile(request): """ 如果登录了, 则返回一个页面显示用户的 三项资料(id, username, note) """ headers = { 'Content-Type': 'text/html', # 'Set-Cookie': 'height=169', # 'Location': '' } username = current_user(request) header = response_with_headers(headers) user = User.find_by(username=username) body = template('profile.html', id=user.id, username=user.username, note=user.note) r = header + '\r\n' + body return r.encode(encoding='utf-8') pass
def route_message(request): """ PATH '/messages' 留言板页面 """ headers = { 'Content-Type': 'text/html', # 'Set-Cookie': 'user=mike; height=169', # 'Location': '/message', } log('本次请求的 method 是', request.method) header = response_with_header(headers) if request.method == 'POST': form = request.form() msg = Message(form) message_list.append(msg) # 应该在这里保存 message_list msgs = '<br>'.join([str(m) for m in message_list]) body = template('html_basic.html', messages=msgs) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_index(): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) todos = Todo.all() def todo_tag(todo): status = todo.status() tag = ('<p class="{}">{} {} @ {}' '<a href="/todo/complete?id={}">完成</a></p>') return tag.format( status, todo.id, todo.content, todo.created_time, todo.id, ) todo_html = '\n'.join([todo_tag(todo) for todo in todos]) body = template('todo_index.html', todos=todo_html) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_message(request): """ 消息页面的路由函数 """ headers = { 'Content-Type': 'text/html', # 'Set-Cookie': 'height=169; gua=1; pwd=2; Path=/', # 'Location': '' } log('本次请求的 method', request.method) username = current_user(request) log('username', username) header = response_with_headers(headers) if request.method == 'POST': form = request.form() msg = Message(form) log('post', form) message_list.append(msg) # 应该在这里保存 message_list msgs = '<br>'.join([str(m) for m in message_list]) body = template('html_basic.html', messages=msgs) r = header + '\r\n' + body return r.encode(encoding='utf-8')