[英]Variable casting in Tornado
我有那些變量:
PERM_READ = 0x01
PERM_WRITE = 0x02
PERM_CMDS = 0x04
以及檢查網絡或傳感器上的許可的功能。 確定什么是用戶許可的功能。
這是功能:
def check_network_access(self, network, access=None, raise_err=True):
if access is None:
access = PERM_READ
else:
assert(access > 0)
assert(access <= (PERM_READ + PERM_WRITE + PERM_CMDS))
usr = self.get_current_user()
usr_id = usr['id']
# Does the network exist?
net = self.get_network(network)
if not net:
raise tornado.web.HTTPError(404, "not found")
self.lock_tables('read', ['nets_permissions'])
perms = self.db.get("SELECT * FROM nets_permissions WHERE network_id=%s AND \
user_id=%s", network, usr_id)
self.unlock_tables()
if (not perms) or ((perms['perm'] & access) != access):
if (raise_err):
raise tornado.web.HTTPError(403, "access forbidden")
else:
return False
return True
現在例如當我使用此處理程序調用頁面時出現錯誤:
class NetworkStatusHandler(BaseHandler):
# Requires authentication
@tornado.web.authenticated
def get(self, nid):
# Retrieve the current user
usr = self.get_current_user()
usr_id = usr['id']
perm = self.db.query("SELECT n.perm FROM nets_permissions as n \
WHERE n.network_id=%s AND n.user_id=%s", nid, int(usr_id))
# Check whether the user has access to the network
perms = self.check_network_access(nid, perm)
net = self.get_network(nid)
# Render the networks page
self.render("networkstatus.html", net=net)
我的錯誤是:
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 988, in _execute
getattr(self, self.request.method.lower())(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 1739, in wrapper
return method(self, *args, **kwargs)
File "./wsn.py", line 663, in get
perms = self.check_network_access(nid, perm)
File "./wsn.py", line 250, in check_network_access
assert(access <= (PERM_READ + PERM_WRITE + PERM_CMDS))
AssertionError
我認為將變量perm傳遞給check_network_access函數時,這是一個問題。
感謝您的幫助!
您需要使用self.db.get
而不是self.db.query
。 類似於以下內容的東西應該起作用:
class NetworkStatusHandler(BaseHandler):
# Requires authentication
@tornado.web.authenticated
def get(self, nid):
# Retrieve the current user
usr = self.get_current_user()
usr_id = usr['id']
row = self.db.get("SELECT n.perm FROM nets_permissions as n \
WHERE n.network_id=%s AND n.user_id=%s", nid, int(usr_id))
perm = row['n.perm']
# Check whether the user has access to the network
perms = self.check_network_access(nid, perm)
net = self.get_network(nid)
# Render the networks page
self.render("networkstatus.html", net=net)
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.