27 import os |
27 import os |
28 import sys |
28 import sys |
29 |
29 |
30 from google.appengine.ext.webapp import util |
30 from google.appengine.ext.webapp import util |
31 |
31 |
32 |
32 import gae_django |
33 # Remove the standard version of Django. |
|
34 for k in [k for k in sys.modules if k.startswith('django')]: |
|
35 del sys.modules[k] |
|
36 |
|
37 # Force sys.path to have our own directory first, in case we want to import |
|
38 # from it. This lets us replace the built-in Django |
|
39 sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) |
|
40 |
|
41 sys.path.insert(0, os.path.abspath('django.zip')) |
|
42 |
|
43 ultimate_sys_path = None |
|
44 |
|
45 # Force Django to reload its settings. |
|
46 from django.conf import settings |
|
47 settings._target = None |
|
48 |
|
49 # Must set this env var before importing any part of Django |
|
50 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
|
51 |
|
52 import django.core.handlers.wsgi |
|
53 import django.core.signals |
|
54 import django.db |
|
55 |
|
56 # Log errors. |
|
57 def log_exception(*args, **kwds): |
|
58 """Function used for logging exceptions. |
|
59 """ |
|
60 logging.exception('Exception in request:') |
|
61 |
|
62 # Log all exceptions detected by Django. |
|
63 django.core.signals.got_request_exception.connect(log_exception) |
|
64 |
|
65 # Unregister the rollback event handler. |
|
66 django.core.signals.got_request_exception.disconnect( |
|
67 django.db._rollback_on_exception) |
|
68 |
33 |
69 |
34 |
70 def profile_main_as_html(): |
35 def profile_main_as_html(): |
71 """Main program for profiling. Profiling data added as HTML to the page. |
36 """Main program for profiling. Profiling data added as HTML to the page. |
72 """ |
37 """ |
115 |
80 |
116 |
81 |
117 def real_main(): |
82 def real_main(): |
118 """Main program without profiling. |
83 """Main program without profiling. |
119 """ |
84 """ |
120 global ultimate_sys_path |
85 import django.core.handlers.wsgi |
121 if ultimate_sys_path is None: |
|
122 ultimate_sys_path = list(sys.path) |
|
123 else: |
|
124 sys.path[:] = ultimate_sys_path |
|
125 |
86 |
126 # Create a Django application for WSGI. |
87 # Create a Django application for WSGI. |
127 application = django.core.handlers.wsgi.WSGIHandler() |
88 application = django.core.handlers.wsgi.WSGIHandler() |
128 |
89 |
129 # Run the WSGI CGI handler with that application. |
90 # Run the WSGI CGI handler with that application. |