def 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 redirect('/login')
    # 找到 user 发布的所有 weibo
    weibos = Tweet.find_all(user_id=user_id)
    log('weibos', weibos)

    def weibo_tag(weibo):
        return '<p>{} from {}@{} <a href="/tweet/delete?id={}">删除</a> <a href="/tweet/edit?id={}">修改</a></p>'.format(
            weibo.content,
            user.username,
            weibo.created_time,
            weibo.id,
            weibo.id,
        )

    weibos = '\n'.join([weibo_tag(w) for w in weibos])
    body = template('weibo_index.html', weibos=weibos)
    r = header + '\r\n' + body
    print(1)
    return r.encode(encoding='utf-8')
Beispiel #2
0
def route_login(request):
    # header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n'
    headers = {
        'Content-Type' : 'text/html',
    }
    username = current_user(request)
    if request.method == 'POST':
        form = request.form()
        u = User.new(form)
        # log('type of u', type(u))
        if u.valid_login():
            session_id = random_str()
            session[session_id] = u.username
            log(session)
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            result = '登录成功'
        else:
            result = '用户名或密码错误'
    else:
        result = ''
    body = templates('login.html')
    body = body.replace('{{result}}', result)
    body = body.replace('{{username}}', username)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #3
0
def edit(request):
    headers = {
        'Content-Type': 'text/html',
    }
    header = response_with_headers(headers)
    # r = header + '\r\n' + body
    # return r.encode(encoding='utf-8')
    pass
Beispiel #4
0
def new(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uid = current_user(request)
    header = response_with_headers(headers)
    user = User.find(uid)
    body = template('weibo_new.html')
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #5
0
def new(request):
    headers = {
        'Content-Type': 'text/html',
    }
    # uid = current_user(request)
    header = response_with_headers(headers)
    # user = User.find(uid)
    tid = request.query.get('tweet_id', -1)
    body = template('comment_new.html', tweet_id=tid)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
def delete(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uid = current_user(request)
    header = response_with_headers(headers)
    user = User.find(uid)
    # 删除微博
    weibo_id = request.query.get('id', None)
    weibo_id = int(weibo_id)
    w = Tweet.find(weibo_id)
    w.delete()
    return redirect('/weibo/index?user_id={}'.format(user.id))
def add(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uid = current_user(request)
    header = response_with_headers(headers)
    user = User.find(uid)
    # 创建微博
    form = request.form()
    w = Tweet(form)
    w.user_id = user.id
    w.save()
    return redirect('/weibo/index?user_id={}'.format(user.id))
def 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 = Tweet.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 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 redirect('/login')
    # 找到 user 发布的所有 weibo
    weibos = Weibo.find_all(user_id=user_id)
    body = template('weibo_index.html', weibos=weibos, user=user)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #10
0
def route_login(request):
    """
    登录页面的路由函数
    """
    # 设置headers
    headers = {
        'Content-Type': 'text/html',
    }
    log('login, cookies', request.cookies)
    if request.method == 'POST':
        # 调用 Request 类的 form 方法来处理 request 的 body 得到字典格式的body,
        # {'messages': 'goo', 'id': '22'}
        form = request.form()
        # 把 body 传给新实例化的u,这里的body其实就是用户登录输入的名字和密码
        u = User(form)
        # 验证u的登录是否合法
        if u.validate_login():
            # 在数据文件中找到登录用户的用户名
            user = User.find_by(username=u.username)
            # 设置 session(每次登录都会设置新的session给user.id)
            session_id = random_str()
            session[session_id] = user.id
            # 把代表用户名的session_id传给headers
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            log('headers response', headers)
            # 登录后定向到 /
            return redirect('/', headers)
    userid = current_user(request)
    if userid != -1:
        username = '******'
        result = '未登录或登录失败'
    else:
        user = User.find(id=userid)
        username = user.__repr__()
        result = '登录成功'
    # 显示登录页面
    # template函数接受一个路径和一系列参数,读取模板并渲染返回
    body = template('login.html')
    body = body.replace('{{result}}', result)
    body = body.replace('{{username}}', str(username))
    # 通过response_with_headers函数拼接响应头和headers
    header = response_with_headers(headers)
    # 拼接 header 和 body 形成一个完整的HTTP响应
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #11
0
def 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 = Tweet.find_all(user_id=user_id)
    for w in weibos:
        w.load_comments()
    log('weibos', weibos)
    body = template('weibo_index.html', weibos=weibos, user=user)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #12
0
def edit(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    if t.user_id != u.id:
        return redirect('/login')
    body = templates('todo_edit.html', todos=t)
    body = body.replace('{{todo_id}}', str(t.id))
    # print('t.id', t.id)
    body = body.replace('{{todo_title}}', str(t.title))
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #13
0
def index(request):
    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_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 = templates('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')