template.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.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. # Sync member
  35. if context['profile'].sync_profile():
  36. context['profile'].save(force_update=True)
  37. context['tabs'] = []
  38. for extension in settings.PROFILE_EXTENSIONS:
  39. profile_module = import_module(extension + '.profile')
  40. try:
  41. append_links = profile_module.register_profile_extension(request, context['profile'])
  42. if append_links:
  43. for link in append_links:
  44. link = list(link)
  45. token = link[0][link[0].find('_') + 1:]
  46. context['tabs'].append({
  47. 'route': link[0],
  48. 'active': context['tab'] == token,
  49. 'name': link[1],
  50. })
  51. except AttributeError:
  52. pass
  53. return DjangoRequestContext(request, context)