Browse files

Changed our use of the DumpData command to be Django 1.5 compatible.

  • Loading branch information...
1 parent 6935680 commit 427a5c4a27fcf65fa2e74bfdf175459f45452b0b Jaap Roes committed Oct 30, 2012
Showing with 39 additions and 11 deletions.
  1. +38 −10 smuggler/utils.py
  2. +1 −1 tests/test_app/tests.py
View
48 smuggler/utils.py
@@ -7,15 +7,19 @@
# Software Foundation. See the file README for copying conditions.
import os
+import sys
from django.core import serializers
+from django.core.management import CommandError
from django.core.management.color import no_style
-
from django.core.management.commands.dumpdata import Command as DumpData
from django.db import connections, transaction, router
from django.db.utils import DEFAULT_DB_ALIAS
from django.http import HttpResponse
-
from smuggler.settings import (SMUGGLER_FORMAT, SMUGGLER_INDENT)
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
def get_file_list(path):
@@ -39,14 +43,38 @@ def save_uploaded_file_on_disk(uploaded_file, destination_path):
def serialize_to_response(app_labels=[], exclude=[], response=None,
format=SMUGGLER_FORMAT, indent=SMUGGLER_INDENT):
response = response or HttpResponse(mimetype='text/plain')
- response.write(DumpData().handle(*app_labels, **{
- 'exclude': exclude,
- 'format': format,
- 'indent': indent,
- 'show_traceback': True,
- 'use_natural_keys': True
- }))
- return response
+ # There's some funky output redirecting going on as Django >= 1.5 writes
+ # to a wrapped output stream, instead of just returning the dumped output.
+ stream = StringIO() # this is going to be our stdout
+ # We need to fake an OutputWrapper as it's only introduced in Django 1.5
+ out = lambda: None
+ out.write = lambda s: stream.write(s) # this seems to be sufficient.
+ try:
+ # Now make sys.stdout our wrapped StringIO instance and start the dump.
+ sys.stdout = out
+ dumpdata = DumpData()
+ dumpdata.stdout = sys.stdout
+ dumpdata.stderr = sys.stderr
+ output = dumpdata.handle(*app_labels, **{
+ 'exclude': exclude,
+ 'format': format,
+ 'indent': indent,
+ 'show_traceback': True,
+ 'use_natural_keys': True
+ })
+ except CommandError:
+ # We expect and re-raise CommandErrors, these contain "user friendly"
+ # error messages.
+ raise
+ else:
+ if output:
+ response.write(output)
+ else:
+ response.write(stream.getvalue())
+ return response
+ finally:
+ # Be nice and cleanup!
+ sys.stdout = sys.__stdout__
def load_requested_data(data):
View
2 tests/test_app/tests.py
@@ -18,7 +18,7 @@ def setUp(self):
f.save()
def normalize(self, out):
- return re.sub(r'\s\s*', ' ', out)
+ return re.sub(r'\s\s*', ' ', out).strip()
def test_serialize_to_response(self):
stream = StringIO.StringIO()

1 comment on commit 427a5c4

@kunitoki

Thanx this is great ! Looking forward to an updated version to be used with pip :)

Please sign in to comment.