Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6436 -- Added check for absolute paths in fixture loading. Fix…

…tures specified as an absolute path were being loaded multiple times. Thanks to btoll@bestweb.net for the report, fix, and catch of a duplicate ticket.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7145 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c1f45c326c4bdeb8821844f6ee4bfd2817ab6a63 1 parent 54546f2
Russell Keith-Magee freakboy3742 authored
1  AUTHORS
@@ -71,6 +71,7 @@ answer newbie questions, and generally made Django that much better:
71 71 boobsd@gmail.com
72 72 Andrew Brehaut <http://brehaut.net/blog>
73 73 brut.alll@gmail.com
  74 + btoll@bestweb.net
74 75 Jonathan Buchanan <jonathan.buchanan@gmail.com>
75 76 Can Burak Çilingir <canburak@cs.bilgi.edu.tr>
76 77 Trevor Caira <trevor@caira.com>
20 django/core/management/commands/loaddata.py
@@ -30,7 +30,8 @@ def handle(self, *fixture_labels, **options):
30 30 show_traceback = options.get('traceback', False)
31 31
32 32 # Keep a count of the installed objects and fixtures
33   - count = [0, 0]
  33 + fixture_count = 0
  34 + object_count = 0
34 35 models = set()
35 36
36 37 humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
@@ -65,7 +66,12 @@ def handle(self, *fixture_labels, **options):
65 66 else:
66 67 print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format)
67 68
68   - for fixture_dir in app_fixtures + list(settings.FIXTURE_DIRS) + ['']:
  69 + if os.path.isabs(fixture_name):
  70 + fixture_dirs = [fixture_name]
  71 + else:
  72 + fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
  73 +
  74 + for fixture_dir in fixture_dirs:
69 75 if verbosity > 1:
70 76 print "Checking %s for fixtures..." % humanize(fixture_dir)
71 77
@@ -86,14 +92,14 @@ def handle(self, *fixture_labels, **options):
86 92 transaction.leave_transaction_management()
87 93 return
88 94 else:
89   - count[1] += 1
  95 + fixture_count += 1
90 96 if verbosity > 0:
91 97 print "Installing %s fixture '%s' from %s." % \
92 98 (format, fixture_name, humanize(fixture_dir))
93 99 try:
94 100 objects = serializers.deserialize(format, fixture)
95 101 for obj in objects:
96   - count[0] += 1
  102 + object_count += 1
97 103 models.add(obj.object.__class__)
98 104 obj.save()
99 105 label_found = True
@@ -113,7 +119,7 @@ def handle(self, *fixture_labels, **options):
113 119 print "No %s fixture '%s' in %s." % \
114 120 (format, fixture_name, humanize(fixture_dir))
115 121
116   - if count[0] > 0:
  122 + if object_count > 0:
117 123 sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
118 124 if sequence_sql:
119 125 if verbosity > 1:
@@ -124,9 +130,9 @@ def handle(self, *fixture_labels, **options):
124 130 transaction.commit()
125 131 transaction.leave_transaction_management()
126 132
127   - if count[0] == 0:
  133 + if object_count == 0:
128 134 if verbosity >= 2:
129 135 print "No fixtures found."
130 136 else:
131 137 if verbosity > 0:
132   - print "Installed %d object(s) from %d fixture(s)" % tuple(count)
  138 + print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
9 tests/regressiontests/fixtures_regress/fixtures/absolute.json
... ... @@ -0,0 +1,9 @@
  1 +[
  2 + {
  3 + "pk": "1",
  4 + "model": "fixtures_regress.absolute",
  5 + "fields": {
  6 + "name": "Load Absolute Path Test"
  7 + }
  8 + }
  9 +]
22 tests/regressiontests/fixtures_regress/models.py
... ... @@ -1,6 +1,7 @@
1 1 from django.db import models
2 2 from django.contrib.auth.models import User
3 3 from django.conf import settings
  4 +import os
4 5
5 6 class Animal(models.Model):
6 7 name = models.CharField(max_length=150)
@@ -28,6 +29,16 @@ def __unicode__(self):
28 29 name = None
29 30 return unicode(name) + u' is owned by ' + unicode(self.owner)
30 31
  32 +class Absolute(models.Model):
  33 + name = models.CharField(max_length=40)
  34 +
  35 + load_count = 0
  36 +
  37 + def __init__(self, *args, **kwargs):
  38 + super(Absolute, self).__init__(*args, **kwargs)
  39 + Absolute.load_count += 1
  40 +
  41 +
31 42 __test__ = {'API_TESTS':"""
32 43 >>> from django.core import management
33 44
@@ -49,4 +60,15 @@ def __unicode__(self):
49 60 >>> Stuff.objects.all()
50 61 [<Stuff: None is owned by None>]
51 62
  63 +###############################################
  64 +# Regression test for ticket #6436 --
  65 +# os.path.join will throw away the initial parts of a path if it encounters
  66 +# an absolute path. This means that if a fixture is specified as an absolute path,
  67 +# we need to make sure we don't discover the absolute path in every fixture directory.
  68 +
  69 +>>> load_absolute_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'absolute.json')
  70 +>>> management.call_command('loaddata', load_absolute_path, verbosity=0)
  71 +>>> Absolute.load_count
  72 +1
  73 +
52 74 """}

0 comments on commit c1f45c3

Please sign in to comment.
Something went wrong with that request. Please try again.