Beispiel #1
0
def route_index(request):
    header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n'
    body = templates('index.html')
    username = current_user(request)
    body = body.replace('{{username}}', username)
    r = header + '\r\n' + body
    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 admin_users(request):
    u = current_user(request)
    log('admin-users user data: ', u)
    if u is not None and u.is_admin():
        users = User.all()
        body = templates('users.html', users=users)
        return http_response(body)
    return redirect('/login')
Beispiel #4
0
def index(request):
    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)
    body = templates('todo_index.html', todos=todo_list)
    return http_response(body)
Beispiel #5
0
def route_admin_users(request):
    u = current_user(request)
    if u is not None and u.is_admin():
        us = User.all()
        body = templates('admin_user.html', users=us)
        return http_response(body)
    else:
        return redirect('/login')
Beispiel #6
0
def index(request):
    user_id = request.query.get('user_id', -1)
    user_id = int(user_id)
    log('user_id', user_id)
    user = User.find(user_id)
    if user is None:
        return redirect('/login')
    weibos = Weibo.find_all(user_id=user_id)
    body = templates('weibo_index.html', weibos=weibos, user=user)
    return http_response(body)
Beispiel #7
0
def route_register(request):
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_register():
            u.save()
            return redirect('/login')
        else:
            return redirect('/register')

    body = templates('register.html')
    return http_response(body)
Beispiel #8
0
def route_message(request):
    username = current_user(request)
    if request.method == 'POST':
        form = request.form()
        msg = Message.new(form)
        msg.save()
        message_list.append(msg)
    header = 'HTTP/1.1 200 F**K\r\nContent-Type: text/html\r\n'
    body = templates('html_basic.html')
    msgs = '<br/>'.join([str(m) for m in message_list])
    body = body.replace('{{messages}}', msgs)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Beispiel #9
0
def route_login(request):
    header = {}
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        if u.validate_login():
            user = User.find_by(username=u.username)
            session_id = random_str()
            session[session_id] = user.id
            header['set-cookie'] = 'user={}'.format(session_id)
            return redirect('/', headers=header)

    body = templates('login.html')
    return http_response(body)
Beispiel #10
0
def route_register(request):
    if request.method == 'POST':
        form = request.form()
        u = User.new(form)
        if u.valid_register():
            u.save()
            result = '注册成功<br> <pre>{}</pre>'.format(User.all())
        else:
            result = '用户名或密码长度必须大于2'
        print(type(result))
    else:
        result = ''
    body = templates('register.html', result=result)
    return http_response(body)
Beispiel #11
0
def route_register(request):
    header = 'HTTP/1.1 210 F**K OK\r\nContent-Type: text/html\r\n'
    if request.method ==  'POST':
        form = request.form()
        u = User.new(form)
        if u.valid_register():
            u.save()
            result = '注册成功<br> <pre>{}</pre>'.format(User.all())
        else:
            result = '用户名或密码长度必须大于2'
        print(type(result))
    else:
        result = ''
    body = templates('register.html')
    body = body.replace('{{result}}', result)
    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 route_login(request):
    # header = 'HTTP/1.1 210 VERY OK\r\nContent-Type: text/html\r\n'
    headers = {}
    if request.method == 'POST':
        form = request.form()
        u = User(form)
        # log('type of u', type(u))
        if u.valid_login():
            user = User.find_by(username=u.username)
            session_id = random_str()
            session[session_id] = user.id
            log('session', session)
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            result = '登录成功'
        else:
            result = '用户名或密码错误'
    else:
        result = ''
    body = templates('login.html', result=result)
    return http_response(body, headers=headers)
Beispiel #14
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')
Beispiel #15
0
def route_index(request):
    body = templates('index.html')
    return http_response(body)
Beispiel #16
0
def edit(request):
    todo = Todo.find_by(id=int(request.query.get('id')))
    log('edit todo :', todo)
    body = templates('simple_todo_edit.html', todo=todo)
    return http_response(body)
Beispiel #17
0
def edit(request):
    weibo = Weibo.find_by(id=int(request.query.get('id', None)))
    body = templates('weibo_edit.html', weibo=weibo)
    return http_response(body)
Beispiel #18
0
def new(request):
    user = current_user(request)
    body = templates('weibo_new.html')
    return http_response(body)
Beispiel #19
0
def index(request):
    user_id = int(request.query.get('user_id', -1))
    user = User.find_by(id=user_id)
    weibos = Weibo.find_all(user_id=user_id)
    body = templates('weibo_index.html', weibos=weibos, user=user)
    return http_response(body)
def edit(request):
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    body = templates('simple_todo_edit.html', todo=t)
    return http_response(body)
def index(request):
    todo_list = Todo.all()
    body = templates('simple_todo_index.html', todos=todo_list)
    return http_response(body)
Beispiel #22
0
def index(request):
    body = templates('weibo_index.html')
    return http_response(body)
Beispiel #23
0
def index(request):
    """
    主页的处理函数, 返回主页的响应
    """
    body = templates('todo_index.html')
    return http_response(body)