checks.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import re
  2. from collections import Sequence
  3. from numbers import Integral
  4. from django.core import checks
  5. from django.utils import six
  6. from .conf import conf
  7. re_type = type(re.compile(''))
  8. @checks.register
  9. def check_settings(app_configs, **kwargs):
  10. errors = []
  11. if not is_sequence(conf.CORS_ALLOW_HEADERS, six.string_types):
  12. errors.append(
  13. checks.Error(
  14. "CORS_ALLOW_HEADERS should be a sequence of strings.",
  15. id="corsheaders.E001"
  16. )
  17. )
  18. if not is_sequence(conf.CORS_ALLOW_METHODS, six.string_types):
  19. errors.append(
  20. checks.Error(
  21. "CORS_ALLOW_METHODS should be a sequence of strings.",
  22. id="corsheaders.E002"
  23. )
  24. )
  25. if not isinstance(conf.CORS_ALLOW_CREDENTIALS, bool):
  26. errors.append(
  27. checks.Error(
  28. "CORS_ALLOW_CREDENTIALS should be a bool.",
  29. id="corsheaders.E003"
  30. )
  31. )
  32. if not isinstance(conf.CORS_PREFLIGHT_MAX_AGE, Integral) or conf.CORS_PREFLIGHT_MAX_AGE < 0:
  33. errors.append(
  34. checks.Error(
  35. "CORS_PREFLIGHT_MAX_AGE should be an integer greater than or equal to zero.",
  36. id="corsheaders.E004"
  37. )
  38. )
  39. if not isinstance(conf.CORS_ORIGIN_ALLOW_ALL, bool):
  40. errors.append(
  41. checks.Error(
  42. "CORS_ORIGIN_ALLOW_ALL should be a bool.",
  43. id="corsheaders.E005"
  44. )
  45. )
  46. if not is_sequence(conf.CORS_ORIGIN_WHITELIST, six.string_types):
  47. errors.append(
  48. checks.Error(
  49. "CORS_ORIGIN_WHITELIST should be a sequence of strings.",
  50. id="corsheaders.E006"
  51. )
  52. )
  53. if not is_sequence(conf.CORS_ORIGIN_REGEX_WHITELIST, six.string_types + (re_type,)):
  54. errors.append(
  55. checks.Error(
  56. "CORS_ORIGIN_REGEX_WHITELIST should be a sequence of strings and/or compiled regexes.",
  57. id="corsheaders.E007"
  58. )
  59. )
  60. if not is_sequence(conf.CORS_EXPOSE_HEADERS, six.string_types):
  61. errors.append(
  62. checks.Error(
  63. "CORS_EXPOSE_HEADERS should be a sequence.",
  64. id="corsheaders.E008"
  65. )
  66. )
  67. if not isinstance(conf.CORS_URLS_REGEX, six.string_types + (re_type,)):
  68. errors.append(
  69. checks.Error(
  70. "CORS_URLS_REGEX should be a string or regex.",
  71. id="corsheaders.E009"
  72. )
  73. )
  74. if conf.CORS_MODEL is not None and not isinstance(conf.CORS_MODEL, six.string_types):
  75. errors.append(
  76. checks.Error(
  77. "CORS_MODEL should be a string or None.",
  78. id="corsheaders.E010"
  79. )
  80. )
  81. if not isinstance(conf.CORS_REPLACE_HTTPS_REFERER, bool):
  82. errors.append(
  83. checks.Error(
  84. "CORS_REPLACE_HTTPS_REFERER should be a bool.",
  85. id="corsheaders.E011"
  86. )
  87. )
  88. return errors
  89. def is_sequence(thing, types):
  90. return (
  91. isinstance(thing, Sequence) and
  92. all(isinstance(x, types) for x in thing)
  93. )