1
0
mirror of https://github.com/django/django.git synced 2025-07-06 18:59:13 +00:00

[multi-db] Merge trunk to [3426]

git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3427 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jason Pellerin 2006-07-23 03:31:52 +00:00
parent d345b89bc6
commit 438c23a6c6
90 changed files with 3128 additions and 956 deletions

View File

@ -2,7 +2,6 @@
import os import os
import sys import sys
import getopt
def compile_messages(): def compile_messages():
basedir = None basedir = None

View File

@ -1,5 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# Need to ensure that the i18n framework is enabled
from django.conf import settings
settings.configure(USE_I18N = True)
from django.utils.translation import templatize from django.utils.translation import templatize
import re import re
import os import os

View File

@ -7,7 +7,6 @@ a list of all possible variables.
""" """
import os import os
import sys
from django.conf import global_settings from django.conf import global_settings
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"

View File

@ -62,6 +62,7 @@ LANGUAGES = (
('sl', gettext_noop('Slovenian')), ('sl', gettext_noop('Slovenian')),
('sr', gettext_noop('Serbian')), ('sr', gettext_noop('Serbian')),
('sv', gettext_noop('Swedish')), ('sv', gettext_noop('Swedish')),
('ta', gettext_noop('Tamil')),
('uk', gettext_noop('Ukrainian')), ('uk', gettext_noop('Ukrainian')),
('zh-cn', gettext_noop('Simplified Chinese')), ('zh-cn', gettext_noop('Simplified Chinese')),
('zh-tw', gettext_noop('Traditional Chinese')), ('zh-tw', gettext_noop('Traditional Chinese')),

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -123,7 +123,7 @@ class DateFieldFilterSpec(FilterSpec):
def choices(self, cl): def choices(self, cl):
for title, param_dict in self.links: for title, param_dict in self.links:
yield {'selected': self.date_params == param_dict, yield {'selected': self.date_params == param_dict,
'query_string': cl.get_query_string(param_dict, self.field_generic), 'query_string': cl.get_query_string(param_dict, [self.field_generic]),
'display': title} 'display': title}
FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec) FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)

View File

@ -20,9 +20,9 @@
<div id="branding"> <div id="branding">
{% block branding %}{% endblock %} {% block branding %}{% endblock %}
</div> </div>
{% if not user.is_anonymous %}{% if user.is_staff %} {% if user.is_authenticated and user.is_staff %}
<div id="user-tools">{% trans 'Welcome,' %} <strong>{% if user.first_name %}{{ user.first_name|escape }}{% else %}{{ user.username }}{% endif %}</strong>. {% block userlinks %}<a href="doc/">{% trans 'Documentation' %}</a> / <a href="password_change/">{% trans 'Change password' %}</a> / <a href="logout/">{% trans 'Log out' %}</a>{% endblock %}</div> <div id="user-tools">{% trans 'Welcome,' %} <strong>{% if user.first_name %}{{ user.first_name|escape }}{% else %}{{ user.username }}{% endif %}</strong>. {% block userlinks %}<a href="doc/">{% trans 'Documentation' %}</a> / <a href="password_change/">{% trans 'Change password' %}</a> / <a href="logout/">{% trans 'Log out' %}</a>{% endblock %}</div>
{% endif %}{% endif %} {% endif %}
{% block nav-global %}{% endblock %} {% block nav-global %}{% endblock %}
</div> </div>
<!-- END Header --> <!-- END Header -->

View File

@ -1,8 +1,6 @@
from django import template
from django.conf import settings from django.conf import settings
from django.contrib.admin.views.main import MAX_SHOW_ALL_ALLOWED, ALL_VAR from django.contrib.admin.views.main import ALL_VAR, EMPTY_CHANGELIST_VALUE
from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR
from django.contrib.admin.views.main import IS_POPUP_VAR, EMPTY_CHANGELIST_VALUE
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import models from django.db import models
from django.utils import dateformat from django.utils import dateformat
@ -119,7 +117,7 @@ def items_for_result(cl, result):
if callable(attr): if callable(attr):
attr = attr() attr = attr()
result_repr = str(attr) result_repr = str(attr)
except AttributeError, ObjectDoesNotExist: except (AttributeError, ObjectDoesNotExist):
result_repr = EMPTY_CHANGELIST_VALUE result_repr = EMPTY_CHANGELIST_VALUE
else: else:
# Strip HTML tags in the resulting text, except if the # Strip HTML tags in the resulting text, except if the

View File

@ -1,9 +1,7 @@
from django import template from django import template
from django.contrib.admin.views.main import AdminBoundField from django.contrib.admin.views.main import AdminBoundField
from django.template import loader from django.template import loader
from django.utils.html import escape
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.functional import curry
from django.db import models from django.db import models
from django.db.models.fields import Field from django.db.models.fields import Field
from django.db.models.related import BoundRelatedObject from django.db.models.related import BoundRelatedObject

View File

@ -3,7 +3,6 @@
import re import re
from email.Parser import HeaderParser from email.Parser import HeaderParser
from email.Errors import HeaderParseError from email.Errors import HeaderParseError
from urlparse import urljoin
try: try:
import docutils.core import docutils.core
import docutils.nodes import docutils.nodes

View File

@ -46,7 +46,7 @@ def staff_member_required(view_func):
member, displaying the login page if necessary. member, displaying the login page if necessary.
""" """
def _checklogin(request, *args, **kwargs): def _checklogin(request, *args, **kwargs):
if not request.user.is_anonymous() and request.user.is_staff: if request.user.is_authenticated() and request.user.is_staff:
# The user is valid. Continue to the admin page. # The user is valid. Continue to the admin page.
if request.POST.has_key('post_data'): if request.POST.has_key('post_data'):
# User must have re-authenticated through a different window # User must have re-authenticated through a different window

View File

@ -28,7 +28,7 @@ def bookmarklets(request):
# Hack! This couples this view to the URL it lives at. # Hack! This couples this view to the URL it lives at.
admin_root = request.path[:-len('doc/bookmarklets/')] admin_root = request.path[:-len('doc/bookmarklets/')]
return render_to_response('admin_doc/bookmarklets.html', { return render_to_response('admin_doc/bookmarklets.html', {
'admin_url': "%s://%s%s" % (os.environ.get('HTTPS') == 'on' and 'https' or 'http', get_host(request), admin_root), 'admin_url': "%s://%s%s" % (request.is_secure() and 'https' or 'http', get_host(request), admin_root),
}, context_instance=RequestContext(request)) }, context_instance=RequestContext(request))
bookmarklets = staff_member_required(bookmarklets) bookmarklets = staff_member_required(bookmarklets)

View File

@ -10,9 +10,6 @@ from django.shortcuts import get_object_or_404, render_to_response
from django.db import models from django.db import models
from django.db.models.query import handle_legacy_orderlist, QuerySet from django.db.models.query import handle_legacy_orderlist, QuerySet
from django.http import Http404, HttpResponse, HttpResponseRedirect from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.template import loader
from django.utils import dateformat
from django.utils.dates import MONTHS
from django.utils.html import escape from django.utils.html import escape
from django.utils.text import capfirst, get_text_list from django.utils.text import capfirst, get_text_list
import operator import operator

View File

