app/django/core/management/commands/loaddata.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 from django.core.management.base import BaseCommand
       
     2 from django.core.management.color import no_style
       
     3 from optparse import make_option
       
     4 import sys
       
     5 import os
       
     6 
       
     7 try:
       
     8     set
       
     9 except NameError:
       
    10     from sets import Set as set   # Python 2.3 fallback
       
    11 
       
    12 class Command(BaseCommand):
       
    13     option_list = BaseCommand.option_list + (
       
    14         make_option('--verbosity', action='store', dest='verbosity', default='1',
       
    15             type='choice', choices=['0', '1', '2'],
       
    16             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
       
    17     )
       
    18     help = 'Installs the named fixture(s) in the database.'
       
    19     args = "fixture [fixture ...]"
       
    20 
       
    21     def handle(self, *fixture_labels, **options):
       
    22         from django.db.models import get_apps
       
    23         from django.core import serializers
       
    24         from django.db import connection, transaction
       
    25         from django.conf import settings
       
    26 
       
    27         self.style = no_style()
       
    28 
       
    29         verbosity = int(options.get('verbosity', 1))
       
    30         show_traceback = options.get('traceback', False)
       
    31 
       
    32         # Keep a count of the installed objects and fixtures
       
    33         fixture_count = 0
       
    34         object_count = 0
       
    35         models = set()
       
    36 
       
    37         humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
       
    38 
       
    39         # Get a cursor (even though we don't need one yet). This has
       
    40         # the side effect of initializing the test database (if
       
    41         # it isn't already initialized).
       
    42         cursor = connection.cursor()
       
    43 
       
    44         # Start transaction management. All fixtures are installed in a
       
    45         # single transaction to ensure that all references are resolved.
       
    46         transaction.commit_unless_managed()
       
    47         transaction.enter_transaction_management()
       
    48         transaction.managed(True)
       
    49 
       
    50         app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
       
    51         for fixture_label in fixture_labels:
       
    52             parts = fixture_label.split('.')
       
    53             if len(parts) == 1:
       
    54                 fixture_name = fixture_label
       
    55                 formats = serializers.get_public_serializer_formats()
       
    56             else:
       
    57                 fixture_name, format = '.'.join(parts[:-1]), parts[-1]
       
    58                 if format in serializers.get_public_serializer_formats():
       
    59                     formats = [format]
       
    60                 else:
       
    61                     formats = []
       
    62 
       
    63             if verbosity >= 2:
       
    64                 if formats:
       
    65                     print "Loading '%s' fixtures..." % fixture_name
       
    66                 else:
       
    67                     print "Skipping fixture '%s': %s is not a known serialization format" % (fixture_name, format)
       
    68 
       
    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:
       
    75                 if verbosity > 1:
       
    76                     print "Checking %s for fixtures..." % humanize(fixture_dir)
       
    77 
       
    78                 label_found = False
       
    79                 for format in formats:
       
    80                     serializer = serializers.get_serializer(format)
       
    81                     if verbosity > 1:
       
    82                         print "Trying %s for %s fixture '%s'..." % \
       
    83                             (humanize(fixture_dir), format, fixture_name)
       
    84                     try:
       
    85                         full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format]))
       
    86                         fixture = open(full_path, 'r')
       
    87                         if label_found:
       
    88                             fixture.close()
       
    89                             print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
       
    90                                 (fixture_name, humanize(fixture_dir)))
       
    91                             transaction.rollback()
       
    92                             transaction.leave_transaction_management()
       
    93                             return
       
    94                         else:
       
    95                             fixture_count += 1
       
    96                             if verbosity > 0:
       
    97                                 print "Installing %s fixture '%s' from %s." % \
       
    98                                     (format, fixture_name, humanize(fixture_dir))
       
    99                             try:
       
   100                                 objects = serializers.deserialize(format, fixture)
       
   101                                 for obj in objects:
       
   102                                     object_count += 1
       
   103                                     models.add(obj.object.__class__)
       
   104                                     obj.save()
       
   105                                 label_found = True
       
   106                             except Exception, e:
       
   107                                 fixture.close()
       
   108                                 transaction.rollback()
       
   109                                 transaction.leave_transaction_management()
       
   110                                 if show_traceback:
       
   111                                     import traceback
       
   112                                     traceback.print_exc()
       
   113                                 else:
       
   114                                     sys.stderr.write(
       
   115                                         self.style.ERROR("Problem installing fixture '%s': %s\n" %
       
   116                                              (full_path, str(e))))
       
   117                                 return
       
   118                             fixture.close()
       
   119                     except:
       
   120                         if verbosity >= 2:
       
   121                             print "No %s fixture '%s' in %s." % \
       
   122                                 (format, fixture_name, humanize(fixture_dir))
       
   123 
       
   124         if object_count > 0:
       
   125             sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
       
   126             if sequence_sql:
       
   127                 if verbosity > 1:
       
   128                     print "Resetting sequences"
       
   129                 for line in sequence_sql:
       
   130                     cursor.execute(line)
       
   131 
       
   132         transaction.commit()
       
   133         transaction.leave_transaction_management()
       
   134 
       
   135         if object_count == 0:
       
   136             if verbosity >= 2:
       
   137                 print "No fixtures found."
       
   138         else:
       
   139             if verbosity > 0:
       
   140                 print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)