webpy注册登陆

index.py

 

#coding=utf-8
import web
import datetime
from web import form
web.config.debug = False
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

urls=('/','Index',
      '/reg','Reg',
      '/login','Login',
      '/logout','Logout',
      '/showusers','Showusers',
      )
app=web.application(urls,globals())
render = web.template.render('templates',base='base')
session = web.session.Session(app, web.session.DiskStore('sessions'),initializer={'login':0,})

db1=web.database(dbn='mysql', 
    db='ideploy1', 
    user='ideploy1',
    host='10.13.38.43',
    pw='123456',
    port=3306,)

###首页
class Index:
    def GET(self):
        #return web.ctx.ip
        return datetime.datetime.now().strftime('%Y%m%d%H%M%S%S')

###注册页
class Reg:
    vusername= form.regexp(r".{3,20}$", '用户名长度为3-20位')
    vpass = form.regexp(r".{6,20}$", '密码长度为6-20位')
    vemail = form.regexp(r".*@.*", "must be a valid email address")
    regForm=web.form.Form(
    form.Textbox('username',vusername,description=u'用户名'),
    form.Password("password",vpass,description=u"密码"),
    form.Password("password2", description=u"确认密码"),
    form.Button(u"马上注册", type="submit", description="submit"),
    validators = [
        form.Validator("两次输入的密码不一致", lambda i: i.password == i.password2)]
    )


    
    def GET(self):
        return render.reg(self.regForm)

    def POST(self):
        formdata=web.input();
        username=web.net.websafe(formdata.username)
        password=web.net.websafe(formdata.password)
        regip=web.ctx.ip
        regdate=datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        if not self.regForm.validates():
            return render.reg(self.regForm)
        #判断用户名是否存在:
        else:
                if len(getUserByUserName(username))>0:
                    return render.reg(self.regForm,u'用户名已存在')
                else:
                    ret=addUser(username,password,regip,regdate)
                    if ret==0:
                        #return u'注册成功'
                        raise web.seeother('/showusers')
                    else:
                        return u'注册失败'



###登陆页面
class Login:
    loginForm=form.Form(form.Textbox('username',description=u'用户名'),
                        form.Password("password",description=u"密码"),
                        form.Button(u"马上登陆", type="submit", description="submit"),)
    def GET(self):
        if logged():
            return 'you are logged'
        else:
            return render.login(self.loginForm)
    def POST(self):
        postdata=web.input()
        username=web.net.websafe(postdata.username)
        password=web.net.websafe(postdata.password)
        rslist=getUserByUserName(username)
        if len(rslist)==0:
            return render.login(self.loginForm,'用户名不存在')
        else:
            if password==rslist[0].password:                
                session.login=1
                #session.login222=1
                return  '登陆成功,欢迎你:'+username+':'+str(session.login222)
            else:
                return render.login(self.loginForm,'用户名及密码不匹配')
         
###注销登陆页面        
class Logout:
    def GET(self):
        session.login=0
        session.kill()
        return '注销成功'

###显示用户列表
class Showusers:
    def GET(self):
        if logged():
            usersList=getUsers()
            return render.users(usersList)
        else:
            return '对不起 您没有登陆无权查看'

###判断用户是否登陆
def logged():
    if session.login==1:
        return True
    else:
        return False


 

################################################ 数据库操作部分 begin ############################################################         

#增加用户
def addUser(username,password,regdate,regip,isactive='0'):
    ret=db1.insert('t_user',username=username ,password=password,regdate=regdate,regip=regip,isactive=isactive)
    return ret

#获取用户列表
def getUsers(limit=10000,orderby='regdate'):
    rs=db1.select('t_user',limit=limit,order=orderby)
    rslist=[]
    for r in rs:
        rslist.append(r)
    return rslist

#根据用户名查询用户信息
def getUserByUserName(username):
    rs=db1.select('t_user',where='username="'+username+'"')
    rslist=[]
    for r in rs:
        rslist.append(r)
    return rslist    
        
################################################ 数据库操作部分 end ############################################################  
 

if __name__=="__main__":
    app.run()


 

base.html

$def with (page)
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<script src="/static/jquery-1.9.1.js"></script>
<link href="/static/base.css" rel="stylesheet" type="text/css" />
<title>hello world</title>
</head>
<body>
<div id="container">
<div id="Header"> 
</div>
<div id="PageBody">$:page</div>
<div id="Footer"> 
<a href='showusers'>查看用户</a>|<a href='reg'>继续注册</a>|
</div>

</body>
</html>


users.htm

$def with (users)
<table>
<tr><td>用户名</td><td>密码</td></tr>
$for i in users:
	<tr><td>$i.username</td><td>$i.password</td><td>$i.regip</td><td>$i.regdate</td></tr>

</table>


reg.htm

$def with(form,message='')
<form method="POST" action='/reg'>
		$:message
    $:form.render()
</form>


login.htm

$def with(form,message='')
<form method="POST" action='/login'>
		$:message
    $:form.render()
</form>


 

 

 

 

 

你可能感兴趣的:(webpy注册登陆)