Author: jpellerin Date: 2006-07-19 13:57:26 -0500 (Wed, 19 Jul 2006) New Revision: 3386
Modified: django/branches/multiple-db-support/django/db/models/manager.py Log: [multi-db] Moved ConnectionInfoDescriptor to django.db, updated imports accordingly. Modified: django/branches/multiple-db-support/django/db/models/manager.py =================================================================== --- django/branches/multiple-db-support/django/db/models/manager.py 2006-07-19 18:56:31 UTC (rev 3385) +++ django/branches/multiple-db-support/django/db/models/manager.py 2006-07-19 18:57:26 UTC (rev 3386) @@ -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,74 +34,8 @@ 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): # Tracks each time a Manager instance is created. Used to retain order. creation_counter = 0 @@ -214,16 +148,12 @@ 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(): --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-updates -~----------~----~----~----~------~----~------~--~---