checks.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import os
  2. from django.core.checks import Error, Tags, Warning, register
  3. @register("staticfiles")
  4. def css_install_check(app_configs, **kwargs):
  5. errors = []
  6. css_path = os.path.join(
  7. os.path.dirname(__file__), "static", "wagtailadmin", "css", "normalize.css"
  8. )
  9. if not os.path.isfile(css_path):
  10. error_hint = (
  11. """
  12. Most likely you are running a development (non-packaged) copy of
  13. Wagtail and have not built the static assets -
  14. see https://docs.wagtail.org/en/latest/contributing/developing.html
  15. File not found: %s
  16. """
  17. % css_path
  18. )
  19. errors.append(
  20. Warning(
  21. "CSS for the Wagtail admin is missing",
  22. hint=error_hint,
  23. id="wagtailadmin.W001",
  24. )
  25. )
  26. return errors
  27. @register(Tags.admin)
  28. def base_form_class_check(app_configs, **kwargs):
  29. from wagtail.admin.forms import WagtailAdminPageForm
  30. from wagtail.models import get_page_models
  31. errors = []
  32. for cls in get_page_models():
  33. if not issubclass(cls.base_form_class, WagtailAdminPageForm):
  34. errors.append(
  35. Error(
  36. "{}.base_form_class does not extend WagtailAdminPageForm".format(
  37. cls.__name__
  38. ),
  39. hint="Ensure that {}.{} extends WagtailAdminPageForm".format(
  40. cls.base_form_class.__module__, cls.base_form_class.__name__
  41. ),
  42. obj=cls,
  43. id="wagtailadmin.E001",
  44. )
  45. )
  46. return errors
  47. @register(Tags.admin)
  48. def get_form_class_check(app_configs, **kwargs):
  49. from wagtail.admin.forms import WagtailAdminPageForm
  50. from wagtail.models import get_page_models
  51. errors = []
  52. for cls in get_page_models():
  53. edit_handler = cls.get_edit_handler()
  54. if not issubclass(edit_handler.get_form_class(), WagtailAdminPageForm):
  55. errors.append(
  56. Error(
  57. "{cls}.get_edit_handler().get_form_class() does not extend WagtailAdminPageForm".format(
  58. cls=cls.__name__
  59. ),
  60. hint="Ensure that the EditHandler for {cls} creates a subclass of WagtailAdminPageForm".format(
  61. cls=cls.__name__
  62. ),
  63. obj=cls,
  64. id="wagtailadmin.E002",
  65. )
  66. )
  67. return errors
  68. @register("panels")
  69. def inline_panel_model_panels_check(app_configs, **kwargs):
  70. from wagtail.models import get_page_models
  71. errors = []
  72. page_models = get_page_models()
  73. for cls in page_models:
  74. errors.extend(check_panels_in_model(cls))
  75. # filter out duplicate errors found for the same model
  76. unique_errors = []
  77. for error in errors:
  78. if error.msg not in [e.msg for e in unique_errors]:
  79. unique_errors.append(error)
  80. return unique_errors
  81. def check_panels_in_model(cls, context="model"):
  82. """Check panels configuration uses `panels` when `edit_handler` not in use."""
  83. from wagtail.admin.edit_handlers import BaseCompositeEditHandler, InlinePanel
  84. from wagtail.models import Page
  85. errors = []
  86. if hasattr(cls, "get_edit_handler"):
  87. # must check the InlinePanel related models
  88. edit_handler = cls.get_edit_handler()
  89. for tab in edit_handler.children:
  90. if isinstance(tab, BaseCompositeEditHandler):
  91. inline_panel_children = [
  92. panel for panel in tab.children if isinstance(panel, InlinePanel)
  93. ]
  94. for inline_panel_child in inline_panel_children:
  95. errors.extend(
  96. check_panels_in_model(
  97. inline_panel_child.db_field.related_model,
  98. context="InlinePanel model",
  99. )
  100. )
  101. if issubclass(cls, Page) or hasattr(cls, "edit_handler"):
  102. # Pages do not need to be checked for standalone tabbed_panel usage
  103. # if edit_handler is used on any model, assume config is correct
  104. return errors
  105. tabbed_panels = [
  106. "content_panels",
  107. "promote_panels",
  108. "settings_panels",
  109. ]
  110. for panel_name in tabbed_panels:
  111. class_name = cls.__name__
  112. if not hasattr(cls, panel_name):
  113. continue
  114. panel_name_short = panel_name.replace("_panels", "").title()
  115. error_title = "{}.{} will have no effect on {} editing".format(
  116. class_name, panel_name, context
  117. )
  118. if "InlinePanel" in context:
  119. error_hint = """Ensure that {} uses `panels` instead of `{}`.
  120. There are no tabs on non-Page model editing within InlinePanels.""".format(
  121. class_name, panel_name
  122. )
  123. else:
  124. error_hint = """Ensure that {} uses `panels` instead of `{}`\
  125. or set up an `edit_handler` if you want a tabbed editing interface.
  126. There are no default tabs on non-Page models so there will be no \
  127. {} tab for the {} to render in.""".format(
  128. class_name, panel_name, panel_name_short, panel_name
  129. )
  130. error = Warning(error_title, hint=error_hint, obj=cls, id="wagtailadmin.W002")
  131. errors.append(error)
  132. return errors