@ -22,7 +22,7 @@ def template_validator(request):
new_data = request.POST.copy() new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data) errors = manipulator.get_validation_errors(new_data)
if not errors: if not errors:
request.user.add_message('The template is valid.') request.user.message_set.create(message='The template is valid.')
return render_to_response('admin/template_validator.html', { return render_to_response('admin/template_validator.html', {
'title': 'Template validator', 'title': 'Template validator',
'form': forms.FormWrapper(manipulator, new_data, errors), 'form': forms.FormWrapper(manipulator, new_data, errors),
@ -32,7 +32,7 @@ template_validator = staff_member_required(template_validator)
class TemplateValidator(forms.Manipulator): class TemplateValidator(forms.Manipulator):
def __init__(self, settings_modules): def __init__(self, settings_modules):
self.settings_modules = settings_modules self.settings_modules = settings_modules
site_list = Site.objects.get_in_bulk(settings_modules.keys()).values() site_list = Site.objects.in_bulk(settings_modules.keys()).values()
self.fields = ( self.fields = (
forms.SelectField('site', is_required=True, choices=[(s.id, s.name) for s in site_list]), forms.SelectField('site', is_required=True, choices=[(s.id, s.name) for s in site_list]),
forms.LargeTextField('template', is_required=True, rows=25, validator_list=[self.isValidTemplate]), forms.LargeTextField('template', is_required=True, rows=25, validator_list=[self.isValidTemplate]),

View File

@ -56,8 +56,14 @@ def logout(request):
""" """
Remove the authenticated user's ID from the request. Remove the authenticated user's ID from the request.
""" """
try:
del request.session[SESSION_KEY] del request.session[SESSION_KEY]
except KeyError:
pass
try:
del request.session[BACKEND_SESSION_KEY] del request.session[BACKEND_SESSION_KEY]
except KeyError:
pass
def get_user(request): def get_user(request):
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser

View File

@ -1,4 +1,4 @@
from django.contrib.auth.models import User, check_password from django.contrib.auth.models import User
class ModelBackend: class ModelBackend:
""" """

View File

@ -17,7 +17,7 @@ def user_passes_test(test_func, login_url=LOGIN_URL):
return _checklogin return _checklogin
return _dec return _dec
login_required = user_passes_test(lambda u: not u.is_anonymous()) login_required = user_passes_test(lambda u: u.is_authenticated())
login_required.__doc__ = ( login_required.__doc__ = (
""" """
Decorator for views that checks that the user is logged in, redirecting Decorator for views that checks that the user is logged in, redirecting

View File

@ -1,4 +1,5 @@
from django.core import validators from django.core import validators
from django.core.exceptions import ImproperlyConfigured
from django.db import backend, connection, models from django.db import backend, connection, models
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -126,6 +127,11 @@ class User(models.Model):
"Always returns False. This is a way of comparing User objects to anonymous users." "Always returns False. This is a way of comparing User objects to anonymous users."
return False return False
def is_authenticated(self):
"""Always return True. This is a way to tell if the user has been authenticated in templates.
"""
return True
def get_full_name(self): def get_full_name(self):
"Returns the first_name plus the last_name, with a space in between." "Returns the first_name plus the last_name, with a space in between."
full_name = '%s %s' % (self.first_name, self.last_name) full_name = '%s %s' % (self.first_name, self.last_name)
@ -293,3 +299,6 @@ class AnonymousUser(object):
def is_anonymous(self): def is_anonymous(self):
return True return True
def is_authenticated(self):
return False

View File

@ -4,7 +4,7 @@ from django import forms
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.template import RequestContext from django.template import RequestContext
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME

View File

@ -1,7 +1,6 @@
from django.conf import settings from django.conf import settings
from django.contrib.comments.models import Comment, FreeComment from django.contrib.comments.models import Comment, FreeComment
from django.contrib.syndication.feeds import Feed from django.contrib.syndication.feeds import Feed
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
class LatestFreeCommentsFeed(Feed): class LatestFreeCommentsFeed(Feed):
@ -37,6 +36,6 @@ class LatestCommentsFeed(LatestFreeCommentsFeed):
qs = qs.filter(is_removed=False) qs = qs.filter(is_removed=False)
if settings.COMMENTS_BANNED_USERS_GROUP: if settings.COMMENTS_BANNED_USERS_GROUP:
where = ['user_id NOT IN (SELECT user_id FROM auth_users_group WHERE group_id = %s)'] where = ['user_id NOT IN (SELECT user_id FROM auth_users_group WHERE group_id = %s)']
params = [COMMENTS_BANNED_USERS_GROUP] params = [settings.COMMENTS_BANNED_USERS_GROUP]
qs = qs.extra(where=where, params=params) qs = qs.extra(where=where, params=params)
return qs return qs

View File

@ -2,10 +2,10 @@
{% if display_form %} {% if display_form %}
<form {% if photos_optional or photos_required %}enctype="multipart/form-data" {% endif %}action="/comments/post/" method="post"> <form {% if photos_optional or photos_required %}enctype="multipart/form-data" {% endif %}action="/comments/post/" method="post">
{% if user.is_anonymous %} {% if user.is_authenticated %}
<p><label for="id_username">{% trans "Username:" %}</label> <input type="text" name="username" id="id_username" /><br />{% trans "Password:" %} <input type="password" name="password" id="id_password" /> (<a href="/accounts/password_reset/">{% trans "Forgotten your password?" %}</a>)</p>
{% else %}
<p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="/accounts/logout/">{% trans "Log out" %}</a>)</p> <p>{% trans "Username:" %} <strong>{{ user.username }}</strong> (<a href="/accounts/logout/">{% trans "Log out" %}</a>)</p>
{% else %}
<p><label for="id_username">{% trans "Username:" %}</label> <input type="text" name="username" id="id_username" /><br />{% trans "Password:" %} <input type="password" name="password" id="id_password" /> (<a href="/accounts/password_reset/">{% trans "Forgotten your password?" %}</a>)</p>
{% endif %} {% endif %}
{% if ratings_optional or ratings_required %} {% if ratings_optional or ratings_required %}

View File

@ -114,7 +114,7 @@ class CommentListNode(template.Node):
comment_list = get_list_function(**kwargs).order_by(self.ordering + 'submit_date').select_related() comment_list = get_list_function(**kwargs).order_by(self.ordering + 'submit_date').select_related()
if not self.free: if not self.free:
if context.has_key('user') and not context['user'].is_anonymous(): if context.has_key('user') and context['user'].is_authenticated():
user_id = context['user'].id user_id = context['user'].id
context['user_can_moderate_comments'] = Comment.objects.user_is_moderator(context['user']) context['user_can_moderate_comments'] = Comment.objects.user_is_moderator(context['user'])
else: else:

View File

@ -5,7 +5,7 @@ from django.http import Http404
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.template import RequestContext from django.template import RequestContext
from django.contrib.comments.models import Comment, FreeComment, PHOTOS_REQUIRED, PHOTOS_OPTIONAL, RATINGS_REQUIRED, RATINGS_OPTIONAL, IS_PUBLIC from django.contrib.comments.models import Comment, FreeComment, RATINGS_REQUIRED, RATINGS_OPTIONAL, IS_PUBLIC
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
@ -63,7 +63,7 @@ class PublicCommentManipulator(AuthenticationForm):
validator_list=get_validator_list(8), validator_list=get_validator_list(8),
), ),
]) ])
if not user.is_anonymous(): if user.is_authenticated():
self["username"].is_required = False self["username"].is_required = False
self["username"].validator_list = [] self["username"].validator_list = []
self["password"].is_required = False self["password"].is_required = False

View File

@ -15,7 +15,7 @@ def vote(request, comment_id, vote):
rating = {'up': 1, 'down': -1}.get(vote, False) rating = {'up': 1, 'down': -1}.get(vote, False)
if not rating: if not rating:
raise Http404, "Invalid vote" raise Http404, "Invalid vote"
if request.user.is_anonymous(): if not request.user.is_authenticated():
raise Http404, _("Anonymous users cannot vote") raise Http404, _("Anonymous users cannot vote")
try: try:
comment = Comment.objects.get(pk=comment_id) comment = Comment.objects.get(pk=comment_id)

View File

@ -22,7 +22,7 @@ def flatpage(request, url):
f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID) f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID)
# If registration is required for accessing this page, and the user isn't # If registration is required for accessing this page, and the user isn't
# logged in, redirect to the login page. # logged in, redirect to the login page.
if f.registration_required and request.user.is_anonymous(): if f.registration_required and not request.user.is_authenticated():
from django.contrib.auth.views import redirect_to_login from django.contrib.auth.views import redirect_to_login
return redirect_to_login(request.path) return redirect_to_login(request.path)
if f.template_name: if f.template_name:

View File

