2014-01-20 10:45:21 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-10-14 21:10:27 +08:00
|
|
|
import inspect
|
2014-01-20 10:45:21 +08:00
|
|
|
import types
|
2015-11-17 13:39:28 +08:00
|
|
|
from itertools import chain
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
from django.apps import apps
|
2014-10-14 21:10:27 +08:00
|
|
|
from django.core.checks import Error, Tags, register
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2014-04-10 20:45:48 +08:00
|
|
|
@register(Tags.models)
|
2014-01-20 10:45:21 +08:00
|
|
|
def check_all_models(app_configs=None, **kwargs):
|
2014-10-14 21:10:27 +08:00
|
|
|
errors = []
|
2015-11-17 13:39:28 +08:00
|
|
|
if app_configs is None:
|
|
|
|
models = apps.get_models()
|
|
|
|
else:
|
|
|
|
models = chain.from_iterable(app_config.get_models() for app_config in app_configs)
|
|
|
|
for model in models:
|
|
|
|
if not inspect.ismethod(model.check):
|
|
|
|
errors.append(
|
|
|
|
Error(
|
2016-02-13 00:36:46 +08:00
|
|
|
"The '%s.check()' class method is currently overridden by %r."
|
|
|
|
% (model.__name__, model.check),
|
2015-11-17 13:39:28 +08:00
|
|
|
obj=model,
|
|
|
|
id='models.E020'
|
2014-10-14 21:10:27 +08:00
|
|
|
)
|
2015-11-17 13:39:28 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
errors.extend(model.check(**kwargs))
|
2014-10-14 21:10:27 +08:00
|
|
|
return errors
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2014-04-10 20:45:48 +08:00
|
|
|
@register(Tags.models, Tags.signals)
|
2014-01-20 10:45:21 +08:00
|
|
|
def check_model_signals(app_configs=None, **kwargs):
|
2014-10-14 21:10:27 +08:00
|
|
|
"""
|
|
|
|
Ensure lazily referenced model signals senders are installed.
|
|
|
|
"""
|
|
|
|
# Avoid circular import
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2014-10-14 21:10:27 +08:00
|
|
|
errors = []
|
2014-01-20 10:45:21 +08:00
|
|
|
for name in dir(models.signals):
|
|
|
|
obj = getattr(models.signals, name)
|
|
|
|
if isinstance(obj, models.signals.ModelSignal):
|
|
|
|
for reference, receivers in obj.unresolved_references.items():
|
|
|
|
for receiver, _, _ in receivers:
|
|
|
|
# The receiver is either a function or an instance of class
|
|
|
|
# defining a `__call__` method.
|
|
|
|
if isinstance(receiver, types.FunctionType):
|
2014-03-03 19:29:50 +08:00
|
|
|
description = "The '%s' function" % receiver.__name__
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
2014-03-03 19:29:50 +08:00
|
|
|
description = "An instance of the '%s' class" % receiver.__class__.__name__
|
2014-01-20 10:45:21 +08:00
|
|
|
errors.append(
|
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"%s was connected to the '%s' signal "
|
2014-01-20 10:45:21 +08:00
|
|
|
"with a lazy reference to the '%s' sender, "
|
|
|
|
"which has not been installed." % (
|
|
|
|
description, name, '.'.join(reference)
|
|
|
|
),
|
|
|
|
obj=receiver.__module__,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='signals.E001'
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return errors
|