feat(check): vérifie la cohérence de la description de la base de donnée de drupal

This commit is contained in:
François Poulain 2020-08-01 11:16:45 +02:00
parent 65fef05364
commit c5e369e873
2 changed files with 64 additions and 0 deletions

View File

@ -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

View File

@ -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 <tech@cliss21.org>.',
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 []