template.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.template import RequestContext as DjangoRequestContext
  4. from django.utils import timezone
  5. from django.utils.importlib import import_module
  6. from misago.users.models import User
  7. def RequestContext(request, context=None):
  8. if not context:
  9. context = {}
  10. context['fallback'] = request.path
  11. # Find out if we ignore or follow this user
  12. context['follows'] = False
  13. context['ignores'] = False
  14. if request.user.is_authenticated() and request.user.pk != context['profile'].pk:
  15. context['follows'] = request.user.is_following(context['profile'])
  16. context['ignores'] = request.user.is_ignoring(context['profile'])
  17. # Find out if this user allows us to see his activity
  18. if request.user.pk != context['profile'].pk:
  19. if context['profile'].hide_activity == 2:
  20. context['hidden'] = True
  21. if context['profile'].hide_activity == 1:
  22. context['hidden'] = context['profile'].is_following(request.user)
  23. else:
  24. context['hidden'] = False
  25. # Find out if this user is online:
  26. if request.user.pk != context['profile'].pk:
  27. try:
  28. context['online'] = context['profile'].sessions.filter(admin=False).filter(last__gt=(timezone.now() - timedelta(minutes=10))).order_by('-last')[0:1][0]
  29. except IndexError:
  30. context['online'] = False
  31. else:
  32. # Fake "right now" time
  33. context['online'] = {'last': timezone.now()}
  34. context['tabs'] = []
  35. for extension in settings.PROFILE_EXTENSIONS:
  36. profile_module = import_module(extension + '.profile')
  37. try:
  38. append_links = profile_module.register_profile_extension(request)
  39. if append_links:
  40. for link in append_links:
  41. link = list(link)
  42. token = link[0][link[0].find('_') + 1:]
  43. context['tabs'].append({
  44. 'route': link[0],
  45. 'active': context['tab'] == token,
  46. 'name': link[1],
  47. })
  48. except AttributeError:
  49. pass
  50. return DjangoRequestContext(request, context)