uiviews.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. Misago UI views controller
  3. UI views are small views that are called asynchronically to give UI knowledge
  4. of changes in APP state and thus opportunity to update themselves in real time
  5. """
  6. from django.core.exceptions import PermissionDenied
  7. from django.core.urlresolvers import resolve
  8. from django.http import Http404, JsonResponse
  9. from misago.core.decorators import ajax_only
  10. from misago.core.utils import is_referer_local
  11. __all__ = ['uiview', 'uiserver']
  12. UI_VIEWS = []
  13. def uiview(name):
  14. """
  15. Decorator for registering UI views
  16. """
  17. def namespace_decorator(f):
  18. UI_VIEWS.append((name, f))
  19. return f
  20. return namespace_decorator
  21. def get_resolver_match(request):
  22. requesting_path = request.META['HTTP_REFERER']
  23. requesting_path = requesting_path[len(request.scheme) + 3:]
  24. requesting_path = requesting_path[len(request.META['HTTP_HOST']):]
  25. return resolve(requesting_path)
  26. @ajax_only
  27. def uiserver(request):
  28. if not is_referer_local(request):
  29. raise PermissionDenied()
  30. resolver_match = get_resolver_match(request)
  31. response_dict = {'total_count': 0}
  32. for name, view in UI_VIEWS:
  33. try:
  34. view_response = view(request, resolver_match)
  35. if view_response:
  36. response_dict['total_count'] += view_response.get('count', 0)
  37. response_dict[name] = view_response
  38. except (Http404, PermissionDenied):
  39. pass
  40. return JsonResponse(response_dict)