| LEFT | RIGHT |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # | 2 # |
| 3 # Copyright 2008 Google Inc. | 3 # Copyright 2008 Google Inc. |
| 4 # | 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); | 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. | 6 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at | 7 # You may obtain a copy of the License at |
| 8 # | 8 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 | 9 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # | 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software | 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, | 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and | 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. | 15 # limitations under the License. |
| 16 | 16 |
| 17 """Support for integrating a Django project with the appengine infrastructure. | 17 """Support for integrating a Django project with the appengine infrastructure. |
| 18 | 18 |
| 19 This works with both Django 0.96 (via much monkey patching) and Django | 19 This works with both Django 0.96 (via much monkey patching) and Django |
| 20 0.97.pre1. | 20 0.97.pre1. |
| 21 | 21 |
| 22 This module enables you to use the Django manage.py utility and *some* of it's | 22 This module enables you to use the Django manage.py utility and *some* of it's |
| 23 subcommands. View the help of manage.py for exact details. | 23 subcommands. View the help of manage.py for exact details. |
| 24 | 24 |
| 25 Additionally this module takes care of initialising the datastore (and a test | 25 Additionally this module takes care of initialising the datastore (and a test |
| 26 datastore) so that the Django test infrastructure can be used for your | 26 datastore) so that the Django test infrastructure can be used for your |
| 27 appengine project. | 27 appengine project. |
| 28 | 28 |
| 29 To use this module add the following two lines to your main.py and manage.py | 29 To use this module add the following two lines to your main.py and manage.py |
| 30 scripts at the end of your imports: | 30 scripts at the end of your imports: |
| 31 from appengine_django import InstallAppengineHelperForDjango | 31 from appengine_django import InstallAppengineHelperForDjango |
| 32 InstallAppengineHelperForDjango() | 32 InstallAppengineHelperForDjango() |
| 33 | 33 |
| 34 If you would like to use a version of Django other than that provided by the | 34 If you would like to use a version of Django other than that provided by the |
| 35 system all you need to do is include it in a directory just above this helper, | 35 system all you need to do is include it in a directory just above this helper, |
| 36 eg: | 36 eg: |
| 37 appengine_django/__init__.py - This file | 37 appengine_django/__init__.py - This file |
| 38 django/... - your private copy of Django. | 38 django/... - your private copy of Django. |
| 39 """ | 39 """ |
| 40 | 40 |
| 41 import logging | 41 import logging |
| 42 import os | 42 import os |
| 43 import sys | 43 import sys |
| 44 | 44 |
| 45 | 45 |
| 46 DIR_PATH = os.path.abspath(os.path.dirname(__file__)) | 46 DIR_PATH = os.path.abspath(os.path.dirname(__file__)) |
| 47 PARENT_DIR = os.path.dirname(DIR_PATH) | 47 PARENT_DIR = os.path.dirname(DIR_PATH) |
| 48 | 48 |
| 49 # Add this project to the start of sys path to enable direct imports. | 49 # Add this project to the start of sys path to enable direct imports. |
| 50 sys.path = [PARENT_DIR,] + sys.path | 50 sys.path = [PARENT_DIR,] + sys.path |
| (...skipping 294 matching lines...) Show 10 above Show 10 below |
| 345 def InstallAppengineHelperForDjango(): | 345 def InstallAppengineHelperForDjango(): |
| 346 """Installs and Patches all of the classes/methods required for integration. | 346 """Installs and Patches all of the classes/methods required for integration. |
| 347 | 347 |
| 348 If the variable DEBUG_APPENGINE_DJANGO is set in the environment verbose | 348 If the variable DEBUG_APPENGINE_DJANGO is set in the environment verbose |
| 349 logging of the actions taken will be enabled. | 349 logging of the actions taken will be enabled. |
| 350 """ | 350 """ |
| 351 if os.getenv("DEBUG_APPENGINE_DJANGO"): | 351 if os.getenv("DEBUG_APPENGINE_DJANGO"): |
| 352 logging.getLogger().setLevel(logging.DEBUG) | 352 logging.getLogger().setLevel(logging.DEBUG) |
| 353 else: | 353 else: |
| 354 logging.getLogger().setLevel(logging.INFO) | 354 logging.getLogger().setLevel(logging.INFO) |
| 355 logging.debug("Loading the Google App Engine Helper for Django...") | 355 logging.debug("Loading the Google App Engine Helper for Django...") |
| 356 | 356 |
| 357 # Force Django to reload its settings. | 357 # Force Django to reload its settings. |
| 358 settings._target = None | 358 settings._target = None |
| 359 | 359 |
| 360 # Must set this env var *before* importing any more of Django. | 360 # Must set this env var *before* importing any more of Django. |
| 361 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' | 361 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
| 362 | 362 |
| 363 LoadAppengineEnvironment() | 363 LoadAppengineEnvironment() |
| 364 InstallReplacementImpModule() | 364 InstallReplacementImpModule() |
| 365 InstallAppengineDatabaseBackend() | 365 InstallAppengineDatabaseBackend() |
| 366 PatchDjangoSerializationModules() | 366 PatchDjangoSerializationModules() |
| 367 CleanupDjangoSettings() | 367 CleanupDjangoSettings() |
| 368 ModifyAvailableCommands() | 368 ModifyAvailableCommands() |
| 369 InstallGoogleSMTPConnection() | 369 InstallGoogleSMTPConnection() |
| 370 InstallAuthentication() | 370 InstallAuthentication() |
| 371 | 371 |
| 372 logging.debug("Successfully loaded the Google App Engine Helper for Django.") | 372 logging.debug("Successfully loaded the Google App Engine Helper for Django.") |
| 373 | 373 |
| 374 | 374 |
| 375 def InstallGoogleSMTPConnection(): | 375 def InstallGoogleSMTPConnection(): |
| 376 from appengine_django import mail as gmail | 376 from appengine_django import mail as gmail |
| 377 from django.core import mail | 377 from django.core import mail |
| 378 if VERSION >= (0, 97, None): | 378 if VERSION >= (0, 97, None): |
| 379 logging.debug("Installing Google Email Adapter for Django 0.97+") | 379 logging.debug("Installing Google Email Adapter for Django 0.97+") |
| 380 mail.SMTPConnection = gmail.GoogleSMTPConnection | 380 mail.SMTPConnection = gmail.GoogleSMTPConnection |
| 381 else: | 381 else: |
| 382 logging.debug("Installing Google Email Adapter for Django 0.96") | 382 logging.debug("Installing Google Email Adapter for Django 0.96") |
| 383 mail.send_mass_mail = gmail.send_mass_mail | 383 mail.send_mass_mail = gmail.send_mass_mail |
| 384 mail.mail_admins = gmail.mail_admins | 384 mail.mail_admins = gmail.mail_admins |
| 385 mail.mail_managers = gmail.mail_managers | 385 mail.mail_managers = gmail.mail_managers |
| 386 | 386 |
| 387 def InstallAuthentication(): | 387 def InstallAuthentication(): |
| 388 logging.debug("Installing authentication framework") | 388 logging.debug("Installing authentication framework") |
| 389 from django.contrib.auth import models as django_models | 389 from django.contrib.auth import models as django_models |
| 390 from appengine_django.auth.models import DjangoUser | 390 from appengine_django.auth.models import DjangoUser |
| 391 django_models.User = DjangoUser | 391 django_models.User = DjangoUser |
| 392 from django.contrib.auth import middleware as django_middleware | 392 from django.contrib.auth import middleware as django_middleware |
| 393 from appengine_django.auth import middleware | 393 from appengine_django.auth import middleware |
| 394 django_middleware.AuthenticationMiddleware = middleware.AuthenticationMiddlewa
re | 394 django_middleware.AuthenticationMiddleware = middleware.AuthenticationMiddlewa
re |
| 395 if VERSION >= (0, 97, None): | |
| 396 from appengine_django.auth import tests | |
| 397 from django.contrib.auth import tests as django_tests | |
| 398 django_tests.__doc__ = tests.__doc__ | |
| LEFT | RIGHT |