old_contents: #!/usr/bin/env python
  import inspect
  import os
  
  from flask.ext.migrate import MigrateCommand, Migrate
  from flask.ext.script import Manager, Shell
  from termcolor import colored, cprint
  
  from application import create_app, db
  # use dev config if env var is not set
  from commands.create_admin import CreateAdmin
  
  config_file_path = os.environ.get('APP_CONFIG', None) or '../dev_config.py'
  
  app = create_app(config_file_path)
  
  manager = Manager(app)
  migrate = Migrate(app, db)
  
  manager.add_command('db', MigrateCommand)
  manager.add_command('create_admin', CreateAdmin())
  
  
  def _make_context():
      """
      Return context dict for a shell session so one can access
      stuff without importing it explicitly.
      """
  
      context_dict = {'app': app, 'db': db}
  
      def print_name(name, desc):
          name = colored(name, 'blue', attrs=['bold'])
          print('  {:15s} - {}'.format(name, desc))
  
      cprint('Names already imported to the shell (by `_make_context`):', 'yellow', attrs=['bold'])
      print_name('app', 'Flask application')
      print_name('db', 'flask.sqlalchemy database object')
  
      # Imports models from specified modules
      from application.authorization import models as authorization_models
  
      cprint('Models:', 'yellow', attrs=['bold'])
      for model in (authorization_models,):
          for attr_name in dir(model):
              attr = getattr(model, attr_name)
  
              if inspect.isclass(attr) and issubclass(attr, db.Model):
                  context_dict[attr.__name__] = attr
                  print_name(
                      attr.__name__,
                      'imported from ' + colored(model.__name__ + '.' + attr.__name__, 'green')
                  )
  
      return context_dict
  
  
  manager.add_command('shell', Shell(make_context=_make_context))
  
  if __name__ == '__main__':
      manager.run()
  
new_contents: #!/usr/bin/env python
  import inspect
  import os
  
  from flask.ext.migrate import MigrateCommand, Migrate
  from flask.ext.script import Manager, Shell
  from termcolor import colored, cprint
  
  from application import create_app, db
  # use dev config if env var is not set
  from commands.create_admin import CreateAdmin
  
  config_file_path = os.environ.get('APP_CONFIG', None) or '../dev_config.py'
  
  app = create_app(config_file_path)
  
  manager = Manager(app)
  migrate = Migrate(app, db)
  
  manager.add_command('db', MigrateCommand)
  manager.add_command('create_admin', CreateAdmin())
  
  
  def _make_context():
      """
      Return context dict for a shell session so one can access
      stuff without importing it explicitly.
      """
  
      def logsql():
          """
          Enables/disables logging of SQLALchemy sql queries.
          """
          import logging
          logging.basicConfig()
          logger = logging.getLogger('sqlalchemy.engine')
  
          if logger.level == logging.NOTSET:
              logger.setLevel(logging.INFO)
          else:
              logger.setLevel(logging.NOTSET)
  
      context_dict = {'app': app, 'db': db, 'logsql': logsql}
  
      def print_name(name, desc):
          name = colored(name, 'blue', attrs=['bold'])
          print('  {:15s} - {}'.format(name, desc))
  
      cprint('Names already imported to the shell (by `_make_context`):', 'yellow', attrs=['bold'])
      print_name('app', 'Flask application')
      print_name('db', 'flask.sqlalchemy database object')
      print_name('logsql', 'utility function to enable/disable logging of SQLAlchemy SQL queries')
  
      # Imports models from specified modules
      from application.authorization import models as authorization_models
  
      cprint('Models:', 'yellow', attrs=['bold'])
      for model in (authorization_models,):
          for attr_name in dir(model):
              attr = getattr(model, attr_name)
  
              if inspect.isclass(attr) and issubclass(attr, db.Model):
                  context_dict[attr.__name__] = attr
                  print_name(
                      attr.__name__,
                      'imported from ' + colored(model.__name__ + '.' + attr.__name__, 'green')
                  )
  
      return context_dict
  
  
  manager.add_command('shell', Shell(make_context=_make_context))
  
  if __name__ == '__main__':
      manager.run()
  
subject: Add `logsql` utility function to `manage.py shell` to log sql queries