本文整理汇总了Python中trytond.cache.Cache类的典型用法代码示例。如果您正苦于以下问题:Python Cache类的具体用法?Python Cache怎么用?Python Cache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cache类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:fulfilio,项目名称:nereid,代码行数:25,代码来源:test_templates.py
示例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)
开发者ID:fulfilio,项目名称:nereid,代码行数:33,代码来源:test_templates.py
示例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
开发者ID:Sisouvan,项目名称:ogh,代码行数:33,代码来源:webdav.py
示例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
开发者ID:coopengo,项目名称:proteus,代码行数:32,代码来源:config.py
示例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
开发者ID:kret0s,项目名称:tryton3_8,代码行数:8,代码来源:test_tryton.py
示例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()
开发者ID:mediafactory,项目名称:tryton_core_daemon,代码行数:9,代码来源:cacheable.py
示例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)
开发者ID:priyankajain18,项目名称:nereid,代码行数:54,代码来源:application.py
示例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)
开发者ID:kret0s,项目名称:gnuhealth-live,代码行数:54,代码来源:__init__.py
示例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)
开发者ID:Sisouvan,项目名称:ogh,代码行数:11,代码来源:webdav.py
示例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
开发者ID:coopengo,项目名称:trytond,代码行数:16,代码来源:dispatcher.py
示例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)
开发者ID:ferchuochoa,项目名称:SIGCoop,代码行数:46,代码来源:config.py
示例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)
开发者ID:fulfilio,项目名称:nereid,代码行数:17,代码来源:test_templates.py
示例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
开发者ID:C3S,项目名称:collecting_society.portal,代码行数:44,代码来源:base.py
示例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())
开发者ID:GauravButola,项目名称:tryton-restful,代码行数:44,代码来源:application.py
示例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
开发者ID:coopengo,项目名称:trytond,代码行数:20,代码来源:dispatcher.py
示例16: transaction
def transaction(request):
"""Yields transaction with installed module.
"""
# Importing transaction directly causes cyclic dependency in 3.6
from trytond.tests.test_tryton import USER, CONTEXT, DB_NAME, POOL
from trytond.tools.singleton import Singleton # noqa
from trytond.transaction import Transaction
from trytond.cache import Cache
# Inject helper functions in instance on which test function was collected.
request.instance.POOL = POOL
request.instance.USER = USER
request.instance.CONTEXT = CONTEXT
request.instance.DB_NAME = DB_NAME
transaction = Transaction()
with transaction.start(DB_NAME, USER, context=CONTEXT) as txn:
yield txn
transaction.rollback()
Cache.drop(DB_NAME)
开发者ID:fulfilio,项目名称:trytond-payment-gateway-stripe,代码行数:22,代码来源:conftest.py
示例17: __init__
def __init__(self, database=None, user='admin', config_file=None):
super(TrytondConfig, self).__init__()
if not database:
database = os.environ.get('TRYTOND_DATABASE_URI')
else:
os.environ['TRYTOND_DATABASE_URI'] = database
if not config_file:
config_file = os.environ.get('TRYTOND_CONFIG')
from trytond.config import config
config.update_etc(config_file)
from trytond.pool import Pool
from trytond.cache import Cache
from trytond.transaction import Transaction
self.database = database
database_name = None
if database:
uri = urlparse.urlparse(database)
database_name = uri.path.strip('/')
if not database_name:
database_name = os.environ['DB_NAME']
self.database_name = database_name
self._user = user
self.config_file = config_file
Pool.start()
self.pool = Pool(database_name)
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)
开发者ID:coopengo,项目名称:proteus,代码行数:38,代码来源:config.py
示例18: execute
def execute(app, database, user, payload_json):
"""
Execute the task identified by the given payload in the given database
as `user`.
"""
if database not in Pool.database_list():
# Initialise the database if this is the first time we see the
# database being used.
with Transaction().start(database, 0, readonly=True):
Pool(database).init()
with Transaction().start(database, 0):
Cache.clean(database)
with Transaction().start(database, user) as transaction:
Async = Pool().get('async.async')
DatabaseOperationalError = backend.get('DatabaseOperationalError')
# De-serialize the payload in the transaction context so that
# active records are constructed in the same transaction cache and
# context.
payload = Async.deserialize_payload(payload_json)
try:
with Transaction().set_context(payload['context']):
results = Async.execute_payload(payload)
except RetryWithDelay, exc:
# A special error that would be raised by Tryton models to
# retry the task after a certain delay. Useful when the task
# got triggered before the record is ready and similar cases.
transaction.cursor.rollback()
raise app.retry(exc=exc, delay=exc.delay)
except DatabaseOperationalError, exc:
# Strict transaction handling may cause this.
# Rollback and Retry the whole transaction if within
# max retries, or raise exception and quit.
transaction.cursor.rollback()
raise app.retry(exc=exc)
开发者ID:prakashpp,项目名称:trytond-async,代码行数:38,代码来源:tasks.py
示例19: threaded_send_email
def threaded_send_email(email, smtp_server):
"""
A new threaded email sender. This is required because there is
no transaction in the new thread that is spawned and sendemail
tries to create a new cursor from an existing transaction.
So create the new transaction here, refresh the active record
objects and call sendmail like the cron would have
"""
with Transaction().start(DB_NAME, USER, context=CONTEXT):
# email active record is from old transaction, so referesh it.
email = EmailQueue(email.id)
database = backend.get('database')
try:
# Now send the email
email.send(smtp_server)
except database.DatabaseOperationalError:
# This specific email could not be sent because of a
# transaction serialization error
searialization_error_q.put(email.id)
finally:
Cache.drop(DB_NAME)
开发者ID:fulfilio,项目名称:email-queue,代码行数:24,代码来源:test_email_queue.py
示例20: 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):
user = current_website.application_user.id
website_context = current_website.get_context()
website_context.update({
'company': current_website.company.id,
})
language = current_locale.language.code
# pop locale if specified in the view_args
req.view_args.pop('locale', None)
active_id = req.view_args.pop('active_id', None)
for count in range(int(config.get('database', 'retry')), -1, -1):
with Transaction().start(
self.database_name, user,
context=website_context,
readonly=rule.is_readonly) as txn:
try:
transaction_start.send(self)
rv = self._dispatch_request(
req, language=language, active_id=active_id
)
txn.commit()
transaction_commit.send(self)
except DatabaseOperationalError:
# Strict transaction handling may cause this.
# Rollback and Retry the whole transaction if within
# max retries, or raise exception and quit.
txn.rollback()
if count:
continue
raise
except Exception:
# Rollback and raise any other exception
txn.rollback()
raise
else:
return rv
finally:
transaction_stop.send(self)
开发者ID:fulfilio,项目名称:nereid,代码行数:63,代码来源:application.py
注:本文中的trytond.cache.Cache类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论