<s> import sys <EOL> from optparse import make_option <EOL> from collections import defaultdict <EOL> import django <EOL> from django . core import serializers <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django . core . management . color import no_style <EOL> from django . db import connections , router , transaction , DEFAULT_DB_ALIAS <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from sitetree . utils import get_tree_model , get_tree_item_model <EOL> MODEL_TREE_CLASS = get_tree_model ( ) <EOL> MODEL_TREE_ITEM_CLASS = get_tree_item_model ( ) <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = DEFAULT_DB_ALIAS , help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = None , help = '<STR_LIT>' ) , <EOL> ) <EOL> help = '<STR_LIT>' <EOL> args = '<STR_LIT>' <EOL> def handle ( self , * fixture_files , ** options ) : <EOL> using = options . get ( '<STR_LIT>' , DEFAULT_DB_ALIAS ) <EOL> mode = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> items_into_tree = options . get ( '<STR_LIT>' , None ) <EOL> if items_into_tree is not None : <EOL> try : <EOL> items_into_tree = MODEL_TREE_CLASS . objects . get ( alias = items_into_tree ) <EOL> except ObjectDoesNotExist : <EOL> raise CommandError ( '<STR_LIT>' % <EOL> items_into_tree ) <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> connection = connections [ using ] <EOL> cursor = connection . cursor ( ) <EOL> self . style = no_style ( ) <EOL> django_version = django . get_version ( ) <EOL> django_version_less_17 = django_version < '<STR_LIT>' <EOL> django_version_less_18 = django_version < '<STR_LIT>' <EOL> if django_version_less_17 : <EOL> transaction . commit_unless_managed ( using = using ) <EOL> if django_version_less_18 : <EOL> transaction . enter_transaction_management ( using = using ) <EOL> if django_version_less_17 : <EOL> transaction . managed ( True , using = using ) <EOL> loaded_object_count = <NUM_LIT:0> <EOL> if mode == '<STR_LIT:replace>' : <EOL> try : <EOL> MODEL_TREE_CLASS . objects . all ( ) . delete ( ) <EOL> MODEL_TREE_ITEM_CLASS . objects . all ( ) . delete ( ) <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> for fixture_file in fixture_files : <EOL> self . stdout . write ( '<STR_LIT>' % fixture_file ) <EOL> fixture = open ( fixture_file , '<STR_LIT:r>' ) <EOL> try : <EOL> objects = serializers . deserialize ( '<STR_LIT>' , fixture , using = using ) <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> trees = [ ] <EOL> tree_items = defaultdict ( list ) <EOL> tree_item_parents = defaultdict ( list ) <EOL> tree_items_new_indexes = { } <EOL> try : <EOL> allow_migrate = router . allow_migrate <EOL> except AttributeError : <EOL> allow_migrate = router . allow_syncdb <EOL> for obj in objects : <EOL> if allow_migrate ( using , obj . object . __class__ ) : <EOL> if isinstance ( obj . object , ( MODEL_TREE_CLASS , MODEL_TREE_ITEM_CLASS ) ) : <EOL> if isinstance ( obj . object , MODEL_TREE_CLASS ) : <EOL> trees . append ( obj . object ) <EOL> else : <EOL> if items_into_tree is not None : <EOL> obj . object . tree_id = items_into_tree . id <EOL> tree_items [ obj . object . tree_id ] . append ( obj . object ) <EOL> tree_item_parents [ obj . object . parent_id ] . append ( obj . object . id ) <EOL> if items_into_tree is not None : <EOL> trees = [ items_into_tree , ] <EOL> try : <EOL> for tree in trees : <EOL> self . stdout . write ( '<STR_LIT>' % tree . alias ) <EOL> orig_tree_id = tree . id <EOL> if items_into_tree is None : <EOL> if mode == '<STR_LIT>' : <EOL> tree . pk = None <EOL> tree . id = None <EOL> tree . save ( using = using ) <EOL> loaded_object_count += <NUM_LIT:1> <EOL> parents_ahead = [ ] <EOL> for tree_item in tree_items [ orig_tree_id ] : <EOL> parent_ahead = False <EOL> self . stdout . write ( '<STR_LIT>' % tree_item . title ) <EOL> tree_item . tree_id = tree . id <EOL> orig_item_id = tree_item . id <EOL> if mode == '<STR_LIT>' : <EOL> tree_item . pk = None <EOL> tree_item . id = None <EOL> if tree_item . id in tree_items_new_indexes : <EOL> tree_item . pk = tree_item . id = tree_items_new_indexes [ tree_item . id ] <EOL> if tree_item . parent_id is not None : <EOL> if tree_item . parent_id in tree_items_new_indexes : <EOL> tree_item . parent_id = tree_items_new_indexes [ tree_item . parent_id ] <EOL> else : <EOL> parent_ahead = True <EOL> tree_item . save ( using = using ) <EOL> loaded_object_count += <NUM_LIT:1> <EOL> if mode == '<STR_LIT>' : <EOL> tree_items_new_indexes [ orig_item_id ] = tree_item . id <EOL> if parent_ahead : <EOL> parents_ahead . append ( tree_item ) <EOL> for tree_item in parents_ahead : <EOL> tree_item . parent_id = tree_items_new_indexes [ tree_item . parent_id ] <EOL> tree_item . save ( using = using ) <EOL> except ( SystemExit , KeyboardInterrupt ) : <EOL> raise <EOL> except Exception : <EOL> import traceback <EOL> fixture . close ( ) <EOL> if django_version_less_18 : <EOL> transaction . rollback ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> self . stderr . write ( <EOL> self . style . ERROR ( '<STR_LIT>' % ( <EOL> fixture_file , '<STR_LIT>' . join ( traceback . format_exception ( <EOL> sys . exc_type , sys . exc_value , sys . exc_traceback <EOL> ) ) <EOL> ) ) <EOL> ) <EOL> fixture . close ( ) <EOL> if loaded_object_count > <NUM_LIT:0> : <EOL> sequence_sql = connection . ops . sequence_reset_sql ( self . style , [ MODEL_TREE_CLASS , MODEL_TREE_ITEM_CLASS ] ) <EOL> if sequence_sql : <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> for line in sequence_sql : <EOL> cursor . execute ( line ) <EOL> if django_version_less_18 : <EOL> transaction . commit ( using = using ) <EOL> transaction . leave_transaction_management ( using = using ) <EOL> connection . close ( ) </s>
