Ticket #14662: multidb_management_patch.diff

File multidb_management_patch.diff, 7.4 KB (added by marcop, 5 years ago)
  • django/contrib/contenttypes/management.py

     
    11from django.contrib.contenttypes.models import ContentType
    22from django.db.models import get_apps, get_models, signals
    33from django.utils.encoding import smart_unicode
     4from django.db import DEFAULT_DB_ALIAS
    45
    56def update_contenttypes(app, created_models, verbosity=2, **kwargs):
    67    """
    78    Creates content types for models in the given app, removing any model
    89    entries that no longer have a matching model class.
    910    """
    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]))
    1214    app_models = get_models(app)
    1315    if not app_models:
    1416        return
    1517    for klass in app_models:
    1618        opts = klass._meta
    1719        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())
    2022            content_types.remove(ct)
    2123        except ContentType.DoesNotExist:
    2224            ct = ContentType(name=smart_unicode(opts.verbose_name_raw),
    2325                app_label=opts.app_label, model=opts.object_name.lower())
    24             ct.save()
     26            ct.save(using=database)
    2527            if verbosity >= 2:
    2628                print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
    2729    # The presence of any remaining content types means the supplied app has an
     
    4547            for ct in content_types:
    4648                if verbosity >= 2:
    4749                    print "Deleting stale content type '%s | %s'" % (ct.app_label, ct.model)
    48                 ct.delete()
     50                ct.delete(using=database)
    4951        else:
    5052            if verbosity >= 2:
    5153                print "Stale content types remain."
    5254
    5355def update_all_contenttypes(verbosity=2, **kwargs):
     56    import pdb; pdb.set_trace()
    5457    for app in get_apps():
    5558        update_contenttypes(app, None, verbosity, **kwargs)
    5659
  • django/contrib/auth/management/commands/createsuperuser.py

     
    1111from django.core import exceptions
    1212from django.core.management.base import BaseCommand, CommandError
    1313from django.utils.translation import ugettext as _
     14from django.db import DEFAULT_DB_ALIAS
    1415
    1516RE_VALID_USERNAME = re.compile('[\w.@+-]+$')
    1617
     
    3435                 'You must use --username and --email with --noinput, and '      \
    3536                 'superusers created with --noinput will not be able to log in '  \
    3637                 '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.'),
    3741    )
    3842    help = 'Used to create a superuser.'
    3943
     
    4246        email = options.get('email', None)
    4347        interactive = options.get('interactive')
    4448        verbosity = int(options.get('verbosity', 1))
     49        database = options.get('database', DEFAULT_DB_ALIAS)
    4550
    4651        # Do quick and dirty validation if --noinput
    4752        if not interactive:
     
    7075        # it as an option.
    7176        if default_username:
    7277            try:
    73                 User.objects.get(username=default_username)
     78                User.objects.using(database).get(username=default_username)
    7479            except User.DoesNotExist:
    7580                pass
    7681            else:
     
    95100                        username = None
    96101                        continue
    97102                    try:
    98                         User.objects.get(username=username)
     103                        User.objects.using(database).get(username=username)
    99104                    except User.DoesNotExist:
    100105                        break
    101106                    else:
     
    132137                sys.stderr.write("\nOperation cancelled.\n")
    133138                sys.exit(1)
    134139
    135         User.objects.create_superuser(username, email, password)
     140        User.objects.db_manager(database).create_superuser(username, email, password)
    136141        if verbosity >= 1:
    137142          self.stdout.write("Superuser created successfully.\n")
    138143
  • django/contrib/auth/management/__init__.py

     
    44
    55from django.contrib.auth import models as auth_app
    66from django.db.models import get_models, signals
     7from django.db import DEFAULT_DB_ALIAS
    78
    8 
    99def _get_permission_codename(action, opts):
    1010    return u'%s_%s' % (action, opts.object_name.lower())
    1111
     
    2121
    2222    app_models = get_models(app)
    2323
     24    database = kwargs.get('db', DEFAULT_DB_ALIAS)
    2425    # This will hold the permissions we're looking for as
    2526    # (content_type, (codename, name))
    2627    searched_perms = set()
    2728    # The codenames and ctypes that should exist.
    2829    ctypes = set()
    2930    for klass in app_models:
    30         ctype = ContentType.objects.get_for_model(klass)
     31        ctype = ContentType.objects.db_manager(database).get_for_model(klass)
    3132        ctypes.add(ctype)
    3233        for perm in _get_all_permissions(klass._meta):
    3334            searched_perms.add((ctype, perm))
     
    3536    # Find all the Permissions that have a context_type for a model we're
    3637    # looking for.  We don't need to check for codenames since we already have
    3738    # 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(
    3940        content_type__in=ctypes,
    4041    ).values_list(
    4142        "content_type", "codename"
     
    4546        # If the permissions exists, move on.
    4647        if (ctype.pk, codename) in all_perms:
    4748            continue
    48         p = auth_app.Permission.objects.create(
     49        p = auth_app.Permission.objects.using(database).create(
    4950            codename=codename,
    5051            name=name,
    5152            content_type=ctype
     
    5657
    5758def create_superuser(app, created_models, verbosity, **kwargs):
    5859    from django.core.management import call_command
     60    database = kwargs.get('db', DEFAULT_DB_ALIAS)
    5961
    6062    if auth_app.User in created_models and kwargs.get('interactive', True):
    6163        msg = ("\nYou just installed Django's auth system, which means you "
     
    6769                confirm = raw_input('Please enter either "yes" or "no": ')
    6870                continue
    6971            if confirm == 'yes':
    70                 call_command("createsuperuser", interactive=True)
     72                call_command("createsuperuser", interactive=True, database=database)
    7173            break
    7274
    7375signals.post_syncdb.connect(create_permissions,
Back to Top