Example #1
0
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)
Example #2
0
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)
Example #3
0
def new(request):
    """
    添加新微博的页面, 路径为 /weibo/new
    """
    user = current_u(request)
    # 找到 user 发布的所有 weibo
    body = j_template('weibo_new.html', user=user)
    return http_response(body)
Example #4
0
def route_index(request):
    header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n'
    user_id = current_user_id(request)
    user = User.find_by(id=user_id)
    body = j_template('index.html', user=user)

    response = header + '\r\n' + body
    return response.encode(encoding='utf-8')
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)
Example #11
0
def index(request):
    """
    用户 weibo 的主页, 前往路径为 /weibo/index?user_id=1
    该页面不登录也可以访问
    """
    user_id = int(request.query.get('user_id', -1))
    user = User.find_by(id=user_id)
    if user is None:
        return redirect('/login')
    weibos = Weibo.find_all(user_id=user_id)
    # 找到 user 发布的所有 weibo
    body = j_template('weibo_index.html', user=user, weibos=weibos)
    return http_response(body)
Example #12
0
def index(request):
    """
    用户 weibo 的主页, 前往路径为 /weibo/index?user_id=1
    该页面不登录也可以访问
    """
    current = False
    user_id = int(request.query.get('user_id', -1))
    u_id = int(current_user_id(request))
    if u_id is not None and u_id == user_id:
        # 说明当前用户不是该微博的主人
        current = True
    user = User.find_by(id=user_id)
    weibos = Weibo.find_all(user_id=user_id)
    # 找到 user 发布的所有 weibo
    body = j_template('weibo_index.html', user=user, weibos=weibos, current=current)
    return http_response(body)
Example #13
0
def todo_index(request):
    """
    todo 首页函数
    """
    body = j_template('todo_index.html')
    return http_response(body)