cat models py coding utf-8 import thread from django conf import setti

 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
$ cat models.py
# -*- coding: utf-8 -*-
import thread
from django.conf import settings
from django.core import signals
from django.db.models import sql, Manager
from django.db.models.query import QuerySet
from django.db.transaction import savepoint_state
from lib import _threading_local
def get_db_wrapper():
if not hasattr(_threading_local, 'DB_POOL'):
_threading_local.DB_POOL = {}
db = getattr(_threading_local, 'DATABASE', 'default')
database = settings.DATABASES[db]
if db in _threading_local.DB_POOL and \
_threading_local.DB_POOL[db]._valid_connection():
return _threading_local.DB_POOL[db]
backend = __import__('django.db.backends.' + database['DATABASE_ENGINE']
+ ".base", {}, {}, ['base'])
backup = {}
for key, value in database.iteritems():
backup[key] = getattr(settings, key)
setattr(settings, key, value)
wrapper = backend.DatabaseWrapper()
wrapper._cursor(settings)
_threading_local.DB_POOL[db] = wrapper
for key, value in backup.iteritems():
setattr(settings, key, value)
return wrapper
def close_connection_pool(**kwargs):
if hasattr(_threading_local, 'DB_POOL'):
for db_name in _threading_local.DB_POOL:
_threading_local.DB_POOL[db_name].close()
signals.request_finished.connect(close_connection_pool)
def __init__(self, model=None, query=None):
self.model = model
self.query = query or sql.Query(self.model, get_db_wrapper())
self._result_cache = None
self._iter = None
self._sticky_filter = False
QuerySet.__init__ = __init__
def _insert(self, values, return_id=False, raw_values=False):
query = sql.InsertQuery(self.model, get_db_wrapper())
query.insert_values(values, raw_values)
ret = query.execute_sql(return_id)
query.connection._commit()
thread_ident = thread.get_ident()
if thread_ident in savepoint_state:
del savepoint_state[thread_ident]
return ret
Manager._insert = _insert