Added new version of Moralność pani Dulskiej to books.
[wolnelektury.git] / apps / south / management / commands / syncdb.py
1 from django.core.management.base import NoArgsCommand
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('--verbosity', action='store', dest='verbosity', default='1',
19             type='choice', choices=['0', '1', '2'],
20             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
21         make_option('--noinput', action='store_false', dest='interactive', default=True,
22             help='Tells Django to NOT prompt the user for input of any kind.'),
23         make_option('--migrate', action='store_true', dest='migrate', default=False,
24             help='Tells South to also perform migrations after the sync. Default for during testing, and other internal calls.'),
25     )
26     help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created, except those which use migrations."
27
28     def handle_noargs(self, **options):
29         # Work out what uses migrations and so doesn't need syncing
30         apps_needing_sync = []
31         apps_migrated = []
32         for app in models.get_apps():
33             app_name = get_app_name(app)
34             migrations = migration.get_app(app)
35             if migrations is None:
36                 apps_needing_sync.append(app_name)
37             else:
38                 # This is a migrated app, leave it
39                 apps_migrated.append(app_name)
40         # Run syncdb on only the ones needed
41         print "Syncing..."
42         old_installed, settings.INSTALLED_APPS = settings.INSTALLED_APPS, apps_needing_sync
43         old_app_store, cache.app_store = cache.app_store, SortedDict([
44             (k, v) for (k, v) in cache.app_store.items()
45             if get_app_name(k) in apps_needing_sync
46         ])
47         syncdb.Command().execute(**options)
48         settings.INSTALLED_APPS = old_installed
49         cache.app_store = old_app_store
50         # Migrate if needed
51         if options.get('migrate', True):
52             print "Migrating..."
53             management.call_command('migrate')
54         # Be obvious about what we did
55         print "\nSynced:\n > %s" % "\n > ".join(apps_needing_sync)
56         
57         if options.get('migrate', True):
58             print "\nMigrated:\n - %s" % "\n - ".join(apps_migrated)
59         else:
60             print "\nNot synced (use migrations):\n - %s" % "\n - ".join(apps_migrated)
61             print "(use ./manage.py migrate to migrate these)"