/usr/lib/python3/dist-packages/trytond/transaction.py is in tryton-server 4.6.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | # This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import logging
from threading import local
from sql import Flavor
from trytond import backend
from trytond.config import config
logger = logging.getLogger(__name__)
class _AttributeManager(object):
'''
Manage Attribute of transaction
'''
def __init__(self, **kwargs):
self.kwargs = kwargs
def __enter__(self):
return Transaction()
def __exit__(self, type, value, traceback):
for name, value in self.kwargs.items():
setattr(Transaction(), name, value)
class _Local(local):
def __init__(self):
# Transaction stack control
self.transactions = []
class Transaction(object):
'''
Control the transaction
'''
_local = _Local()
cache_keys = {'language', 'fuzzy_translation', '_datetime'}
database = None
readonly = False
connection = None
close = None
user = None
context = None
create_records = None
delete_records = None
delete = None # TODO check to merge with delete_records
timestamp = None
def __new__(cls, new=False):
transactions = cls._local.transactions
if new or not transactions:
instance = super(Transaction, cls).__new__(cls)
instance.cache = {}
instance._atexit = []
transactions.append(instance)
else:
instance = transactions[-1]
return instance
def get_cache(self):
from trytond.cache import LRUDict
keys = tuple(((key, self.context[key])
for key in sorted(self.cache_keys)
if key in self.context))
return self.cache.setdefault((self.user, keys),
LRUDict(config.getint('cache', 'model')))
def start(self, database_name, user, readonly=False, context=None,
close=False, autocommit=False, _nocache=False):
'''
Start transaction
'''
Database = backend.get('Database')
assert self.user is None
assert self.database is None
assert self.close is None
assert self.context is None
if not database_name:
database = Database().connect()
else:
database = Database(database_name).connect()
Flavor.set(Database.flavor)
self.user = user
self.database = database
self.readonly = readonly
self.connection = database.get_connection(readonly=readonly,
autocommit=autocommit)
self.close = close
self.context = context or {}
self.create_records = {}
self.delete_records = {}
self.delete = {}
self.timestamp = {}
self.counter = 0
self._datamanagers = []
self._nocache = _nocache
if not _nocache:
from trytond.cache import Cache
Cache.clean(database.name)
return self
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
transactions = self._local.transactions
try:
if transactions.count(self) == 1:
try:
try:
if type is None and not self.readonly:
self.commit()
else:
self.rollback()
finally:
self.database.put_connection(
self.connection, self.close)
finally:
self.database = None
self.readonly = False
self.connection = None
self.close = None
self.user = None
self.context = None
self.create_records = None
self.delete_records = None
self.delete = None
self.timestamp = None
self._datamanagers = []
for func, args, kwargs in self._atexit:
func(*args, **kwargs)
finally:
current_instance = transactions.pop()
assert current_instance is self, transactions
def set_context(self, context=None, **kwargs):
if context is None:
context = {}
manager = _AttributeManager(context=self.context)
self.context = self.context.copy()
self.context.update(context)
if kwargs:
self.context.update(kwargs)
return manager
def reset_context(self):
manager = _AttributeManager(context=self.context)
self.context = {}
return manager
def set_user(self, user, set_context=False):
if user != 0 and set_context:
raise ValueError('set_context only allowed for root')
manager = _AttributeManager(user=self.user,
context=self.context)
self.context = self.context.copy()
if set_context:
if user != self.user:
self.context['user'] = self.user
else:
self.context.pop('user', None)
self.user = user
return manager
def set_current_transaction(self, transaction):
self._local.transactions.append(transaction)
return transaction
def new_transaction(self, autocommit=False, readonly=False,
_nocache=False):
transaction = Transaction(new=True)
return transaction.start(self.database.name, self.user,
context=self.context, close=self.close, readonly=readonly,
autocommit=autocommit, _nocache=_nocache)
def commit(self):
try:
if self._datamanagers:
for datamanager in self._datamanagers:
datamanager.tpc_begin(self)
for datamanager in self._datamanagers:
datamanager.commit(self)
for datamanager in self._datamanagers:
datamanager.tpc_vote(self)
self.connection.commit()
except:
self.rollback()
raise
else:
try:
for datamanager in self._datamanagers:
datamanager.tpc_finish(self)
except:
logger.critical('A datamanager raised an exception in'
' tpc_finish, the data might be inconsistant',
exc_info=True)
if not self._nocache:
from trytond.cache import Cache
Cache.resets(self.database.name)
def rollback(self):
for cache in self.cache.values():
cache.clear()
for datamanager in self._datamanagers:
datamanager.tpc_abort(self)
self.connection.rollback()
if not self._nocache:
from trytond.cache import Cache
Cache.resets(self.database.name)
def join(self, datamanager):
try:
idx = self._datamanagers.index(datamanager)
return self._datamanagers[idx]
except ValueError:
self._datamanagers.append(datamanager)
return datamanager
def atexit(self, func, *args, **kwargs):
self._atexit.append((func, args, kwargs))
@property
def language(self):
def get_language():
from trytond.pool import Pool
Config = Pool().get('ir.configuration')
return Config.get_language()
if self.context:
return self.context.get('language') or get_language()
return get_language()
|