1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

[multi-db] Moved ConnectionInfoDescriptor to django.db, updated imports

accordingly.


git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3386 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jason Pellerin 2006-07-19 18:57:26 +00:00
parent 5b6017edbf
commit d67abd9307

View File

@ -1,7 +1,7 @@
from django import core
from django.utils.functional import curry
from django.core.exceptions import ImproperlyConfigured
from django.db import connections, _default
from django.db import ConnectionInfoDescriptor
from django.db.models.query import QuerySet
from django.dispatch import dispatcher
from django.db.models import signals, get_apps, get_models
@ -34,72 +34,6 @@ def ensure_default_manager(sender):
cls.add_to_class('objects', Manager())
dispatcher.connect(ensure_default_manager, signal=signals.class_prepared)
class ConnectionInfoDescriptor(object):
"""Descriptor used to access database connection information from a
manager or other connection holder. Keeps a thread-local cache of
connections per instance, and always returns the same connection for an
instance in particular thread during a particular request.
Any object that includes an attribute ``model`` that holds a model class
can use this descriptor to manage connections.
"""
def __init__(self):
self.cnx = local()
self.cnx.cache = {}
def __get__(self, instance, type=None):
if instance is None:
raise AttributeError, \
"ConnectionInfo is accessible only through an instance"
instance_connection = self.cnx.cache.get(instance, None)
if instance_connection is None:
instance_connection = self.get_connection(instance)
def reset():
self.reset(instance)
dispatcher.connect(reset, signal=core.signals.request_finished)
self.cnx.cache[instance] = instance_connection
return instance_connection
def __set__(self, instance, value):
self.cnx.cache[instance] = instance_connection
def __delete__(self, instance):
self.reset(instance)
def get_connection(self, instance):
from django.conf import settings
app = instance.model._meta.app_label
model = instance.model.__name__
app_model = "%s.%s" % (app, model)
# Quick exit if no OTHER_DATABASES defined
if (not hasattr(settings, 'OTHER_DATABASES')
or not settings.OTHER_DATABASES):
return connections[_default]
# Look in MODELS for the best match: app_label.Model. If that isn't
# found, take just app_label. If nothing is found, use the default
maybe = None
for name, db_def in settings.OTHER_DATABASES.items():
if not 'MODELS' in db_def:
continue
mods = db_def['MODELS']
# Can't get a better match than this
if app_model in mods:
return connections[name]
elif app in mods:
if maybe is not None:
raise ImproperlyConfigured, \
"App %s appears in more than one OTHER_DATABASES " \
"setting (%s and %s)" % (maybe, name)
maybe = name
if maybe:
return connections[name]
# No named connection for this model; use the default
return connections[_default]
def reset(self, instance):
self.cnx.cache[instance] = None
class Manager(object):
@ -214,16 +148,12 @@ class Manager(object):
pending.setdefault(klass, []).extend(statements)
return pending
def load_initial_data(self):
"""Load initial data for my model into the database."""
pass # FIXME
def drop(self):
def drop(self, cascade=False):
"""Drop my model's table."""
pass # FIXME
def get_installed_models(self, table_list):
"""Get list of models installed, given a list of tables
"""Get list of models installed, given a list of tables.
"""
all_models = []
for app in get_apps():