Exemple #1
0
def route_login(request):
    headers = {
        'Content-Type': 'text/html',
        # 'Set-Cookie': 'height=169; gua=1; pwd=2; Path=/',
    }
    # log('login, headers', request.headers)
    log('login, cookies', request.cookies)
    username = current_user(request)
    if request.method == 'POST':
        form = request.form()
        u = User.new(form)
        if u.validate_login():
            session_id = random_str()
            session[session_id] = u.username
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            # save username to cookie
            # headers['Set-Cookie'] = 'user={}'.format(u.username)
            result = '登录成功'
        else:
            result = '用户名或者密码错误'
    else:
        result = ''
    body = template('login.html')
    body = body.replace('{{result}}', result)
    body = body.replace('{{username}}', username)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    log('login response: ', r)
    return r.encode(encoding='utf-8')
Exemple #2
0
 def add_cookies(self):
     cookies = self.headers.get('Cookie', '')
     kvs = cookies.split('; ')
     log('cookie', kvs)
     for kv in kvs:
         if '=' in kv:
             k, v = kv.split('=')
             self.cookies[k] = v
Exemple #3
0
def route_admin_users(request):
    u = current_user(request)
    log('admin users', u)
    if u is not None and u.is_admin():
        us = User.all()
        body = template('admin_users.html', users=us)
        return http_response(body)
    else:
        return redirect('/login')
Exemple #4
0
 def find_by(cls, **kwargs):
     log('kwargs, ', kwargs, type(kwargs))
     k, v = '', ''
     for key, value in kwargs.items():
         k, v = key, value
     all = cls.all()
     for m in all:
         if v == m.__dict__[k]:
             return m
     return None
Exemple #5
0
 def find_all(cls, **kwargs):
     ms = []
     log('kwargs, ', kwargs, type(kwargs))
     k, v = '', ''
     for key, value in kwargs.items():
         k, v = key, value
     all = cls.all()
     for m in all:
         if v == m.__dict__[k]:
             ms.append(m)
     return ms
Exemple #6
0
def response_for_path(path):
    path, query = parsed_path(path)
    request.path = path
    request.query = query
    log('path and query', path, query)
    r = {'/static': route_dict}

    r.update(simpletodo_routes)
    r.update(user_routes)
    response = r.get(path, error)
    return response(request)
Exemple #7
0
def route_register(request):

    if request.method == 'POST':
        form = request.form()
        u = User.new(form)
        if u.validate_register():
            u.save()
            log('register success')
            return redirect('/login')
        else:
            return redirect('/register')
    body = template('register.html')
    return http_response(body)
Exemple #8
0
def route_login(request):

    headers = {}
    log('login, cookies', request.cookies)

    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
            headers['Set-Cookie'] = 'user={}'.format(session_id)
            log('headers response', headers)
            return redirect('/', headers)

    body = template('login.html')
    return http_response(body, headers=headers)
Exemple #9
0
 def save(self):
     models = self.all()
     if self.id is None:
         if len(models) == 0:
             self.id = 1
         else:
             m = models[-1]
             # log('m', m)
             self.id = m.id + 1
         models.append(self)
     else:
         # index = self.find(self.id)
         index = -1
         for i, m in enumerate(models):
             if m.id == self.id:
                 index = i
                 break
         log('debug', index)
         models[index] = self
     l = [m.__dict__ for m in models]
     path = self.db_path()
     save(l, path)
Exemple #10
0
def run(host, port):
    log('start at', '{}:{}'.format(host, port))
    with socket.socket() as s:
        s.bind((host, port))

        while True:
            s.listen(5)
            connection, address = s.accept()
            r = connection.recv(1024)
            r = r.decode(encoding='utf-8')
            log('ip and request: {}\n{}'.format(address, r))
            if len(r.split()) < 2:
                continue
            path = r.split()[1]
            request.method = r.split()[0]
            request.add_headers(r.split('\r\n\r\n', 1)[0].split('\r\n')[1:])
            request.body = r.split('\r\n\r\n', 1)[1]
            response = response_for_path(path)
            log('**debug', 'send all')
            connection.sendall(response)
            connection.close()