本文整理汇总了Python中trytond.cache.Cache类的典型用法代码示例。如果您正苦于以下问题:Python Cache类的具体用法?Python Cache怎么用?Python Cache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_0040_inheritance
def test_0040_inheritance(self):
'''Test if templates are read in the order of the tryton
module dependency graph. To test this we install the test
module now and then try to load a template which is different
with the test module.
'''
trytond.tests.test_tryton.install_module('nereid_test')
with Transaction().start(DB_NAME, USER, CONTEXT) as txn: # noqa
# Add nereid_test also to list of modules installed so
# that it is also added to the templates path
self.setup_defaults()
app = self.get_app()
self.assertEqual(len(app.jinja_loader.loaders), 3)
with app.test_request_context('/'):
self.assertEqual(
render_template('tests/from-module.html'),
'from-nereid-test-module'
)
txn.rollback()
Cache.drop(DB_NAME)
示例2: test_0020_pickling
def test_0020_pickling(self):
'''
Test if the lazy rendering object can be pickled and rendered
with a totally different context (when no application, request
or transaction bound objects are present).
'''
with Transaction().start(DB_NAME, USER, CONTEXT) as txn:
self.setup_defaults()
app = self.get_app()
with app.test_request_context('/'):
response = render_template(
'tests/test-changing-context.html',
variable="a"
)
self.assertEqual(response, 'a')
pickled_response = pickle.dumps(response)
txn.rollback()
# Drop the cache as the transaction is rollbacked
Cache.drop(DB_NAME)
with Transaction().start(DB_NAME, USER, CONTEXT) as txn:
self.setup_defaults()
app = self.get_app()
with app.test_request_context('/'):
response = pickle.loads(pickled_response)
self.assertEqual(response, 'a')
txn.rollback()
# Drop the cache as the transaction is rollbacked
Cache.drop(DB_NAME)
示例3: get_userinfo
def get_userinfo(self, user, password, command=''):
path = urlparse.urlparse(self.path).path
dbname = urllib.unquote_plus(path.split('/', 2)[1])
database = Database().connect()
cursor = database.cursor()
databases = database.list(cursor)
cursor.close()
if not dbname or dbname not in databases:
return True
if user:
user = int(login(dbname, user, password, cache=False))
if not user:
return None
else:
url = urlparse.urlparse(self.path)
query = urlparse.parse_qs(url.query)
path = url.path[len(dbname) + 2:]
if 'key' in query:
key, = query['key']
with Transaction().start(dbname, 0) as transaction:
database_list = Pool.database_list()
pool = Pool(dbname)
if not dbname in database_list:
pool.init()
Share = pool.get('webdav.share')
user = Share.get_login(key, command, path)
transaction.cursor.commit()
if not user:
return None
Transaction().start(dbname, user)
Cache.clean(dbname)
return user
示例4: __call__
def __call__(self, *args):
from trytond.cache import Cache
from trytond.transaction import Transaction
from trytond.rpc import RPC
if self._name in self._object.__rpc__:
rpc = self._object.__rpc__[self._name]
elif self._name in getattr(self._object, '_buttons', {}):
rpc = RPC(readonly=False, instantiate=0)
else:
raise TypeError('%s is not callable' % self._name)
with Transaction().start(self._config.database_name,
self._config.user, readonly=rpc.readonly) as transaction:
Cache.clean(self._config.database_name)
args, kwargs, transaction.context, transaction.timestamp = \
rpc.convert(self._object, *args)
meth = getattr(self._object, self._name)
if not hasattr(meth, 'im_self') or meth.im_self:
result = rpc.result(meth(*args, **kwargs))
else:
assert rpc.instantiate == 0
inst = args.pop(0)
if hasattr(inst, self._name):
result = rpc.result(meth(inst, *args, **kwargs))
else:
result = [rpc.result(meth(i, *args, **kwargs))
for i in inst]
if not rpc.readonly:
transaction.commit()
Cache.resets(self._config.database_name)
return result
示例5: wrapper
def wrapper(*args, **kwargs):
transaction = Transaction()
with transaction.start(DB_NAME, user, context=context):
result = func(*args, **kwargs)
transaction.cursor.rollback()
# Drop the cache as the transaction is rollbacked
Cache.drop(DB_NAME)
return result
示例6: clear
def clear(self):
dbname = Transaction().cursor.dbname
self._lock.acquire()
try:
self._cache.setdefault(dbname, {})
self._cache[dbname].clear()
Cache.reset(dbname, self._name)
finally:
self._lock.release()
示例7: dispatch_request
def dispatch_request(self):
"""
Does the request dispatching. Matches the URL and returns the
return value of the view or error handler. This does not have to
be a response object.
"""
DatabaseOperationalError = backend.get('DatabaseOperationalError')
req = _request_ctx_stack.top.request
if req.routing_exception is not None:
self.raise_routing_exception(req)
rule = req.url_rule
# if we provide automatic options for this URL and the
# request came with the OPTIONS method, reply automatically
if getattr(rule, 'provide_automatic_options', False) \
and req.method == 'OPTIONS':
return self.make_default_options_response()
with Transaction().start(self.database_name, 0):
Cache.clean(self.database_name)
Cache.resets(self.database_name)
with Transaction().start(self.database_name, 0, readonly=True):
Website = Pool().get('nereid.website')
website = Website.get_from_host(req.host)
user, company = website.application_user.id, website.company.id
for count in range(int(config.get('database', 'retry')), -1, -1):
with Transaction().start(
self.database_name, user,
context={'company': company},
readonly=rule.is_readonly) as txn:
try:
transaction_start.send(self)
rv = self._dispatch_request(req)
txn.cursor.commit()
except DatabaseOperationalError:
# Strict transaction handling may cause this.
# Rollback and Retry the whole transaction if within
# max retries, or raise exception and quit.
txn.cursor.rollback()
if count:
continue
raise
except Exception:
# Rollback and raise any other exception
txn.cursor.rollback()
raise
else:
return rv
finally:
transaction_stop.send(self)
示例8: _load_modules
def _load_modules():
global res
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor
# Migration from 3.6: remove double module
old_table = 'ir_module_module'
new_table = 'ir_module'
if TableHandler.table_exist(cursor, old_table):
TableHandler.table_rename(cursor, old_table, new_table)
if update:
cursor.execute(*ir_module.select(ir_module.name,
where=ir_module.state.in_(('installed', 'to install',
'to upgrade', 'to remove'))))
else:
cursor.execute(*ir_module.select(ir_module.name,
where=ir_module.state.in_(('installed', 'to upgrade',
'to remove'))))
module_list = [name for (name,) in cursor.fetchall()]
if update:
module_list += update
graph = create_graph(module_list)[0]
try:
load_module_graph(graph, pool, update, lang)
except Exception:
cursor.rollback()
raise
if update:
cursor.execute(*ir_module.select(ir_module.name,
where=(ir_module.state == 'to remove')))
fetchall = cursor.fetchall()
if fetchall:
for (mod_name,) in fetchall:
# TODO check if ressource not updated by the user
cursor.execute(*ir_model_data.select(ir_model_data.model,
ir_model_data.db_id,
where=(ir_model_data.module == mod_name),
order_by=ir_model_data.id.desc))
for rmod, rid in cursor.fetchall():
Model = pool.get(rmod)
Model.delete([Model(rid)])
cursor.commit()
cursor.execute(*ir_module.update([ir_module.state],
['uninstalled'],
where=(ir_module.state == 'to remove')))
cursor.commit()
res = False
Module = pool.get('ir.module')
Module.update_list()
cursor.commit()
Cache.resets(database_name)
示例9: finish
def finish(self):
WebDAVServer.DAVRequestHandler.finish(self)
global CACHE
CACHE = LocalDict()
if not Transaction().cursor:
return
dbname = Transaction().cursor.database_name
Transaction().stop()
if dbname:
Cache.resets(dbname)
示例10: login
def login(request, database_name, user, password):
Database = backend.get('Database')
DatabaseOperationalError = backend.get('DatabaseOperationalError')
try:
Database(database_name).connect()
except DatabaseOperationalError:
logger.error('fail to connect to %s', database_name, exc_info=True)
return False
session = security.login(database_name, user, password)
with Transaction().start(database_name, 0):
Cache.clean(database_name)
Cache.resets(database_name)
msg = 'successful login' if session else 'bad login or password'
logger.info('%s \'%s\' from %s using %s on database \'%s\'',
msg, user, request.remote_addr, request.scheme, database_name)
return session
示例11: __init__
def __init__(self, database_name=None, user='admin', database_type=None,
language='en_US', password='', config_file=None):
super(TrytondConfig, self).__init__()
from trytond.config import CONFIG
CONFIG.update_etc(config_file)
CONFIG.set_timezone()
if database_type is not None:
CONFIG['db_type'] = database_type
from trytond.pool import Pool
from trytond import backend
from trytond.protocols.dispatcher import create
from trytond.cache import Cache
from trytond.transaction import Transaction
self.database_type = CONFIG['db_type']
if database_name is None:
if self.database_type == 'sqlite':
database_name = ':memory:'
else:
database_name = 'test_%s' % int(time.time())
self.database_name = database_name
self._user = user
self.config_file = config_file
Pool.start()
with Transaction().start(None, 0) as transaction:
cursor = transaction.cursor
databases = backend.get('Database').list(cursor)
if database_name not in databases:
create(database_name, CONFIG['admin_passwd'], language, password)
database_list = Pool.database_list()
self.pool = Pool(database_name)
if database_name not in database_list:
self.pool.init()
with Transaction().start(self.database_name, 0) as transaction:
Cache.clean(database_name)
User = self.pool.get('res.user')
transaction.context = self.context
self.user = User.search([
('login', '=', user),
], limit=1)[0].id
with transaction.set_user(self.user):
self._context = User.get_preferences(context_only=True)
Cache.resets(database_name)
示例12: test_0040_headers
def test_0040_headers(self):
'''
Change registrations headers and check
'''
trytond.tests.test_tryton.install_module('nereid_test')
with Transaction().start(DB_NAME, USER, CONTEXT) as txn:
self.setup_defaults()
app = self.get_app()
with app.test_client() as c:
response = c.get('/test-lazy-renderer')
self.assertEqual(response.headers['X-Test-Header'], 'TestValue')
self.assertEqual(response.status_code, 201)
txn.rollback()
# Drop the cache as the transaction is rollbacked
Cache.drop(DB_NAME)
示例13: wrapper
def wrapper(*args, **kwargs):
_db = Tdb._db
_readonly = True
if readonly is not None:
_readonly = readonly
elif 'request' in kwargs:
_readonly = not (kwargs['request'].method
in ('PUT', 'POST', 'DELETE', 'PATCH'))
_user = user or 0
_context = {}
_retry = Tdb._retry or 0
_is_open = (Transaction().cursor)
if not _is_open:
with Transaction().start(_db, 0):
Cache.clean(_db)
_context.update(default_context())
else:
# Transaction().new_cursor(readonly=_readonly)
pass
_context.update(context or {})
# _context.update({'company': Tdb._company})
for count in range(_retry, -1, -1):
with NoTransaction() if _is_open else Transaction().start(
_db, _user, readonly=_readonly, context=_context):
cursor = Transaction().cursor
if withhold:
cursor.cursor.withhold = True
try:
result = func(*args, **kwargs)
if not _readonly:
cursor.commit()
except DatabaseOperationalError:
cursor.rollback()
if count and not _readonly:
continue
raise
except Exception:
cursor.rollback()
raise
Cache.resets(_db)
return result
示例14: wrapper
def wrapper(database, *args, **kwargs):
DatabaseOperationalError = backend.get('DatabaseOperationalError')
Cache.clean(database)
# Intialise the pool. The init method is smart enough not to
# reinitialise if it is already initialised.
Pool(database).init()
# get the context from the currently logged in user
with Transaction().start(database, g.current_user, readonly=True):
User = Pool().get('res.user')
context = User.get_preferences(context_only=True)
readonly = request.method == 'GET'
for count in range(int(CONFIG['retry']), -1, -1):
with Transaction().start(
database, g.current_user,
readonly=readonly,
context=context) as transaction:
cursor = transaction.cursor
try:
result = function(*args, **kwargs)
if not readonly:
cursor.commit()
except DatabaseOperationalError, exc:
cursor.rollback()
if count and not readonly:
continue
result = jsonify(error=unicode(exc)), 500
except UserError, exc:
cursor.rollback()
result = jsonify(error={
'type': 'UserError',
'message': exc.message,
'description': exc.description,
'code': exc.code,
}), 500
current_app.logger.error(traceback.format_exc())
except Exception, exc:
cursor.rollback()
result = jsonify(error=unicode(exc)), 500
current_app.logger.error(traceback.format_exc())
示例15: drop
def drop(request, database_name, password):
Database = backend.get('Database')
security.check_super(password)
database = Database(database_name)
database.close()
# Sleep to let connections close
time.sleep(1)
with Transaction().start(None, 0, close=True, autocommit=True) \
as transaction:
try:
database.drop(transaction.connection, database_name)
except Exception:
logger.error('DROP DB: %s failed', database_name, exc_info=True)
raise
else:
logger.info('DROP DB: %s', database_name)
Pool.stop(database_name)
Cache.drop(database_name)
return True