app/gae_django.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 24 Jul 2009 20:49:59 +0200
changeset 2675 49cf6a8af70a
parent 2414 a95ba3595554
permissions -rw-r--r--
Added template for a notification upon arrival of a new Request.
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
2414
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    30
if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    31
  
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    32
  # Remove the standard version of Django.
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    33
  for k in [k for k in sys.modules if k.startswith('django')]:
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    34
    del sys.modules[k]
2335
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
2414
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    36
  # Force sys.path to have our own directory first, in case we want to import
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    37
  # from it. This lets us replace the built-in Django
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    38
  sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    39
  sys.path.insert(0, os.path.abspath('django'))
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    40
  sys.path.insert(0, os.path.abspath('django.zip'))
2335
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
2414
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    42
else:
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    43
  # Declare the Django version we need.
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    44
  from google.appengine.dist import use_library
a95ba3595554 Use build-in django 1.0.x on deployed version of Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2335
diff changeset
    45
  use_library('django', '1.0')
2335
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
# Force Django to reload its settings.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
from django.conf import settings
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    49
settings._target = None
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
# 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
    52
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
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
import django.core.signals
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
import django.db
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
# Log errors.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
def log_exception(*args, **kwds):
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
  """Function used for logging exceptions.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
  """
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
  logging.exception('Exception in request:')
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
# Log all exceptions detected by Django.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
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
    65
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
# Unregister the rollback event handler.
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
django.core.signals.got_request_exception.disconnect(
366e64ecba91 Add web based python shell to Melange.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
    django.db._rollback_on_exception)