Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[soc2009/multidb] Merged up to trunk r11804.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11805 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
Showing
53 changed files
with
2,286 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from api import * | ||
from constants import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from django.contrib.messages import constants | ||
from django.utils.functional import lazy, memoize | ||
|
||
__all__ = ( | ||
'add_message', 'get_messages', | ||
'debug', 'info', 'success', 'warning', 'error', | ||
) | ||
|
||
|
||
class MessageFailure(Exception): | ||
pass | ||
|
||
|
||
def add_message(request, level, message, extra_tags='', fail_silently=False): | ||
""" | ||
Attempts to add a message to the request using the 'messages' app, falling | ||
back to the user's message_set if MessageMiddleware hasn't been enabled. | ||
""" | ||
if hasattr(request, '_messages'): | ||
return request._messages.add(level, message, extra_tags) | ||
if hasattr(request, 'user') and request.user.is_authenticated(): | ||
return request.user.message_set.create(message=message) | ||
if not fail_silently: | ||
raise MessageFailure('Without the django.contrib.messages ' | ||
'middleware, messages can only be added to ' | ||
'authenticated users.') | ||
|
||
|
||
def get_messages(request): | ||
""" | ||
Returns the message storage on the request if it exists, otherwise returns | ||
user.message_set.all() as the old auth context processor did. | ||
""" | ||
if hasattr(request, '_messages'): | ||
return request._messages | ||
|
||
def get_user(): | ||
if hasattr(request, 'user'): | ||
return request.user | ||
else: | ||
from django.contrib.auth.models import AnonymousUser | ||
return AnonymousUser() | ||
|
||
return lazy(memoize(get_user().get_and_delete_messages, {}, 0), list)() | ||
|
||
|
||
def debug(request, message, extra_tags='', fail_silently=False): | ||
""" | ||
Adds a message with the ``DEBUG`` level. | ||
""" | ||
add_message(request, constants.DEBUG, message, extra_tags=extra_tags, | ||
fail_silently=fail_silently) | ||
|
||
|
||
def info(request, message, extra_tags='', fail_silently=False): | ||
""" | ||
Adds a message with the ``INFO`` level. | ||
""" | ||
add_message(request, constants.INFO, message, extra_tags=extra_tags, | ||
fail_silently=fail_silently) | ||
|
||
|
||
def success(request, message, extra_tags='', fail_silently=False): | ||
""" | ||
Adds a message with the ``SUCCESS`` level. | ||
""" | ||
add_message(request, constants.SUCCESS, message, extra_tags=extra_tags, | ||
fail_silently=fail_silently) | ||
|
||
|
||
def warning(request, message, extra_tags='', fail_silently=False): | ||
""" | ||
Adds a message with the ``WARNING`` level. | ||
""" | ||
add_message(request, constants.WARNING, message, extra_tags=extra_tags, | ||
fail_silently=fail_silently) | ||
|
||
|
||
def error(request, message, extra_tags='', fail_silently=False): | ||
""" | ||
Adds a message with the ``ERROR`` level. | ||
""" | ||
add_message(request, constants.ERROR, message, extra_tags=extra_tags, | ||
fail_silently=fail_silently) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
DEBUG = 10 | ||
INFO = 20 | ||
SUCCESS = 25 | ||
WARNING = 30 | ||
ERROR = 40 | ||
|
||
DEFAULT_TAGS = { | ||
DEBUG: 'debug', | ||
INFO: 'info', | ||
SUCCESS: 'success', | ||
WARNING: 'warning', | ||
ERROR: 'error', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.contrib.messages.api import get_messages | ||
|
||
|
||
def messages(request): | ||
""" | ||
Returns a lazy 'messages' context variable. | ||
""" | ||
return {'messages': get_messages(request)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django.conf import settings | ||
from django.contrib.messages.storage import default_storage | ||
|
||
|
||
class MessageMiddleware(object): | ||
""" | ||
Middleware that handles temporary messages. | ||
""" | ||
|
||
def process_request(self, request): | ||
request._messages = default_storage(request) | ||
|
||
def process_response(self, request, response): | ||
""" | ||
Updates the storage backend (i.e., saves the messages). | ||
If not all messages could not be stored and ``DEBUG`` is ``True``, a | ||
``ValueError`` is raised. | ||
""" | ||
# A higher middleware layer may return a request which does not contain | ||
# messages storage, so make no assumption that it will be there. | ||
if hasattr(request, '_messages'): | ||
unstored_messages = request._messages.update(response) | ||
if unstored_messages and settings.DEBUG: | ||
raise ValueError('Not all temporary messages could be stored.') | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Models module required so tests are discovered. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.utils.importlib import import_module | ||
|
||
|
||
def get_storage(import_path): | ||
""" | ||
Imports the message storage class described by import_path, where | ||
import_path is the full Python path to the class. | ||
""" | ||
try: | ||
dot = import_path.rindex('.') | ||
except ValueError: | ||
raise ImproperlyConfigured("%s isn't a Python path." % import_path) | ||
module, classname = import_path[:dot], import_path[dot + 1:] | ||
try: | ||
mod = import_module(module) | ||
except ImportError, e: | ||
raise ImproperlyConfigured('Error importing module %s: "%s"' % | ||
(module, e)) | ||
try: | ||
return getattr(mod, classname) | ||
except AttributeError: | ||
raise ImproperlyConfigured('Module "%s" does not define a "%s" ' | ||
'class.' % (module, classname)) | ||
|
||
|
||
# Callable with the same interface as the storage classes i.e. accepts a | ||
# 'request' object. It is wrapped in a lambda to stop 'settings' being used at | ||
# the module level | ||
default_storage = lambda request: get_storage(settings.MESSAGE_STORAGE)(request) |
Oops, something went wrong.