Esempio n. 1
0
def user_profile(request):
	us = User.find_by(username=current_user(request).username)
	body = template(
		'profile.html',
		id=str(getattr(us, 'id')),
		username=current_user(request).username,
		note=current_user(request).username
	)
	return http_response(body)
Esempio n. 2
0
 def wrapper(request):
     u = current_user(request)
     if u.id == -1:
         log('非用户登陆')
         return redirect('/login')
     else:
         return route_function(request)
Esempio n. 3
0
 def wrapper(request):
     # 验证管理员用户登陆
     u = current_user(request)
     if u.role == 1:
         return route_function(request)
     else:
         return redirect('/login')
Esempio n. 4
0
 def f(request):
     # 加载任意内容前先验证是否登陆
     uname = current_user(request)
     u = User.find_by(username=uname)
     if u is None:
         # 说明用户没登陆,重定向到 login 页面
         redirect('/login')
     return route_function(request)
Esempio n. 5
0
def todo_add(request):
    if request.method == 'POST':
        data = request.form()
        u = current_user(request)
        if len(data) > 0:
            t = Todo.new(data)
            t.user_id = u.id
            t.created_time = formatted_time(int(time.time()))
            t.updated_time = formatted_time(int(time.time()))
            t.save()
    return redirect('/todo')
Esempio n. 6
0
 def wrapper(request):
     u = current_user(request)
     if request.method == 'POST':
         data = request.form()
         todo_id = int(data.get('id', -1))
     else:
         todo_id = int(request.query.get('id', -1))
     t = Todo.find_by(id=todo_id)
     if u.id != t.user_id:
         return redirect('/login')
     else:
         return route_function(request)
Esempio n. 7
0
def add(request):
    # 添加事件前验证是否登陆
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    if request.method == 'POST':
        form = request.form()
        t = Todo.new(form)
        t.user_id = u.id
        t.created_time = current_time()
        t.save()
    '''
    我们看到页面刷新的过程:
    README.md 中的 网络部分 5. 请求数据和发送数据(具体到各阶段的原始报文信息)
    '''
    return redirect('/todo')
Esempio n. 8
0
def delete_todo(request):
    # 删除事件前验证是否登陆
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')

    todo_id = int(request.query.get('id'))
    t = Todo.find_by(id=todo_id)

    if t.user_id != u.id:
        return redirect('login')

    if t is not None:
        t.remove()

    return redirect('/todo')
Esempio n. 9
0
def index(request):
    # 加载主页前先验证是否登陆
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        # 说明用户没登陆,重定向到 login 页面
        return redirect('/login')

    headers = {
        'Content-Type': 'text/html'
    }
    todo_list = Todo.find_all(user_id=u.id)

    body = templateM('todo_index.html', todos=todo_list)

    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Esempio n. 10
0
def user_login(request):
	# header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n'
	# 仅添加content-type字段
	session = None
	if request.method == 'POST':
		form = request.form()
		username = form.get('username')
		password = form.get('password')
		if User.validate_login(username, password):
			u = User.find_by(username=username)
			# 向headers中添加set-cookie字段
			# 通过设置一个随机字符串生成session_id
			session_id = random_str()
			form = dict(
				session_id=session_id,
				user_id=u.id,
			)
			s = Session.new(form)
			s.save()
			session = {'Set-Cookie': 'session_id={}'.format(
				session_id
			)}
			result = '登录成功'
		else:
			result = '用户名或者密码错误'
			u = User.guest()
	else:
		result = ''
		u = current_user(request)

	body = template(
		'login.html',
		username=u.username,
		result=result
	)
	return http_response(body, session)
Esempio n. 11
0
def todo_index(request):
    body = template('todo_index.html',
                    todos=Todo.find_all(user_id=current_user(request).id))
    return http_response(body)