app/gae_django.py
author Sverre Rabbelier <sverre@rabbelier.nl>
Mon, 08 Jun 2009 22:31:28 +0200
changeset 2411 19e9fdde919e
parent 2335 366e64ecba91
child 2414 a95ba3595554
permissions -rw-r--r--
Set new Melange version number to 0-5-20090604 in app.yaml.template
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2335
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
"""Module containing Melange Django 1.0+ configuration for Google App Engine.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
import logging
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
import os
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
import sys
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
__authors__ = [
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
  # alphabetical order by last name, please
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
  ]
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
# Remove the standard version of Django.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
for k in [k for k in sys.modules if k.startswith('django')]:
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
  del sys.modules[k]
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
# Force sys.path to have our own directory first, in case we want to import
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
# from it. This lets us replace the built-in Django
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
sys.path.insert(0, os.path.abspath('django.zip'))
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
# Force Django to reload its settings.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
from django.conf import settings
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    42
settings._target = None
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    43
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    44
# Must set this env var before importing any part of Django
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
import django.core.signals
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
import django.db
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    49
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
# Log errors.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
def log_exception(*args, **kwds):
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    52
  """Function used for logging exceptions.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    53
  """
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    54
  logging.exception('Exception in request:')
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
# Log all exceptions detected by Django.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
django.core.signals.got_request_exception.connect(log_exception)
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
# Unregister the rollback event handler.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
django.core.signals.got_request_exception.disconnect(
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
    django.db._rollback_on_exception)