我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.core.management.color.no_style()。
def reset_sequences(*models): """ After loading data the sequences must be reset in the database if the primary keys are manually specified. This is handled automatically by django for fixtures. Much of this is modeled after django.core.management.commands.loaddata. """ # connection = connections[DEFAULT_DB_ALIAS] # cursor = connection.cursor() # sequence_sql = connection.ops.sequence_reset_sql(no_style(), models) # if sequence_sql: # for line in sequence_sql: # cursor.execute(line) # # transaction.commit_unless_managed() # cursor.close() pass
def update_site(sender, **kwargs): """ Update `Site` object matching `SITE_ID` setting with `SITE_DOMAIN` and `SITE_PORT` settings. """ Site = apps.get_model('sites', 'Site') domain = settings.SITE_DOMAIN if settings.SITE_PORT: domain += ':%s' % settings.SITE_PORT Site.objects.update_or_create( pk=settings.SITE_ID, defaults=dict( domain=domain, name=settings.SITE_NAME)) # We set an explicit pk instead of relying on auto-incrementation, # so we need to reset the database sequence. sequence_sql = connection.ops.sequence_reset_sql(no_style(), [Site]) if sequence_sql: cursor = connection.cursor() for command in sequence_sql: cursor.execute(command)
def create_default_site(app, created_models, verbosity, db, **kwargs): # Only create the default sites in databases where Django created the table if Site in created_models and router.allow_syncdb(db, Site) : # The default settings set SITE_ID = 1, and some tests in Django's test # suite rely on this value. However, if database sequences are reused # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that # the next id will be 1, so we coerce it. See #15573 and #16353. This # can also crop up outside of tests - see #15346. if verbosity >= 2: print("Creating example.com Site object") Site(pk=1, domain="example.com", name="example.com").save(using=db) # We set an explicit pk instead of relying on auto-incrementation, # so we need to reset the database sequence. See #17415. sequence_sql = connections[db].ops.sequence_reset_sql(no_style(), [Site]) if sequence_sql: if verbosity >= 2: print("Resetting sequence") cursor = connections[db].cursor() for command in sequence_sql: cursor.execute(command) Site.objects.clear_cache()
def __init__(self, stdout=None, stderr=None, no_color=False): self.stdout = OutputWrapper(stdout or sys.stdout) self.stderr = OutputWrapper(stderr or sys.stderr) if no_color: self.style = no_style() else: self.style = color_style() self.stderr.style_func = self.style.ERROR
def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs): try: Site = apps.get_model('sites', 'Site') except LookupError: return if not router.allow_migrate_model(using, Site): return if not Site.objects.using(using).exists(): # The default settings set SITE_ID = 1, and some tests in Django's test # suite rely on this value. However, if database sequences are reused # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that # the next id will be 1, so we coerce it. See #15573 and #16353. This # can also crop up outside of tests - see #15346. if verbosity >= 2: print("Creating example.com Site object") Site(pk=getattr(settings, 'SITE_ID', 1), domain="example.com", name="example.com").save(using=using) # We set an explicit pk instead of relying on auto-incrementation, # so we need to reset the database sequence. See #17415. sequence_sql = connections[using].ops.sequence_reset_sql(no_style(), [Site]) if sequence_sql: if verbosity >= 2: print("Resetting sequence") with connections[using].cursor() as cursor: for command in sequence_sql: cursor.execute(command)
def __init__(self, *args, **kwargs): super(Command, self).__init__(*args, **kwargs) self.copied_files = [] self.symlinked_files = [] self.unmodified_files = [] self.post_processed_files = [] self.storage = staticfiles_storage self.style = no_style() try: self.storage.path('') except NotImplementedError: self.local = False else: self.local = True
def create_default_site(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs): try: Site = apps.get_model('sites', 'Site') except LookupError: return if not router.allow_migrate_model(using, Site): return if not Site.objects.using(using).exists(): # The default settings set SITE_ID = 1, and some tests in Django's test # suite rely on this value. However, if database sequences are reused # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that # the next id will be 1, so we coerce it. See #15573 and #16353. This # can also crop up outside of tests - see #15346. if verbosity >= 2: print("Creating example.com Site object") Site(pk=getattr(settings, 'SITE_ID', 1), domain="example.com", name="example.com").save(using=using) # We set an explicit pk instead of relying on auto-incrementation, # so we need to reset the database sequence. See #17415. sequence_sql = connections[using].ops.sequence_reset_sql(no_style(), [Site]) if sequence_sql: if verbosity >= 2: print("Resetting sequence") with connections[using].cursor() as cursor: for command in sequence_sql: cursor.execute(command)
def __init__(self, *args, **kwargs): super(Command, self).__init__(*args, **kwargs) self.copied_files = [] self.symlinked_files = [] self.unmodified_files = [] self.post_processed_files = [] self.storage = staticfiles_storage self.style = no_style()
def __init__(self, root, cmd, database, options, stdout=None, stderr=None, no_color=False): self._root = root self._cmd = cmd self._database = database self._django_options = options self.stdout = stdout self.stderr = stderr if no_color: self.style = no_style() else: self.style = color_style() self.stderr.style_func = self.style.ERROR
def __init__(self, stdout=None, stderr=None, no_color=False): self.stdout = OutputWrapper(stdout or sys.stdout) self.stderr = OutputWrapper(stderr or sys.stderr) if no_color: self.style = no_style() else: self.style = color_style() self.stderr.style_func = self.style.ERROR # `requires_model_validation` is deprecated in favor of # `requires_system_checks`. If both options are present, an error is # raised. Otherwise the present option is used. If none of them is # defined, the default value (True) is used. has_old_option = hasattr(self, 'requires_model_validation') has_new_option = hasattr(self, 'requires_system_checks') if has_old_option: warnings.warn( '"requires_model_validation" is deprecated ' 'in favor of "requires_system_checks".', RemovedInDjango19Warning) if has_old_option and has_new_option: raise ImproperlyConfigured( 'Command %s defines both "requires_model_validation" ' 'and "requires_system_checks", which is illegal. Use only ' '"requires_system_checks".' % self.__class__.__name__) self.requires_system_checks = ( self.requires_system_checks if has_new_option else self.requires_model_validation if has_old_option else True)
def reset_sequences(self): connection = connections['default'] cursor = connection.cursor() style = no_style() sequence_sql = connection.ops.sequence_reset_sql(style, [EmailAddress, EmailConfirmation]) if sequence_sql: print("Resetting sequences") for line in sequence_sql: cursor.execute(line)
def handle(self, *args, **options): if options[self.subcommand_dest] in self.instances: command = self.instances[options[self.subcommand_dest]] if options.get('no_color'): command.style = no_style() command.stderr.style_func = None if options.get('stdout'): command.stdout = OutputWrapper(options['stdout']) if options.get('stderr'): command.stderr = OutputWrapper(options.get('stderr'), command.stderr.style_func) command.handle(*args, **options) else: self.print_help('manage.py', 'cms')