def index(request): """ 返回 Todolist 主页, 若请求没有 session 则添加一个 Set-Cookie 字段 """ body = template('index.html') u = current_user(request) s = current_session(request) if u is None and s is None: # 为游客的 cookie 有效期设置为 1 个月 headers = add_session_headers(expired_month=1) return http_response(body, headers) return http_response(body)
def comment_edit(request): comment_id = int(request.query.get('id', -1)) c = Comment.find(comment_id) user = current_user(request) # 生成一个 edit 页面 body = template('comment_edit.html', comment=c, user=user) return http_response(body)
def route_index(request): user_id = current_user_id(request) user = User.find_by(id=user_id) users = User.all() body = j_template('index.html', user=user, users=users) return http_response(body)
def edit_password(request): user_id = int(request.query.get('id', -1)) log('edit_password', user_id) u = User.find(user_id) # 替换模板文件中的标记字符串 body = template('admin_password_edit.html', user=u) return http_response(body)
def route_message(request): """ 主页的处理函数, 返回主页的响应 """ log('from route_message -->本次请求的 method', request.method) if request.method == 'POST': form = request.form() msg = Message.new(form) # 增加一个存储功能 from kiwi # msg 和 message_list是2个不同的东西 # msg是Message类的一个实例,msg.save()会将数据存入txt中 # message_list是临时定义的空列表,其中元素是msg实例,每次启动会清零 # log('msg: type ', type(msg)) # log('msg: str ', str(msg)) # log('msg: ', msg) log('post', form) # 应该在这里保存 message_list # 列表推倒 # 注意str(m) msgs = '<br>'.join([str(m) for m in Message.all()]) # 上面的列表推倒相当于下面的功能 # messages = [] # for m in message_list: # messages.append(str(m)) # msgs = '<br>'.join(messages) body = template('html_basic.html', messages=msgs) return http_response(body)
def index(request): """ 主页的处理函数, 返回主页的响应 """ u = current_user(request) body = template('weibo_index.html') return http_response(body)
def route_login(request): """ 登录页面的路由函数 """ log('login, cookies', request.cookies) if request.method == 'POST': form = request.form() u = User(form) if u.validate_login(): session_id = random_str() u = User.find_by(username=u.username) s = Session.new(dict( session_id=session_id, user_id=u.id, )) s.save() log('session', s) headers = { 'Set-Cookie': 'sid={}'.format(session_id) } # 登录后定向到 / return redirect('/', headers) # 显示登录页面 body = template('login.html') return http_response(body)
def route_login(request): """ 登录页面的路由函数 """ headers = { 'Content-Type': 'text/html', # 'Set-Cookie': 'a=b; c=d' } # username 默认是有课, session_id 默认是 '' username = current_user(request) if request.method == 'POST': # 得到 post 过来的 body 数据 # 说明是第一次登录 form = request.form() # 根据发送来的数据创建一个对象, 和数据库里面的对象比较 u = User(form) if u.validate_login(): session_id = random_str() session[session_id] = u.username # session 变为 {'fdsafeaf1213': '游客注册的用户名'} headers['Set-Cookie'] = f'user={session_id}' result = '登录成功' else: result = '用户名或者密码错误' else: # Get 请求, 打开这个页面的时候的处理 result = '' body = j_template('login.html', username=username, result=result) # 拼接 header return http_response(body, headers=headers)
def edit(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find(weibo_id) user = current_user(request) # 生成一个 edit 页面 body = template('weibo_edit.html', weibo=w, user=user) return http_response(body)
def new(request): """ 添加新微博的页面, 路径为 /weibo/new """ user = current_u(request) # 找到 user 发布的所有 weibo body = j_template('weibo_new.html', user=user) return http_response(body)
def route_index(request): """ 主页的处理函数, 返回主页的响应 """ u = current_user(request) log('current user', u) body = template('index.html', username=u.username) return http_response(body)
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 route_profile(request): u = current_user(request) body = '<h1>id:{} ' \ 'username: {} ' \ 'note: {}</h1>'.format(u.id, u.username, u.note) return http_response(body)
def edit(request): """ 更新 weibo 的主页, 前往路径为 /weibo/edit?user_id=1 """ weibo_id = int(request.query.get('id', -1)) weibo = Weibo.find_by(id=weibo_id) # 找到 user 发布的所有 weibo body = j_template('weibo_edit.html', weibo=weibo) return http_response(body)
def index(request): author_id = int(request.query.get('user_id', -1)) user = current_user(request) if author_id == -1: author_id = user.id weibos = Weibo.all() body = template('weibo_index.html', weibos=weibos, user=user) return http_response(body)
def edit(request): """ todo 首页的路由函数 """ todo_id = int(request.query.get('id')) t = Todo.find_by(id=todo_id) # 替换模板文件中的标记字符串 body = template('todo_edit.html', todo=t) return http_response(body)
def edit(request): u = current_user(request) tweet_id = int(request.query.get('id', -1)) t = Tweet.find(tweet_id) if u.id == t.user_id: body = template('tweet_edit.html', tweet_id=t.id, tweet_content=t.content) return http_response(body) return redirect('/tweet/index?user_id={}'.format(u.id))
def register(request): if request.method == 'POST': form = request.form() if User.validate_register(form): return redirect('/login') else: return redirect('/register') else: body = render_template('register.html') return http_response(body)
def comment_edit(request): u = current_user(request) comment_id = int(request.query.get('id', -1)) c = Comment.find(comment_id) if u.id == c.user_id: body = template('comment_edit.html', comment_id=c.id, comment_content=c.content) return http_response(body) return redirect('/tweet/index?user_id={}'.format(u.id))
def todo_edit(request): """ 编辑页面显示 """ todo_id = request.query.get('id', -1) if todo_id == -1: # 没找到, 返回错误页面 return error(request) t = Todo.find_by(id=int(todo_id)) body = j_template('todo_edit.html', todo=t) return http_response(body)
def new(request): """ 添加新微博的页面, 路径为 /weibo/new?user_id=1 """ user_id = int(current_user_id(request)) user = User.find_by(id=user_id) if user is None: return redirect('/login') # 找到 user 发布的所有 weibo body = j_template('weibo_new.html', user=user) return http_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 route_profile(request): """ 显示用户的个人信息界面 判断, 如果没有登录, 重定向到登录页面登录 若登录了就返回个人信息 """ username = current_user(request) u = User.find_by(username=username) body = j_template('profile.html', user=u) return http_response(body)
def route_index(request): """ 主页的处理函数, 返回主页的响应 """ body = template('index.html', username='******') # 增加用户识别功能,并在主页显示名字 user = current_user(request) log('routes_index ----> check current_user 返回值的type: ', user) if user is not None: body = template('index.html', username=user.username) return http_response(body)
def route_register(request): if request.method == 'POST': form = request.form() u = User(form) if u.validate_register() is not None: result = f'注册成功<br> <pre>{User.all()}</pre>' else: result = '用户名或者密码长度必须大于2' else: result = '' body = j_template('register.html', result=result) return http_response(body)
def index_(request): """ 主页的处理函数, 返回主页的响应 """ u = current_user(request) log('uuu',u) if u is None: username = '******' else: username = u.username body = template('index.html', username=username) return http_response(body)
def user_login(request): """ 登录页面的路由函数 """ if request.method == 'POST': form = request.form() if User.validate_login(form): user = User.find_by(username=form['username']) headers = add_session_headers(user, expired_month=1) return redirect('/', headers) body = template('login.html') return http_response(body)
def login(request): if request.method == 'POST': form = request.form() u = User.validate_login(form) if u is not None: sid = str(uuid.uuid4()) kwargs = dict(sid=sid, user_id=u.id) Session.new(**kwargs) headers = {'Set-Cookie': 'sid={}'.format(sid)} return redirect('/', headers) body = render_template('login.html') return http_response(body)
def user_register(request): """ 注册页面的路由函数 """ if request.method == 'POST': form = request.form() if User.register(form): return redirect('/user/login') else: return redirect('/register') body = template('register.html') return http_response(body)
def comment_edit(request): u = current_user(request) comment_id = int(request.query.get('id', -1)) comment = Comment.find_by(id=comment_id) # 若当前用户为 comment 的作者,允许修改 if comment.user_id == u.id: body = template('comment_edit.html', id=comment.id, content=comment.content) return http_response(body) # 否则重定向到用户微博的主页 else: return redirect('/weibo/index?user_id={}'.format(comment.user_id))