本文整理汇总了Python中MaKaC.common.cache.GenericCache类的典型用法代码示例。如果您正苦于以下问题:Python GenericCache类的具体用法?Python GenericCache怎么用?Python GenericCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GenericCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RHRoomBookingMapOfRoomsWidget
class RHRoomBookingMapOfRoomsWidget(RHRoomBookingBase):
def __init__(self, *args, **kwargs):
RHRoomBookingBase.__init__(self, *args, **kwargs)
self._cache = GenericCache('MapOfRooms')
def _checkParams(self):
RHRoomBookingBase._checkParams(self, request.args)
self._room_id = request.args.get('roomID')
def _process(self):
key = str(sorted(dict(request.args, lang=session.lang, user=session.user.getId()).items()))
html = self._cache.get(key)
if not html:
default_location = Location.default_location
aspects = [a.to_serializable() for a in default_location.aspects]
buildings = default_location.get_buildings()
html = WPRoomBookingMapOfRoomsWidget(self,
aspects=aspects,
buildings=buildings,
room_id=self._room_id,
default_repeat='{}|0'.format(RepeatFrequency.NEVER),
default_start_dt=datetime.combine(date.today(),
Location.working_time_start),
default_end_dt=datetime.combine(date.today(),
Location.working_time_end),
repeat_mapping=RepeatMapping.getMapping()).display()
self._cache.set(key, html, 3600)
return html
示例2: _send_confirmation
def _send_confirmation(self, email):
token_storage = GenericCache('confirm-email')
data = {'email': email, 'user_id': self.user.id}
token = make_unique_token(lambda t: not token_storage.get(t))
token_storage.set(token, data, 24 * 3600)
GenericMailer.send(make_email(email, template=get_template_module('users/emails/verify_email.txt',
user=self.user, email=email, token=token)))
示例3: _get_user_data
def _get_user_data(self):
user_id = request.args.get('user')
if user_id is None:
return {}
elif user_id.isdigit():
# existing indico user
user = User.find_first(id=user_id, is_deleted=False)
return {t.name: getattr(user, t.name, None) if user else '' for t in PersonalDataType}
else:
# non-indico user
data = GenericCache('pending_identities').get(user_id, {})
return {t.name: data.get(t.name) for t in PersonalDataType}
示例4: send_login_info
def send_login_info(user, event=None):
token_storage = GenericCache('resetpass')
endpoint = 'event.confLogin-resetPassword' if event else 'user.signIn-resetPassword'
idList = user.getIdentityList()
logins = []
for id in idList:
if not hasattr(id, 'setPassword'):
config = Config.getInstance()
extra_message = config.getAuthenticatorConfigById(id.getAuthenticatorTag()).get("ResetPasswordMessage")
msg = _("Sorry, you are using an externally managed account (%s) to login into Indico.") % id.getLogin()
if extra_message:
msg += "\n" + extra_message
logins.append({
'tag': id.getAuthenticatorTag(),
'login': id.getLogin(),
'error': msg
})
else:
tag = id.getAuthenticatorTag()
login = id.getLogin()
data = {'tag': tag, 'login': login}
token = str(uuid.uuid4())
while token_storage.get(token):
token = str(uuid.uuid4())
token_storage.set(token, data, 6*3600)
url = url_for(endpoint, event, token=token, _external=True, _secure=True)
logins.append({
'tag': tag,
'login': login,
'link': url
})
if not logins:
url = urlHandlers.UHUserDetails.getURL(user)
text = _("Sorry, we did not find your login.\nPlease, create one here:\n%s") % url
else:
text = _("You can use the following links within the next six hours to reset your password.")
for entry in logins:
text += "\n\n==================\n"
if 'link' in entry:
text += _("Click below to reset your password for the %s login '%s':\n") % (entry['tag'],
entry['login'])
text += entry['link']
else:
text += entry['error']
text += "\n==================\n"
maildata = {
"fromAddr": "Indico Mailer <%s>" % Config.getInstance().getNoReplyEmail(),
"toList": [user.getEmail()],
"subject": _("[%s] Login Information") % getSubjectIndicoTitle(),
"body": text
}
GenericMailer.send(GenericNotification(maildata))
示例5: has_member
def has_member(self, user):
cache = GenericCache('group-membership')
key = '{}:{}:{}'.format(self.provider, self.name, user.id)
rv = cache.get(key)
if rv is not None:
return rv
elif self.group is None:
warn('Tried to check if {} is in invalid group {}'.format(user, self))
rv = False
else:
rv = any(x[1] in self.group for x in user.iter_identifiers(check_providers=True, providers={self.provider}))
cache.set(key, rv, 1800)
return rv
示例6: _get_user_data
def _get_user_data(self):
user_id = request.args.get("user")
if user_id is None:
return {}
elif user_id.isdigit():
# existing indico user
user = User.find_first(id=user_id, is_deleted=False)
user_data = {t.name: getattr(user, t.name, None) if user else "" for t in PersonalDataType}
else:
# non-indico user
data = GenericCache("pending_identities").get(user_id, {})
user_data = {t.name: data.get(t.name) for t in PersonalDataType}
user_data["title"] = get_title_uuid(self.regform, user_data["title"])
return user_data
示例7: get
def get(cls, *args, **kwargs):
"""Create and return a serializable Report object, retrieved from cache if possible"""
from indico_piwik.plugin import PiwikPlugin
if not PiwikPlugin.settings.get('cache_enabled'):
return cls(*args, **kwargs).to_serializable()
cache = GenericCache('Piwik.Report')
key = u'{}-{}-{}'.format(cls.__name__, args, kwargs)
report = cache.get(key)
if not report:
report = cls(*args, **kwargs)
cache.set(key, report, PiwikPlugin.settings.get('cache_ttl'))
return report.to_serializable()
示例8: CachedReport
class CachedReport(object):
"""
This class acts as a wrapper for functions which return a report object,
by decorating the get<report> methods with the memonize function in the
BaseStatisticsReport object, the result is wrapped here and its age
is compared with the TTL if in the cache, either returning said item or
allowing the method to generate a new one.
"""
_config = StatisticsConfig()
def __init__(self, function):
self._function = function
# Cache bucket per implementation
plugin = function.__module__.split('.')[3]
self._cache = GenericCache(plugin + 'StatisticsCache')
def getReport(self, *args, **kwargs):
"""
Ascertain if live updating first, if so disregard and continue.
"""
ttl = self._config.getUpdateInterval()
if not self._config.hasCacheEnabled():
return self._function(*args, **kwargs)
keyParams = list(args)
keyParams.extend([self._function.__module__, self._function.__name__])
key = self._generateKey(keyParams)
resource = self._cache.get(key, None)
if not resource:
result = self._function(*args, **kwargs)
self._cache.set(key, result, ttl)
return result
else:
return resource
def _generateKey(self, params):
"""
Generates a unique key for caching against the params given.
"""
return reduce(lambda x, y: str(x) + '-' + str(y), params)
示例9: principal_from_fossil
def principal_from_fossil(fossil, allow_pending=False, allow_groups=True, legacy=True, allow_missing_groups=False,
allow_emails=False, allow_networks=False):
from indico.modules.networks.models.networks import IPNetworkGroup
from indico.modules.groups import GroupProxy
from indico.modules.users import User
type_ = fossil['_type']
id_ = fossil['id']
if type_ == 'Avatar':
if isinstance(id_, int) or id_.isdigit():
# regular user
user = User.get(int(id_))
elif allow_pending:
data = GenericCache('pending_identities').get(id_)
if not data:
raise ValueError("Cannot find user '{}' in cache".format(id_))
data = {k: '' if v is None else v for (k, v) in data.items()}
email = data['email'].lower()
# check if there is not already a (pending) user with that e-mail
# we need to check for non-pending users too since the search may
# show a user from external results even though the email belongs
# to an indico account in case some of the search criteria did not
# match the indico account
user = User.find_first(User.all_emails.contains(email), ~User.is_deleted)
if not user:
user = User(first_name=data.get('first_name') or '', last_name=data.get('last_name') or '',
email=email,
address=data.get('address', ''), phone=data.get('phone', ''),
affiliation=data.get('affiliation', ''), is_pending=True)
db.session.add(user)
db.session.flush()
else:
raise ValueError("Id '{}' is not a number and allow_pending=False".format(id_))
if user is None:
raise ValueError('User does not exist: {}'.format(id_))
return user.as_avatar if legacy else user
elif allow_emails and type_ == 'Email':
return EmailPrincipal(id_)
elif allow_networks and type_ == 'IPNetworkGroup':
group = IPNetworkGroup.get(int(id_))
if group is None:
raise ValueError('IP network group does not exist: {}'.format(id_))
return group
elif allow_groups and type_ in {'LocalGroupWrapper', 'LocalGroup'}:
group = GroupProxy(int(id_))
if group.group is None:
raise ValueError('Local group does not exist: {}'.format(id_))
return group.as_legacy_group if legacy else group
elif allow_groups and type_ in {'LDAPGroupWrapper', 'MultipassGroup'}:
provider = fossil['provider']
group = GroupProxy(id_, provider)
if group.group is None and not allow_missing_groups:
raise ValueError('Multipass group does not exist: {}:{}'.format(provider, id_))
return group.as_legacy_group if legacy else group
else:
raise ValueError('Unexpected fossil type: {}'.format(type_))
示例10: principal_from_fossil
def principal_from_fossil(fossil, allow_pending=False, legacy=True):
"""Gets a GroupWrapper or AvatarUserWrapper from a fossil"""
type_ = fossil['_type']
id_ = fossil['id']
if type_ == 'Avatar':
if isinstance(id_, int) or id_.isdigit():
# regular user
user = User.get(int(id_))
elif allow_pending:
data = GenericCache('pending_identities').get(id_)
if not data:
raise ValueError("Cannot find user '{}' in cache".format(id_))
data = {k: '' if v is None else v for (k, v) in data.items()}
# check if there is not already a pending user with that e-mail
user = User.find_first(email=data['email'], is_pending=True)
if not user:
user = User(first_name=data['first_name'], last_name=data['last_name'], email=data['email'],
address=data.get('address', ''), phone=data.get('phone', ''),
affiliation=data.get('affiliation', ''), is_pending=True)
db.session.add(user)
db.session.flush()
else:
raise ValueError("Id '{}' is not a number and allow_pending=False".format(id_))
if user is None:
raise ValueError('User does not exist: {}'.format(id_))
return user.as_avatar if legacy else user
elif type_ == 'LocalGroupWrapper':
group = GroupProxy(int(id_))
if group.group is None:
raise ValueError('Local group does not exist: {}'.format(id_))
return group.as_legacy_group if legacy else group
elif type_ == 'LDAPGroupWrapper':
provider = fossil['provider']
group = GroupProxy(id_, provider)
if group.group is None:
raise ValueError('Multipass group does not exist: {}:{}'.format(provider, id_))
return group.as_legacy_group if legacy else group
else:
raise ValueError('Unexpected fossil type: {}'.format(type_))
示例11: wrapper
def wrapper(*args, **kwargs):
cache = GenericCache('task-locks')
name = current_task.name
if cache.get(name):
Logger.get('celery').warning('Task {} is locked; not executing it'.format(name))
return
cache.set(name, True)
try:
return f(*args, **kwargs)
finally:
cache.delete(name)
示例12: SudsCache
class SudsCache(Cache):
_instance = None
def __init__(self, duration=DEFAULT_CACHE_TTL):
self._cache = GenericCache("SudsCache")
self._duration = duration
def get(self, key):
self._cache.get(key)
def put(self, key, val):
self._cache.set(key, val, self._duration)
def purge(self, key):
self._cache.delete(key)
示例13: SudsCache
class SudsCache(Cache):
_instance = None
@classmethod
def getInstance(cls, duration=None):
if cls._instance is None:
cls._instance = SudsCache(duration)
return cls._instance
def __init__(self, duration=None):
self._cache = GenericCache("SudsCache")
if duration is None:
duration = 24 * 3600 # we put as default 1 day cache
self._duration = duration
def get(self, key):
self._cache.get(key)
def put(self, key, val):
self._cache.set(key, val, self._duration)
def purge(self, key):
self._cache.delete(key)
示例14: handler
def handler(req, **params):
ContextManager.destroy()
logger = Logger.get('httpapi')
path, query = req.URLFields['PATH_INFO'], req.URLFields['QUERY_STRING']
if req.method == 'POST':
# Convert POST data to a query string
queryParams = dict(req.form)
for key, value in queryParams.iteritems():
queryParams[key] = [str(value)]
query = urllib.urlencode(remove_lists(queryParams))
else:
# Parse the actual query string
queryParams = parse_qs(query)
dbi = DBMgr.getInstance()
dbi.startRequest()
minfo = HelperMaKaCInfo.getMaKaCInfoInstance()
if minfo.getRoomBookingModuleActive():
Factory.getDALManager().connect()
apiKey = get_query_parameter(queryParams, ['ak', 'apikey'], None)
cookieAuth = get_query_parameter(queryParams, ['ca', 'cookieauth'], 'no') == 'yes'
signature = get_query_parameter(queryParams, ['signature'])
timestamp = get_query_parameter(queryParams, ['timestamp'], 0, integer=True)
noCache = get_query_parameter(queryParams, ['nc', 'nocache'], 'no') == 'yes'
pretty = get_query_parameter(queryParams, ['p', 'pretty'], 'no') == 'yes'
onlyPublic = get_query_parameter(queryParams, ['op', 'onlypublic'], 'no') == 'yes'
onlyAuthed = get_query_parameter(queryParams, ['oa', 'onlyauthed'], 'no') == 'yes'
# Get our handler function and its argument and response type
hook, dformat = HTTPAPIHook.parseRequest(path, queryParams)
if hook is None or dformat is None:
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
# Disable caching if we are not just retrieving data (or the hook requires it)
if req.method == 'POST' or hook.NO_CACHE:
noCache = True
ak = error = result = None
ts = int(time.time())
typeMap = {}
try:
session = None
if cookieAuth:
session = getSessionForReq(req)
if not session.getUser(): # ignore guest sessions
session = None
if apiKey or not session:
# Validate the API key (and its signature)
ak, enforceOnlyPublic = checkAK(apiKey, signature, timestamp, path, query)
if enforceOnlyPublic:
onlyPublic = True
# Create an access wrapper for the API key's user
aw = buildAW(ak, req, onlyPublic)
# Get rid of API key in cache key if we did not impersonate a user
if ak and aw.getUser() is None:
cacheKey = normalizeQuery(path, query,
remove=('ak', 'apiKey', 'signature', 'timestamp', 'nc', 'nocache',
'oa', 'onlyauthed'))
else:
cacheKey = normalizeQuery(path, query,
remove=('signature', 'timestamp', 'nc', 'nocache', 'oa', 'onlyauthed'))
if signature:
# in case the request was signed, store the result under a different key
cacheKey = 'signed_' + cacheKey
else:
# We authenticated using a session cookie.
if Config.getInstance().getCSRFLevel() >= 2:
token = req.headers_in.get('X-CSRF-Token', get_query_parameter(queryParams, ['csrftoken']))
if session.csrf_token != token:
raise HTTPAPIError('Invalid CSRF token', apache.HTTP_FORBIDDEN)
aw = AccessWrapper()
if not onlyPublic:
aw.setUser(session.getUser())
userPrefix = 'user-' + session.getUser().getId() + '_'
cacheKey = userPrefix + normalizeQuery(path, query,
remove=('nc', 'nocache', 'ca', 'cookieauth', 'oa', 'onlyauthed',
'csrftoken'))
# Bail out if the user requires authentication but is not authenticated
if onlyAuthed and not aw.getUser():
raise HTTPAPIError('Not authenticated', apache.HTTP_FORBIDDEN)
obj = None
addToCache = not hook.NO_CACHE
cache = GenericCache('HTTPAPI')
cacheKey = RE_REMOVE_EXTENSION.sub('', cacheKey)
if not noCache:
obj = cache.get(cacheKey)
if obj is not None:
result, extra, ts, complete, typeMap = obj
addToCache = False
if result is None:
# Perform the actual exporting
res = hook(aw, req)
if isinstance(res, tuple) and len(res) == 4:
result, extra, complete, typeMap = res
else:
result, extra, complete, typeMap = res, {}, True, {}
#.........这里部分代码省略.........
示例15: handler
def handler(req, **params):
ContextManager.destroy()
logger = Logger.get("httpapi")
path, query = req.URLFields["PATH_INFO"], req.URLFields["QUERY_STRING"]
if req.method == "POST":
# Convert POST data to a query string
queryParams = dict(req.form)
for key, value in queryParams.iteritems():
queryParams[key] = [str(value)]
query = urllib.urlencode(remove_lists(queryParams))
else:
# Parse the actual query string
queryParams = parse_qs(query)
dbi = DBMgr.getInstance()
dbi.startRequest()
minfo = HelperMaKaCInfo.getMaKaCInfoInstance()
if minfo.getRoomBookingModuleActive():
Factory.getDALManager().connect()
apiKey = get_query_parameter(queryParams, ["ak", "apikey"], None)
cookieAuth = get_query_parameter(queryParams, ["ca", "cookieauth"], "no") == "yes"
signature = get_query_parameter(queryParams, ["signature"])
timestamp = get_query_parameter(queryParams, ["timestamp"], 0, integer=True)
noCache = get_query_parameter(queryParams, ["nc", "nocache"], "no") == "yes"
pretty = get_query_parameter(queryParams, ["p", "pretty"], "no") == "yes"
onlyPublic = get_query_parameter(queryParams, ["op", "onlypublic"], "no") == "yes"
onlyAuthed = get_query_parameter(queryParams, ["oa", "onlyauthed"], "no") == "yes"
# Get our handler function and its argument and response type
hook, dformat = HTTPAPIHook.parseRequest(path, queryParams)
if hook is None or dformat is None:
raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
# Disable caching if we are not just retrieving data (or the hook requires it)
if req.method == "POST" or hook.NO_CACHE:
noCache = True
ak = error = result = None
ts = int(time.time())
typeMap = {}
try:
sessionUser = getSessionForReq(req).getUser() if cookieAuth else None
if apiKey or not sessionUser:
# Validate the API key (and its signature)
ak, enforceOnlyPublic = checkAK(apiKey, signature, timestamp, path, query)
if enforceOnlyPublic:
onlyPublic = True
# Create an access wrapper for the API key's user
aw = buildAW(ak, req, onlyPublic)
# Get rid of API key in cache key if we did not impersonate a user
if ak and aw.getUser() is None:
cacheKey = normalizeQuery(
path, query, remove=("ak", "apiKey", "signature", "timestamp", "nc", "nocache", "oa", "onlyauthed")
)
else:
cacheKey = normalizeQuery(
path, query, remove=("signature", "timestamp", "nc", "nocache", "oa", "onlyauthed")
)
if signature:
# in case the request was signed, store the result under a different key
cacheKey = "signed_" + cacheKey
else:
# We authenticated using a session cookie.
# Reject POST for security reasons (CSRF)
if req.method == "POST":
raise HTTPAPIError("Cannot POST when using cookie authentication", apache.HTTP_FORBIDDEN)
aw = AccessWrapper()
if not onlyPublic:
aw.setUser(sessionUser)
userPrefix = "user-" + sessionUser.getId() + "_"
cacheKey = userPrefix + normalizeQuery(
path, query, remove=("nc", "nocache", "ca", "cookieauth", "oa", "onlyauthed")
)
# Bail out if the user requires authentication but is not authenticated
if onlyAuthed and not aw.getUser():
raise HTTPAPIError("Not authenticated", apache.HTTP_FORBIDDEN)
obj = None
addToCache = not hook.NO_CACHE
cache = GenericCache("HTTPAPI")
cacheKey = RE_REMOVE_EXTENSION.sub("", cacheKey)
if not noCache:
obj = cache.get(cacheKey)
if obj is not None:
result, extra, ts, complete, typeMap = obj
addToCache = False
if result is None:
# Perform the actual exporting
res = hook(aw, req)
if isinstance(res, tuple) and len(res) == 4:
result, extra, complete, typeMap = res
else:
result, extra, complete, typeMap = res, {}, True, {}
if result is not None and addToCache:
ttl = HelperMaKaCInfo.getMaKaCInfoInstance().getAPICacheTTL()
cache.set(cacheKey, (result, extra, ts, complete, typeMap), ttl)
except HTTPAPIError, e:
error = e
#.........这里部分代码省略.........