<s> from django . db import models <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_unicode , force_unicode <EOL> class ContentTypeManager ( models . Manager ) : <EOL> _cache = { } <EOL> def get_by_natural_key ( self , app_label , model ) : <EOL> try : <EOL> ct = self . __class__ . _cache [ self . db ] [ ( app_label , model ) ] <EOL> except KeyError : <EOL> ct = self . get ( app_label = app_label , model = model ) <EOL> self . _add_to_cache ( self . db , ct ) <EOL> return ct <EOL> def _get_opts ( self , model ) : <EOL> return model . _meta . concrete_model . _meta <EOL> def _get_from_cache ( self , opts ) : <EOL> key = ( opts . app_label , opts . object_name . lower ( ) ) <EOL> return self . __class__ . _cache [ self . db ] [ key ] <EOL> def get_for_model ( self , model ) : <EOL> """<STR_LIT>""" <EOL> opts = self . _get_opts ( model ) <EOL> try : <EOL> ct = self . _get_from_cache ( opts ) <EOL> except KeyError : <EOL> ct , created = self . get_or_create ( <EOL> app_label = opts . app_label , <EOL> model = opts . object_name . lower ( ) , <EOL> defaults = { '<STR_LIT:name>' : smart_unicode ( opts . verbose_name_raw ) } , <EOL> ) <EOL> self . _add_to_cache ( self . db , ct ) <EOL> return ct <EOL> def get_for_models ( self , * models ) : <EOL> """<STR_LIT>""" <EOL> results = { } <EOL> needed_app_labels = set ( ) <EOL> needed_models = set ( ) <EOL> needed_opts = set ( ) <EOL> for model in models : <EOL> opts = self . _get_opts ( model ) <EOL> try : <EOL> ct = self . _get_from_cache ( opts ) <EOL> except KeyError : <EOL> needed_app_labels . add ( opts . app_label ) <EOL> needed_models . add ( opts . object_name . lower ( ) ) <EOL> needed_opts . add ( opts ) <EOL> else : <EOL> results [ model ] = ct <EOL> if needed_opts : <EOL> cts = self . filter ( <EOL> app_label__in = needed_app_labels , <EOL> model__in = needed_models <EOL> ) <EOL> for ct in cts : <EOL> model = ct . model_class ( ) <EOL> if model . _meta in needed_opts : <EOL> results [ model ] = ct <EOL> needed_opts . remove ( model . _meta ) <EOL> self . _add_to_cache ( self . db , ct ) <EOL> for opts in needed_opts : <EOL> ct = self . create ( <EOL> app_label = opts . app_label , <EOL> model = opts . object_name . lower ( ) , <EOL> name = smart_unicode ( opts . verbose_name_raw ) , <EOL> ) <EOL> self . _add_to_cache ( self . db , ct ) <EOL> results [ ct . model_class ( ) ] = ct <EOL> return results <EOL> def get_for_id ( self , id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ct = self . __class__ . _cache [ self . db ] [ id ] <EOL> except KeyError : <EOL> ct = self . get ( pk = id ) <EOL> self . _add_to_cache ( self . db , ct ) <EOL> return ct <EOL> def clear_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __class__ . _cache . clear ( ) <EOL> def _add_to_cache ( self , using , ct ) : <EOL> """<STR_LIT>""" <EOL> model = ct . model_class ( ) <EOL> key = ( model . _meta . app_label , model . _meta . object_name . lower ( ) ) <EOL> self . __class__ . _cache . setdefault ( using , { } ) [ key ] = ct <EOL> self . __class__ . _cache . setdefault ( using , { } ) [ ct . id ] = ct <EOL> class ContentType ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> app_label = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> model = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> ) <EOL> objects = ContentTypeManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> db_table = '<STR_LIT>' <EOL> ordering = ( '<STR_LIT:name>' , ) <EOL> unique_together = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> def __unicode__ ( self ) : <EOL> model = self . model_class ( ) <EOL> if not model or self . name != model . _meta . verbose_name_raw : <EOL> return self . name <EOL> else : <EOL> return force_unicode ( model . _meta . verbose_name ) <EOL> def model_class ( self ) : <EOL> "<STR_LIT>" <EOL> from django . db import models <EOL> return models . get_model ( self . app_label , self . model , <EOL> only_installed = False ) <EOL> def get_object_for_this_type ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . model_class ( ) . _base_manager . using ( self . _state . db ) . get ( ** kwargs ) <EOL> def get_all_objects_for_this_type ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . model_class ( ) . _base_manager . using ( self . _state . db ) . filter ( ** kwargs ) <EOL> def natural_key ( self ) : <EOL> return ( self . app_label , self . model ) </s>
