diff options
| author | Justin Bronn <jbronn@gmail.com> | 2008-01-02 16:57:53 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2008-01-02 16:57:53 +0000 |
| commit | 7e322b5908bf5ec255c90c615d1b45b358ecee5f (patch) | |
| tree | 3390dc70c5b4c4a8ff583308bd904eb749f97c04 /django/core | |
| parent | ef0f46f1d0b9e41c811d3d6b833f1652ad46aec3 (diff) | |
gis: Merged revisions 6920-6989 via svnmerge from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6990 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/mail.py | 60 | ||||
| -rw-r--r-- | django/core/management/base.py | 2 | ||||
| -rw-r--r-- | django/core/management/commands/dumpdata.py | 12 | ||||
| -rw-r--r-- | django/core/management/commands/loaddata.py | 11 | ||||
| -rw-r--r-- | django/core/management/sql.py | 3 | ||||
| -rw-r--r-- | django/core/serializers/__init__.py | 5 | ||||
| -rw-r--r-- | django/core/serializers/base.py | 4 | ||||
| -rw-r--r-- | django/core/serializers/json.py | 2 | ||||
| -rw-r--r-- | django/core/serializers/python.py | 4 | ||||
| -rw-r--r-- | django/core/serializers/pyyaml.py | 2 | ||||
| -rw-r--r-- | django/core/servers/basehttp.py | 16 |
11 files changed, 79 insertions, 42 deletions
diff --git a/django/core/mail.py b/django/core/mail.py index 77ab8007e6..153dcb6e63 100644 --- a/django/core/mail.py +++ b/django/core/mail.py @@ -67,42 +67,32 @@ def make_msgid(idstring=None): class BadHeaderError(ValueError): pass +def forbid_multi_line_headers(name, val): + "Forbids multi-line headers, to prevent header injection." + if '\n' in val or '\r' in val: + raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (val, name)) + try: + val = force_unicode(val).encode('ascii') + except UnicodeEncodeError: + if name.lower() in ('to', 'from', 'cc'): + result = [] + for item in val.split(', '): + nm, addr = parseaddr(item) + nm = str(Header(nm, settings.DEFAULT_CHARSET)) + result.append(formataddr((nm, str(addr)))) + val = ', '.join(result) + else: + val = Header(force_unicode(val), settings.DEFAULT_CHARSET) + return name, val + class SafeMIMEText(MIMEText): def __setitem__(self, name, val): - "Forbids multi-line headers, to prevent header injection." - if '\n' in val or '\r' in val: - raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) - try: - val = force_unicode(val).encode('ascii') - except UnicodeEncodeError: - if name.lower() in ('to', 'from', 'cc'): - result = [] - for item in val.split(', '): - nm, addr = parseaddr(item) - nm = str(Header(nm, settings.DEFAULT_CHARSET)) - result.append(formataddr((nm, str(addr)))) - val = ', '.join(result) - else: - val = Header(force_unicode(val), settings.DEFAULT_CHARSET) + name, val = forbid_multi_line_headers(name, val) MIMEText.__setitem__(self, name, val) class SafeMIMEMultipart(MIMEMultipart): def __setitem__(self, name, val): - "Forbids multi-line headers, to prevent header injection." - if '\n' in val or '\r' in val: - raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) - try: - val = force_unicode(val).encode('ascii') - except UnicodeEncodeError: - if name.lower() in ('to', 'from', 'cc'): - result = [] - for item in val.split(', '): - nm, addr = parseaddr(item) - nm = str(Header(nm, settings.DEFAULT_CHARSET)) - result.append(formataddr((nm, str(addr)))) - val = ', '.join(result) - else: - val = Header(force_unicode(val), settings.DEFAULT_CHARSET) + name, val = forbid_multi_line_headers(name, val) MIMEMultipart.__setitem__(self, name, val) class SMTPConnection(object): @@ -209,8 +199,14 @@ class EmailMessage(object): bytestrings). The SafeMIMEText class will handle any necessary encoding conversions. """ - self.to = to or [] - self.bcc = bcc or [] + if to: + self.to = list(to) + else: + self.to = [] + if bcc: + self.bcc = list(bcc) + else: + self.bcc = [] self.from_email = from_email or settings.DEFAULT_FROM_EMAIL self.subject = subject self.body = body diff --git a/django/core/management/base.py b/django/core/management/base.py index 31c2849075..7b8a3e987f 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -27,6 +27,8 @@ class BaseCommand(object): help='The Python path to a settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.'), make_option('--pythonpath', help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".'), + make_option('--traceback', action='store_true', + help='Print traceback on exception'), ) help = '' args = '' diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 7bdcc4271f..2642ae925e 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -1,11 +1,12 @@ from django.core.management.base import BaseCommand, CommandError +from django.core import serializers from optparse import make_option class Command(BaseCommand): option_list = BaseCommand.option_list + ( make_option('--format', default='json', dest='format', - help='Specifies the output serialization format for fixtures'), + help='Specifies the output serialization format for fixtures.'), make_option('--indent', default=None, dest='indent', type='int', help='Specifies the indent level to use when pretty-printing output'), ) @@ -14,10 +15,10 @@ class Command(BaseCommand): def handle(self, *app_labels, **options): from django.db.models import get_app, get_apps, get_models - from django.core import serializers format = options.get('format', 'json') indent = options.get('indent', None) + show_traceback = options.get('traceback', False) if len(app_labels) == 0: app_list = get_apps() @@ -26,6 +27,9 @@ class Command(BaseCommand): # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. + if format not in serializers.get_public_serializer_formats(): + raise CommandError("Unknown serialization format: %s" % format) + try: serializers.get_serializer(format) except KeyError: @@ -34,8 +38,10 @@ class Command(BaseCommand): objects = [] for app in app_list: for model in get_models(app): - objects.extend(model.objects.all()) + objects.extend(model._default_manager.all()) try: return serializers.serialize(format, objects, indent=indent) except Exception, e: + if show_traceback: + raise raise CommandError("Unable to serialize database: %s" % e) diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 216db09141..fb0325906d 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -27,6 +27,7 @@ class Command(BaseCommand): self.style = no_style() verbosity = int(options.get('verbosity', 1)) + show_traceback = options.get('traceback', False) # Keep a count of the installed objects and fixtures count = [0, 0] @@ -50,10 +51,10 @@ class Command(BaseCommand): parts = fixture_label.split('.') if len(parts) == 1: fixture_name = fixture_label - formats = serializers.get_serializer_formats() + formats = serializers.get_public_serializer_formats() else: fixture_name, format = '.'.join(parts[:-1]), parts[-1] - if format in serializers.get_serializer_formats(): + if format in serializers.get_public_serializer_formats(): formats = [format] else: formats = [] @@ -98,11 +99,13 @@ class Command(BaseCommand): label_found = True except Exception, e: fixture.close() + transaction.rollback() + transaction.leave_transaction_management() + if show_traceback: + raise sys.stderr.write( self.style.ERROR("Problem installing fixture '%s': %s\n" % (full_path, str(e)))) - transaction.rollback() - transaction.leave_transaction_management() return fixture.close() except: diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 49f4a80732..46b8d4a985 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -116,6 +116,7 @@ def sql_delete(app, style): "Returns a list of the DROP TABLE SQL statements for the given app." from django.db import connection, models, get_introspection_module from django.db.backends.util import truncate_name + from django.contrib.contenttypes import generic introspection = get_introspection_module() # This should work even if a connection isn't available @@ -179,6 +180,8 @@ def sql_delete(app, style): for model in app_models: opts = model._meta for f in opts.many_to_many: + if isinstance(f.rel, generic.GenericRel): + continue if cursor and table_name_converter(f.m2m_db_table()) in table_names: output.append("%s %s;" % (style.SQL_KEYWORD('DROP TABLE'), style.SQL_TABLE(qn(f.m2m_db_table())))) diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py index 1e24e2bd22..47dfb0c87a 100644 --- a/django/core/serializers/__init__.py +++ b/django/core/serializers/__init__.py @@ -53,6 +53,11 @@ def get_serializer_formats(): _load_serializers() return _serializers.keys() +def get_public_serializer_formats(): + if not _serializers: + _load_serializers() + return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only] + def get_deserializer(format): if not _serializers: _load_serializers() diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index ee9e4dd621..3c7dcfa02e 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -22,6 +22,10 @@ class Serializer(object): Abstract serializer base class. """ + # Indicates if the implemented serializer is only available for + # internal Django use. + internal_use_only = False + def serialize(self, queryset, **options): """ Serialize a queryset. diff --git a/django/core/serializers/json.py b/django/core/serializers/json.py index fa2dca7295..e17b821f52 100644 --- a/django/core/serializers/json.py +++ b/django/core/serializers/json.py @@ -20,6 +20,8 @@ class Serializer(PythonSerializer): """ Convert a queryset to JSON. """ + internal_use_only = False + def end_serialization(self): self.options.pop('stream', None) self.options.pop('fields', None) diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py index 6fc13d76b5..cedab06be9 100644 --- a/django/core/serializers/python.py +++ b/django/core/serializers/python.py @@ -13,7 +13,9 @@ class Serializer(base.Serializer): """ Serializes a QuerySet to basic Python objects. """ - + + internal_use_only = True + def start_serialization(self): self._current = None self.objects = [] diff --git a/django/core/serializers/pyyaml.py b/django/core/serializers/pyyaml.py index 772f34a2e3..4c32a9686f 100644 --- a/django/core/serializers/pyyaml.py +++ b/django/core/serializers/pyyaml.py @@ -19,6 +19,8 @@ class Serializer(PythonSerializer): Convert a queryset to YAML. """ + internal_use_only = False + def handle_field(self, obj, field): # A nasty special case: base YAML doesn't support serialization of time # types (as opposed to dates or datetimes, which it does support). Since diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py index e98957da2c..05f8756655 100644 --- a/django/core/servers/basehttp.py +++ b/django/core/servers/basehttp.py @@ -398,8 +398,20 @@ class ServerHandler(object): self.bytes_sent += len(data) # XXX check Content-Length and truncate if too many bytes written? - self._write(data) - self._flush() + + # If data is too large, socket will choke, so write chunks no larger + # than 32MB at a time. + length = len(data) + if length > 33554432: + offset = 0 + while offset < length: + chunk_size = min(33554432, length) + self._write(data[offset:offset+chunk_size]) + self._flush() + offset += chunk_size + else: + self._write(data) + self._flush() def sendfile(self): """Platform-specific file transmission |
