1 Star 0 Fork 0

aspirefhaha / udisk_man_web

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
app.py 7.41 KB
Copy Edit Web IDE Raw Blame History
aspirefhaha authored 2016-08-25 22:11 . add change disk name function
# -*- coding: utf-8 -*-
import web, middleware
from web.contrib.template import render_jinja
from models import *
from forms import *
import datetime
import json
import sys
#from models import *
default_encoding='utf-8'
if sys.getdefaultencoding() != default_encoding :
reload(sys)
sys.setdefaultencoding(default_encoding)
urls = (
'/index','index',
'/test','test',
'/form','showform',
'/login','login',
'/logout','logout',
'/udisk/(\d+)/name','udiskname',
'/udisk/(\d+)/usearea','udiskarea',
'/udisk/(\d+)/usetime','udisktime',
'/udisk/(\d+)/curloc','udiskloc',
'/udisk','udisk',
'/(.*)','index'
)
#allowed = (('admin','123123'),)
#web.debug=False
app = web.application(urls,globals())
app.add_processor(middleware.set_orm)
if web.config.get("_session") is None:
from web import utils
store = web.session.DiskStore('sessions')
user = utils.Storage({
"id": "",
"name": "",
"email": "",
"privilege": "",
})
session = web.session.Session(app, store,
initializer={
"status": 0,
"user": user,
})
web.config._session = session
else:
session = web.session.Session(app, web.session.DiskStore('sessions'))
render = render_jinja (
'templates',
encoding = 'utf-8',
)
class BaseView(object):
def __init__(self):
self.db = web.ctx.orm
class index:
def GET(self,name = None):
if not name:
name = 'World'
return render.index(welcomeinformation = '欢迎使用')
#def GET(self):
# return render.index(welcomeinformation = '欢迎使用')
class udiskname(BaseView):
def POST(self,index):
tdisk=self.db.query(GDisk).filter("id="+index).one()
webinput=web.input()
print "new diskname:",webinput.get("name")
tdisk.name=webinput.get("name")
self.db.flush()
return json.dumps({"result":"success"})
class udiskarea(BaseView):
def GET(self,index):
tdisk=self.db.query(GDisk).filter("id="+index).one()
return json.dumps({"index":tdisk.id,"areatype":tdisk.areatype,"areadata":tdisk.area})
def POST(self,index):
tdisk=self.db.query(GDisk).filter("id="+index).one()
webinput=web.input()
#print webinput
tdisk.areatype=webinput.get("areatype")
tdisk.area=webinput.get("areadata")
#print webinput.get("areadata")
#print tdisk.area
self.db.flush()
return json.dumps({"result":"success"})
class udiskloc(BaseView):
def GET(self,index):
tdisk=self.db.query(GDisk).filter("id="+index).one()
retloc = json.loads(tdisk.curloc)
return json.dumps({"diskloc":retloc,"index":tdisk.id})
def POST(self,index):
tdisk=self.db.query(GDisk).filter("id="+index).one()
webinput=web.input()
lat = webinput.get("lat")
lng = webinput.get("lng")
tloc = {"lat":lat,"lng":lng}
tdisk.curloc= json.dumps(tloc)
print tdisk.curloc
self.db.flush()
return json.dumps({"result":"success"})
class udisktime(BaseView):
def GET(self,index):
print "index",index
tdisk = self.db.query(GDisk).filter("id="+index).one()
print "id",tdisk.id
print dir(tdisk)
print dir(tdisk.starttime)
print tdisk.starttime
print tdisk.endtime
return json.dumps({"starttime":tdisk.starttime.strftime("%m/%d/%Y %H:%M"),
"endtime":tdisk.endtime.strftime("%m/%d/%Y %H:%M"),
"index":tdisk.id})
def POST(self,index):
#print index
#print web.data()
#print web.input()
#timedata=json.loads(web.data())
starttime=web.input().get("starttime")
endtime = web.input().get("endtime")
#print starttime
tdisk = self.db.query(GDisk).filter("id="+index).one()
tdisk.starttime = datetime.datetime.strptime(starttime,"%m/%d/%Y %H:%M")
tdisk.endtime=datetime.datetime.strptime(endtime,"%m/%d/%Y %H:%M")
self.db.flush()
return json.dumps({"result":"success"})
class udisk(BaseView):
def GET(self):
tdisks = self.db.query(GDisk).all()
objs=[]
for disk in tdisks:
tdisk={"name":disk.name,"address":disk.address,"index":disk.id}
objs.append(tdisk)
#obj = [{'name':'unknown','address':'11:22:33:44:55:66'},{'name':'真绿盘','address':'11:22:33:44:55:66'}]
encodedjson = json.dumps(objs)
return encodedjson
def POST(self):
i = web.input()
newdisk = GDisk(name=i.get("name"),address=i.get("address"))
self.db.add(newdisk)
self.db.flush()
return json.dumps({"result":"success"})
class test(BaseView):
def GET(self):
print 'test'
print dir(session)
#print session.logged_in
print session.get('logged_in')
islogged = False
if session.get('logged_in'):
islogged = True
else :
print web.cookies()
cookieslogged_in = web.cookies().get("logged_in")
print cookieslogged_in
print cookieslogged_in is not None
print cookieslogged_in == True
print True == True
if ((cookieslogged_in is not None) and cookieslogged_in):
islogged = True
if islogged:
print "has logged_in"
query = self.db.query(User)
return render.test(usercount=query.count())
print "not logged_in"
raise web.seeother("/login")
class login(BaseView):
"""docstring for login"""
def __init__(self, arg=None):
super(login, self).__init__()
self.arg = arg
def GET(self):
return render.login()
def POST(self):
i = web.input()
username = i.get('username')
passwd = i.get("passwd")
adminuser,createresult = get_or_create(self.db,User,name=username.decode('utf8'))
print adminuser , createresult
if createresult==False :
allowed = (adminuser.name,adminuser.password)
print allowed
if (username,calcPassword(passwd)) == allowed:
print 'allowed'
session.logged_in = True
print dir(session)
web.setcookie('logged_in', True, 3600)
raise web.seeother('/test')
else:
print 'not allowed'
return render.login()
else :
return '<h1>Login Error!!!</h1><a rel="nofollow">Login</a>'
class logout(object):
"""docstring for logout"""
def __init__(self, arg=None):
super(logout, self).__init__()
self.arg = arg
def GET(self):
session.logged_in = False
web.setcookie('logged_in',False,-1)
raise web.seeother("/login")
class showform(BaseView):
def __init__(self):
super(showform, self).__init__()
form = UserForm()
self.fs = form.write_render(self)
def GET(self):
return render.form(form=self.fs)
def POST(self):
data = web.input()
self.fs.rebind(data=data)
if self.fs.validate():
self.fs.sync()
return "Success"
return render.form(form=self.fs)
if __name__ == '__main__':
app.run()

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/fhaha/udisk_man_web.git
git@gitee.com:fhaha/udisk_man_web.git
fhaha
udisk_man_web
udisk_man_web
master

Search