@ -32,11 +32,21 @@ class SessionManager(models.Manager):
return s return s
class Session(models.Model): class Session(models.Model):
"""Django provides full support for anonymous sessions. The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID -- not the data itself. """
Django provides full support for anonymous sessions. The session
framework lets you store and retrieve arbitrary data on a
per-site-visitor basis. It stores data on the server side and
abstracts the sending and receiving of cookies. Cookies contain a
session ID -- not the data itself.
The Django sessions framework is entirely cookie-based. It does not fall back to putting session IDs in URLs. This is an intentional design decision. Not only does that behavior make URLs ugly, it makes your site vulnerable to session-ID theft via the "Referer" header. The Django sessions framework is entirely cookie-based. It does
not fall back to putting session IDs in URLs. This is an intentional
design decision. Not only does that behavior make URLs ugly, it makes
your site vulnerable to session-ID theft via the "Referer" header.
For complete documentation on using Sessions in your code, consult the sessions documentation that is shipped with Django (also available on the Django website). For complete documentation on using Sessions in your code, consult
the sessions documentation that is shipped with Django (also available
on the Django website).
""" """
session_key = models.CharField(_('session key'), maxlength=40, primary_key=True) session_key = models.CharField(_('session key'), maxlength=40, primary_key=True)
session_data = models.TextField(_('session data')) session_data = models.TextField(_('session data'))

View File

@ -73,7 +73,7 @@ class Feed(object):
link = link, link = link,
description = self.__get_dynamic_attr('description', obj), description = self.__get_dynamic_attr('description', obj),
language = settings.LANGUAGE_CODE.decode(), language = settings.LANGUAGE_CODE.decode(),
feed_url = add_domain(current_site, self.feed_url), feed_url = add_domain(current_site, self.__get_dynamic_attr('feed_url', obj)),
author_name = self.__get_dynamic_attr('author_name', obj), author_name = self.__get_dynamic_attr('author_name', obj),
author_link = self.__get_dynamic_attr('author_link', obj), author_link = self.__get_dynamic_attr('author_link', obj),
author_email = self.__get_dynamic_attr('author_email', obj), author_email = self.__get_dynamic_attr('author_email', obj),

View File

@ -1,7 +1,7 @@
"Database cache backend." "Database cache backend."
from django.core.cache.backends.base import BaseCache from django.core.cache.backends.base import BaseCache
from django.db import connection, transaction from django.db import connection, transaction, DatabaseError
import base64, time import base64, time
from datetime import datetime from datetime import datetime
try: try:

View File

@ -3,10 +3,6 @@
from django.core.cache.backends.simple import CacheClass as SimpleCacheClass from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
from django.utils.synch import RWLock from django.utils.synch import RWLock
import copy, time import copy, time
try:
import cPickle as pickle
except ImportError:
import pickle
class CacheClass(SimpleCacheClass): class CacheClass(SimpleCacheClass):
def __init__(self, host, params): def __init__(self, host, params):

View File

@ -119,7 +119,6 @@ class BaseHandler(object):
Returns an HttpResponse that displays a PUBLIC error message for a Returns an HttpResponse that displays a PUBLIC error message for a
fundamental error. fundamental error.
""" """
from django.core import urlresolvers
callback, param_dict = resolver.resolve500() callback, param_dict = resolver.resolve500()
return callback(request, **param_dict) return callback(request, **param_dict)

View File

@ -23,6 +23,9 @@ class ModPythonRequest(http.HttpRequest):
def get_full_path(self): def get_full_path(self):
return '%s%s' % (self.path, self._req.args and ('?' + self._req.args) or '') return '%s%s' % (self.path, self._req.args and ('?' + self._req.args) or '')
def is_secure(self):
return self._req.subprocess_env.has_key('HTTPS') and self._req.subprocess_env['HTTPS'] == 'on'
def _load_post_and_files(self): def _load_post_and_files(self):
"Populates self._post and self._files" "Populates self._post and self._files"
if self._req.headers_in.has_key('content-type') and self._req.headers_in['content-type'].startswith('multipart'): if self._req.headers_in.has_key('content-type') and self._req.headers_in['content-type'].startswith('multipart'):
@ -145,7 +148,6 @@ class ModPythonHandler(BaseHandler):
def populate_apache_request(http_response, mod_python_req): def populate_apache_request(http_response, mod_python_req):
"Populates the mod_python request object with an HttpResponse" "Populates the mod_python request object with an HttpResponse"
from django.conf import settings
mod_python_req.content_type = http_response['Content-Type'] mod_python_req.content_type = http_response['Content-Type']
for key, value in http_response.headers.items(): for key, value in http_response.headers.items():
if key != 'Content-Type': if key != 'Content-Type':

View File

@ -58,14 +58,16 @@ class WSGIRequest(http.HttpRequest):
self.method = environ['REQUEST_METHOD'].upper() self.method = environ['REQUEST_METHOD'].upper()
def __repr__(self): def __repr__(self):
from pprint import pformat return '<WSGIRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
return '<DjangoRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
(pformat(self.GET), pformat(self.POST), pformat(self.COOKIES), (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),
pformat(self.META)) pformat(self.META))
def get_full_path(self): def get_full_path(self):
return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '') return '%s%s' % (self.path, self.environ.get('QUERY_STRING', '') and ('?' + self.environ.get('QUERY_STRING', '')) or '')
def is_secure(self):
return self.environ.has_key('HTTPS') and self.environ['HTTPS'] == 'on'
def _load_post_and_files(self): def _load_post_and_files(self):
# Populates self._post and self._files # Populates self._post and self._files
if self.method == 'POST': if self.method == 'POST':

View File

