Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

django_wsgi.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. """ module used to build the django wsgi application """
  6. from __future__ import print_function
  7. import os
  8. import re
  9. import sys
  10. import time
  11. try:
  12. from StringIO import StringIO
  13. except:
  14. from io import StringIO
  15. from imp import reload
  16. from django.conf import settings
  17. from django.core.management.validation import get_validation_errors
  18. from django.utils import translation
  19. try:
  20. from django.core.servers.basehttp import get_internal_wsgi_application
  21. django14 = True
  22. except ImportError:
  23. from django.core.handlers.wsgi import WSGIHandler
  24. django14 = False
  25. from gunicorn import util
  26. def make_wsgi_application():
  27. # validate models
  28. s = StringIO()
  29. if get_validation_errors(s):
  30. s.seek(0)
  31. error = s.read()
  32. msg = "One or more models did not validate:\n%s" % error
  33. print(msg, file=sys.stderr)
  34. sys.stderr.flush()
  35. sys.exit(1)
  36. translation.activate(settings.LANGUAGE_CODE)
  37. if django14:
  38. return get_internal_wsgi_application()
  39. return WSGIHandler()
  40. def reload_django_settings():
  41. mod = util.import_module(os.environ['DJANGO_SETTINGS_MODULE'])
  42. # Reload module.
  43. reload(mod)
  44. # Reload settings.
  45. # Use code from django.settings.Settings module.
  46. # Settings that should be converted into tuples if they're mistakenly entered
  47. # as strings.
  48. tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
  49. for setting in dir(mod):
  50. if setting == setting.upper():
  51. setting_value = getattr(mod, setting)
  52. if setting in tuple_settings and type(setting_value) == str:
  53. setting_value = (setting_value,) # In case the user forgot the comma.
  54. setattr(settings, setting, setting_value)
  55. # Expand entries in INSTALLED_APPS like "django.contrib.*" to a list
  56. # of all those apps.
  57. new_installed_apps = []
  58. for app in settings.INSTALLED_APPS:
  59. if app.endswith('.*'):
  60. app_mod = util.import_module(app[:-2])
  61. appdir = os.path.dirname(app_mod.__file__)
  62. app_subdirs = os.listdir(appdir)
  63. name_pattern = re.compile(r'[a-zA-Z]\w*')
  64. for d in sorted(app_subdirs):
  65. if (name_pattern.match(d) and
  66. os.path.isdir(os.path.join(appdir, d))):
  67. new_installed_apps.append('%s.%s' % (app[:-2], d))
  68. else:
  69. new_installed_apps.append(app)
  70. setattr(settings, "INSTALLED_APPS", new_installed_apps)
  71. if hasattr(time, 'tzset') and settings.TIME_ZONE:
  72. # When we can, attempt to validate the timezone. If we can't find
  73. # this file, no check happens and it's harmless.
  74. zoneinfo_root = '/usr/share/zoneinfo'
  75. if (os.path.exists(zoneinfo_root) and not
  76. os.path.exists(os.path.join(zoneinfo_root,
  77. *(settings.TIME_ZONE.split('/'))))):
  78. raise ValueError("Incorrect timezone setting: %s" %
  79. settings.TIME_ZONE)
  80. # Move the time zone info into os.environ. See ticket #2315 for why
  81. # we don't do this unconditionally (breaks Windows).
  82. os.environ['TZ'] = settings.TIME_ZONE
  83. time.tzset()
  84. # Settings are configured, so we can set up the logger if required
  85. if getattr(settings, 'LOGGING_CONFIG', False):
  86. # First find the logging configuration function ...
  87. logging_config_path, logging_config_func_name = settings.LOGGING_CONFIG.rsplit('.', 1)
  88. logging_config_module = util.import_module(logging_config_path)
  89. logging_config_func = getattr(logging_config_module, logging_config_func_name)
  90. # ... then invoke it with the logging settings
  91. logging_config_func(settings.LOGGING)
  92. def make_command_wsgi_application(admin_mediapath):
  93. reload_django_settings()
  94. try:
  95. from django.core.servers.basehttp import AdminMediaHandler
  96. return AdminMediaHandler(make_wsgi_application(), admin_mediapath)
  97. except ImportError:
  98. return make_wsgi_application()