Source code for google.appengine.ext.ndb.django_middleware
# Copyright 2008 The ndb Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Django middleware for NDB."""
__author__ = 'James A. Morrison'
from . import eventloop, tasklets
"""Django middleware for NDB.
To use NDB with django, add
to the MIDDLEWARE_CLASSES entry in your Django settings.py file.
Or, if you are using the ndb version from the SDK, use
It's best to insert it in front of any other middleware classes,
since some other middleware may make datastore calls and those won't be
handled properly if that middleware is invoked before this middleware.
[docs] def process_request(self, unused_request):
"""Called by Django before deciding which view to execute."""
# Compare to the first half of toplevel() in context.py.
# Create and install a new context.
ctx = tasklets.make_default_context()
# Compare to the finally clause in toplevel() in context.py.
ctx = tasklets.get_context()
eventloop.run() # Ensure writes are flushed, etc.
[docs] def process_response(self, request, response):
"""Called by Django just before returning a response."""
[docs] def process_exception(self, unused_request, unused_exception):
"""Called by Django when a view raises an exception."""