<s> from django . db . models import get_apps <EOL> from datetime import datetime <EOL> import logging <EOL> from django . db import transaction <EOL> POPULATE_MODULE_NAME = "<STR_LIT>" <EOL> class AlreadyRegistered ( Exception ) : pass <EOL> class NotRegistered ( Exception ) : pass <EOL> class PopulateCache ( object ) : <EOL> def __init__ ( self ) : <EOL> self . models = { } <EOL> self . discovered = False <EOL> def register ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> model = kwargs . pop ( '<STR_LIT>' ) <EOL> elif args : <EOL> args = list ( args ) <EOL> model = args . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if model in self . models : <EOL> raise AlreadyRegistered ( "<STR_LIT>" % model ) <EOL> self . models [ model ] = Populator ( model , * args , ** kwargs ) <EOL> def discover ( self ) : <EOL> if self . discovered : <EOL> return <EOL> for app in get_apps ( ) : <EOL> app_name = app . __name__ . split ( "<STR_LIT:.>" ) [ : - <NUM_LIT:1> ] <EOL> module_name = "<STR_LIT:.>" . join ( app_name + [ POPULATE_MODULE_NAME ] ) <EOL> try : <EOL> populate_module = __import__ ( module_name , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> logging . debug ( "<STR_LIT>" % module_name ) <EOL> except ImportError : <EOL> pass <EOL> self . discovered = True <EOL> def __getitem__ ( self , key ) : <EOL> self . discover ( ) <EOL> return self . models [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> args = [ value ] <EOL> self . register ( key , * args ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . models [ key ] <EOL> def __len__ ( self ) : <EOL> return len ( self . models ) <EOL> def __iter__ ( self ) : <EOL> return self . models . __iter__ <EOL> def __contains__ ( self , item ) : <EOL> self . discover ( ) <EOL> return item in self . models <EOL> def get_model ( self , model ) : <EOL> self . discover ( ) <EOL> try : <EOL> return self . models [ model ] <EOL> except ( KeyError , TypeError ) : <EOL> raise NotRegistered ( '<STR_LIT>' % ( model . __name__ , self . models ) ) <EOL> from rollyourown . populate import DEFAULT_GENERATOR_FUNCTIONS <EOL> import random <EOL> import logging <EOL> class Populator ( object ) : <EOL> def __init__ ( self , model , instances = <NUM_LIT:10> , clear_existing = False , data_functions = None , max_many_to_many_connections = <NUM_LIT:5> ) : <EOL> self . model = model <EOL> self . number_instances = instances <EOL> self . clear_existing = clear_existing <EOL> self . data_functions = data_functions <EOL> self . instances = [ ] <EOL> self . max_many_to_many_connections = max_many_to_many_connections <EOL> def populate ( self ) : <EOL> if not self . instances : <EOL> if self . clear_existing : <EOL> self . model . _default_manager . all ( ) . delete ( ) <EOL> for i in range ( self . number_instances ) : <EOL> instance = self . populate_instance ( i ) <EOL> self . instances . append ( instance ) <EOL> print "<STR_LIT>" % ( len ( self . instances ) , self . model . _meta . app_label , self . model . _meta . object_name ) <EOL> logging . debug ( "<STR_LIT>" ) <EOL> def populate_instance ( self , counter = None ) : <EOL> instance = self . model ( ) <EOL> for field in self . model . _meta . fields : <EOL> if field . choices : <EOL> setattr ( instance , field . name , random . choice ( field . choices ) [ <NUM_LIT:0> ] ) <EOL> elif blankable ( field ) : <EOL> pass <EOL> else : <EOL> generator_function = DEFAULT_GENERATOR_FUNCTIONS . get ( field . __class__ . __name__ , None ) <EOL> if not generator_function : <EOL> generator_function = DEFAULT_GENERATOR_FUNCTIONS . get ( field . get_internal_type ( ) , None ) <EOL> if generator_function : <EOL> value = generator_function ( field , instance , counter ) <EOL> if hasattr ( generator_function , '<STR_LIT>' ) : <EOL> generator_function . add_to_instance ( field , instance , value ) <EOL> else : <EOL> setattr ( instance , field . name , value ) <EOL> transaction . enter_transaction_management ( ) <EOL> transaction . managed ( True ) <EOL> try : <EOL> instance . save ( ) <EOL> except : <EOL> transaction . rollback ( ) <EOL> else : <EOL> transaction . commit ( ) <EOL> return instance <EOL> def populate_many_to_many ( self ) : <EOL> for instance in self . instances : <EOL> for field in self . model . _meta . many_to_many : <EOL> rel_to_instances = list ( field . rel . to . _default_manager . all ( ) . order_by ( '<STR_LIT:?>' ) [ : self . max_many_to_many_connections ] ) <EOL> for i in range ( random . randint ( <NUM_LIT:0> , len ( rel_to_instances ) ) ) : <EOL> getattr ( instance , field . name ) . add ( rel_to_instances [ i ] ) <EOL> def blankable ( field ) : <EOL> """<STR_LIT>""" <EOL> return ( not field . editable and not field . default == datetime . now ) or ( <EOL> field . blank <EOL> and random . choice ( ( True , False , False , False ) ) <EOL> and not field . default == datetime . now <EOL> ) <EOL> registry = PopulateCache ( ) </s>