@ -92,11 +92,11 @@ def get_sql_create(app):
final_output = [] final_output = []
app_models = models.get_models(app, creation_order=True) app_models = models.get_models(app, creation_order=True)
for klass in app_models: for model in app_models:
opts = klass._meta opts = model._meta
connection_name = model_connection_name(klass) connection_name = model_connection_name(model)
output = connection_output.setdefault(connection_name, []) output = connection_output.setdefault(connection_name, [])
db = klass._default_manager.db db = model._default_manager.db
creation = db.get_creation_module() creation = db.get_creation_module()
data_types = creation.DATA_TYPES data_types = creation.DATA_TYPES
if not data_types: if not data_types:
@ -110,25 +110,25 @@ def get_sql_create(app):
sys.exit(1) sys.exit(1)
# Get installed models, so we generate REFERENCES right # Get installed models, so we generate REFERENCES right
manager = klass._default_manager manager = model._default_manager
tables = manager.get_table_list() tables = manager.get_table_list()
installed_models = manager.get_installed_models(tables) installed_models = manager.get_installed_models(tables)
models_output = set(installed_models) models_output = set(installed_models)
builder = creation.builder builder = creation.builder
builder.models_already_seen.update(models_output) builder.models_already_seen.update(models_output)
model_output, references = builder.get_create_table(klass, style) model_output, references = builder.get_create_table(model, style)
output.extend(model_output) output.extend(model_output)
for refto, refs in references.items(): for refto, refs in references.items():
try: try:
pending_references[refto].extend(refs) pending_references[refto].extend(refs)
except KeyError: except KeyError:
pending_references[refto] = refs pending_references[refto] = refs
if klass in pending_references: if model in pending_references:
output.extend(pending_references.pop(klass)) output.extend(pending_references.pop(model))
# Create the many-to-many join tables. # Create the many-to-many join tables.
many_many = builder.get_create_many_to_many(klass, style) many_many = builder.get_create_many_to_many(model, style)
for refklass, statements in many_many.items(): for refmodel, statements in many_many.items():
output.extend(statements) output.extend(statements)
final_output = _collate(connection_output) final_output = _collate(connection_output)
@ -137,9 +137,9 @@ def get_sql_create(app):
not_installed_models = set(pending_references.keys()) not_installed_models = set(pending_references.keys())
if not_installed_models: if not_installed_models:
final_output.append('-- The following references should be added but depend on non-existant tables:') final_output.append('-- The following references should be added but depend on non-existant tables:')
for klass in not_installed_models: for model in not_installed_models:
final_output.extend(['-- ' + sql final_output.extend(['-- ' + sql
for sql in pending_references.pop(klass)]) for sql in pending_references.pop(model)])
# convert BoundStatements into strings # convert BoundStatements into strings
final_output = map(str, final_output) final_output = map(str, final_output)
@ -155,18 +155,18 @@ def get_sql_delete(app):
connection_output = {} connection_output = {}
final_output = [] final_output = []
app_models = models.get_models(app, creation_order=True) app_models = models.get_models(app, creation_order=True)
for klass in app_models: for model in app_models:
db = klass._default_manager.db db = model._default_manager.db
connection = db.connection connection = db.connection
try: try:
cursor = connection.cursor() cursor = connection.cursor()
except: except:
cursor = None cursor = None
builder = db.get_creation_module().builder builder = db.get_creation_module().builder
connection_name = model_connection_name(klass) connection_name = model_connection_name(model)
output = connection_output.setdefault(connection_name, []) output = connection_output.setdefault(connection_name, [])
output.extend(map(str, output.extend(map(str,
builder.get_drop_table(klass, builder.get_drop_table(model,
cascade=True, style=style))) cascade=True, style=style)))
if cursor: if cursor:
# Close database connection explicitly, in case this # Close database connection explicitly, in case this
@ -193,12 +193,12 @@ def get_sql_initial_data(app):
connection_output = {} connection_output = {}
app_models = get_models(app) app_models = get_models(app)
for klass in app_models: for model in app_models:
opts = klass._meta opts = model._meta
connection_name = model_connection_name(klass) connection_name = model_connection_name(model)
output = connection_output.setdefault(connection_name, []) output = connection_output.setdefault(connection_name, [])
builder = klass._default_manager.db.get_creation_module().builder builder = model._default_manager.db.get_creation_module().builder
output.extend(builder.get_initialdata(klass)) output.extend(builder.get_initialdata(model))
return _collate(connection_output) return _collate(connection_output)
get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)." get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)."
@ -208,18 +208,18 @@ def get_sql_sequence_reset(app):
"Returns a list of the SQL statements to reset PostgreSQL sequences for the given app." "Returns a list of the SQL statements to reset PostgreSQL sequences for the given app."
from django.db import backend, models from django.db import backend, models
output = [] output = []
for klass in models.get_models(app): for model in models.get_models(app):
for f in klass._meta.fields: for f in model._meta.fields:
if isinstance(f, models.AutoField): if isinstance(f, models.AutoField):
output.append("%s setval('%s', (%s max(%s) %s %s));" % \ output.append("%s setval('%s', (%s max(%s) %s %s));" % \
(style.SQL_KEYWORD('SELECT'), (style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_%s_seq' % (klass._meta.db_table, f.column)), style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
style.SQL_KEYWORD('SELECT'), style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(backend.quote_name(f.column)), style.SQL_FIELD(backend.quote_name(f.column)),
style.SQL_KEYWORD('FROM'), style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(backend.quote_name(klass._meta.db_table)))) style.SQL_TABLE(backend.quote_name(model._meta.db_table))))
break # Only one AutoField is allowed per model, so don't bother continuing. break # Only one AutoField is allowed per model, so don't bother continuing.
for f in klass._meta.many_to_many: for f in model._meta.many_to_many:
output.append("%s setval('%s', (%s max(%s) %s %s));" % \ output.append("%s setval('%s', (%s max(%s) %s %s));" % \
(style.SQL_KEYWORD('SELECT'), (style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()), style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
@ -237,12 +237,12 @@ def get_sql_indexes(app):
from django.db.models import get_models from django.db.models import get_models
connection_output = {} connection_output = {}
for klass in get_models(app): for model in get_models(app):
opts = klass._meta opts = model._meta
connection_name = model_connection_name(klass) connection_name = model_connection_name(model)
output = connection_output.setdefault(connection_name, []) output = connection_output.setdefault(connection_name, [])
builder = klass._default_manager.db.get_creation_module().builder builder = model._default_manager.db.get_creation_module().builder
output.extend(map(str, builder.get_create_indexes(klass, style))) output.extend(map(str, builder.get_create_indexes(model, style)))
return _collate(connection_output) return _collate(connection_output)
get_sql_indexes.help_doc = "Prints the CREATE INDEX SQL statements for the given model module name(s)." get_sql_indexes.help_doc = "Prints the CREATE INDEX SQL statements for the given model module name(s)."
@ -374,14 +374,14 @@ def get_admin_index(app):
app_label = app_models[0]._meta.app_label app_label = app_models[0]._meta.app_label
output.append('{%% if perms.%s %%}' % app_label) output.append('{%% if perms.%s %%}' % app_label)
output.append('<div class="module"><h2>%s</h2><table>' % app_label.title()) output.append('<div class="module"><h2>%s</h2><table>' % app_label.title())
for klass in app_models: for model in app_models:
if klass._meta.admin: if model._meta.admin:
output.append(MODULE_TEMPLATE % { output.append(MODULE_TEMPLATE % {
'app': app_label, 'app': app_label,
'mod': klass._meta.module_name, 'mod': model._meta.module_name,
'name': capfirst(klass._meta.verbose_name_plural), 'name': capfirst(model._meta.verbose_name_plural),
'addperm': klass._meta.get_add_permission(), 'addperm': model._meta.get_add_permission(),
'changeperm': klass._meta.get_change_permission(), 'changeperm': model._meta.get_change_permission(),
}) })
output.append('</table></div>') output.append('</table></div>')
output.append('{% endif %}') output.append('{% endif %}')
@ -432,23 +432,23 @@ def install(app):
pending = {} pending = {}
for model in models.get_models(app, creation_order=True): for model in models.get_models(app, creation_order=True):
new_pending = model._default_manager.install(initial_data=True) new_pending = model._default_manager.install(initial_data=True)
for klass, statements in new_pending.items(): for model, statements in new_pending.items():
pending.setdefault(klass, []).extend(statements) pending.setdefault(model, []).extend(statements)
# execute any pending statements that were waiting for this model # execute any pending statements that were waiting for this model
if model in pending: if model in pending:
for statement in pending.pop(model): for statement in pending.pop(model):
statement.execute() statement.execute()
if pending: if pending:
for klass, statements in pending.items(): for model, statements in pending.items():
tables = klass._default_manager.get_table_list() tables = model._default_manager.get_table_list()
models_installed = klass._default_manager.get_installed_models(tables) models_installed = model._default_manager.get_installed_models(tables)
if klass in models_installed: if model in models_installed:
for statement in statements: for statement in statements:
statement.execute() statement.execute()
else: else:
raise Exception("%s is not installed, but there are " raise Exception("%s is not installed, but there are "
"pending statements that need it: %s" "pending statements that need it: %s"
% (klass, statements)) % (model, statements))
except Exception, e: except Exception, e:
sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons: sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons:
* The database isn't running or isn't configured correctly. * The database isn't running or isn't configured correctly.
@ -465,7 +465,6 @@ install.args = APP_ARGS
def reset(app): def reset(app):
"Executes the equivalent of 'get_sql_reset' in the current database." "Executes the equivalent of 'get_sql_reset' in the current database."
from django.db import connection, transaction from django.db import connection, transaction
from cStringIO import StringIO
app_name = app.__name__.split('.')[-2] app_name = app.__name__.split('.')[-2]
disable_termcolors() disable_termcolors()
@ -565,7 +564,6 @@ startapp.args = "[appname]"
def inspectdb(): def inspectdb():
"Generator that introspects the tables in the given database name and returns a Django model, one line at a time." "Generator that introspects the tables in the given database name and returns a Django model, one line at a time."
from django.db import connection, get_introspection_module from django.db import connection, get_introspection_module
from django.conf import settings
import keyword import keyword
introspection_module = get_introspection_module() introspection_module = get_introspection_module()

View File

@ -1,4 +1,3 @@
from copy import copy
from math import ceil from math import ceil
class InvalidPage(Exception): class InvalidPage(Exception):

View File

@ -79,7 +79,7 @@ def Deserializer(object_list, **options):
elif field.rel and isinstance(field.rel, models.ManyToOneRel): elif field.rel and isinstance(field.rel, models.ManyToOneRel):
try: try:
data[field.name] = field.rel.to._default_manager.get(pk=field_value) data[field.name] = field.rel.to._default_manager.get(pk=field_value)
except RelatedModel.DoesNotExist: except field.rel.to.DoesNotExist:
data[field.name] = None data[field.name] = None
# Handle all other fields # Handle all other fields

View File

@ -8,7 +8,7 @@ been reviewed for security issues. Don't use it for production use.
""" """
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from types import ListType, StringType, TupleType from types import ListType, StringType
import os, re, sys, time, urllib import os, re, sys, time, urllib
__version__ = "0.1" __version__ = "0.1"

View File

@ -40,7 +40,7 @@ class MysqlDebugWrapper:
def executemany(self, sql, param_list): def executemany(self, sql, param_list):
try: try:
return self.cursor.executemany(sql, param_list) return self.cursor.executemany(sql, param_list)
except Database.Warning: except Database.Warning, w:
self.cursor.execute("SHOW WARNINGS") self.cursor.execute("SHOW WARNINGS")
raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall()) raise Database.Warning, "%s: %s" % (w, self.cursor.fetchall())

View File

@ -1,4 +1,3 @@
from django.db import transaction
from django.db.backends.mysql.base import quote_name from django.db.backends.mysql.base import quote_name
from MySQLdb import ProgrammingError, OperationalError from MySQLdb import ProgrammingError, OperationalError
from MySQLdb.constants import FIELD_TYPE from MySQLdb.constants import FIELD_TYPE

View File

@ -10,7 +10,6 @@ try:
except ImportError, e: except ImportError, e:
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured, "Error loading cx_Oracle module: %s" % e raise ImproperlyConfigured, "Error loading cx_Oracle module: %s" % e
import types
DatabaseError = Database.Error DatabaseError = Database.Error

View File

@ -1,5 +1,3 @@
from django.db import transaction
from django.db.backends.oracle.base import quote_name
import re import re
foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)") foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")

