Django Library Users
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from django.contrib.auth.models import User
  2. from django.utils import timezone
  3. from django.utils import translation
  4. import pytz
  5. class SettingsMiddleware(object):
  6. def __init__(self, get_response):
  7. self.get_response = get_response
  8. def __call__(self, request):
  9. #
  10. # Set Timzone
  11. #
  12. if request.user.is_authenticated:
  13. try:
  14. timezone.activate(pytz.timezone(request.user.userprofile.timezone))
  15. except User.userprofile.RelatedObjectDoesNotExist:
  16. pass # No profile
  17. except AttributeError:
  18. pass # No User logged in
  19. else:
  20. timezone.deactivate()
  21. #
  22. # Set Language
  23. #
  24. try:
  25. language = request.user.userprofile.language_code
  26. except User.userprofile.RelatedObjectDoesNotExist:
  27. pass # No profile
  28. except AttributeError:
  29. pass # No User logged in
  30. else:
  31. translation.activate(language)
  32. request.LANGUAGE_CODE = translation.get_language()
  33. response = self.get_response(request)
  34. # Code to be executed for each request/response after
  35. # the view is called.
  36. return response