Ticket #14662: multidb_management_patch.2.diff
File multidb_management_patch.2.diff, 7.2 KB (added by , 14 years ago) |
---|
-
django/contrib/contenttypes/management.py
1 1 from django.contrib.contenttypes.models import ContentType 2 2 from django.db.models import get_apps, get_models, signals 3 3 from django.utils.encoding import smart_unicode 4 from django.db import DEFAULT_DB_ALIAS 4 5 5 6 def update_contenttypes(app, created_models, verbosity=2, **kwargs): 6 7 """ 7 8 Creates content types for models in the given app, removing any model 8 9 entries that no longer have a matching model class. 9 10 """ 10 ContentType.objects.clear_cache() 11 content_types = list(ContentType.objects.filter(app_label=app.__name__.split('.')[-2])) 11 database = kwargs.get('db', DEFAULT_DB_ALIAS) 12 ContentType.objects.db_manager(database).clear_cache() 13 content_types = list(ContentType.objects.using(database).filter(app_label=app.__name__.split('.')[-2])) 12 14 app_models = get_models(app) 13 15 if not app_models: 14 16 return 15 17 for klass in app_models: 16 18 opts = klass._meta 17 19 try: 18 ct = ContentType.objects. get(app_label=opts.app_label,19 model=opts.object_name.lower())20 ct = ContentType.objects.using(database).get(app_label=opts.app_label, 21 model=opts.object_name.lower()) 20 22 content_types.remove(ct) 21 23 except ContentType.DoesNotExist: 22 24 ct = ContentType(name=smart_unicode(opts.verbose_name_raw), 23 25 app_label=opts.app_label, model=opts.object_name.lower()) 24 ct.save( )26 ct.save(using=database) 25 27 if verbosity >= 2: 26 28 print "Adding content type '%s | %s'" % (ct.app_label, ct.model) 27 29 # The presence of any remaining content types means the supplied app has an … … 45 47 for ct in content_types: 46 48 if verbosity >= 2: 47 49 print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model) 48 ct.delete( )50 ct.delete(using=database) 49 51 else: 50 52 if verbosity >= 2: 51 53 print "Stale content types remain." -
django/contrib/auth/management/commands/createsuperuser.py
11 11 from django.core import exceptions 12 12 from django.core.management.base import BaseCommand, CommandError 13 13 from django.utils.translation import ugettext as _ 14 from django.db import DEFAULT_DB_ALIAS 14 15 15 16 RE_VALID_USERNAME = re.compile('[\w.@+-]+$') 16 17 … … 34 35 'You must use --username and --email with --noinput, and ' \ 35 36 'superusers created with --noinput will not be able to log in ' \ 36 37 'until they\'re given a valid password.'), 38 make_option('--database', action='store', dest='database', 39 default=DEFAULT_DB_ALIAS, 40 help='Nominates a database for superuser creation. Defaults to the "default" database.'), 37 41 ) 38 42 help = 'Used to create a superuser.' 39 43 … … 42 46 email = options.get('email', None) 43 47 interactive = options.get('interactive') 44 48 verbosity = int(options.get('verbosity', 1)) 49 database = options.get('database', DEFAULT_DB_ALIAS) 45 50 46 51 # Do quick and dirty validation if --noinput 47 52 if not interactive: … … 70 75 # it as an option. 71 76 if default_username: 72 77 try: 73 User.objects. get(username=default_username)78 User.objects.using(database).get(username=default_username) 74 79 except User.DoesNotExist: 75 80 pass 76 81 else: … … 95 100 username = None 96 101 continue 97 102 try: 98 User.objects. get(username=username)103 User.objects.using(database).get(username=username) 99 104 except User.DoesNotExist: 100 105 break 101 106 else: … … 132 137 sys.stderr.write("\nOperation cancelled.\n") 133 138 sys.exit(1) 134 139 135 User.objects. create_superuser(username, email, password)140 User.objects.db_manager(database).create_superuser(username, email, password) 136 141 if verbosity >= 1: 137 142 self.stdout.write("Superuser created successfully.\n") 138 143 -
django/contrib/auth/management/__init__.py
4 4 5 5 from django.contrib.auth import models as auth_app 6 6 from django.db.models import get_models, signals 7 from django.db import DEFAULT_DB_ALIAS 7 8 8 9 9 def _get_permission_codename(action, opts): 10 10 return u'%s_%s' % (action, opts.object_name.lower()) 11 11 … … 21 21 22 22 app_models = get_models(app) 23 23 24 database = kwargs.get('db', DEFAULT_DB_ALIAS) 24 25 # This will hold the permissions we're looking for as 25 26 # (content_type, (codename, name)) 26 27 searched_perms = set() 27 28 # The codenames and ctypes that should exist. 28 29 ctypes = set() 29 30 for klass in app_models: 30 ctype = ContentType.objects. get_for_model(klass)31 ctype = ContentType.objects.db_manager(database).get_for_model(klass) 31 32 ctypes.add(ctype) 32 33 for perm in _get_all_permissions(klass._meta): 33 34 searched_perms.add((ctype, perm)) … … 35 36 # Find all the Permissions that have a context_type for a model we're 36 37 # looking for. We don't need to check for codenames since we already have 37 38 # a list of the ones we're going to create. 38 all_perms = set(auth_app.Permission.objects. filter(39 all_perms = set(auth_app.Permission.objects.using(database).filter( 39 40 content_type__in=ctypes, 40 41 ).values_list( 41 42 "content_type", "codename" … … 45 46 # If the permissions exists, move on. 46 47 if (ctype.pk, codename) in all_perms: 47 48 continue 48 p = auth_app.Permission.objects. create(49 p = auth_app.Permission.objects.using(database).create( 49 50 codename=codename, 50 51 name=name, 51 52 content_type=ctype … … 56 57 57 58 def create_superuser(app, created_models, verbosity, **kwargs): 58 59 from django.core.management import call_command 60 database = kwargs.get('db', DEFAULT_DB_ALIAS) 59 61 60 62 if auth_app.User in created_models and kwargs.get('interactive', True): 61 63 msg = ("\nYou just installed Django's auth system, which means you " … … 67 69 confirm = raw_input('Please enter either "yes" or "no": ') 68 70 continue 69 71 if confirm == 'yes': 70 call_command("createsuperuser", interactive=True )72 call_command("createsuperuser", interactive=True, database=database) 71 73 break 72 74 73 75 signals.post_syncdb.connect(create_permissions,