diff --git a/drupal2spip_lal/base/apps.py b/drupal2spip_lal/base/apps.py index cd9faf9..51b0108 100644 --- a/drupal2spip_lal/base/apps.py +++ b/drupal2spip_lal/base/apps.py @@ -4,3 +4,6 @@ from django.apps import AppConfig class BaseConfig(AppConfig): name = 'drupal2spip_lal.base' verbose_name = "Base" + + def ready(self): + from . import checks # noqa diff --git a/drupal2spip_lal/base/checks.py b/drupal2spip_lal/base/checks.py new file mode 100644 index 0000000..6752d12 --- /dev/null +++ b/drupal2spip_lal/base/checks.py @@ -0,0 +1,61 @@ +import io +from difflib import unified_diff + +from django.core.checks import Error, Warning, register +from django.core.management import call_command + +no_models = Warning( + "Unable to import drupal2spip_lal/drupal/models.py", + hint='Verify your database settings and that your DB has been ' + 'inspected.', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.W003', +) + + +@register() +def check_inspected_database(app_configs, **kwargs): + buf = io.StringIO() + rc = call_command('inspectdb', '--database=drupal', stdout=buf) + if rc is not None: + return [ + Error( + "Command inspectdb failed.", + hint='Try to launch "./manage.py inspectdb --database=drupal" ' + 'and report an issue to .', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.E001', + ) + ] + + buf.seek(0) + inspected_db = buf.read() + buf.close() + try: + with open('drupal2spip_lal/drupal/models.py', 'r') as f: + if f.read() != inspected_db: + f.seek(0) + for line in unified_diff( + f.read().split('\n'), inspected_db.split('\n') + ): + print(line) + return [ + Warning( + "Outdated drupal2spip_lal/drupal/models.py", + hint='Refresh it using "make inspectdb" ' + 'and then run migrations.', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.W002', + ) + ] + except Exception: + return [ + Warning( + "Missing drupal2spip_lal/drupal/models.py", + hint='Create it using "make inspectdb" ' + 'and then run migrations.', + obj='drupal2spip_lal.base', + id='drupal2spip_lal.W001', + ) + ] + return []