template.py 2.4 KB

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