View File

@ -1,4 +1,3 @@
from django.db import transaction
from django.db.backends.postgresql.base import quote_name from django.db.backends.postgresql.base import quote_name
def get_table_list(cursor): def get_table_list(cursor):

View File

@ -1,4 +1,3 @@
from django.db import transaction
from django.db.backends.postgresql_psycopg2.base import quote_name from django.db.backends.postgresql_psycopg2.base import quote_name
def get_table_list(cursor): def get_table_list(cursor):

View File

@ -1,4 +1,3 @@
from django.db import transaction
from django.db.backends.sqlite3.base import quote_name from django.db.backends.sqlite3.base import quote_name
def get_table_list(cursor): def get_table_list(cursor):

View File

@ -4,8 +4,7 @@ from django.core import validators
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist
from django.db.models.fields.related import OneToOneRel, ManyToOneRel from django.db.models.fields.related import OneToOneRel, ManyToOneRel
from django.db.models.related import RelatedObject from django.db.models.query import delete_objects
from django.db.models.query import orderlist2sql, delete_objects
from django.db.models.options import Options, AdminOptions from django.db.models.options import Options, AdminOptions
from django.db import transaction from django.db import transaction
from django.db.models import signals from django.db.models import signals

View File

@ -4,7 +4,7 @@ from django.conf import settings
from django.core import validators from django.core import validators
from django import forms from django import forms
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import curry, lazy from django.utils.functional import curry
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.translation import gettext, gettext_lazy from django.utils.translation import gettext, gettext_lazy
import datetime, os, time import datetime, os, time
@ -364,8 +364,8 @@ class BooleanField(Field):
def to_python(self, value): def to_python(self, value):
if value in (True, False): return value if value in (True, False): return value
if value is 't': return True if value in ('t', 'True'): return True
if value is 'f': return False if value in ('f', 'False'): return False
raise validators.ValidationError, gettext("This value must be either True or False.") raise validators.ValidationError, gettext("This value must be either True or False.")
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):

View File

@ -1,12 +1,8 @@
from django import core
from django.utils.functional import curry
from django.core.exceptions import ImproperlyConfigured
from django.db import ConnectionInfoDescriptor from django.db import ConnectionInfoDescriptor
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.dispatch import dispatcher from django.dispatch import dispatcher
from django.db.models import signals, get_apps, get_models from django.db.models import signals, get_apps, get_models
from django.db.models.fields import FieldDoesNotExist from django.db.models.fields import FieldDoesNotExist
from django.utils.datastructures import SortedDict
try: try:
# Only exists in Python 2.4+ # Only exists in Python 2.4+

View File

@ -5,7 +5,7 @@ from django.db.models.fields import FileField, AutoField
from django.dispatch import dispatcher from django.dispatch import dispatcher
from django.db.models import signals from django.db.models import signals
from django.utils.functional import curry from django.utils.functional import curry
from django.utils.datastructures import DotExpandedDict, MultiValueDict from django.utils.datastructures import DotExpandedDict
from django.utils.text import capfirst from django.utils.text import capfirst
import types import types
@ -76,7 +76,7 @@ class AutomaticManipulator(forms.Manipulator):
# Add field for ordering. # Add field for ordering.
if self.change and self.opts.get_ordered_objects(): if self.change and self.opts.get_ordered_objects():
self.fields.append(formfields.CommaSeparatedIntegerField(field_name="order_")) self.fields.append(forms.CommaSeparatedIntegerField(field_name="order_"))
def save(self, new_data): def save(self, new_data):
# TODO: big cleanup when core fields go -> use recursive manipulators. # TODO: big cleanup when core fields go -> use recursive manipulators.

View File

@ -18,7 +18,7 @@ QUERY_TERMS = (
'exact', 'iexact', 'contains', 'icontains', 'exact', 'iexact', 'contains', 'icontains',
'gt', 'gte', 'lt', 'lte', 'in', 'gt', 'gte', 'lt', 'lte', 'in',
'startswith', 'istartswith', 'endswith', 'iendswith', 'startswith', 'istartswith', 'endswith', 'iendswith',
'range', 'year', 'month', 'day', 'isnull', 'range', 'year', 'month', 'day', 'isnull', 'search',
) )
# Size of each "chunk" for get_iterator calls. # Size of each "chunk" for get_iterator calls.

View File

@ -45,5 +45,3 @@ def robustApply(receiver, *arguments, **named):
if arg not in acceptable: if arg not in acceptable:
del named[arg] del named[arg]
return receiver(*arguments, **named) return receiver(*arguments, **named)

View File

@ -1,3 +1,4 @@
import os
from Cookie import SimpleCookie from Cookie import SimpleCookie
from pprint import pformat from pprint import pformat
from urllib import urlencode, quote from urllib import urlencode, quote
@ -38,6 +39,9 @@ class HttpRequest(object):
def get_full_path(self): def get_full_path(self):
return '' return ''
def is_secure(self):
return os.environ.get("HTTPS") == "on"
def parse_file_upload(header_dict, post_data): def parse_file_upload(header_dict, post_data):
"Returns a tuple of (POST MultiValueDict, FILES MultiValueDict)" "Returns a tuple of (POST MultiValueDict, FILES MultiValueDict)"
import email, email.Message import email, email.Message

View File

