conf.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import annotations
  2. from typing import List
  3. from typing import Pattern
  4. from typing import Sequence
  5. from typing import Tuple
  6. from typing import Union
  7. from typing import cast
  8. from django.conf import settings
  9. from corsheaders.defaults import default_headers
  10. from corsheaders.defaults import default_methods
  11. class Settings:
  12. """
  13. Shadow Django's settings with a little logic
  14. """
  15. @property
  16. def CORS_ALLOW_HEADERS(self) -> Sequence[str]:
  17. return getattr(settings, "CORS_ALLOW_HEADERS", default_headers)
  18. @property
  19. def CORS_ALLOW_METHODS(self) -> Sequence[str]:
  20. return getattr(settings, "CORS_ALLOW_METHODS", default_methods)
  21. @property
  22. def CORS_ALLOW_CREDENTIALS(self) -> bool:
  23. return getattr(settings, "CORS_ALLOW_CREDENTIALS", False)
  24. @property
  25. def CORS_ALLOW_PRIVATE_NETWORK(self) -> bool:
  26. return getattr(settings, "CORS_ALLOW_PRIVATE_NETWORK", False)
  27. @property
  28. def CORS_PREFLIGHT_MAX_AGE(self) -> int:
  29. return getattr(settings, "CORS_PREFLIGHT_MAX_AGE", 86400)
  30. @property
  31. def CORS_ALLOW_ALL_ORIGINS(self) -> bool:
  32. return getattr(
  33. settings,
  34. "CORS_ALLOW_ALL_ORIGINS",
  35. getattr(settings, "CORS_ORIGIN_ALLOW_ALL", False),
  36. )
  37. @property
  38. def CORS_ALLOWED_ORIGINS(self) -> list[str] | tuple[str]:
  39. value = getattr(
  40. settings,
  41. "CORS_ALLOWED_ORIGINS",
  42. getattr(settings, "CORS_ORIGIN_WHITELIST", ()),
  43. )
  44. return cast(Union[List[str], Tuple[str]], value)
  45. @property
  46. def CORS_ALLOWED_ORIGIN_REGEXES(self) -> Sequence[str | Pattern[str]]:
  47. return getattr(
  48. settings,
  49. "CORS_ALLOWED_ORIGIN_REGEXES",
  50. getattr(settings, "CORS_ORIGIN_REGEX_WHITELIST", ()),
  51. )
  52. @property
  53. def CORS_EXPOSE_HEADERS(self) -> Sequence[str]:
  54. return getattr(settings, "CORS_EXPOSE_HEADERS", ())
  55. @property
  56. def CORS_URLS_REGEX(self) -> str | Pattern[str]:
  57. return getattr(settings, "CORS_URLS_REGEX", r"^.*$")
  58. conf = Settings()