本文整理汇总了Python中werkzeug.datastructures.CallbackDict.update方法的典型用法代码示例。如果您正苦于以下问题:Python CallbackDict.update方法的具体用法?Python CallbackDict.update怎么用?Python CallbackDict.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.datastructures.CallbackDict
的用法示例。
在下文中一共展示了CallbackDict.update方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UserInfo
# 需要导入模块: from werkzeug.datastructures import CallbackDict [as 别名]
# 或者: from werkzeug.datastructures.CallbackDict import update [as 别名]
class UserInfo(CombinedMultiDict, UserMixin):
"""Provide legacy implementation.
Methods that Flask-Login and Invenio 1.x expect user objects to have.
"""
def __init__(self, uid=None, force=False):
"""Retrieve information about user."""
def on_update(self):
"""Change own status when the user info is modified."""
self.modified = True
self.modified = False
self.uid = uid
self.req = self._get_request_info()
acc = {}
if uid is not None and uid > 0:
data = self._login(uid, force)
acc = self._precache(data, force)
else:
data = self._create_guest()
self.info = CallbackDict(data, on_update)
# FIXME remove req after everybody start using flask request.
CombinedMultiDict.__init__(self, [self.req, self.info, acc, dict(CFG_USER_DEFAULT_INFO)])
self.save()
def get_key(self):
"""Generate key for caching user information."""
key = "current_user::" + str(self.uid)
return key
def get_acc_key(self):
"""Generate key for caching authorizations."""
remote_ip = str(request.remote_addr) if has_request_context() else "0"
return "current_user::" + str(self.uid) + "::" + remote_ip
def save(self):
"""Save modified data permanently for logged users."""
if not self.is_guest and self.modified:
timeout = current_app.config.get("CFG_WEBSESSION_EXPIRY_LIMIT_DEFAULT", 0) * 3600
cache.set(self.get_key(), dict(self.info), timeout=timeout)
def reload(self):
"""Reload user login information and saves them."""
data = self._login(self.uid, force=True)
acc = self._precache(data, force=True)
self.info.update(data)
CombinedMultiDict.__init__(self, [self.req, self.info, acc, dict(CFG_USER_DEFAULT_INFO)])
self.save()
def update_request_info(self):
"""Update request information."""
self.req = self._get_request_info()
def _get_request_info(self):
"""Get request information."""
# FIXME: we should support IPV6 too. (hint for FireRole)
data = {}
if has_request_context():
data["remote_ip"] = request.remote_addr or ""
data["remote_host"] = request.environ.get("REMOTE_HOST", "")
data["referer"] = request.referrer
data["uri"] = request.environ["PATH_INFO"] or ""
data["agent"] = request.headers.get("User-Agent", "N/A")
return data
def _create_guest(self):
# Minimal information about user.
return {"settings": {}, "id": 0, "uid": 0}
def _login(self, uid, force=False):
"""Get account information about currently logged user from database.
Should raise an exception when session.uid is not valid User.id.
"""
data = cache.get(self.get_key())
if not force and data is not None:
return data
from invenio_accounts.models import User
data = {}
try:
user = User.query.get(uid)
data["id"] = data["uid"] = user.id or -1
data["nickname"] = user.nickname or ""
data["given_names"] = user.given_names or ""
data["family_name"] = user.family_name or ""
data["email"] = user.email or ""
data["note"] = user.note or ""
data["group"] = map(lambda x: x.group.name, getattr(user, "groups", []))
data.update(user.settings or {})
data["settings"] = user.settings or {}
data["guest"] = str(int(user.guest)) # '1' or '0'
self.modified = True
#.........这里部分代码省略.........
示例2: UserInfo
# 需要导入模块: from werkzeug.datastructures import CallbackDict [as 别名]
# 或者: from werkzeug.datastructures.CallbackDict import update [as 别名]
class UserInfo(CombinedMultiDict, UserMixin):
"""Provide legacy implementation.
Methods that Flask-Login and Invenio 1.x expect user objects to have.
"""
def __init__(self, uid=None, force=False):
"""Retrieve information about user."""
def on_update(self):
"""Change own status when the user info is modified."""
self.modified = True
self.modified = False
self.uid = uid
self.req = self._get_request_info()
acc = {}
if uid is not None and uid > 0:
data = self._login(uid, force)
acc = self._precache(data, force)
else:
data = self._create_guest()
self.info = CallbackDict(data, on_update)
# FIXME remove req after everybody start using flask request.
CombinedMultiDict.__init__(self, [self.req, self.info, acc,
dict(CFG_USER_DEFAULT_INFO)])
self.save()
def get_key(self):
"""Generate key for caching user information."""
key = 'current_user::' + str(self.uid)
return key
def get_acc_key(self):
"""Generate key for caching authorizations."""
remote_ip = str(request.remote_addr) if has_request_context() else '0'
return 'current_user::' + str(self.uid) + '::' + remote_ip
def save(self):
"""Save modified data permanently for logged users."""
if not self.is_guest and self.modified:
timeout = current_app.config.get(
'CFG_WEBSESSION_EXPIRY_LIMIT_DEFAULT', 0) * 3600
cache.set(self.get_key(), dict(self.info),
timeout=timeout)
def reload(self):
"""Reload user login information and saves them."""
data = self._login(self.uid, force=True)
acc = self._precache(data, force=True)
self.info.update(data)
CombinedMultiDict.__init__(self, [self.req, self.info, acc,
dict(CFG_USER_DEFAULT_INFO)])
self.save()
def update_request_info(self):
"""Update request information."""
self.req = self._get_request_info()
def _get_request_info(self):
"""Get request information."""
# FIXME: we should support IPV6 too. (hint for FireRole)
data = {}
if has_request_context():
data['remote_ip'] = request.remote_addr or ''
data['remote_host'] = request.environ.get('REMOTE_HOST', '')
data['referer'] = request.referrer
data['uri'] = request.environ['PATH_INFO'] or ''
data['agent'] = request.headers.get('User-Agent', 'N/A')
return data
def _create_guest(self):
# Minimal information about user.
return {'settings': {}, 'id': 0, 'uid': 0}
def _login(self, uid, force=False):
"""Get account information about currently logged user from database.
Should raise an exception when session.uid is not valid User.id.
"""
data = cache.get(self.get_key())
if not force and data is not None:
return data
from invenio_accounts.models import User
data = {}
try:
user = User.query.get(uid)
data['id'] = data['uid'] = user.id or -1
data['nickname'] = user.nickname or ''
data['given_names'] = user.given_names or ''
data['family_name'] = user.family_name or ''
data['email'] = user.email or ''
data['note'] = user.note or ''
data['group'] = map(lambda x: x.group.name,
getattr(user, 'groups', []))
data.update(user.settings or {})
#.........这里部分代码省略.........
示例3: UserInfo
# 需要导入模块: from werkzeug.datastructures import CallbackDict [as 别名]
# 或者: from werkzeug.datastructures.CallbackDict import update [as 别名]
class UserInfo(CombinedMultiDict, UserMixin):
"""
This provides legacy implementations for the methods that Flask-Login
and Invenio 1.x expects user objects to have.
"""
def __init__(self, uid=None, force=False):
"""Retreave information about user."""
def on_update(self):
""" Changes own status when the user info is modified. """
self.modified = True
self.modified = False
self.uid = uid
self.req = self._get_request_info()
acc = {}
if uid is not None and uid > 0:
data = self._login(uid, force)
acc = self._precache(data, force)
else:
data = self._create_guest()
self.info = CallbackDict(data, on_update)
# FIXME remove req after everybody start using flask request.
CombinedMultiDict.__init__(self, [self.req, self.info, acc,
dict(CFG_USER_DEFAULT_INFO)])
self.save()
def get_key(self):
"""Generates key for caching user information."""
key = 'current_user::' + str(self.uid)
return key
def get_acc_key(self):
"""Generates key for caching autorizations."""
remote_ip = str(request.remote_addr) if has_request_context() else '0'
return 'current_user::' + str(self.uid) + '::' + remote_ip
def save(self):
"""Save modified data pernamently for logged users."""
if not self.is_guest and self.modified:
timeout = current_app.config.get(
'CFG_WEBSESSION_EXPIRY_LIMIT_DEFAULT', 0)*3600
cache.set(self.get_key(), dict(self.info),
timeout=timeout)
def reload(self):
"""Reload user login information and saves them."""
data = self._login(self.uid, force=True)
acc = self._precache(data, force=True)
self.info.update(data)
CombinedMultiDict.__init__(self, [self.req, self.info, acc,
dict(CFG_USER_DEFAULT_INFO)])
self.save()
def update_request_info(self):
self.req = self._get_request_info()
def _get_request_info(self):
"""Get request information."""
# FIXME: we should support IPV6 too. (hint for FireRole)
data = {}
if has_request_context():
data['remote_ip'] = request.remote_addr or ''
data['remote_host'] = request.environ.get('REMOTE_HOST', '')
data['referer'] = request.referrer
data['uri'] = request.environ['PATH_INFO'] or ''
data['agent'] = request.headers.get('User-Agent', 'N/A')
return data
def _create_guest(self):
data = {'settings': {}}
if current_app.config.get(
'CFG_WEBSESSION_DIFFERENTIATE_BETWEEN_GUESTS', False):
from invenio.ext.sqlalchemy import db
from invenio.modules.accounts.models import User
note = '1' if current_app.config.get(
'CFG_ACCESS_CONTROL_LEVEL_GUESTS', 0) == 0 else '0'
u = User(email='', note=note, password='guest')
db.session.add(u)
db.session.commit()
data.update(u.__dict__)
else:
# Minimal information about user.
data['id'] = data['uid'] = 0
return data
def _login(self, uid, force=False):
"""Get account information about currently logged user from database.
Should raise an exception when session.uid is not valid User.id.
"""
data = cache.get(self.get_key())
if not force and data is not None:
return data
from invenio.modules.accounts.models import User
#.........这里部分代码省略.........