@ -1,7 +1,6 @@
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers from django.utils.cache import get_cache_key, learn_cache_key, patch_response_headers
from django.http import HttpResponseNotModified
class CacheMiddleware(object): class CacheMiddleware(object):
""" """
@ -10,6 +9,11 @@ class CacheMiddleware(object):
Only parameter-less GET or HEAD-requests with status code 200 are cached. Only parameter-less GET or HEAD-requests with status code 200 are cached.
If CACHE_MIDDLEWARE_ANONYMOUS_ONLY is set to True, only anonymous requests
(i.e., those node made by a logged-in user) will be cached. This is a
simple and effective way of avoiding the caching of the Django admin (and
any other user-specific content).
This middleware expects that a HEAD request is answered with a response This middleware expects that a HEAD request is answered with a response
exactly like the corresponding GET request. exactly like the corresponding GET request.
@ -23,13 +27,17 @@ class CacheMiddleware(object):
This middleware also sets ETag, Last-Modified, Expires and Cache-Control This middleware also sets ETag, Last-Modified, Expires and Cache-Control
headers on the response object. headers on the response object.
""" """
def __init__(self, cache_timeout=None, key_prefix=None): def __init__(self, cache_timeout=None, key_prefix=None, cache_anonymous_only=None):
self.cache_timeout = cache_timeout self.cache_timeout = cache_timeout
if cache_timeout is None: if cache_timeout is None:
self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS self.cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
self.key_prefix = key_prefix self.key_prefix = key_prefix
if key_prefix is None: if key_prefix is None:
self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
if cache_anonymous_only is None:
self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
else:
self.cache_anonymous_only = cache_anonymous_only
def process_request(self, request): def process_request(self, request):
"Checks whether the page is already cached and returns the cached version if available." "Checks whether the page is already cached and returns the cached version if available."
@ -37,6 +45,10 @@ class CacheMiddleware(object):
request._cache_update_cache = False request._cache_update_cache = False
return None # Don't bother checking the cache. return None # Don't bother checking the cache.
if self.cache_anonymous_only and request.user.is_authenticated():
request._cache_update_cache = False
return None # Don't cache requests from authenticated users.
cache_key = get_cache_key(request, self.key_prefix) cache_key = get_cache_key(request, self.key_prefix)
if cache_key is None: if cache_key is None:
request._cache_update_cache = True request._cache_update_cache = True

View File

@ -1,7 +1,7 @@
from django.conf import settings from django.conf import settings
from django import http from django import http
from django.core.mail import mail_managers from django.core.mail import mail_managers
import md5, os import md5
class CommonMiddleware(object): class CommonMiddleware(object):
""" """
@ -44,7 +44,7 @@ class CommonMiddleware(object):
if new_url != old_url: if new_url != old_url:
# Redirect # Redirect
if new_url[0]: if new_url[0]:
newurl = "%s://%s%s" % (os.environ.get('HTTPS') == 'on' and 'https' or 'http', new_url[0], new_url[1]) newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', new_url[0], new_url[1])
else: else:
newurl = new_url[1] newurl = new_url[1]
if request.GET: if request.GET:

View File

@ -1,4 +1,3 @@
from django.conf import settings
from django.db import transaction from django.db import transaction
class TransactionMiddleware(object): class TransactionMiddleware(object):

View File

@ -708,9 +708,9 @@ class DebugNodeList(NodeList):
if not hasattr(e, 'source'): if not hasattr(e, 'source'):
e.source = node.source e.source = node.source
raise raise
except Exception: except Exception, e:
from sys import exc_info from sys import exc_info
wrapped = TemplateSyntaxError('Caught an exception while rendering.') wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % e)
wrapped.source = node.source wrapped.source = node.source
wrapped.exc_info = exc_info() wrapped.exc_info = exc_info()
raise wrapped raise wrapped
@ -817,7 +817,7 @@ class Library(object):
self.filters[name] = filter_func self.filters[name] = filter_func
return filter_func return filter_func
else: else:
raise InvalidTemplateLibrary, "Unsupported arguments to Library.filter: (%r, %r, %r)", (name, compile_function, has_arg) raise InvalidTemplateLibrary, "Unsupported arguments to Library.filter: (%r, %r)", (name, filter_func)
def filter_function(self, func): def filter_function(self, func):
self.filters[func.__name__] = func self.filters[func.__name__] = func

View File

@ -21,7 +21,7 @@
# installed, because pkg_resources is necessary to read eggs. # installed, because pkg_resources is necessary to read eggs.
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.template import Origin, StringOrigin, Template, Context, TemplateDoesNotExist, add_to_builtins from django.template import Origin, Template, Context, TemplateDoesNotExist, add_to_builtins
from django.conf import settings from django.conf import settings
template_source_loaders = None template_source_loaders = None

View File

@ -1,5 +1,5 @@
from django.template import TemplateSyntaxError, TemplateDoesNotExist, resolve_variable from django.template import TemplateSyntaxError, TemplateDoesNotExist, resolve_variable
from django.template import Library, Context, Node from django.template import Library, Node
from django.template.loader import get_template, get_template_from_string, find_template_source from django.template.loader import get_template, get_template_from_string, find_template_source
from django.conf import settings from django.conf import settings

View File

@ -1,8 +1,7 @@
from django.template import Node, NodeList, Template, Context, resolve_variable from django.template import Node, resolve_variable
from django.template import TemplateSyntaxError, TokenParser, Library from django.template import TemplateSyntaxError, TokenParser, Library
from django.template import TOKEN_BLOCK, TOKEN_TEXT, TOKEN_VAR from django.template import TOKEN_TEXT, TOKEN_VAR
from django.utils import translation from django.utils import translation
import re, sys
register = Library() register = Library()
@ -228,7 +227,7 @@ def do_block_translate(parser, token):
break break
if countervar and counter: if countervar and counter:
if token.contents.strip() != 'plural': if token.contents.strip() != 'plural':
raise TemplateSyntaxError, "'blocktrans' doesn't allow other block tags inside it" % tag raise TemplateSyntaxError, "'blocktrans' doesn't allow other block tags inside it"
while parser.tokens: while parser.tokens:
token = parser.next_token() token = parser.next_token()
if token.token_type in (TOKEN_VAR, TOKEN_TEXT): if token.token_type in (TOKEN_VAR, TOKEN_TEXT):

View File

@ -2,8 +2,6 @@
termcolors.py termcolors.py
""" """
import types
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
foreground = dict([(color_names[x], '3%s' % x) for x in range(8)]) foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
background = dict([(color_names[x], '4%s' % x) for x in range(8)]) background = dict([(color_names[x], '4%s' % x) for x in range(8)])

View File

@ -3,8 +3,6 @@ from django.template import Template, Context, TemplateDoesNotExist
from django.utils.html import escape from django.utils.html import escape
from django.http import HttpResponseServerError, HttpResponseNotFound from django.http import HttpResponseServerError, HttpResponseNotFound
import os, re import os, re
from itertools import count, izip
from os.path import dirname, join as pathjoin
HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD') HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD')
@ -124,7 +122,7 @@ def technical_500_response(request, exc_type, exc_value, tb):
'frames': frames, 'frames': frames,
'lastframe': frames[-1], 'lastframe': frames[-1],
'request': request, 'request': request,
'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", 'request_protocol': request.is_secure() and "https" or "http",
'settings': get_safe_settings(), 'settings': get_safe_settings(),
'template_info': template_info, 'template_info': template_info,
'template_does_not_exist': template_does_not_exist, 'template_does_not_exist': template_does_not_exist,
@ -149,7 +147,7 @@ def technical_404_response(request, exception):
'urlpatterns': tried, 'urlpatterns': tried,
'reason': str(exception), 'reason': str(exception),
'request': request, 'request': request,
'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", 'request_protocol': request.is_secure() and "https" or "http",
'settings': get_safe_settings(), 'settings': get_safe_settings(),
}) })
return HttpResponseNotFound(t.render(c), mimetype='text/html') return HttpResponseNotFound(t.render(c), mimetype='text/html')

View File

@ -10,7 +10,6 @@ example, as that is unique across a Django project.
Additionally, all headers from the response's Vary header will be taken into Additionally, all headers from the response's Vary header will be taken into
account on caching -- just like the middleware does. account on caching -- just like the middleware does.
""" """
import re
from django.utils.decorators import decorator_from_middleware from django.utils.decorators import decorator_from_middleware
from django.utils.cache import patch_cache_control, add_never_cache_headers from django.utils.cache import patch_cache_control, add_never_cache_headers

