web/lib/django/core/management/commands/flush.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     1 from django.core.management.base import NoArgsCommand, CommandError
       
     2 from django.core.management.color import no_style
       
     3 from django.utils.importlib import import_module
       
     4 from optparse import make_option
       
     5 
       
     6 class Command(NoArgsCommand):
       
     7     option_list = NoArgsCommand.option_list + (
       
     8         make_option('--noinput', action='store_false', dest='interactive', default=True,
       
     9             help='Tells Django to NOT prompt the user for input of any kind.'),
       
    10     )
       
    11     help = "Executes ``sqlflush`` on the current database."
       
    12 
       
    13     def handle_noargs(self, **options):
       
    14         from django.conf import settings
       
    15         from django.db import connection, transaction, models
       
    16         from django.core.management.sql import sql_flush, emit_post_sync_signal
       
    17 
       
    18         verbosity = int(options.get('verbosity', 1))
       
    19         interactive = options.get('interactive')
       
    20 
       
    21         self.style = no_style()
       
    22 
       
    23         # Import the 'management' module within each installed app, to register
       
    24         # dispatcher events.
       
    25         for app_name in settings.INSTALLED_APPS:
       
    26             try:
       
    27                 import_module('.management', app_name)
       
    28             except ImportError:
       
    29                 pass
       
    30 
       
    31         sql_list = sql_flush(self.style, only_django=True)
       
    32 
       
    33         if interactive:
       
    34             confirm = raw_input("""You have requested a flush of the database.
       
    35 This will IRREVERSIBLY DESTROY all data currently in the %r database,
       
    36 and return each table to the state it was in after syncdb.
       
    37 Are you sure you want to do this?
       
    38 
       
    39     Type 'yes' to continue, or 'no' to cancel: """ % settings.DATABASE_NAME)
       
    40         else:
       
    41             confirm = 'yes'
       
    42 
       
    43         if confirm == 'yes':
       
    44             try:
       
    45                 cursor = connection.cursor()
       
    46                 for sql in sql_list:
       
    47                     cursor.execute(sql)
       
    48             except Exception, e:
       
    49                 transaction.rollback_unless_managed()
       
    50                 raise CommandError("""Database %s couldn't be flushed. Possible reasons:
       
    51       * The database isn't running or isn't configured correctly.
       
    52       * At least one of the expected database tables doesn't exist.
       
    53       * The SQL was invalid.
       
    54     Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
       
    55     The full error: %s""" % (settings.DATABASE_NAME, e))
       
    56             transaction.commit_unless_managed()
       
    57 
       
    58             # Emit the post sync signal. This allows individual
       
    59             # applications to respond as if the database had been
       
    60             # sync'd from scratch.
       
    61             emit_post_sync_signal(models.get_models(), verbosity, interactive)
       
    62 
       
    63             # Reinstall the initial_data fixture.
       
    64             from django.core.management import call_command
       
    65             call_command('loaddata', 'initial_data', **options)
       
    66 
       
    67         else:
       
    68             print "Flush cancelled."