1
0

accept.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from ..datastructures import CharsetAccept
  2. from ..datastructures import LanguageAccept
  3. from ..datastructures import MIMEAccept
  4. from ..http import parse_accept_header
  5. from ..utils import cached_property
  6. class AcceptMixin(object):
  7. """A mixin for classes with an :attr:`~BaseResponse.environ` attribute
  8. to get all the HTTP accept headers as
  9. :class:`~werkzeug.datastructures.Accept` objects (or subclasses
  10. thereof).
  11. """
  12. @cached_property
  13. def accept_mimetypes(self):
  14. """List of mimetypes this client supports as
  15. :class:`~werkzeug.datastructures.MIMEAccept` object.
  16. """
  17. return parse_accept_header(self.environ.get("HTTP_ACCEPT"), MIMEAccept)
  18. @cached_property
  19. def accept_charsets(self):
  20. """List of charsets this client supports as
  21. :class:`~werkzeug.datastructures.CharsetAccept` object.
  22. """
  23. return parse_accept_header(
  24. self.environ.get("HTTP_ACCEPT_CHARSET"), CharsetAccept
  25. )
  26. @cached_property
  27. def accept_encodings(self):
  28. """List of encodings this client accepts. Encodings in a HTTP term
  29. are compression encodings such as gzip. For charsets have a look at
  30. :attr:`accept_charset`.
  31. """
  32. return parse_accept_header(self.environ.get("HTTP_ACCEPT_ENCODING"))
  33. @cached_property
  34. def accept_languages(self):
  35. """List of languages this client accepts as
  36. :class:`~werkzeug.datastructures.LanguageAccept` object.
  37. .. versionchanged 0.5
  38. In previous versions this was a regular
  39. :class:`~werkzeug.datastructures.Accept` object.
  40. """
  41. return parse_accept_header(
  42. self.environ.get("HTTP_ACCEPT_LANGUAGE"), LanguageAccept
  43. )