View File

@ -4,7 +4,6 @@ from django import forms
from django.db.models import FileField from django.db.models import FileField
from django.contrib.auth.views import redirect_to_login from django.contrib.auth.views import redirect_to_login
from django.template import RequestContext from django.template import RequestContext
from django.core.paginator import ObjectPaginator, InvalidPage
from django.http import Http404, HttpResponse, HttpResponseRedirect from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
@ -20,7 +19,7 @@ def create_object(request, model, template_name=None,
the form wrapper for the object the form wrapper for the object
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
if login_required and request.user.is_anonymous(): if login_required and not request.user.is_authenticated():
return redirect_to_login(request.path) return redirect_to_login(request.path)
manipulator = model.AddManipulator(follow=follow) manipulator = model.AddManipulator(follow=follow)
@ -39,7 +38,7 @@ def create_object(request, model, template_name=None,
# No errors -- this means we can save the data! # No errors -- this means we can save the data!
new_object = manipulator.save(new_data) new_object = manipulator.save(new_data)
if not request.user.is_anonymous(): if request.user.is_authenticated():
request.user.message_set.create(message="The %s was created successfully." % model._meta.verbose_name) request.user.message_set.create(message="The %s was created successfully." % model._meta.verbose_name)
# Redirect to the new object: first by trying post_save_redirect, # Redirect to the new object: first by trying post_save_redirect,
@ -86,7 +85,7 @@ def update_object(request, model, object_id=None, slug=None,
the original object being edited the original object being edited
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
if login_required and request.user.is_anonymous(): if login_required and not request.user.is_authenticated():
return redirect_to_login(request.path) return redirect_to_login(request.path)
# Look up the object to be edited # Look up the object to be edited
@ -113,7 +112,7 @@ def update_object(request, model, object_id=None, slug=None,
if not errors: if not errors:
object = manipulator.save(new_data) object = manipulator.save(new_data)
if not request.user.is_anonymous(): if request.user.is_authenticated():
request.user.message_set.create(message="The %s was updated successfully." % model._meta.verbose_name) request.user.message_set.create(message="The %s was updated successfully." % model._meta.verbose_name)
# Do a post-after-redirect so that reload works, etc. # Do a post-after-redirect so that reload works, etc.
@ -162,7 +161,7 @@ def delete_object(request, model, post_delete_redirect,
the original object being deleted the original object being deleted
""" """
if extra_context is None: extra_context = {} if extra_context is None: extra_context = {}
if login_required and request.user.is_anonymous(): if login_required and not request.user.is_authenticated():
return redirect_to_login(request.path) return redirect_to_login(request.path)
# Look up the object to be edited # Look up the object to be edited
@ -180,7 +179,7 @@ def delete_object(request, model, post_delete_redirect,
if request.method == 'POST': if request.method == 'POST':
object.delete() object.delete()
if not request.user.is_anonymous(): if request.user.is_authenticated():
request.user.message_set.create(message="The %s was deleted." % model._meta.verbose_name) request.user.message_set.create(message="The %s was deleted." % model._meta.verbose_name)
return HttpResponseRedirect(post_delete_redirect) return HttpResponseRedirect(post_delete_redirect)
else: else:

View File

@ -1,5 +1,4 @@
from django.template import loader from django.template import loader
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified
from django.template import Template, Context, TemplateDoesNotExist from django.template import Template, Context, TemplateDoesNotExist
import mimetypes import mimetypes

View File

@ -95,7 +95,11 @@ In addition to those automatic API methods, ``User`` objects have the following
custom methods: custom methods:
* ``is_anonymous()`` -- Always returns ``False``. This is a way of * ``is_anonymous()`` -- Always returns ``False``. This is a way of
comparing ``User`` objects to anonymous users. differentiating ``User`` and ``AnonymousUser`` objects. Generally, you
should prefer using ``is_authenticated()`` to this method.
* ``is_authenticated()`` -- Always returns ``True``. This is a way to
tell if the user has been authenticated.
* ``get_full_name()`` -- Returns the ``first_name`` plus the ``last_name``, * ``get_full_name()`` -- Returns the ``first_name`` plus the ``last_name``,
with a space in between. with a space in between.
@ -219,6 +223,7 @@ the ``django.contrib.auth.models.User`` interface, with these differences:
* ``id`` is always ``None``. * ``id`` is always ``None``.
* ``is_anonymous()`` returns ``True`` instead of ``False``. * ``is_anonymous()`` returns ``True`` instead of ``False``.
* ``is_authenticated()`` returns ``False`` instead of ``True``.
* ``has_perm()`` always returns ``False``. * ``has_perm()`` always returns ``False``.
* ``set_password()``, ``check_password()``, ``save()``, ``delete()``, * ``set_password()``, ``check_password()``, ``save()``, ``delete()``,
``set_groups()`` and ``set_permissions()`` raise ``NotImplementedError``. ``set_groups()`` and ``set_permissions()`` raise ``NotImplementedError``.
@ -254,12 +259,12 @@ Once you have those middlewares installed, you'll be able to access
``request.user`` in views. ``request.user`` will give you a ``User`` object ``request.user`` in views. ``request.user`` will give you a ``User`` object
representing the currently logged-in user. If a user isn't currently logged in, representing the currently logged-in user. If a user isn't currently logged in,
``request.user`` will be set to an instance of ``AnonymousUser`` (see the ``request.user`` will be set to an instance of ``AnonymousUser`` (see the
previous section). You can tell them apart with ``is_anonymous()``, like so:: previous section). You can tell them apart with ``is_authenticated()``, like so::
if request.user.is_anonymous(): if request.user.is_authenticated():
# Do something for anonymous users. # Do something for authenticated users.
else: else:
# Do something for logged-in users. # Do something for anonymous users.
.. _request objects: http://www.djangoproject.com/documentation/request_response/#httprequest-objects .. _request objects: http://www.djangoproject.com/documentation/request_response/#httprequest-objects
.. _session documentation: http://www.djangoproject.com/documentation/sessions/ .. _session documentation: http://www.djangoproject.com/documentation/sessions/
@ -323,19 +328,19 @@ The raw way
~~~~~~~~~~~ ~~~~~~~~~~~
The simple, raw way to limit access to pages is to check The simple, raw way to limit access to pages is to check
``request.user.is_anonymous()`` and either redirect to a login page:: ``request.user.is_authenticated()`` and either redirect to a login page::
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
def my_view(request): def my_view(request):
if request.user.is_anonymous(): if not request.user.is_authenticated():
return HttpResponseRedirect('/login/?next=%s' % request.path) return HttpResponseRedirect('/login/?next=%s' % request.path)
# ... # ...
...or display an error message:: ...or display an error message::
def my_view(request): def my_view(request):
if request.user.is_anonymous(): if not request.user.is_authenticated():
return render_to_response('myapp/login_error.html') return render_to_response('myapp/login_error.html')
# ... # ...
@ -439,7 +444,7 @@ For example, this view checks to make sure the user is logged in and has the
permission ``polls.can_vote``:: permission ``polls.can_vote``::
def my_view(request): def my_view(request):
if request.user.is_anonymous() or not request.user.has_perm('polls.can_vote'): if not (request.user.is_authenticated() and request.user.has_perm('polls.can_vote')):
return HttpResponse("You can't vote in this poll.") return HttpResponse("You can't vote in this poll.")
# ... # ...
@ -605,10 +610,10 @@ Users
The currently logged-in user, either a ``User`` instance or an``AnonymousUser`` The currently logged-in user, either a ``User`` instance or an``AnonymousUser``
instance, is stored in the template variable ``{{ user }}``:: instance, is stored in the template variable ``{{ user }}``::
{% if user.is_anonymous %} {% if user.is_authenticated %}
<p>Welcome, new user. Please log in.</p>
{% else %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p> <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %} {% endif %}
Permissions Permissions

View File

@ -230,8 +230,13 @@ Then, add the following required settings to your Django settings file:
collisions. Use an empty string if you don't care. collisions. Use an empty string if you don't care.
The cache middleware caches every page that doesn't have GET or POST The cache middleware caches every page that doesn't have GET or POST
parameters. Additionally, ``CacheMiddleware`` automatically sets a few headers parameters. Optionally, if the ``CACHE_MIDDLEWARE_ANONYMOUS_ONLY`` setting is
in each ``HttpResponse``: ``True``, only anonymous requests (i.e., not those made by a logged-in user)
will be cached. This is a simple and effective way of disabling caching for any
user-specific pages (include Django's admin interface).
Additionally, ``CacheMiddleware`` automatically sets a few headers in each
``HttpResponse``:
* Sets the ``Last-Modified`` header to the current date/time when a fresh * Sets the ``Last-Modified`` header to the current date/time when a fresh
(uncached) version of the page is requested. (uncached) version of the page is requested.

View File

@ -192,6 +192,14 @@ documentation.
.. _serving static files: http://www.djangoproject.com/documentation/static_files/ .. _serving static files: http://www.djangoproject.com/documentation/static_files/
Turning off auto-reload
~~~~~~~~~~~~~~~~~~~~~~~
To disable auto-reloading of code while the development server is running, use the
``--noreload`` option, like so::
django-admin.py runserver --noreload
shell shell
----- -----

View File

@ -535,6 +535,14 @@ If you're sure your username and password are correct, make sure your user
account has ``is_active`` and ``is_staff`` set to True. The admin site only account has ``is_active`` and ``is_staff`` set to True. The admin site only
allows access to users with those two fields both set to True. allows access to users with those two fields both set to True.
How can I prevent the cache middleware from caching the admin site?
-------------------------------------------------------------------
Set the ``CACHE_MIDDLEWARE_ANONYMOUS_ONLY`` setting to ``True``. See the
`cache documentation`_ for more information.
.. _cache documentation: ../cache/#the-per-site-cache
How do I automatically set a field's value to the user who last edited the object in the admin? How do I automatically set a field's value to the user who last edited the object in the admin?
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------

View File

@ -1225,6 +1225,24 @@ A few special cases to note about ``list_display``:
return self.birthday.strftime('%Y')[:3] + "0's" return self.birthday.strftime('%Y')[:3] + "0's"
decade_born_in.short_description = 'Birth decade' decade_born_in.short_description = 'Birth decade'
* If the string given is a method of the model, Django will HTML-escape the
output by default. If you'd rather not escape the output of the method,
give the method an ``allow_tags`` attribute whose value is ``True``.
Here's a full example model::
class Person(models.Model):
first_name = models.CharField(maxlength=50)
last_name = models.CharField(maxlength=50)
color_code = models.CharField(maxlength=6)
class Admin:
list_display = ('first_name', 'last_name', 'colored_name')
def colored_name(self):
return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True
``list_display_links`` ``list_display_links``
---------------------- ----------------------

View File

@ -106,12 +106,12 @@ All attributes except ``session`` should be considered read-only.
A ``django.contrib.auth.models.User`` object representing the currently A ``django.contrib.auth.models.User`` object representing the currently
logged-in user. If the user isn't currently logged in, ``user`` will be set logged-in user. If the user isn't currently logged in, ``user`` will be set
to an instance of ``django.contrib.auth.models.AnonymousUser``. You to an instance of ``django.contrib.auth.models.AnonymousUser``. You
can tell them apart with ``is_anonymous()``, like so:: can tell them apart with ``is_authenticated()``, like so::
if request.user.is_anonymous(): if request.user.is_authenticated():
# Do something for anonymous users.
else:
# Do something for logged-in users. # Do something for logged-in users.
else:
# Do something for anonymous users.
``user`` is only available if your Django installation has the ``user`` is only available if your Django installation has the
``AuthenticationMiddleware`` activated. For more, see ``AuthenticationMiddleware`` activated. For more, see
@ -150,6 +150,10 @@ Methods
Example: ``"/music/bands/the_beatles/?print=true"`` Example: ``"/music/bands/the_beatles/?print=true"``
``is_secure()``
Returns ``True`` if the request is secure; that is, if it was made with
HTTPS.
QueryDict objects QueryDict objects
----------------- -----------------

View File

@ -346,6 +346,8 @@ Note the following:
the SQL to the database. the SQL to the database.
If you're interested, also run the following commands: If you're interested, also run the following commands:
* ``python manage.py validate polls`` -- Checks for any errors in the
construction of your models.
* ``python manage.py sqlinitialdata polls`` -- Outputs any initial data * ``python manage.py sqlinitialdata polls`` -- Outputs any initial data
required for Django's admin framework and your models. required for Django's admin framework and your models.

View File

@ -189,7 +189,7 @@ publication date::
from django.http import HttpResponse from django.http import HttpResponse
def index(request): def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date') latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
output = ', '.join([p.question for p in latest_poll_list]) output = ', '.join([p.question for p in latest_poll_list])
return HttpResponse(output) return HttpResponse(output)
@ -202,7 +202,7 @@ So let's use Django's template system to separate the design from Python::
from django.http import HttpResponse from django.http import HttpResponse
def index(request): def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date') latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
t = loader.get_template('polls/index.html') t = loader.get_template('polls/index.html')
c = Context({ c = Context({
'latest_poll_list': latest_poll_list, 'latest_poll_list': latest_poll_list,
@ -288,7 +288,7 @@ exception if a poll with the requested ID doesn't exist.
A shortcut: get_object_or_404() A shortcut: get_object_or_404()
------------------------------- -------------------------------
It's a very common idiom to use ``get_object()`` and raise ``Http404`` if the It's a very common idiom to use ``get()`` and raise ``Http404`` if the
object doesn't exist. Django provides a shortcut. Here's the ``detail()`` view, object doesn't exist. Django provides a shortcut. Here's the ``detail()`` view,
rewritten:: rewritten::
@ -313,8 +313,8 @@ exist.
foremost design goals of Django is to maintain loose coupling. foremost design goals of Django is to maintain loose coupling.
There's also a ``get_list_or_404()`` function, which works just as There's also a ``get_list_or_404()`` function, which works just as
``get_object_or_404()`` -- except using ``get_list()`` instead of ``get_object_or_404()`` -- except using ``filter()`` instead of
``get_object()``. It raises ``Http404`` if the list is empty. ``get()``. It raises ``Http404`` if the list is empty.
Write a 404 (page not found) view Write a 404 (page not found) view
================================= =================================

View File

@ -41,6 +41,7 @@ setup(
'locale/sl/LC_MESSAGES/*', 'locale/sl/LC_MESSAGES/*',
'locale/sr/LC_MESSAGES/*', 'locale/sr/LC_MESSAGES/*',
'locale/sv/LC_MESSAGES/*', 'locale/sv/LC_MESSAGES/*',
'locale/ta/LC_MESSAGES/*',
'locale/uk/LC_MESSAGES/*', 'locale/uk/LC_MESSAGES/*',
'locale/zh_CN/LC_MESSAGES/*', 'locale/zh_CN/LC_MESSAGES/*',
'locale/zh_TW/LC_MESSAGES/*'], 'locale/zh_TW/LC_MESSAGES/*'],

View File

@ -1,6 +1,7 @@
# Quick tests for the markup templatetags (django.contrib.markup) # Quick tests for the markup templatetags (django.contrib.markup)
from django.template import Template, Context, add_to_builtins from django.template import Template, Context, add_to_builtins
import re
add_to_builtins('django.contrib.markup.templatetags.markup') add_to_builtins('django.contrib.markup.templatetags.markup')
@ -47,7 +48,8 @@ markdown_content = """Paragraph 1
t = Template("{{ markdown_content|markdown }}") t = Template("{{ markdown_content|markdown }}")
rendered = t.render(Context(locals())).strip() rendered = t.render(Context(locals())).strip()
if markdown: if markdown:
assert rendered == """<p>Paragraph 1</p><h2>An h2</h2>""" pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
assert pattern.match(rendered)
else: else:
assert rendered == markdown_content assert rendered == markdown_content