Ticket #8348: 8343-prompt.diff

File 8343-prompt.diff, 4.9 KB (added by Graham King, 16 years ago)
  • django/db/backends/util.py

     
    4545    def __iter__(self):
    4646        return iter(self.cursor)
    4747
     48class CursorInteractiveWrapper(object):
     49    def __init__(self, cursor, db):
     50        self.cursor = cursor
     51        self.db = db # Instance of a BaseDatabaseWrapper subclass
     52
     53    def _confirm(self, sql, params):
     54        """Ask the user whether to execute this SQL"""
     55        if sql and not sql.lower().startswith("select") and not sql.lower().startswith("show"):
     56            print sql
     57            if params:
     58                print "PARAMS:", params
     59            answer = raw_input("Execute this [y/N]?")
     60            print '-'*20
     61            if not answer or answer.lower() != 'y':
     62                return False
     63        return True
     64
     65    def execute(self, sql, params=()):
     66        if self._confirm(sql, params):
     67            return self.cursor.execute(sql, params)
     68        else:
     69            return None
     70
     71    def executemany(self, sql, param_list):
     72        if self._confirm(sql, param_list):
     73            return self.cursor.executemany(sql, param_list)
     74        else:
     75            return None
     76
     77    def __getattr__(self, attr):
     78        if attr in self.__dict__:
     79            return self.__dict__[attr]
     80        else:
     81            return getattr(self.cursor, attr)
     82
     83    def __iter__(self):
     84        return iter(self.cursor)
     85
    4886###############################################
    4987# Converters from database (string) to Python #
    5088###############################################
  • django/db/backends/__init__.py

     
    2828        self.connection = None
    2929        self.queries = []
    3030        self.options = kwargs
    31 
     31        self.is_interactive = False
     32       
    3233    def _commit(self):
    3334        if self.connection is not None:
    3435            return self.connection.commit()
     
    5859            self.connection = None
    5960
    6061    def cursor(self):
    61         from django.conf import settings
     62        from django.conf import settings       
    6263        cursor = self._cursor(settings)
    6364        if settings.DEBUG:
    64             return self.make_debug_cursor(cursor)
     65            cursor = self.make_debug_cursor(cursor)
     66        if self.is_interactive:
     67            cursor = self.make_interactive_cursor(cursor)
    6568        return cursor
    6669
    6770    def make_debug_cursor(self, cursor):
    6871        return util.CursorDebugWrapper(cursor, self)
    6972
     73    def make_interactive_cursor(self, cursor):
     74        return util.CursorInteractiveWrapper(cursor, self)
     75
    7076class BaseDatabaseFeatures(object):
    7177    allows_group_by_pk = False
    7278    # True if django.db.backend.utils.typecast_timestamp is used on values
  • django/core/management/commands/syncdb.py

     
    1212    option_list = NoArgsCommand.option_list + (
    1313        make_option('--noinput', action='store_false', dest='interactive', default=True,
    1414            help='Tells Django to NOT prompt the user for input of any kind.'),
     15        make_option('--prompt', action='store_true', dest='prompt', default=False,
     16            help='Show each SQL statement before executing, and prompt whether to run it')
    1517    )
    1618    help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
    1719
     
    2325        verbosity = int(options.get('verbosity', 1))
    2426        interactive = options.get('interactive')
    2527        show_traceback = options.get('traceback', False)
     28        connection.is_interactive = options.get('prompt', False)
    2629
    2730        self.style = no_style()
    2831
  • docs/ref/django-admin.txt

     
    665665"Are you sure?" confirmation messages. This is useful if ``django-admin.py``
    666666is being executed as an unattended, automated script.
    667667
     668--prompt
     669~~~~~~~~
     670.. versionadded:: 1.1
     671
     672Use the ``--prompt`` option to be prompted before each line of SQL that changes data
     673gets executed (i.e. you won't get prompted for SELECT statement). The SQL will be displayed along
     674with a yes or no prompt, and if you say no that line of SQL will not get executed.
     675
     676Note that some of the SQL will depend on previous statements having been executed. For example,
     677if you say no to the SQL to create a table, then yes to the SQL to insert into it,
     678you're on your own.
     679
     680If you say both ``--noinput`` and ``--prompt``, you wil get prompted. Don't use both.
     681
    668682test
    669683----
    670684
Back to Top