我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.core.checks.Warning()。
def _check_ignored_options(self, **kwargs): warnings = [] if self.has_null_arg: warnings.append( checks.Warning( 'null has no effect on ManyToManyField.', hint=None, obj=self, id='fields.W340', ) ) if len(self._validators) > 0: warnings.append( checks.Warning( 'ManyToManyField does not support validators.', hint=None, obj=self, id='fields.W341', ) ) return warnings
def check_duplicate_emails(app_configs=None, **kwargs): from accounts.utils import get_duplicate_emails errors = [] try: if len(get_duplicate_emails()): errors.append( checks.Warning( _("There are user accounts with duplicate emails. This " "will not be allowed in Pootle 2.8."), hint=_("Try using 'pootle find_duplicate_emails', and " "then update user emails with 'pootle " "update_user_email username email'. You might also " "want to consider using pootle merge_user or " "purge_user commands"), id="pootle.W017" ) ) except (OperationalError, ProgrammingError): # no accounts set up - most likely in a test pass return errors
def check_users(app_configs=None, **kwargs): from django.contrib.auth import get_user_model errors = [] User = get_user_model() try: admin_user = User.objects.get(username='admin') except (User.DoesNotExist, OperationalError, ProgrammingError): pass else: if admin_user.check_password('admin'): errors.append(checks.Warning( _("The default 'admin' user still has a password set to " "'admin'."), hint=_("Remove the 'admin' user or change its password."), id="pootle.W016", )) return errors
def _check_sql_mode(self, **kwargs): with self.connection.cursor() as cursor: cursor.execute("SELECT @@sql_mode") sql_mode = cursor.fetchone() modes = set(sql_mode[0].split(',') if sql_mode else ()) if not (modes & {'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES'}): return [checks.Warning( "MySQL Strict Mode is not set for database connection '%s'" % self.connection.alias, hint="MySQL's Strict Mode fixes many data integrity problems in MySQL, " "such as data truncation upon insertion, by escalating warnings into " "errors. It is strongly recommended you activate it. See: " "https://docs.djangoproject.com/en/%s/ref/databases/#mysql-sql-mode" % (get_docs_version(),), id='mysql.W002', )] return []
def _check_pattern_startswith_slash(self): """ Check that the pattern does not begin with a forward slash. """ regex_pattern = self.regex.pattern if not settings.APPEND_SLASH: # Skip check as it can be useful to start a URL pattern with a slash # when APPEND_SLASH=False. return [] if (regex_pattern.startswith('/') or regex_pattern.startswith('^/')) and not regex_pattern.endswith('/'): warning = Warning( "Your URL pattern {} has a regex beginning with a '/'. Remove this " "slash as it is unnecessary. If this pattern is targeted in an " "include(), ensure the include() pattern has a trailing '/'.".format( self.describe() ), id="urls.W002", ) return [warning] else: return []
def _check_ignored_options(self, **kwargs): warnings = [] if self.null: warnings.append( checks.Warning( 'null has no effect on ManyToManyField.', hint=None, obj=self, id='fields.W340', ) ) if len(self._validators) > 0: warnings.append( checks.Warning( 'ManyToManyField does not support validators.', hint=None, obj=self, id='fields.W341', ) ) return warnings
def _check_unique(self, **kwargs): return [ checks.Warning( 'Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.', hint='ForeignKey(unique=True) is usually better served by a OneToOneField.', obj=self, id='fields.W342', ) ] if self.unique else []
def _check_deprecation_details(self): if self.system_check_removed_details is not None: return [ checks.Error( self.system_check_removed_details.get( 'msg', '%s has been removed except for support in historical ' 'migrations.' % self.__class__.__name__ ), hint=self.system_check_removed_details.get('hint'), obj=self, id=self.system_check_removed_details.get('id', 'fields.EXXX'), ) ] elif self.system_check_deprecated_details is not None: return [ checks.Warning( self.system_check_deprecated_details.get( 'msg', '%s has been deprecated.' % self.__class__.__name__ ), hint=self.system_check_deprecated_details.get('hint'), obj=self, id=self.system_check_deprecated_details.get('id', 'fields.WXXX'), ) ] return []
def _check_max_length_warning(self): if self.max_length is not None: return [ checks.Warning( "'max_length' is ignored when used with IntegerField", hint="Remove 'max_length' from field", obj=self, id='fields.W122', ) ] return []
def _check_search_fields(cls, **kwargs): errors = [] for field in cls.get_search_fields(): message = "{model}.search_fields contains field '{name}' but it doesn't exist" if not cls._has_field(field.field_name): errors.append( checks.Warning( message.format(model=cls.__name__, name=field.field_name), obj=cls, ) ) return errors
def check_library_versions(app_configs=None, **kwargs): from django import VERSION as DJANGO_VERSION from lxml.etree import LXML_VERSION from translate.__version__ import ver as ttk_version errors = [] if DJANGO_VERSION < DJANGO_MINIMUM_REQUIRED_VERSION: errors.append(checks.Critical( _("Your version of Django is too old."), hint=_("Try pip install --upgrade 'Django==%s'", _version_to_string(DJANGO_MINIMUM_REQUIRED_VERSION)), id="pootle.C002", )) if LXML_VERSION < LXML_MINIMUM_REQUIRED_VERSION: errors.append(checks.Warning( _("Your version of lxml is too old."), hint=_("Try pip install --upgrade lxml"), id="pootle.W003", )) if ttk_version < TTK_MINIMUM_REQUIRED_VERSION: errors.append(checks.Critical( _("Your version of Translate Toolkit is too old."), hint=_("Try pip install --upgrade translate-toolkit"), id="pootle.C003", )) return errors
def check_redis(app_configs=None, **kwargs): from django_rq.queues import get_queue from django_rq.workers import Worker errors = [] try: queue = get_queue() Worker.all(queue.connection) except Exception as e: conn_settings = queue.connection.connection_pool.connection_kwargs errors.append(checks.Critical( _("Could not connect to Redis (%s)", e), hint=_("Make sure Redis is running on " "%(host)s:%(port)s") % conn_settings, id="pootle.C001", )) else: redis_version = tuple(int(x) for x in (queue.connection .info()["redis_version"].split("."))) if redis_version < REDIS_MINIMUM_REQUIRED_VERSION: errors.append(checks.Critical( _("Your version of Redis is too old."), hint=_("Update your system's Redis server package to at least " "version %s", str(REDIS_MINIMUM_REQUIRED_VERSION)), id="pootle.C007", )) if len(queue.connection.smembers(Worker.redis_workers_keys)) == 0: # We need to check we're not running manage.py rqworker right now.. import sys if len(sys.argv) > 1 and sys.argv[1] in RQWORKER_WHITELIST: errors.append(checks.Warning( _("No RQ Worker running."), hint=_("Run new workers with manage.py rqworker"), id="pootle.W001", )) return errors
def warning(app_configs, **kwargs): return [checks.Warning('some warning', id='tests.checks.W001')]
def check_migrations_applied(app_configs, **kwargs): """ A Django check to see if all migrations have been applied correctly. """ from django.db.migrations.loader import MigrationLoader errors = [] # Load migrations from disk/DB try: loader = MigrationLoader(connection, ignore_no_migrations=True) except (ImproperlyConfigured, ProgrammingError, OperationalError): msg = "Can't connect to database to check migrations" return [checks.Info(msg, id=INFO_CANT_CHECK_MIGRATIONS)] if app_configs: app_labels = [app.label for app in app_configs] else: app_labels = loader.migrated_apps for node, migration in loader.graph.nodes.items(): if migration.app_label not in app_labels: continue if node not in loader.applied_migrations: msg = 'Unapplied migration {}'.format(migration) # NB: This *must* be a Warning, not an Error, because Errors # prevent migrations from being run. errors.append(checks.Warning(msg, id=WARNING_UNAPPLIED_MIGRATION)) return errors
def _check_ignored_options(self, **kwargs): warnings = [] if self.has_null_arg: warnings.append( checks.Warning( 'null has no effect on ManyToManyField.', obj=self, id='fields.W340', ) ) if len(self._validators) > 0: warnings.append( checks.Warning( 'ManyToManyField does not support validators.', obj=self, id='fields.W341', ) ) if (self.remote_field.limit_choices_to and self.remote_field.through and not self.remote_field.through._meta.auto_created): warnings.append( checks.Warning( 'limit_choices_to has no effect on ManyToManyField ' 'with a through model.', obj=self, id='fields.W343', ) ) return warnings
def _check_pattern_name(self): """ Check that the pattern name does not contain a colon. """ if self.name is not None and ":" in self.name: warning = Warning( "Your URL pattern {} has a name including a ':'. Remove the colon, to " "avoid ambiguous namespace references.".format(self.describe()), id="urls.W003", ) return [warning] else: return []
def _check_include_trailing_dollar(self): """ Check that include is not used with a regex ending with a dollar. """ regex_pattern = self.regex.pattern if regex_pattern.endswith('$') and not regex_pattern.endswith(r'\$'): warning = Warning( "Your URL pattern {} uses include with a regex ending with a '$'. " "Remove the dollar from the regex to avoid problems including " "URLs.".format(self.describe()), id="urls.W001", ) return [warning] else: return []
def check_speedups(app_configs, **kwargs): errors = [] if not speedups.available: errors.append( checks.Warning( 'Your shapely version does not have speedups enabled. This will significantly slow down c3nav!', obj='rtree.index.Index', id='c3nav.mapdata.W001', ) ) return errors
def check_svg_renderer(app_configs, **kwargs): errors = [] if not rtree_index: errors.append( checks.Warning( 'The libspatialindex_c library is missing. This will slow down c3nav in future versions.', obj='rtree.index.Index', id='c3nav.mapdata.W002', ) ) return errors
def _check_salt_is_set(self): if self.salt is None or self.salt == "": return [ checks.Warning( "'salt' is not set", hint="Pass a salt value in your field or set settings.HASHID_FIELD_SALT", obj=self, id="HashidField.W001", ) ] return []
def test_context_processor_not_installed_in_TEMPLATES(self): templates = deepcopy(settings.TEMPLATES) templates[0]['OPTIONS']['context_processors'].remove(apps.CONTEXT) with self.settings(TEMPLATES=templates): errors = apps.check_context_processor_installed() self.assertEqual(1, len(errors)) self.assertTrue(isinstance(errors[0], checks.Warning)) self.assertEqual('boardinghouse.W001', errors[0].id)
def test_no_context_processors_found(self): del settings.TEMPLATES errors = apps.check_context_processor_installed() self.assertEqual(1, len(errors)) self.assertTrue(isinstance(errors[0], checks.Warning)) self.assertEqual('boardinghouse.W001', errors[0].id)
def check_context_processor_installed(app_configs=None, **kwargs): "Warn if our context processor is not installed." from django.conf import settings errors = [] if hasattr(settings, 'TEMPLATES'): for i, engine in enumerate(settings.TEMPLATES): # We only check for the context processor if using the default django backend. if engine['BACKEND'] != 'django.template.backends.django.DjangoTemplates': continue if CONTEXT not in engine.get('OPTIONS', {}).get('context_processors', []): errors.append(Warning( 'Missing boardinghouse context processor', hint="Add '{1}' to settings.TEMPLATES[{0}]" "['OPTIONS']['context_processors']".format(i, CONTEXT), id='boardinghouse.W001' )) elif hasattr(settings, 'TEMPLATE_CONTEXT_PROCESSORS'): if CONTEXT not in settings.TEMPLATE_CONTEXT_PROCESSORS: errors.append(Warning( 'Missing boardinghouse context processor', hint="Add '{0}' to settings.TEMPLATE_CONTEXT_PROCESSORS".format(CONTEXT), id='boardinghouse.W001' )) else: errors.append(Warning( 'Missing boardinghouse context processor (no TEMPLATES defined)', hint="Configure settings.TEMPLATES and add '{0}'".format(CONTEXT), id='boardinghouse.W001', )) return errors
def check_user_model(**kwargs): errors = [] cls = apps.get_model(settings.AUTH_USER_MODEL) # Check that REQUIRED_FIELDS is a list if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)): errors.append( checks.Error( "'REQUIRED_FIELDS' must be a list or tuple.", hint=None, obj=cls, id='auth.E001', ) ) # Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS. if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS: errors.append( checks.Error( ("The field named as the 'USERNAME_FIELD' " "for a custom user model must not be included in 'REQUIRED_FIELDS'."), hint=None, obj=cls, id='auth.E002', ) ) # Check that the username field is unique if not cls._meta.get_field(cls.USERNAME_FIELD).unique: if (settings.AUTHENTICATION_BACKENDS == ['django.contrib.auth.backends.ModelBackend']): errors.append( checks.Error( "'%s.%s' must be unique because it is named as the 'USERNAME_FIELD'." % ( cls._meta.object_name, cls.USERNAME_FIELD ), hint=None, obj=cls, id='auth.E003', ) ) else: errors.append( checks.Warning( "'%s.%s' is named as the 'USERNAME_FIELD', but it is not unique." % ( cls._meta.object_name, cls.USERNAME_FIELD ), hint=('Ensure that your authentication backend(s) can handle ' 'non-unique usernames.'), obj=cls, id='auth.W004', ) ) return errors
def _check_fix_default_value(self): """ Adds a warning to the checks framework stating, that using an actual date or datetime value is probably wrong; it's only being evaluated on server start-up. For details see ticket #21905 """ if not self.has_default(): return [] now = timezone.now() if not timezone.is_naive(now): now = timezone.make_naive(now, timezone.utc) value = self.default if isinstance(value, datetime.datetime): if not timezone.is_naive(value): value = timezone.make_naive(value, timezone.utc) value = value.date() elif isinstance(value, datetime.date): # Nothing to do, as dates don't have tz information pass else: # No explicit date / datetime value -- no checks necessary return [] offset = datetime.timedelta(days=1) lower = (now - offset).date() upper = (now + offset).date() if lower <= value <= upper: return [ checks.Warning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=self, id='fields.W161', ) ] return []
def _check_fix_default_value(self): """ Adds a warning to the checks framework stating, that using an actual time or datetime value is probably wrong; it's only being evaluated on server start-up. For details see ticket #21905 """ if not self.has_default(): return [] now = timezone.now() if not timezone.is_naive(now): now = timezone.make_naive(now, timezone.utc) value = self.default if isinstance(value, datetime.datetime): second_offset = datetime.timedelta(seconds=10) lower = now - second_offset upper = now + second_offset if timezone.is_aware(value): value = timezone.make_naive(value, timezone.utc) elif isinstance(value, datetime.time): second_offset = datetime.timedelta(seconds=10) lower = now - second_offset upper = now + second_offset value = datetime.datetime.combine(now.date(), value) if timezone.is_aware(value): value = timezone.make_naive(value, timezone.utc).time() else: # No explicit time / datetime value -- no checks necessary return [] if lower <= value <= upper: return [ checks.Warning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=self, id='fields.W161', ) ] return []