You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 1.1KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.http import HttpResponseBadRequest
  4. from .utils import add_default_headers
  5. def events(request, **kwargs):
  6. from .eventrequest import EventRequest
  7. from .eventstream import EventPermissionError, get_events
  8. from .utils import sse_error_response
  9. try:
  10. event_request = EventRequest(request, view_kwargs=kwargs)
  11. event_response = get_events(event_request)
  12. response = event_response.to_http_response(request)
  13. except EventRequest.ResumeNotAllowedError as e:
  14. response = HttpResponseBadRequest(
  15. 'Invalid request: %s.\n' % str(e))
  16. except EventRequest.GripError as e:
  17. if request.grip.proxied:
  18. response = sse_error_response(
  19. 'internal-error',
  20. 'Invalid internal request.')
  21. else:
  22. response = sse_error_response(
  23. 'bad-request',
  24. 'Invalid request: %s.' % str(e))
  25. except EventRequest.Error as e:
  26. response = sse_error_response(
  27. 'bad-request',
  28. 'Invalid request: %s.' % str(e))
  29. except EventPermissionError as e:
  30. response = sse_error_response(
  31. 'forbidden',
  32. str(e),
  33. {'channels': e.channels})
  34. add_default_headers(response)
  35. return response