~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/core/management/commands/loaddata.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-10-12 11:34:35 UTC
  • mfrom: (1.2.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: james.westby@ubuntu.com-20101012113435-9lnsrh0i3mxozbt0
Tags: upstream-1.2.3
ImportĀ upstreamĀ versionĀ 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
 
48
48
        # Keep a count of the installed objects and fixtures
49
49
        fixture_count = 0
50
 
        object_count = 0
 
50
        loaded_object_count = 0
 
51
        fixture_object_count = 0
51
52
        models = set()
52
53
 
53
54
        humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
112
113
 
113
114
            if formats:
114
115
                if verbosity > 1:
115
 
                    print "Loading '%s' fixtures..." % fixture_name
 
116
                    self.stdout.write("Loading '%s' fixtures...\n" % fixture_name)
116
117
            else:
117
 
                sys.stderr.write(
118
 
                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
 
118
                self.stderr.write(
 
119
                    self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format.\n" %
119
120
                        (fixture_name, format)))
120
121
                transaction.rollback(using=using)
121
122
                transaction.leave_transaction_management(using=using)
128
129
 
129
130
            for fixture_dir in fixture_dirs:
130
131
                if verbosity > 1:
131
 
                    print "Checking %s for fixtures..." % humanize(fixture_dir)
 
132
                    self.stdout.write("Checking %s for fixtures...\n" % humanize(fixture_dir))
132
133
 
133
134
                label_found = False
134
135
                for combo in product([using, None], formats, compression_formats):
141
142
                    )
142
143
 
143
144
                    if verbosity > 1:
144
 
                        print "Trying %s for %s fixture '%s'..." % \
145
 
                            (humanize(fixture_dir), file_name, fixture_name)
 
145
                        self.stdout.write("Trying %s for %s fixture '%s'...\n" % \
 
146
                            (humanize(fixture_dir), file_name, fixture_name))
146
147
                    full_path = os.path.join(fixture_dir, file_name)
147
148
                    open_method = compression_types[compression_format]
148
149
                    try:
149
150
                        fixture = open_method(full_path, 'r')
150
151
                        if label_found:
151
152
                            fixture.close()
152
 
                            print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
153
 
                                (fixture_name, humanize(fixture_dir)))
 
153
                            self.stderr.write(self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting.\n" %
 
154
                                (fixture_name, humanize(fixture_dir))))
154
155
                            transaction.rollback(using=using)
155
156
                            transaction.leave_transaction_management(using=using)
156
157
                            return
157
158
                        else:
158
159
                            fixture_count += 1
159
160
                            objects_in_fixture = 0
 
161
                            loaded_objects_in_fixture = 0
160
162
                            if verbosity > 0:
161
 
                                print "Installing %s fixture '%s' from %s." % \
162
 
                                    (format, fixture_name, humanize(fixture_dir))
 
163
                                self.stdout.write("Installing %s fixture '%s' from %s.\n" % \
 
164
                                    (format, fixture_name, humanize(fixture_dir)))
163
165
                            try:
164
166
                                objects = serializers.deserialize(format, fixture, using=using)
165
167
                                for obj in objects:
 
168
                                    objects_in_fixture += 1
166
169
                                    if router.allow_syncdb(using, obj.object.__class__):
167
 
                                        objects_in_fixture += 1
 
170
                                        loaded_objects_in_fixture += 1
168
171
                                        models.add(obj.object.__class__)
169
172
                                        obj.save(using=using)
170
 
                                object_count += objects_in_fixture
 
173
                                loaded_object_count += loaded_objects_in_fixture
 
174
                                fixture_object_count += objects_in_fixture
171
175
                                label_found = True
172
176
                            except (SystemExit, KeyboardInterrupt):
173
177
                                raise
179
183
                                if show_traceback:
180
184
                                    traceback.print_exc()
181
185
                                else:
182
 
                                    sys.stderr.write(
 
186
                                    self.stderr.write(
183
187
                                        self.style.ERROR("Problem installing fixture '%s': %s\n" %
184
188
                                             (full_path, ''.join(traceback.format_exception(sys.exc_type,
185
189
                                                 sys.exc_value, sys.exc_traceback)))))
189
193
                            # If the fixture we loaded contains 0 objects, assume that an
190
194
                            # error was encountered during fixture loading.
191
195
                            if objects_in_fixture == 0:
192
 
                                sys.stderr.write(
193
 
                                    self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
 
196
                                self.stderr.write(
 
197
                                    self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)\n" %
194
198
                                        (fixture_name)))
195
199
                                transaction.rollback(using=using)
196
200
                                transaction.leave_transaction_management(using=using)
198
202
 
199
203
                    except Exception, e:
200
204
                        if verbosity > 1:
201
 
                            print "No %s fixture '%s' in %s." % \
202
 
                                (format, fixture_name, humanize(fixture_dir))
 
205
                            self.stdout.write("No %s fixture '%s' in %s.\n" % \
 
206
                                (format, fixture_name, humanize(fixture_dir)))
203
207
 
204
208
        # If we found even one object in a fixture, we need to reset the
205
209
        # database sequences.
206
 
        if object_count > 0:
 
210
        if loaded_object_count > 0:
207
211
            sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
208
212
            if sequence_sql:
209
213
                if verbosity > 1:
210
 
                    print "Resetting sequences"
 
214
                    self.stdout.write("Resetting sequences\n")
211
215
                for line in sequence_sql:
212
216
                    cursor.execute(line)
213
217
 
215
219
            transaction.commit(using=using)
216
220
            transaction.leave_transaction_management(using=using)
217
221
 
218
 
        if object_count == 0:
 
222
        if fixture_object_count == 0:
219
223
            if verbosity > 0:
220
 
                print "No fixtures found."
 
224
                self.stdout.write("No fixtures found.\n")
221
225
        else:
222
226
            if verbosity > 0:
223
 
                print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
 
227
                if fixture_object_count == loaded_object_count:
 
228
                    self.stdout.write("Installed %d object(s) from %d fixture(s)\n" % (
 
229
                        loaded_object_count, fixture_count))
 
230
                else:
 
231
                    self.stdout.write("Installed %d object(s) (of %d) from %d fixture(s)\n" % (
 
232
                        loaded_object_count, fixture_object_count, fixture_count))
224
233
 
225
234
        # Close the DB connection. This is required as a workaround for an
226
235
        # edge case in MySQL: if the same connection is used to