summaryrefslogtreecommitdiff
path: root/django/core
diff options
context:
space:
mode:
authorChristopher Long <indirecthit@gmail.com>2007-06-17 22:18:54 +0000
committerChristopher Long <indirecthit@gmail.com>2007-06-17 22:18:54 +0000
commitae22b6d403dcf25098c77f0dfcf59ae58b186461 (patch)
treec37fc631e99a7e4d909d6b6d236f495003731ea7 /django/core
parent0cf7bc439129c66df8d64601e885f83b256b4f25 (diff)
per-object-permissions: Merged to trunk [5486] NOTE: Not fully tested, will be working on this over the next few weeks.
git-svn-id: http://code.djangoproject.com/svn/django/branches/per-object-permissions@5488 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core')
-rw-r--r--django/core/cache/backends/base.py3
-rw-r--r--django/core/cache/backends/dummy.py6
-rw-r--r--django/core/cache/backends/memcached.py7
-rw-r--r--django/core/cache/backends/simple.py2
-rw-r--r--django/core/context_processors.py7
-rw-r--r--django/core/handlers/modpython.py4
-rw-r--r--django/core/handlers/wsgi.py2
-rw-r--r--django/core/mail.py241
-rw-r--r--django/core/management.py460
-rw-r--r--django/core/serializers/__init__.py12
-rw-r--r--django/core/serializers/base.py34
-rw-r--r--django/core/serializers/json.py34
-rw-r--r--django/core/serializers/python.py28
-rw-r--r--django/core/serializers/pyyaml.py38
-rw-r--r--django/core/serializers/xml_serializer.py58
-rw-r--r--django/core/servers/basehttp.py21
-rw-r--r--django/core/servers/fastcgi.py25
-rw-r--r--django/core/urlresolvers.py20
-rw-r--r--django/core/validators.py49
19 files changed, 778 insertions, 273 deletions
diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
index ef5f6a6b3e..bb67399f3b 100644
--- a/django/core/cache/backends/base.py
+++ b/django/core/cache/backends/base.py
@@ -54,3 +54,6 @@ class BaseCache(object):
Returns True if the key is in the cache and has not expired.
"""
return self.get(key) is not None
+
+ __contains__ = has_key
+
diff --git a/django/core/cache/backends/dummy.py b/django/core/cache/backends/dummy.py
index c68f33616c..4c64161538 100644
--- a/django/core/cache/backends/dummy.py
+++ b/django/core/cache/backends/dummy.py
@@ -6,8 +6,8 @@ class CacheClass(BaseCache):
def __init__(self, *args, **kwargs):
pass
- def get(self, *args, **kwargs):
- pass
+ def get(self, key, default=None):
+ return default
def set(self, *args, **kwargs):
pass
@@ -16,7 +16,7 @@ class CacheClass(BaseCache):
pass
def get_many(self, *args, **kwargs):
- pass
+ return {}
def has_key(self, *args, **kwargs):
return False
diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
index 180f95da73..1ab019221a 100644
--- a/django/core/cache/backends/memcached.py
+++ b/django/core/cache/backends/memcached.py
@@ -3,9 +3,12 @@
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
try:
- import memcache
+ import cmemcache as memcache
except ImportError:
- raise InvalidCacheBackendError, "Memcached cache backend requires the 'memcache' library"
+ try:
+ import memcache
+ except:
+ raise InvalidCacheBackendError("Memcached cache backend requires either the 'memcache' or 'cmemcache' library")
class CacheClass(BaseCache):
def __init__(self, server, params):
diff --git a/django/core/cache/backends/simple.py b/django/core/cache/backends/simple.py
index 175944a75a..3fcad8c7ad 100644
--- a/django/core/cache/backends/simple.py
+++ b/django/core/cache/backends/simple.py
@@ -52,7 +52,7 @@ class CacheClass(BaseCache):
pass
def has_key(self, key):
- return self._cache.has_key(key)
+ return key in self._cache
def _cull(self):
if self._cull_frequency == 0:
diff --git a/django/core/context_processors.py b/django/core/context_processors.py
index f4b288dfc4..3c826b1a7d 100644
--- a/django/core/context_processors.py
+++ b/django/core/context_processors.py
@@ -42,6 +42,13 @@ def i18n(request):
return context_extras
+def media(request):
+ """
+ Adds media-related context variables to the context.
+
+ """
+ return {'MEDIA_URL': settings.MEDIA_URL}
+
def request(request):
return {'request': request}
diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py
index 5fc41a048b..6370cab47c 100644
--- a/django/core/handlers/modpython.py
+++ b/django/core/handlers/modpython.py
@@ -42,11 +42,11 @@ class ModPythonRequest(http.HttpRequest):
def is_secure(self):
# Note: modpython 3.2.10+ has req.is_https(), but we need to support previous versions
- return self._req.subprocess_env.has_key('HTTPS') and self._req.subprocess_env['HTTPS'] == 'on'
+ return 'HTTPS' in self._req.subprocess_env and self._req.subprocess_env['HTTPS'] == 'on'
def _load_post_and_files(self):
"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 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'):
self._post, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data)
else:
self._post, self._files = http.QueryDict(self.raw_post_data), datastructures.MultiValueDict()
diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
index 71cfecd9a0..4320b69627 100644
--- a/django/core/handlers/wsgi.py
+++ b/django/core/handlers/wsgi.py
@@ -103,7 +103,7 @@ class WSGIRequest(http.HttpRequest):
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'
+ return 'HTTPS' in self.environ and self.environ['HTTPS'] == 'on'
def _load_post_and_files(self):
# Populates self._post and self._files
diff --git a/django/core/mail.py b/django/core/mail.py
index a5af6e610f..8661d84287 100644
--- a/django/core/mail.py
+++ b/django/core/mail.py
@@ -1,14 +1,56 @@
-# Use this module for e-mailing.
+"""
+Tools for sending email.
+"""
from django.conf import settings
from email.MIMEText import MIMEText
from email.Header import Header
-import smtplib, rfc822
+from email.Utils import formatdate
+from email import Charset
+import os
+import smtplib
import socket
import time
import random
-DNS_NAME = socket.getfqdn() # Cache the hostname
+# Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
+# some spam filters.
+Charset.add_charset('utf-8', Charset.SHORTEST, Charset.QP, 'utf-8')
+
+# Cache the hostname, but do it lazily: socket.getfqdn() can take a couple of
+# seconds, which slows down the restart of the server.
+class CachedDnsName(object):
+ def __str__(self):
+ return self.get_fqdn()
+
+ def get_fqdn(self):
+ if not hasattr(self, '_fqdn'):
+ self._fqdn = socket.getfqdn()
+ return self._fqdn
+
+DNS_NAME = CachedDnsName()
+
+# Copied from Python standard library and modified to used the cached hostname
+# for performance.
+def make_msgid(idstring=None):
+ """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
+
+ <20020201195627.33539.96671@nightshade.la.mastaler.com>
+
+ Optional idstring if given is a string used to strengthen the
+ uniqueness of the message id.
+ """
+ timeval = time.time()
+ utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval))
+ pid = os.getpid()
+ randint = random.randrange(100000)
+ if idstring is None:
+ idstring = ''
+ else:
+ idstring = '.' + idstring
+ idhost = DNS_NAME
+ msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost)
+ return msgid
class BadHeaderError(ValueError):
pass
@@ -22,62 +64,173 @@ class SafeMIMEText(MIMEText):
val = Header(val, settings.DEFAULT_CHARSET)
MIMEText.__setitem__(self, name, val)
-def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
+class SMTPConnection(object):
+ """
+ A wrapper that manages the SMTP network connection.
+ """
+
+ def __init__(self, host=None, port=None, username=None, password=None,
+ use_tls=None, fail_silently=False):
+ self.host = host or settings.EMAIL_HOST
+ self.port = port or settings.EMAIL_PORT
+ self.username = username or settings.EMAIL_HOST_USER
+ self.password = password or settings.EMAIL_HOST_PASSWORD
+ self.use_tls = (use_tls is not None) and use_tls or settings.EMAIL_USE_TLS
+ self.fail_silently = fail_silently
+ self.connection = None
+
+ def open(self):
+ """
+ Ensure we have a connection to the email server. Returns whether or not
+ a new connection was required.
+ """
+ if self.connection:
+ # Nothing to do if the connection is already open.
+ return False
+ try:
+ self.connection = smtplib.SMTP(self.host, self.port)
+ if self.use_tls:
+ self.connection.ehlo()
+ self.connection.starttls()
+ self.connection.ehlo()
+ if self.username and self.password:
+ self.connection.login(self.username, self.password)
+ return True
+ except:
+ if not self.fail_silently:
+ raise
+
+ def close(self):
+ """Close the connection to the email server."""
+ try:
+ try:
+ self.connection.quit()
+ except socket.sslerror:
+ # This happens when calling quit() on a TLS connection
+ # sometimes.
+ self.connection.close()
+ except:
+ if self.fail_silently:
+ return
+ raise
+ finally:
+ self.connection = None
+
+ def send_messages(self, email_messages):
+ """
+ Send one or more EmailMessage objects and return the number of email
+ messages sent.
+ """
+ if not email_messages:
+ return
+ new_conn_created = self.open()
+ if not self.connection:
+ # We failed silently on open(). Trying to send would be pointless.
+ return
+ num_sent = 0
+ for message in email_messages:
+ sent = self._send(message)
+ if sent:
+ num_sent += 1
+ if new_conn_created:
+ self.close()
+ return num_sent
+
+ def _send(self, email_message):
+ """A helper method that does the actual sending."""
+ if not email_message.to:
+ return False
+ try:
+ self.connection.sendmail(email_message.from_email,
+ email_message.recipients(),
+ email_message.message().as_string())
+ except:
+ if not self.fail_silently:
+ raise
+ return False
+ return True
+
+class EmailMessage(object):
+ """
+ A container for email information.
+ """
+ def __init__(self, subject='', body='', from_email=None, to=None, bcc=None, connection=None):
+ self.to = to or []
+ self.bcc = bcc or []
+ self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
+ self.subject = subject
+ self.body = body
+ self.connection = connection
+
+ def get_connection(self, fail_silently=False):
+ if not self.connection:
+ self.connection = SMTPConnection(fail_silently=fail_silently)
+ return self.connection
+
+ def message(self):
+ msg = SafeMIMEText(self.body, 'plain', settings.DEFAULT_CHARSET)
+ msg['Subject'] = self.subject
+ msg['From'] = self.from_email
+ msg['To'] = ', '.join(self.to)
+ msg['Date'] = formatdate()
+ msg['Message-ID'] = make_msgid()
+ if self.bcc:
+ msg['Bcc'] = ', '.join(self.bcc)
+ return msg
+
+ def recipients(self):
+ """
+ Returns a list of all recipients of the email (includes direct
+ addressees as well as Bcc entries).
+ """
+ return self.to + self.bcc
+
+ def send(self, fail_silently=False):
+ """Send the email message."""
+ return self.get_connection(fail_silently).send_messages([self])
+
+def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None):
"""
Easy wrapper for sending a single message to a recipient list. All members
of the recipient list will see the other recipients in the 'To' field.
+
+ If auth_user is None, the EMAIL_HOST_USER setting is used.
+ If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
+
+ NOTE: This method is deprecated. It exists for backwards compatibility.
+ New code should use the EmailMessage class directly.
"""
- return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
+ connection = SMTPConnection(username=auth_user, password=auth_password,
+ fail_silently=fail_silently)
+ return EmailMessage(subject, message, from_email, recipient_list, connection=connection).send()
-def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
+def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None):
"""
Given a datatuple of (subject, message, from_email, recipient_list), sends
each message to each recipient list. Returns the number of e-mails sent.
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
If auth_user and auth_password are set, they're used to log in.
+ If auth_user is None, the EMAIL_HOST_USER setting is used.
+ If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
+
+ NOTE: This method is deprecated. It exists for backwards compatibility.
+ New code should use the EmailMessage class directly.
"""
- try:
- server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
- if auth_user and auth_password:
- server.login(auth_user, auth_password)
- except:
- if fail_silently:
- return
- raise
- num_sent = 0
- for subject, message, from_email, recipient_list in datatuple:
- if not recipient_list:
- continue
- from_email = from_email or settings.DEFAULT_FROM_EMAIL
- msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET)
- msg['Subject'] = subject
- msg['From'] = from_email
- msg['To'] = ', '.join(recipient_list)
- msg['Date'] = rfc822.formatdate()
- try:
- random_bits = str(random.getrandbits(64))
- except AttributeError: # Python 2.3 doesn't have random.getrandbits().
- random_bits = ''.join([random.choice('1234567890') for i in range(19)])
- msg['Message-ID'] = "<%d.%s@%s>" % (time.time(), random_bits, DNS_NAME)
- try:
- server.sendmail(from_email, recipient_list, msg.as_string())
- num_sent += 1
- except:
- if not fail_silently:
- raise
- try:
- server.quit()
- except:
- if fail_silently:
- return
- raise
- return num_sent
+ connection = SMTPConnection(username=auth_user, password=auth_password,
+ fail_silently=fail_silently)
+ messages = [EmailMessage(subject, message, sender, recipient) for subject, message, sender, recipient in datatuple]
+ return connection.send_messages(messages)
def mail_admins(subject, message, fail_silently=False):
"Sends a message to the admins, as defined by the ADMINS setting."
- send_mail(settings.EMAIL_SUBJECT_PREFIX + subject, message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS], fail_silently)
+ EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
+ settings.SERVER_EMAIL, [a[1] for a in
+ settings.ADMINS]).send(fail_silently=fail_silently)
def mail_managers(subject, message, fail_silently=False):
"Sends a message to the managers, as defined by the MANAGERS setting."
- send_mail(settings.EMAIL_SUBJECT_PREFIX + subject, message, settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS], fail_silently)
+ EmailMessage(settings.EMAIL_SUBJECT_PREFIX + subject, message,
+ settings.SERVER_EMAIL, [a[1] for a in
+ settings.MANAGERS]).send(fail_silently=fail_silently)
+
diff --git a/django/core/management.py b/django/core/management.py
index d1a97c4a53..213eb4602c 100644
--- a/django/core/management.py
+++ b/django/core/management.py
@@ -3,14 +3,17 @@
import django
from django.core.exceptions import ImproperlyConfigured
-import os, re, shutil, sys, textwrap
from optparse import OptionParser
from django.utils import termcolors
+import os, re, shutil, sys, textwrap
# For Python 2.3
if not hasattr(__builtins__, 'set'):
from sets import Set as set
+# For backwards compatibility: get_version() used to be in this module.
+get_version = django.get_version
+
MODULE_TEMPLATE = ''' {%% if perms.%(app)s.%(addperm)s or perms.%(app)s.%(changeperm)s %%}
<tr>
<th>{%% if perms.%(app)s.%(changeperm)s %%}<a href="%(app)s/%(mod)s/">{%% endif %%}%(name)s{%% if perms.%(app)s.%(changeperm)s %%}</a>{%% endif %%}</th>
@@ -25,7 +28,7 @@ APP_ARGS = '[appname ...]'
# which has been installed.
PROJECT_TEMPLATE_DIR = os.path.join(django.__path__[0], 'conf', '%s_template')
-INVALID_PROJECT_NAMES = ('django', 'test')
+INVALID_PROJECT_NAMES = ('django', 'site', 'test')
# Set up the terminal color scheme.
class dummy: pass
@@ -68,20 +71,31 @@ def _get_table_list():
cursor = connection.cursor()
return get_introspection_module().get_table_list(cursor)
+def _get_sequence_list():
+ "Returns a list of information about all DB sequences for all models in all apps"
+ from django.db import models
+
+ apps = models.get_apps()
+ sequence_list = []
+
+ for app in apps:
+ for model in models.get_models(app):
+ for f in model._meta.fields:
+ if isinstance(f, models.AutoField):
+ sequence_list.append({'table':model._meta.db_table,'column':f.column,})
+ break # Only one AutoField is allowed per model, so don't bother continuing.
+
+ for f in model._meta.many_to_many:
+ sequence_list.append({'table':f.m2m_db_table(),'column':None,})
+
+ return sequence_list
+
# If the foreign key points to an AutoField, a PositiveIntegerField or a
# PositiveSmallIntegerField, the foreign key should be an IntegerField, not the
# referred field type. Otherwise, the foreign key should be the same type of
# field as the field to which it points.
get_rel_data_type = lambda f: (f.get_internal_type() in ('AutoField', 'PositiveIntegerField', 'PositiveSmallIntegerField')) and 'IntegerField' or f.get_internal_type()
-def get_version():
- "Returns the version as a human-format string."
- from django import VERSION
- v = '.'.join([str(i) for i in VERSION[:-1]])
- if VERSION[-1]:
- v += '-' + VERSION[-1]
- return v
-
def get_sql_create(app):
"Returns a list of the CREATE TABLE SQL statements for the given app."
from django.db import get_creation_module, models
@@ -149,6 +163,8 @@ def _get_sql_model_create(model, known_models=set()):
for f in opts.fields:
if isinstance(f, (models.ForeignKey, models.OneToOneField)):
rel_field = f.rel.get_related_field()
+ while isinstance(rel_field, (models.ForeignKey, models.OneToOneField)):
+ rel_field = rel_field.rel.get_related_field()
data_type = get_rel_data_type(rel_field)
else:
rel_field = f
@@ -167,7 +183,8 @@ def _get_sql_model_create(model, known_models=set()):
if f.rel.to in known_models:
field_output.append(style.SQL_KEYWORD('REFERENCES') + ' ' + \
style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)) + ' (' + \
- style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')'
+ style.SQL_FIELD(backend.quote_name(f.rel.to._meta.get_field(f.rel.field_name).column)) + ')' +
+ backend.get_deferrable_sql()
)
else:
# We haven't yet created the table to which this field
@@ -210,40 +227,43 @@ def _get_sql_for_pending_references(model, pending_references):
# For MySQL, r_name must be unique in the first 64 characters.
# So we are careful with character usage here.
r_name = '%s_refs_%s_%x' % (r_col, col, abs(hash((r_table, table))))
- final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s);' % \
+ final_output.append(style.SQL_KEYWORD('ALTER TABLE') + ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % \
(backend.quote_name(r_table), r_name,
- backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col)))
+ backend.quote_name(r_col), backend.quote_name(table), backend.quote_name(col),
+ backend.get_deferrable_sql()))
del pending_references[model]
return final_output
def _get_many_to_many_sql_for_model(model):
from django.db import backend, get_creation_module
- from django.db.models import GenericRel
+ from django.contrib.contenttypes import generic
data_types = get_creation_module().DATA_TYPES
opts = model._meta
final_output = []
for f in opts.many_to_many:
- if not isinstance(f.rel, GenericRel):
+ if not isinstance(f.rel, generic.GenericRel):
table_output = [style.SQL_KEYWORD('CREATE TABLE') + ' ' + \
style.SQL_TABLE(backend.quote_name(f.m2m_db_table())) + ' (']
table_output.append(' %s %s %s,' % \
(style.SQL_FIELD(backend.quote_name('id')),
style.SQL_COLTYPE(data_types['AutoField']),
style.SQL_KEYWORD('NOT NULL PRIMARY KEY')))
- table_output.append(' %s %s %s %s (%s),' % \
+ table_output.append(' %s %s %s %s (%s)%s,' % \
(style.SQL_FIELD(backend.quote_name(f.m2m_column_name())),
style.SQL_COLTYPE(data_types[get_rel_data_type(opts.pk)] % opts.pk.__dict__),
style.SQL_KEYWORD('NOT NULL REFERENCES'),
style.SQL_TABLE(backend.quote_name(opts.db_table)),
- style.SQL_FIELD(backend.quote_name(opts.pk.column))))
- table_output.append(' %s %s %s %s (%s),' % \
+ style.SQL_FIELD(backend.quote_name(opts.pk.column)),
+ backend.get_deferrable_sql()))
+ table_output.append(' %s %s %s %s (%s)%s,' % \
(style.SQL_FIELD(backend.quote_name(f.m2m_reverse_name())),
style.SQL_COLTYPE(data_types[get_rel_data_type(f.rel.to._meta.pk)] % f.rel.to._meta.pk.__dict__),
style.SQL_KEYWORD('NOT NULL REFERENCES'),
style.SQL_TABLE(backend.quote_name(f.rel.to._meta.db_table)),
- style.SQL_FIELD(backend.quote_name(f.rel.to._meta.pk.column))))
+ style.SQL_FIELD(backend.quote_name(f.rel.to._meta.pk.column)),
+ backend.get_deferrable_sql()))
table_output.append(' %s (%s, %s)' % \
(style.SQL_KEYWORD('UNIQUE'),
style.SQL_FIELD(backend.quote_name(f.m2m_column_name())),
@@ -257,7 +277,7 @@ def get_sql_delete(app):
from django.db import backend, connection, models, get_introspection_module
introspection = get_introspection_module()
- # This should work even if a connecton isn't available
+ # This should work even if a connection isn't available
try:
cursor = connection.cursor()
except:
@@ -291,7 +311,7 @@ def get_sql_delete(app):
# Drop the table now
output.append('%s %s;' % (style.SQL_KEYWORD('DROP TABLE'),
style.SQL_TABLE(backend.quote_name(model._meta.db_table))))
- if backend.supports_constraints and references_to_delete.has_key(model):
+ if backend.supports_constraints and model in references_to_delete:
for rel_class, f in references_to_delete[model]:
table = rel_class._meta.db_table
col = f.column
@@ -330,7 +350,15 @@ def get_sql_reset(app):
get_sql_reset.help_doc = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)."
get_sql_reset.args = APP_ARGS
-def get_sql_initial_data_for_model(model):
+def get_sql_flush():
+ "Returns a list of the SQL statements used to flush the database"
+ from django.db import backend
+ statements = backend.get_sql_flush(style, _get_table_list(), _get_sequence_list())
+ return statements
+get_sql_flush.help_doc = "Returns a list of the SQL statements required to return all tables in the database to the state they were in just after they were installed."
+get_sql_flush.args = ''
+
+def get_custom_sql_for_model(model):
from django.db import models
from django.conf import settings
@@ -357,8 +385,8 @@ def get_sql_initial_data_for_model(model):
return output
-def get_sql_initial_data(app):
- "Returns a list of the initial INSERT SQL statements for the given app."
+def get_custom_sql(app):
+ "Returns a list of the custom table modifying SQL statements for the given app."
from django.db.models import get_models
output = []
@@ -366,37 +394,23 @@ def get_sql_initial_data(app):
app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql'))
for model in app_models:
- output.extend(get_sql_initial_data_for_model(model))
+ output.extend(get_custom_sql_for_model(model))
return output
-get_sql_initial_data.help_doc = "Prints the initial INSERT SQL statements for the given app name(s)."
-get_sql_initial_data.args = APP_ARGS
+get_custom_sql.help_doc = "Prints the custom table modifying SQL statements for the given app name(s)."
+get_custom_sql.args = APP_ARGS
+
+def get_sql_initial_data(apps):
+ "Returns a list of the initial INSERT SQL statements for the given app."
+ return style.ERROR("This action has been renamed. Try './manage.py sqlcustom %s'." % ' '.join(apps and apps or ['app1', 'app2']))
+get_sql_initial_data.help_doc = "RENAMED: see 'sqlcustom'"
+get_sql_initial_data.args = ''
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 sequences for the given app."
from django.db import backend, models
- output = []
- for model in models.get_models(app):
- for f in model._meta.fields:
- if isinstance(f, models.AutoField):
- output.append("%s setval('%s', (%s max(%s) %s %s));" % \
- (style.SQL_KEYWORD('SELECT'),
- style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
- style.SQL_KEYWORD('SELECT'),
- style.SQL_FIELD(backend.quote_name(f.column)),
- style.SQL_KEYWORD('FROM'),
- style.SQL_TABLE(backend.quote_name(model._meta.db_table))))
- break # Only one AutoField is allowed per model, so don't bother continuing.
- for f in model._meta.many_to_many:
- output.append("%s setval('%s', (%s max(%s) %s %s));" % \
- (style.SQL_KEYWORD('SELECT'),
- style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
- style.SQL_KEYWORD('SELECT'),
- style.SQL_FIELD(backend.quote_name('id')),
- style.SQL_KEYWORD('FROM'),
- style.SQL_TABLE(f.m2m_db_table())))
- return output
-get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)."
+ return backend.get_sql_sequence_reset(style, models.get_models(app))
+get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting sequences for the given app name(s)."
get_sql_sequence_reset.args = APP_ARGS
def get_sql_indexes(app):
@@ -419,7 +433,7 @@ def get_sql_indexes_for_model(model):
unique = f.unique and 'UNIQUE ' or ''
output.append(
style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
- style.SQL_TABLE('%s_%s' % (model._meta.db_table, f.column)) + ' ' + \
+ style.SQL_TABLE(backend.quote_name('%s_%s' % (model._meta.db_table, f.column))) + ' ' + \
style.SQL_KEYWORD('ON') + ' ' + \
style.SQL_TABLE(backend.quote_name(model._meta.db_table)) + ' ' + \
"(%s);" % style.SQL_FIELD(backend.quote_name(f.column))
@@ -428,16 +442,26 @@ def get_sql_indexes_for_model(model):
def get_sql_all(app):
"Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
- return get_sql_create(app) + get_sql_initial_data(app) + get_sql_indexes(app)
+ return get_sql_create(app) + get_custom_sql(app) + get_sql_indexes(app)
get_sql_all.help_doc = "Prints the CREATE TABLE, initial-data and CREATE INDEX SQL statements for the given model module name(s)."
get_sql_all.args = APP_ARGS
+def _emit_post_sync_signal(created_models, verbosity, interactive):
+ from django.db import models
+ from django.dispatch import dispatcher
+ # Emit the post_sync signal for every application.
+ for app in models.get_apps():
+ app_name = app.__name__.split('.')[-2]
+ if verbosity >= 2:
+ print "Running post-sync handlers for application", app_name
+ dispatcher.send(signal=models.signals.post_syncdb, sender=app,
+ app=app, created_models=created_models,
+ verbosity=verbosity, interactive=interactive)
+
def syncdb(verbosity=1, interactive=True):
"Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
from django.db import connection, transaction, models, get_creation_module
- from django.db.models import signals
from django.conf import settings
- from django.dispatch import dispatcher
disable_termcolors()
@@ -465,6 +489,7 @@ def syncdb(verbosity=1, interactive=True):
created_models = set()
pending_references = {}
+ # Create the tables for each model
for app in models.get_apps():
app_name = app.__name__.split('.')[-2]
model_list = models.get_models(app)
@@ -486,6 +511,11 @@ def syncdb(verbosity=1, interactive=True):
cursor.execute(statement)
table_list.append(model._meta.db_table)
+ # Create the m2m tables. This must be done after all tables have been created
+ # to ensure that all referred tables will exist.
+ for app in models.get_apps():
+ app_name = app.__name__.split('.')[-2]
+ model_list = models.get_models(app)
for model in model_list:
if model in created_models:
sql = _get_many_to_many_sql_for_model(model)
@@ -495,31 +525,27 @@ def syncdb(verbosity=1, interactive=True):
for statement in sql:
cursor.execute(statement)
- transaction.commit_unless_managed()
+ transaction.commit_unless_managed()
# Send the post_syncdb signal, so individual apps can do whatever they need
# to do at this point.
+ _emit_post_sync_signal(created_models, verbosity, interactive)
+
+ # Install custom SQL for the app (but only if this
+ # is a model we've just created)
for app in models.get_apps():
app_name = app.__name__.split('.')[-2]
- if verbosity >= 2:
- print "Running post-sync handlers for application", app_name
- dispatcher.send(signal=signals.post_syncdb, sender=app,
- app=app, created_models=created_models,
- verbosity=verbosity, interactive=interactive)
-
- # Install initial data for the app (but only if this is a model we've
- # just created)
for model in models.get_models(app):
if model in created_models:
- initial_sql = get_sql_initial_data_for_model(model)
- if initial_sql:
+ custom_sql = get_custom_sql_for_model(model)
+ if custom_sql:
if verbosity >= 1:
- print "Installing initial data for %s.%s model" % (app_name, model._meta.object_name)
+ print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)
try:
- for sql in initial_sql:
+ for sql in custom_sql:
cursor.execute(sql)
except Exception, e:
- sys.stderr.write("Failed to install initial SQL data for %s.%s model: %s" % \
+ sys.stderr.write("Failed to install custom SQL for %s.%s model: %s" % \
(app_name, model._meta.object_name, e))
transaction.rollback_unless_managed()
else:
@@ -544,7 +570,10 @@ def syncdb(verbosity=1, interactive=True):
else:
transaction.commit_unless_managed()
-syncdb.args = ''
+ # Install the 'initialdata' fixture, using format discovery
+ load_data(['initial_data'], verbosity=verbosity)
+syncdb.help_doc = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created."
+syncdb.args = '[--verbosity] [--noinput]'
def get_admin_index(app):
"Returns admin-index template snippet (in list form) for the given app."
@@ -597,36 +626,6 @@ def diffsettings():
print '\n'.join(output)
diffsettings.args = ""
-def install(app):
- "Executes the equivalent of 'get_sql_all' in the current database."
- from django.db import connection, transaction
-
- app_name = app.__name__.split('.')[-2]
-
- disable_termcolors()
-
- # First, try validating the models.
- _check_for_validation_errors(app)
-
- sql_list = get_sql_all(app)
-
- try:
- cursor = connection.cursor()
- for sql in sql_list:
- cursor.execute(sql)
- except Exception, e:
- sys.stderr.write(style.ERROR("""Error: %s couldn't be installed. Possible reasons:
- * The database isn't running or isn't configured correctly.
- * At least one of the database tables already exists.
- * The SQL was invalid.
-Hint: Look at the output of 'django-admin.py sqlall %s'. That's the SQL this command wasn't able to run.
-The full error: """ % (app_name, app_name)) + style.ERROR_OUTPUT(str(e)) + '\n')
- transaction.rollback_unless_managed()
- sys.exit(1)
- transaction.commit_unless_managed()
-install.help_doc = "Executes ``sqlall`` for the given app(s) in the current database."
-install.args = APP_ARGS
-
def reset(app, interactive=True):
"Executes the equivalent of 'get_sql_reset' in the current database."
from django.db import connection, transaction
@@ -668,7 +667,68 @@ The full error: """ % (app_name, app_name)) + style.ERROR_OUTPUT(str(e)) + '\n')
else:
print "Reset cancelled."
reset.help_doc = "Executes ``sqlreset`` for the given app(s) in the current database."
-reset.args = APP_ARGS
+reset.args = '[--noinput]' + APP_ARGS
+
+def flush(verbosity=1, interactive=True):
+ "Returns all tables in the database to the same state they were in immediately after syncdb."
+ from django.conf import settings
+ from django.db import connection, transaction, models
+ from django.dispatch import dispatcher
+
+ disable_termcolors()
+
+ # First, try validating the models.
+ _check_for_validation_errors()
+
+ # Import the 'management' module within each installed app, to register
+ # dispatcher events.
+ for app_name in settings.INSTALLED_APPS:
+ try:
+ __import__(app_name + '.management', {}, {}, [''])
+ except ImportError:
+ pass
+
+ sql_list = get_sql_flush()
+
+ if interactive:
+ confirm = raw_input("""
+You have requested a flush of the database.
+This will IRREVERSIBLY DESTROY all data currently in the database,
+and return each table to the state it was in after syncdb.
+Are you sure you want to do this?
+
+Type 'yes' to continue, or 'no' to cancel: """)
+ else:
+ confirm = 'yes'
+
+ if confirm == 'yes':
+ try:
+ cursor = connection.cursor()
+ for sql in sql_list:
+ cursor.execute(sql)
+ except Exception, e:
+ sys.stderr.write(style.ERROR("""Error: Database %s couldn't be flushed. Possible reasons:
+ * The database isn't running or isn't configured correctly.
+ * At least one of the expected database tables doesn't exist.
+ * The SQL was invalid.
+Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
+The full error: """ % settings.DATABASE_NAME + style.ERROR_OUTPUT(str(e)) + '\n'))
+ transaction.rollback_unless_managed()
+ sys.exit(1)
+ transaction.commit_unless_managed()
+
+ # Emit the post sync signal. This allows individual
+ # applications to respond as if the database had been
+ # sync'd from scratch.
+ _emit_post_sync_signal(models.get_models(), verbosity, interactive)
+
+ # Reinstall the initial_data fixture
+ load_data(['initial_data'], verbosity=verbosity)
+
+ else:
+ print "Flush cancelled."
+flush.help_doc = "Executes ``sqlflush`` on the current database."
+flush.args = '[--verbosity] [--noinput]'
def _start_helper(app_or_project, name, directory, other_name=''):
other = {'project': 'app', 'app': 'project'}[app_or_project]
@@ -708,7 +768,7 @@ def startproject(project_name, directory):
"Creates a Django project for the given project_name in the given directory."
from random import choice
if project_name in INVALID_PROJECT_NAMES:
- sys.stderr.write(style.ERROR("Error: %r isn't a valid project name. Please try another.\n" % project_name))
+ sys.stderr.write(style.ERROR("Error: '%r' conflicts with the name of an existing Python module and cannot be used as a project name. Please try another name.\n" % project_name))
sys.exit(1)
_start_helper('project', project_name, directory)
# Create a random SECRET_KEY hash, and put it in the main settings.
@@ -727,11 +787,12 @@ def startapp(app_name, directory):
# Determine the project_name a bit naively -- by looking at the name of
# the parent directory.
project_dir = os.path.normpath(os.path.join(directory, '..'))
- project_name = os.path.basename(project_dir)
- if app_name == os.path.basename(directory):
+ parent_dir = os.path.basename(project_dir)
+ project_name = os.path.basename(directory)
+ if app_name == project_name:
sys.stderr.write(style.ERROR("Error: You cannot create an app with the same name (%r) as your project.\n" % app_name))
sys.exit(1)
- _start_helper('app', app_name, directory, project_name)
+ _start_helper('app', app_name, directory, parent_dir)
startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
startapp.args = "[appname]"
@@ -751,7 +812,7 @@ def inspectdb():
yield "# * Make sure each model has one field with primary_key=True"
yield "# Feel free to rename the models, but don't rename db_table values or field names."
yield "#"
- yield "# Also note: You'll have to insert the output of 'django-admin.py sqlinitialdata [appname]'"
+ yield "# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'"
yield "# into your database."
yield ''
yield 'from django.db import models'
@@ -780,7 +841,7 @@ def inspectdb():
att_name += '_field'
comment_notes.append('Field renamed because it was a Python reserved word.')
- if relations.has_key(i):
+ if i in relations:
rel_to = relations[i][1] == table_name and "'self'" or table2model(relations[i][1])
field_type = 'ForeignKey(%s' % rel_to
if att_name.endswith('_id'):
@@ -804,7 +865,7 @@ def inspectdb():
if field_type == 'CharField' and row[3]:
extra_params['maxlength'] = row[3]
- if field_type == 'FloatField':
+ if field_type == 'DecimalField':
extra_params['max_digits'] = row[4]
extra_params['decimal_places'] = row[5]
@@ -879,11 +940,11 @@ def get_validation_errors(outfile, app=None):
e.add(opts, '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.' % f.name)
if isinstance(f, models.CharField) and f.maxlength in (None, 0):
e.add(opts, '"%s": CharFields require a "maxlength" attribute.' % f.name)
- if isinstance(f, models.FloatField):
+ if isinstance(f, models.DecimalField):
if f.decimal_places is None:
- e.add(opts, '"%s": FloatFields require a "decimal_places" attribute.' % f.name)
+ e.add(opts, '"%s": DecimalFields require a "decimal_places" attribute.' % f.name)
if f.max_digits is None:
- e.add(opts, '"%s": FloatFields require a "max_digits" attribute.' % f.name)
+ e.add(opts, '"%s": DecimalFields require a "max_digits" attribute.' % f.name)
if isinstance(f, models.FileField) and not f.upload_to:
e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name)
if isinstance(f, models.ImageField):
@@ -1072,7 +1133,7 @@ def validate(outfile=sys.stdout, silent_success=False):
return
outfile.write('%s error%s found.\n' % (num_errors, num_errors != 1 and 's' or ''))
except ImproperlyConfigured:
- outfile.write("Skipping validation because things aren't configured properly.")
+ outfile.write("Skipping validation because things aren't configured properly.\n")
validate.args = ''
def _check_for_validation_errors(app=None):
@@ -1174,6 +1235,11 @@ createcachetable.args = "[tablename]"
def run_shell(use_plain=False):
"Runs a Python interactive interpreter. Tries to use IPython, if it's available."
+ # XXX: (Temporary) workaround for ticket #1796: force early loading of all
+ # models from installed apps.
+ from django.db.models.loading import get_models
+ loaded_models = get_models()
+
try:
if use_plain:
# Don't bother loading IPython, because the user wants plain Python.
@@ -1235,10 +1301,154 @@ def test(app_labels, verbosity=1):
test_module = __import__(test_module_name, {}, {}, test_path[-1])
test_runner = getattr(test_module, test_path[-1])
- test_runner(app_list, verbosity)
+ failures = test_runner(app_list, verbosity)
+ if failures:
+ sys.exit(failures)
+
test.help_doc = 'Runs the test suite for the specified applications, or the entire site if no apps are specified'
test.args = '[--verbosity] ' + APP_ARGS
+def load_data(fixture_labels, verbosity=1):
+ "Installs the provided fixture file(s) as data in the database."
+ from django.db.models import get_apps
+ from django.core import serializers
+ from django.db import connection, transaction, backend
+ from django.conf import settings
+ import sys
+
+ disable_termcolors()
+
+ # Keep a count of the installed objects and fixtures
+ count = [0,0]
+ models = set()
+
+ humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
+
+ # Get a cursor (even though we don't need one yet). This has
+ # the side effect of initializing the test database (if
+ # it isn't already initialized).
+ cursor = connection.cursor()
+
+ # Start transaction management. All fixtures are installed in a
+ # single transaction to ensure that all references are resolved.
+ transaction.commit_unless_managed()
+ transaction.enter_transaction_management()
+ transaction.managed(True)
+
+ app_fixtures = [os.path.join(os.path.dirname(app.__file__),'fixtures') for app in get_apps()]
+ for fixture_label in fixture_labels:
+ parts = fixture_label.split('.')
+ if len(parts) == 1:
+ fixture_name = fixture_label
+ formats = serializers.get_serializer_formats()
+ else:
+ fixture_name, format = '.'.join(parts[:-1]), parts[-1]
+ if format in serializers.get_serializer_formats():
+ formats = [format]
+ else:
+ formats = []
+
+ if verbosity > 0:
+ if formats:
+ print "Loading '%s' fixtures..." % fixture_name
+ else:
+ print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format)
+
+ for fixture_dir in app_fixtures + list(settings.FIXTURE_DIRS) + ['']:
+ if verbosity > 1:
+ print "Checking %s for fixtures..." % humanize(fixture_dir)
+
+ label_found = False
+ for format in formats:
+ serializer = serializers.get_serializer(format)
+ if verbosity > 1:
+ print "Trying %s for %s fixture '%s'..." % \
+ (humanize(fixture_dir), format, fixture_name)
+ try:
+ full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format]))
+ fixture = open(full_path, 'r')
+ if label_found:
+ fixture.close()
+ print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
+ (fixture_name, humanize(fixture_dir)))
+ transaction.rollback()
+ transaction.leave_transaction_management()
+ return
+ else:
+ count[1] += 1
+ if verbosity > 0:
+ print "Installing %s fixture '%s' from %s." % \
+ (format, fixture_name, humanize(fixture_dir))
+ try:
+ objects = serializers.deserialize(format, fixture)
+ for obj in objects:
+ count[0] += 1
+ models.add(obj.object.__class__)
+ obj.save()
+ label_found = True
+ except Exception, e:
+ fixture.close()
+ sys.stderr.write(
+ style.ERROR("Problem installing fixture '%s': %s\n" %
+ (full_path, str(e))))
+ transaction.rollback()
+ transaction.leave_transaction_management()
+ return
+ fixture.close()
+ except:
+ if verbosity > 1:
+ print "No %s fixture '%s' in %s." % \
+ (format, fixture_name, humanize(fixture_dir))
+
+ if count[0] > 0:
+ sequence_sql = backend.get_sql_sequence_reset(style, models)
+ if sequence_sql:
+ if verbosity > 1:
+ print "Resetting sequences"
+ for line in sequence_sql:
+ cursor.execute(line)
+
+ transaction.commit()
+ transaction.leave_transaction_management()
+
+ if count[0] == 0:
+ if verbosity > 0:
+ print "No fixtures found."
+ else:
+ if verbosity > 0:
+ print "Installed %d object(s) from %d fixture(s)" % tuple(count)
+
+load_data.help_doc = 'Installs the named fixture(s) in the database'
+load_data.args = "[--verbosity] fixture, fixture, ..."
+
+def dump_data(app_labels, format='json', indent=None):
+ "Output the current contents of the database as a fixture of the given format"
+ from django.db.models import get_app, get_apps, get_models
+ from django.core import serializers
+
+ if len(app_labels) == 0:
+ app_list = get_apps()
+ else:
+ app_list = [get_app(app_label) for app_label in app_labels]
+
+ # Check that the serialization format exists; this is a shortcut to
+ # avoid collating all the objects and _then_ failing.
+ try:
+ serializers.get_serializer(format)
+ except KeyError:
+ sys.stderr.write(style.ERROR("Unknown serialization format: %s\n" % format))
+
+ objects = []
+ for app in app_list:
+ for model in get_models(app):
+ objects.extend(model.objects.all())
+ try:
+ return serializers.serialize(format, objects, indent=indent)
+ except Exception, e:
+ sys.stderr.write(style.ERROR("Unable to serialize database: %s\n" % e))
+dump_data.help_doc = 'Output the contents of the database as a fixture of the given format'
+dump_data.args = '[--format] [--indent]' + APP_ARGS
+
# Utilities for command-line script
DEFAULT_ACTION_MAPPING = {
@@ -1246,8 +1456,10 @@ DEFAULT_ACTION_MAPPING = {
'createcachetable' : createcachetable,
'dbshell': dbshell,
'diffsettings': diffsettings,
+ 'dumpdata': dump_data,
+ 'flush': flush,
'inspectdb': inspectdb,
- 'install': install,
+ 'loaddata': load_data,
'reset': reset,
'runfcgi': runfcgi,
'runserver': runserver,
@@ -1255,6 +1467,8 @@ DEFAULT_ACTION_MAPPING = {
'sql': get_sql_create,
'sqlall': get_sql_all,
'sqlclear': get_sql_delete,
+ 'sqlcustom': get_custom_sql,
+ 'sqlflush': get_sql_flush,
'sqlindexes': get_sql_indexes,
'sqlinitialdata': get_sql_initial_data,
'sqlreset': get_sql_reset,
@@ -1271,7 +1485,6 @@ NO_SQL_TRANSACTION = (
'createcachetable',
'dbshell',
'diffsettings',
- 'install',
'reset',
'sqlindexes',
'syncdb',
@@ -1318,6 +1531,10 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
help='Tells Django to NOT prompt the user for input of any kind.')
parser.add_option('--noreload', action='store_false', dest='use_reloader', default=True,
help='Tells Django to NOT use the auto-reloader when running the development server.')
+ parser.add_option('--format', default='json', dest='format',
+ help='Specifies the output serialization format for fixtures')
+ parser.add_option('--indent', default=None, dest='indent',
+ type='int', help='Specifies the indent level to use when pretty-printing output')
parser.add_option('--verbosity', action='store', dest='verbosity', default='1',
type='choice', choices=['0', '1', '2'],
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
@@ -1337,7 +1554,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
action = args[0]
except IndexError:
parser.print_usage_and_exit()
- if not action_mapping.has_key(action):
+ if action not in action_mapping:
print_error("Your action, %r, was invalid." % action, argv[0])
# Switch to English, because django-admin.py creates database content
@@ -1351,7 +1568,7 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
action_mapping[action](options.plain is True)
elif action in ('validate', 'diffsettings', 'dbshell'):
action_mapping[action]()
- elif action == 'syncdb':
+ elif action in ('flush', 'syncdb'):
action_mapping[action](int(options.verbosity), options.interactive)
elif action == 'inspectdb':
try:
@@ -1365,11 +1582,16 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
action_mapping[action](args[1])
except IndexError:
parser.print_usage_and_exit()
- elif action == 'test':
+ elif action in ('test', 'loaddata'):
try:
action_mapping[action](args[1:], int(options.verbosity))
except IndexError:
parser.print_usage_and_exit()
+ elif action == 'dumpdata':
+ try:
+ print action_mapping[action](args[1:], options.format, options.indent)
+ except IndexError:
+ parser.print_usage_and_exit()
elif action in ('startapp', 'startproject'):
try:
name = args[1]
@@ -1388,6 +1610,10 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
action_mapping[action](addr, port, options.use_reloader, options.admin_media_path)
elif action == 'runfcgi':
action_mapping[action](args[1:])
+ elif action == 'sqlinitialdata':
+ print action_mapping[action](args[1:])
+ elif action == 'sqlflush':
+ print '\n'.join(action_mapping[action]())
else:
from django.db import models
validate(silent_success=True)
diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
index a1268321f2..494393f3cf 100644
--- a/django/core/serializers/__init__.py
+++ b/django/core/serializers/__init__.py
@@ -25,6 +25,13 @@ BUILTIN_SERIALIZERS = {
"json" : "django.core.serializers.json",
}
+# Check for PyYaml and register the serializer if it's available.
+try:
+ import yaml
+ BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
+except ImportError:
+ pass
+
_serializers = {}
def register_serializer(format, serializer_module):
@@ -40,6 +47,11 @@ def get_serializer(format):
if not _serializers:
_load_serializers()
return _serializers[format].Serializer
+
+def get_serializer_formats():
+ if not _serializers:
+ _load_serializers()
+ return _serializers.keys()
def get_deserializer(format):
if not _serializers:
diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py
index 5b0acdc480..86d0037c17 100644
--- a/django/core/serializers/base.py
+++ b/django/core/serializers/base.py
@@ -34,17 +34,17 @@ class Serializer(object):
for obj in queryset:
self.start_object(obj)
for field in obj._meta.fields:
- if field is obj._meta.pk:
- continue
- elif field.rel is None:
- if self.selected_fields is None or field.attname in self.selected_fields:
- self.handle_field(obj, field)
- else:
- if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
- self.handle_fk_field(obj, field)
+ if field.serialize:
+ if field.rel is None:
+ if self.selected_fields is None or field.attname in self.selected_fields:
+ self.handle_field(obj, field)
+ else:
+ if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
+ self.handle_fk_field(obj, field)
for field in obj._meta.many_to_many:
- if self.selected_fields is None or field.attname in self.selected_fields:
- self.handle_m2m_field(obj, field)
+ if field.serialize:
+ if self.selected_fields is None or field.attname in self.selected_fields:
+ self.handle_m2m_field(obj, field)
self.end_object(obj)
self.end_serialization()
return self.getvalue()
@@ -54,11 +54,7 @@ class Serializer(object):
Convert a field's value to a string.
"""
if isinstance(field, models.DateTimeField):
- value = getattr(obj, field.name)
- if value is None:
- value = ''
- else:
- value = value.strftime("%Y-%m-%d %H:%M:%S")
+ value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
elif isinstance(field, models.FileField):
value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
else:
@@ -109,9 +105,11 @@ class Serializer(object):
def getvalue(self):
"""
- Return the fully serialized queryset.
+ Return the fully serialized queryset (or None if the output stream is
+ not seekable).
"""
- return self.stream.getvalue()
+ if callable(getattr(self.stream, 'getvalue', None)):
+ return self.stream.getvalue()
class Deserializer(object):
"""
@@ -141,7 +139,7 @@ class Deserializer(object):
class DeserializedObject(object):
"""
- A deserialzed model.
+ A deserialized model.
Basically a container for holding the pre-saved deserialized data along
with the many-to-many data saved with the object.
diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py
index 15770f160e..fa2dca7295 100644
--- a/django/core/serializers/json.py
+++ b/django/core/serializers/json.py
@@ -4,22 +4,30 @@ Serialize data to/from JSON
import datetime
from django.utils import simplejson
+from django.utils.simplejson import decoder
from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
+try:
+ import decimal
+except ImportError:
+ from django.utils import _decimal as decimal # Python 2.3 fallback
class Serializer(PythonSerializer):
"""
Convert a queryset to JSON.
"""
def end_serialization(self):
- simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, **self.options)
-
+ self.options.pop('stream', None)
+ self.options.pop('fields', None)
+ simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
+
def getvalue(self):
- return self.stream.getvalue()
+ if callable(getattr(self.stream, 'getvalue', None)):
+ return self.stream.getvalue()
def Deserializer(stream_or_string, **options):
"""
@@ -31,15 +39,15 @@ def Deserializer(stream_or_string, **options):
stream = stream_or_string
for obj in PythonDeserializer(simplejson.load(stream)):
yield obj
-
-class DateTimeAwareJSONEncoder(simplejson.JSONEncoder):
+
+class DjangoJSONEncoder(simplejson.JSONEncoder):
"""
- JSONEncoder subclass that knows how to encode date/time types
+ JSONEncoder subclass that knows how to encode date/time and decimal types.
"""
-
- DATE_FORMAT = "%Y-%m-%d"
+
+ DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
-
+
def default(self, o):
if isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))
@@ -47,5 +55,11 @@ class DateTimeAwareJSONEncoder(simplejson.JSONEncoder):
return o.strftime(self.DATE_FORMAT)
elif isinstance(o, datetime.time):
return o.strftime(self.TIME_FORMAT)
+ elif isinstance(o, decimal.Decimal):
+ return str(o)
else:
- return super(DateTimeAwareJSONEncoder, self).default(o)
+ return super(DjangoJSONEncoder, self).default(o)
+
+# Older, deprecated class name (for backwards compatibility purposes).
+DateTimeAwareJSONEncoder = DjangoJSONEncoder
+
diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py
index 859816c226..5fbb3163f7 100644
--- a/django/core/serializers/python.py
+++ b/django/core/serializers/python.py
@@ -37,7 +37,12 @@ class Serializer(base.Serializer):
def handle_fk_field(self, obj, field):
related = getattr(obj, field.name)
if related is not None:
- related = related._get_pk_val()
+ if field.rel.field_name == related._meta.pk.name:
+ # Related to remote object via primary key
+ related = related._get_pk_val()
+ else:
+ # Related to remote object via other field
+ related = getattr(related, field.rel.field_name)
self._current[field.name] = related
def handle_m2m_field(self, obj, field):
@@ -57,7 +62,7 @@ def Deserializer(object_list, **options):
for d in object_list:
# Look up the model and starting build a dict of data for it.
Model = _get_model(d["model"])
- data = {Model._meta.pk.name : d["pk"]}
+ data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
m2m_data = {}
# Handle each field
@@ -67,20 +72,23 @@ def Deserializer(object_list, **options):
field = Model._meta.get_field(field_name)
- # Handle M2M relations (with in_bulk() for performance)
+ # Handle M2M relations
if field.rel and isinstance(field.rel, models.ManyToManyRel):
pks = []
+ m2m_convert = field.rel.to._meta.pk.to_python
for pk in field_value:
if isinstance(pk, unicode):
- pk = pk.encode(options.get("encoding", settings.DEFAULT_CHARSET))
- m2m_data[field.name] = field.rel.to._default_manager.in_bulk(field_value).values()
+ pks.append(m2m_convert(pk.encode(options.get("encoding", settings.DEFAULT_CHARSET))))
+ else:
+ pks.append(m2m_convert(pk))
+ m2m_data[field.name] = pks
# Handle FK fields
- elif field.rel and isinstance(field.rel, models.ManyToOneRel) and field_value is not None:
- try:
- data[field.name] = field.rel.to._default_manager.get(pk=field_value)
- except field.rel.to.DoesNotExist:
- data[field.name] = None
+ elif field.rel and isinstance(field.rel, models.ManyToOneRel):
+ if field_value:
+ data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
+ else:
+ data[field.attname] = None
# Handle all other fields
else:
diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py
new file mode 100644
index 0000000000..d3444280c5
--- /dev/null
+++ b/django/core/serializers/pyyaml.py
@@ -0,0 +1,38 @@
+"""
+YAML serializer.
+
+Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
+"""
+
+import datetime
+from django.core.serializers.python import Serializer as PythonSerializer
+from django.core.serializers.python import Deserializer as PythonDeserializer
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
+import yaml
+
+class Serializer(PythonSerializer):
+ """
+ Convert a queryset to YAML.
+ """
+ def end_serialization(self):
+ self.options.pop('stream', None)
+ self.options.pop('fields', None)
+ yaml.dump(self.objects, self.stream, **self.options)
+
+ def getvalue(self):
+ return self.stream.getvalue()
+
+def Deserializer(stream_or_string, **options):
+ """
+ Deserialize a stream or string of YAML data.
+ """
+ if isinstance(stream_or_string, basestring):
+ stream = StringIO(stream_or_string)
+ else:
+ stream = stream_or_string
+ for obj in PythonDeserializer(yaml.load(stream)):
+ yield obj
+
diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py
index 512b8c6176..3e4a6f3e79 100644
--- a/django/core/serializers/xml_serializer.py
+++ b/django/core/serializers/xml_serializer.py
@@ -13,6 +13,10 @@ class Serializer(base.Serializer):
Serializes a QuerySet to XML.
"""
+ def indent(self, level):
+ if self.options.get('indent', None) is not None:
+ self.xml.ignorableWhitespace('\n' + ' ' * self.options.get('indent', None) * level)
+
def start_serialization(self):
"""
Start serialization -- open the XML document and the root element.
@@ -25,6 +29,7 @@ class Serializer(base.Serializer):
"""
End serialization -- end the document.
"""
+ self.indent(0)
self.xml.endElement("django-objects")
self.xml.endDocument()
@@ -35,6 +40,7 @@ class Serializer(base.Serializer):
if not hasattr(obj, "_meta"):
raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj))
+ self.indent(1)
self.xml.startElement("object", {
"pk" : str(obj._get_pk_val()),
"model" : str(obj._meta),
@@ -44,6 +50,7 @@ class Serializer(base.Serializer):
"""
Called after handling all fields for an object.
"""
+ self.indent(1)
self.xml.endElement("object")
def handle_field(self, obj, field):
@@ -51,16 +58,19 @@ class Serializer(base.Serializer):
Called to handle each field on an object (except for ForeignKeys and
ManyToManyFields)
"""
+ self.indent(2)
self.xml.startElement("field", {
"name" : field.name,
"type" : field.get_internal_type()
})
# Get a "string version" of the object's data (this is handled by the
- # serializer base class). None is handled specially.
- value = self.get_string_value(obj, field)
- if value is not None:
+ # serializer base class).
+ if getattr(obj, field.name) is not None:
+ value = self.get_string_value(obj, field)
self.xml.characters(str(value))
+ else:
+ self.xml.addQuickElement("None")
self.xml.endElement("field")
@@ -72,7 +82,13 @@ class Serializer(base.Serializer):
self._start_relational_field(field)
related = getattr(obj, field.name)
if related is not None:
- self.xml.characters(str(related._get_pk_val()))
+ if field.rel.field_name == related._meta.pk.name:
+ # Related to remote object via primary key
+ related = related._get_pk_val()
+ else:
+ # Related to remote object via other field
+ related = getattr(related, field.rel.field_name)
+ self.xml.characters(str(related))
else:
self.xml.addQuickElement("None")
self.xml.endElement("field")
@@ -92,6 +108,7 @@ class Serializer(base.Serializer):
"""
Helper to output the <field> element for relational fields
"""
+ self.indent(2)
self.xml.startElement("field", {
"name" : field.name,
"rel" : field.rel.__class__.__name__,
@@ -127,7 +144,8 @@ class Deserializer(base.Deserializer):
pk = node.getAttribute("pk")
if not pk:
raise base.DeserializationError("<object> node is missing the 'pk' attribute")
- data = {Model._meta.pk.name : pk}
+
+ data = {Model._meta.pk.attname : Model._meta.pk.to_python(pk)}
# Also start building a dict of m2m data (this is saved as
# {m2m_accessor_attribute : [list_of_related_objects]})
@@ -148,41 +166,37 @@ class Deserializer(base.Deserializer):
# As is usually the case, relation fields get the special treatment.
if field.rel and isinstance(field.rel, models.ManyToManyRel):
- m2m_data[field.name] = self._handle_m2m_field_node(field_node)
+ m2m_data[field.name] = self._handle_m2m_field_node(field_node, field)
elif field.rel and isinstance(field.rel, models.ManyToOneRel):
- data[field.name] = self._handle_fk_field_node(field_node)
+ data[field.attname] = self._handle_fk_field_node(field_node, field)
else:
- value = field.to_python(getInnerText(field_node).strip().encode(self.encoding))
+ if len(field_node.childNodes) == 1 and field_node.childNodes[0].nodeName == 'None':
+ value = None
+ else:
+ value = field.to_python(getInnerText(field_node).strip().encode(self.encoding))
data[field.name] = value
# Return a DeserializedObject so that the m2m data has a place to live.
return base.DeserializedObject(Model(**data), m2m_data)
- def _handle_fk_field_node(self, node):
+ def _handle_fk_field_node(self, node, field):
"""
Handle a <field> node for a ForeignKey
"""
- # Try to set the foreign key by looking up the foreign related object.
- # If it doesn't exist, set the field to None (which might trigger
- # validation error, but that's expected).
- RelatedModel = self._get_model_from_node(node, "to")
# Check if there is a child node named 'None', returning None if so.
if len(node.childNodes) == 1 and node.childNodes[0].nodeName == 'None':
return None
else:
- return RelatedModel.objects.get(pk=getInnerText(node).strip().encode(self.encoding))
+ return field.rel.to._meta.get_field(field.rel.field_name).to_python(
+ getInnerText(node).strip().encode(self.encoding))
- def _handle_m2m_field_node(self, node):
+ def _handle_m2m_field_node(self, node, field):
"""
Handle a <field> node for a ManyToManyField
"""
- # Load the related model
- RelatedModel = self._get_model_from_node(node, "to")
-
- # Look up all the related objects. Using the in_bulk() lookup ensures
- # that missing related objects don't cause an exception
- related_ids = [c.getAttribute("pk").encode(self.encoding) for c in node.getElementsByTagName("object")]
- return RelatedModel._default_manager.in_bulk(related_ids).values()
+ return [field.rel.to._meta.pk.to_python(
+ c.getAttribute("pk").encode(self.encoding))
+ for c in node.getElementsByTagName("object")]
def _get_model_from_node(self, node, attr):
"""
diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
index a16b8b675a..9e603b42d4 100644
--- a/django/core/servers/basehttp.py
+++ b/django/core/servers/basehttp.py
@@ -208,15 +208,15 @@ def guess_scheme(environ):
else:
return 'http'
-_hoppish = {
+_hop_headers = {
'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
'upgrade':1
-}.has_key
+}
def is_hop_by_hop(header_name):
"""Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
- return _hoppish(header_name.lower())
+ return header_name.lower() in _hop_headers
class ServerHandler(object):
"""Manage the invocation of a WSGI application"""
@@ -309,7 +309,7 @@ class ServerHandler(object):
"""
if not self.result_is_file() and not self.sendfile():
for data in self.result:
- self.write(data)
+ self.write(data, False)
self.finish_content()
self.close()
@@ -334,7 +334,7 @@ class ServerHandler(object):
Subclasses can extend this to add other defaults.
"""
- if not self.headers.has_key('Content-Length'):
+ if 'Content-Length' not in self.headers:
self.set_content_length()
def start_response(self, status, headers,exc_info=None):
@@ -368,16 +368,16 @@ class ServerHandler(object):
if self.origin_server:
if self.client_is_modern():
self._write('HTTP/%s %s\r\n' % (self.http_version,self.status))
- if not self.headers.has_key('Date'):
+ if 'Date' not in self.headers:
self._write(
'Date: %s\r\n' % time.asctime(time.gmtime(time.time()))
)
- if self.server_software and not self.headers.has_key('Server'):
+ if self.server_software and 'Server' not in self.headers:
self._write('Server: %s\r\n' % self.server_software)
else:
self._write('Status: %s\r\n' % self.status)
- def write(self, data):
+ def write(self, data, flush=True):
"""'write()' callable as specified by PEP 333"""
assert type(data) is StringType,"write() argument must be string"
@@ -394,7 +394,8 @@ class ServerHandler(object):
# XXX check Content-Length and truncate if too many bytes written?
self._write(data)
- self._flush()
+ if flush:
+ self._flush()
def sendfile(self):
"""Platform-specific file transmission
@@ -421,8 +422,6 @@ class ServerHandler(object):
if not self.headers_sent:
self.headers['Content-Length'] = "0"
self.send_headers()
- else:
- pass # XXX check if content-length was too short?
def close(self):
try:
diff --git a/django/core/servers/fastcgi.py b/django/core/servers/fastcgi.py
index 649dd6942d..619758a0b1 100644
--- a/django/core/servers/fastcgi.py
+++ b/django/core/servers/fastcgi.py
@@ -1,5 +1,5 @@
"""
-FastCGI server that implements the WSGI protocol.
+FastCGI (or SCGI, or AJP1.3 ...) server that implements the WSGI protocol.
Uses the flup python package: http://www.saddi.com/software/flup/
@@ -18,15 +18,16 @@ __version__ = "0.1"
__all__ = ["runfastcgi"]
FASTCGI_HELP = r"""runfcgi:
- Run this project as a fastcgi application. To do this, the
- flup package from http://www.saddi.com/software/flup/ is
- required.
+ Run this project as a fastcgi (or some other protocol supported
+ by flup) application. To do this, the flup package from
+ http://www.saddi.com/software/flup/ is required.
Usage:
django-admin.py runfcgi --settings=yourproject.settings [fcgi settings]
manage.py runfcgi [fcgi settings]
Optional Fcgi settings: (setting=value)
+ protocol=PROTOCOL fcgi, scgi, ajp, ... (default fcgi)
host=HOSTNAME hostname to listen on..
port=PORTNUM port to listen on.
socket=FILE UNIX socket to listen on.
@@ -45,8 +46,8 @@ Examples:
(for webservers which spawn your processes for you)
$ manage.py runfcgi method=threaded
- Run a fastcgi server on a TCP host/port
- $ manage.py runfcgi method=prefork host=127.0.0.1 port=8025
+ Run a scgi server on a TCP host/port
+ $ manage.py runfcgi protocol=scgi method=prefork host=127.0.0.1 port=8025
Run a fastcgi server on a UNIX domain socket (posix platforms only)
$ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock
@@ -58,6 +59,7 @@ Examples:
"""
FASTCGI_OPTIONS = {
+ 'protocol': 'fcgi',
'host': None,
'port': None,
'socket': None,
@@ -100,16 +102,17 @@ def runfastcgi(argset=[], **kwargs):
print >> sys.stderr, " installed flup, then make sure you have it in your PYTHONPATH."
return False
+ flup_module = 'server.' + options['protocol']
+
if options['method'] in ('prefork', 'fork'):
- from flup.server.fcgi_fork import WSGIServer
wsgi_opts = {
'maxSpare': int(options["maxspare"]),
'minSpare': int(options["minspare"]),
'maxChildren': int(options["maxchildren"]),
'maxRequests': int(options["maxrequests"]),
}
+ flup_module += '_fork'
elif options['method'] in ('thread', 'threaded'):
- from flup.server.fcgi import WSGIServer
wsgi_opts = {
'maxSpare': int(options["maxspare"]),
'minSpare': int(options["minspare"]),
@@ -120,6 +123,12 @@ def runfastcgi(argset=[], **kwargs):
wsgi_opts['debug'] = False # Turn off flup tracebacks
+ try:
+ WSGIServer = getattr(__import__('flup.' + flup_module, '', '', flup_module), 'WSGIServer')
+ except:
+ print "Can't import flup." + flup_module
+ return False
+
# Prep up and go
from django.core.handlers.wsgi import WSGIHandler
diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
index 93c9c30cca..38b3263da1 100644
--- a/django/core/urlresolvers.py
+++ b/django/core/urlresolvers.py
@@ -16,7 +16,7 @@ class Resolver404(Http404):
class NoReverseMatch(Exception):
# Don't make this raise an error when used in a template.
- silent_variable_failure = True
+ silent_variable_failure = True
def get_mod_func(callback):
# Converts 'django.views.news.stories.story_detail' to
@@ -88,7 +88,7 @@ class MatchChecker(object):
return str(value) # TODO: Unicode?
class RegexURLPattern(object):
- def __init__(self, regex, callback, default_args=None):
+ def __init__(self, regex, callback, default_args=None, name=None):
# regex is a string representing a regular expression.
# callback is either a string like 'foo.views.news.stories.story_detail'
# which represents the path to a module and a view function name, or a
@@ -100,6 +100,15 @@ class RegexURLPattern(object):
self._callback = None
self._callback_str = callback
self.default_args = default_args or {}
+ self.name = name
+
+ def add_prefix(self, prefix):
+ """
+ Adds the prefix string to a string-based callback.
+ """
+ if not prefix or not hasattr(self, '_callback_str'):
+ return
+ self._callback_str = prefix + '.' + self._callback_str
def resolve(self, path):
match = self.regex.search(path)
@@ -110,7 +119,7 @@ class RegexURLPattern(object):
kwargs = match.groupdict()
if kwargs:
args = ()
- if not kwargs:
+ else:
args = match.groups()
# In both cases, pass any extra_kwargs as **kwargs.
kwargs.update(self.default_args)
@@ -205,14 +214,15 @@ class RegexURLResolver(object):
try:
lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
except (ImportError, AttributeError):
- raise NoReverseMatch
+ if func_name != '':
+ raise NoReverseMatch
for pattern in self.urlconf_module.urlpatterns:
if isinstance(pattern, RegexURLResolver):
try:
return pattern.reverse_helper(lookup_view, *args, **kwargs)
except NoReverseMatch:
continue
- elif pattern.callback == lookup_view:
+ elif pattern.callback == lookup_view or pattern.name == lookup_view:
try:
return pattern.reverse_helper(*args, **kwargs)
except NoReverseMatch:
diff --git a/django/core/validators.py b/django/core/validators.py
index a2f3dc3bc3..293f0e1a8c 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -25,6 +25,7 @@ email_re = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
+decimal_re = re.compile(r'^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')
integer_re = re.compile(r'^-?\d+$')
ip4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE)
@@ -140,7 +141,8 @@ def _isValidDate(date_string):
try:
date(year, month, day)
except ValueError, e:
- raise ValidationError, gettext('Invalid date: %s.' % e)
+ msg = gettext('Invalid date: %s') % gettext(str(e))
+ raise ValidationError, msg
def isValidANSIDate(field_data, all_data):
if not ansi_date_re.search(field_data):
@@ -283,7 +285,7 @@ class ValidateIfOtherFieldEquals(object):
self.always_test = True
def __call__(self, field_data, all_data):
- if all_data.has_key(self.other_field) and all_data[self.other_field] == self.other_value:
+ if self.other_field in all_data and all_data[self.other_field] == self.other_value:
for v in self.validator_list:
v(field_data, all_data)
@@ -312,27 +314,29 @@ class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
class RequiredIfOtherFieldEquals(object):
- def __init__(self, other_field, other_value, error_message=None):
+ def __init__(self, other_field, other_value, error_message=None, other_label=None):
self.other_field = other_field
self.other_value = other_value
+ other_label = other_label or other_value
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is %(value)s"), {
- 'field': other_field, 'value': other_value})
+ 'field': other_field, 'value': other_label})
self.always_test = True
def __call__(self, field_data, all_data):
- if all_data.has_key(self.other_field) and all_data[self.other_field] == self.other_value and not field_data:
+ if self.other_field in all_data and all_data[self.other_field] == self.other_value and not field_data:
raise ValidationError(self.error_message)
class RequiredIfOtherFieldDoesNotEqual(object):
- def __init__(self, other_field, other_value, error_message=None):
+ def __init__(self, other_field, other_value, other_label=None, error_message=None):
self.other_field = other_field
self.other_value = other_value
+ other_label = other_label or other_value
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is not %(value)s"), {
- 'field': other_field, 'value': other_value})
+ 'field': other_field, 'value': other_label})
self.always_test = True
def __call__(self, field_data, all_data):
- if all_data.has_key(self.other_field) and all_data[self.other_field] != self.other_value and not field_data:
+ if self.other_field in all_data and all_data[self.other_field] != self.other_value and not field_data:
raise ValidationError(self.error_message)
class IsLessThanOtherField(object):
@@ -361,7 +365,7 @@ class NumberIsInRange(object):
self.lower, self.upper = lower, upper
if not error_message:
if lower and upper:
- self.error_message = gettext("This value must be between %s and %s.") % (lower, upper)
+ self.error_message = gettext("This value must be between %(lower)s and %(upper)s.") % {'lower': lower, 'upper': upper}
elif lower:
self.error_message = gettext("This value must be at least %s.") % lower
elif upper:
@@ -403,28 +407,35 @@ class IsAPowerOf(object):
if val != int(val):
raise ValidationError, gettext("This value must be a power of %s.") % self.power_of
-class IsValidFloat(object):
+class IsValidDecimal(object):
def __init__(self, max_digits, decimal_places):
self.max_digits, self.decimal_places = max_digits, decimal_places
def __call__(self, field_data, all_data):
- data = str(field_data)
- try:
- float(data)
- except ValueError:
+ match = decimal_re.search(str(field_data))
+ if not match:
raise ValidationError, gettext("Please enter a valid decimal number.")
- # Negative floats require more space to input.
- max_allowed_length = data.startswith('-') and (self.max_digits + 2) or (self.max_digits + 1)
- if len(data) > max_allowed_length:
+
+ digits = len(match.group('digits') or '')
+ decimals = len(match.group('decimals') or '')
+
+ if digits + decimals > self.max_digits:
raise ValidationError, ngettext("Please enter a valid decimal number with at most %s total digit.",
"Please enter a valid decimal number with at most %s total digits.", self.max_digits) % self.max_digits
- if (not '.' in data and len(data) > (max_allowed_length - self.decimal_places - 1)) or ('.' in data and len(data) > (max_allowed_length - (self.decimal_places - len(data.split('.')[1])))):
+ if digits > (self.max_digits - self.decimal_places):
raise ValidationError, ngettext( "Please enter a valid decimal number with a whole part of at most %s digit.",
"Please enter a valid decimal number with a whole part of at most %s digits.", str(self.max_digits-self.decimal_places)) % str(self.max_digits-self.decimal_places)
- if '.' in data and len(data.split('.')[1]) > self.decimal_places:
+ if decimals > self.decimal_places:
raise ValidationError, ngettext("Please enter a valid decimal number with at most %s decimal place.",
"Please enter a valid decimal number with at most %s decimal places.", self.decimal_places) % self.decimal_places
+def isValidFloat(field_data, all_data):
+ data = str(field_data)
+ try:
+ float(data)
+ except ValueError:
+ raise ValidationError, gettext("Please enter a valid floating point number.")
+
class HasAllowableSize(object):
"""
Checks that the file-upload field data is a certain size. min_size and