uiviews.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.users.online.tracker import mute_tracker
  10. from misago.core.decorators import ajax_only
  11. from misago.core.utils import is_referer_local
  12. __all__ = ['uiview', 'uiserver']
  13. UI_VIEWS = []
  14. def uiview(name):
  15. """
  16. Decorator for registering UI views
  17. """
  18. def namespace_decorator(f):
  19. UI_VIEWS.append((name, f))
  20. return f
  21. return namespace_decorator
  22. def get_resolver_match(request):
  23. requesting_path = request.META['HTTP_REFERER']
  24. requesting_path = requesting_path[len(request.scheme) + 3:]
  25. requesting_path = requesting_path[len(request.META['HTTP_HOST']):]
  26. return resolve(requesting_path)
  27. @ajax_only
  28. def uiserver(request):
  29. mute_tracker(request)
  30. if not is_referer_local(request):
  31. raise PermissionDenied()
  32. resolver_match = get_resolver_match(request)
  33. response_dict = {'total_count': 0}
  34. for name, view in UI_VIEWS:
  35. try:
  36. view_response = view(request, resolver_match)
  37. if view_response:
  38. response_dict['total_count'] += view_response.get('count', 0)
  39. response_dict[name] = view_response
  40. except (Http404, PermissionDenied):
  41. pass
  42. return JsonResponse(response_dict)