Uaktualnienie django-south.
[wolnelektury.git] / apps / south / management / commands / syncdb.py
1 from django.core.management.base import NoArgsCommand, BaseCommand 
2 from django.core.management.color import no_style
3 from django.utils.datastructures import SortedDict
4 from optparse import make_option
5 from south import migration
6 from django.core.management.commands import syncdb
7 from django.conf import settings
8 from django.db import models
9 from django.db.models.loading import cache
10 from django.core import management
11 import sys
12
13 def get_app_name(app):
14     return '.'.join( app.__name__.split('.')[0:-1] )
15
16 class Command(NoArgsCommand):
17     option_list = NoArgsCommand.option_list + (
18         make_option('--noinput', action='store_false', dest='interactive', default=True,
19             help='Tells Django to NOT prompt the user for input of any kind.'),
20         make_option('--migrate', action='store_true', dest='migrate', default=False,
21             help='Tells South to also perform migrations after the sync. Default for during testing, and other internal calls.'),
22     )
23     if '--verbosity' not in [opt.get_opt_string() for opt in BaseCommand.option_list]:
24         option_list += (
25             make_option('--verbosity', action='store', dest='verbosity', default='1',
26             type='choice', choices=['0', '1', '2'],
27             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
28         )
29     help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created, except those which use migrations."
30
31     def handle_noargs(self, **options):
32         # Work out what uses migrations and so doesn't need syncing
33         apps_needing_sync = []
34         apps_migrated = []
35         for app in models.get_apps():
36             app_name = get_app_name(app)
37             migrations = migration.get_app(app)
38             if migrations is None:
39                 apps_needing_sync.append(app_name)
40             else:
41                 # This is a migrated app, leave it
42                 apps_migrated.append(app_name)
43         verbosity = int(options.get('verbosity', 0))
44         # Run syncdb on only the ones needed
45         if verbosity > 0:
46             print "Syncing..."
47         old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, apps_needing_sync
48         old_app_store, cache.app_store = cache.app_store, SortedDict([
49             (k, v) for (k, v) in cache.app_store.items()
50             if get_app_name(k) in apps_needing_sync
51         ])
52         syncdb.Command().execute(**options)
53         settings.INSTALLED_APPS = old_installed
54         cache.app_store = old_app_store
55         # Migrate if needed
56         if options.get('migrate', True):
57             if verbosity > 0:
58                 print "Migrating..."
59             management.call_command('migrate', **options)
60         # Be obvious about what we did
61         if verbosity > 0:
62             print "\nSynced:\n > %s" % "\n > ".join(apps_needing_sync)
63         
64         if options.get('migrate', True):
65             if verbosity > 0:
66                 print "\nMigrated:\n - %s" % "\n - ".join(apps_migrated)
67         else:
68             if verbosity > 0:
69                 print "\nNot synced (use migrations):\n - %s" % "\n - ".join(apps_migrated)
70                 print "(use ./manage.py migrate to migrate these)"