| 1 |
from itertools import chain |
| 2 |
|
| 3 |
from django.apps import apps |
| 4 |
from django.core.checks import Error |
| 5 |
|
| 6 |
|
| 7 |
def check_generic_foreign_keys(app_configs=None, **kwargs): |
| 8 |
from .fields import GenericForeignKey |
| 9 |
|
| 10 |
if app_configs is None: |
| 11 |
models = apps.get_models() |
| 12 |
else: |
| 13 |
models = chain.from_iterable( |
| 14 |
app_config.get_models() for app_config in app_configs |
| 15 |
) |
| 16 |
errors = [] |
| 17 |
fields = ( |
| 18 |
obj |
| 19 |
for model in models |
| 20 |
for obj in vars(model).values() |
| 21 |
if isinstance(obj, GenericForeignKey) |
| 22 |
) |
| 23 |
for field in fields: |
| 24 |
errors.extend(field.check()) |
| 25 |
return errors |
| 26 |
|
| 27 |
|
| 28 |
def check_model_name_lengths(app_configs=None, **kwargs): |
| 29 |
if app_configs is None: |
| 30 |
models = apps.get_models() |
| 31 |
else: |
| 32 |
models = chain.from_iterable( |
| 33 |
app_config.get_models() for app_config in app_configs |
| 34 |
) |
| 35 |
errors = [] |
| 36 |
for model in models: |
| 37 |
if len(model._meta.model_name) > 100: |
| 38 |
errors.append( |
| 39 |
Error( |
| 40 |
"Model names must be at most 100 characters (got %d)." |
| 41 |
% (len(model._meta.model_name),), |
| 42 |
obj=model, |
| 43 |
id="contenttypes.E005", |
| 44 |
) |
| 45 |
) |
| 46 |
return errors |