<s> import re <EOL> from bisect import bisect <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> from django . conf import settings <EOL> from django . db . models . related import RelatedObject <EOL> from django . db . models . fields . related import ManyToManyRel <EOL> from django . db . models . fields import AutoField , FieldDoesNotExist <EOL> from django . db . models . fields . proxy import OrderWrt <EOL> from django . db . models . loading import get_models , app_cache_ready <EOL> from django . utils . translation import activate , deactivate_all , get_language , string_concat <EOL> from django . utils . encoding import force_unicode , smart_str <EOL> from django . utils . datastructures import SortedDict <EOL> get_verbose_name = lambda class_name : re . sub ( '<STR_LIT>' , '<STR_LIT>' , class_name ) . lower ( ) . strip ( ) <EOL> DEFAULT_NAMES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Options ( object ) : <EOL> def __init__ ( self , meta , app_label = None ) : <EOL> self . local_fields , self . local_many_to_many = [ ] , [ ] <EOL> self . virtual_fields = [ ] <EOL> self . module_name , self . verbose_name = None , None <EOL> self . verbose_name_plural = None <EOL> self . db_table = '<STR_LIT>' <EOL> self . ordering = [ ] <EOL> self . unique_together = [ ] <EOL> self . permissions = [ ] <EOL> self . object_name , self . app_label = None , app_label <EOL> self . get_latest_by = None <EOL> self . order_with_respect_to = None <EOL> self . db_tablespace = settings . DEFAULT_TABLESPACE <EOL> self . admin = None <EOL> self . meta = meta <EOL> self . pk = None <EOL> self . has_auto_field , self . auto_field = False , None <EOL> self . abstract = False <EOL> self . managed = True <EOL> self . proxy = False <EOL> self . proxy_for_model = None <EOL> self . parents = SortedDict ( ) <EOL> self . duplicate_targets = { } <EOL> self . abstract_managers = [ ] <EOL> self . concrete_managers = [ ] <EOL> def contribute_to_class ( self , cls , name ) : <EOL> from django . db import connection <EOL> from django . db . backends . util import truncate_name <EOL> cls . _meta = self <EOL> self . installed = re . sub ( '<STR_LIT>' , '<STR_LIT>' , cls . __module__ ) in settings . INSTALLED_APPS <EOL> self . object_name = cls . __name__ <EOL> self . module_name = self . object_name . lower ( ) <EOL> self . verbose_name = get_verbose_name ( self . object_name ) <EOL> if self . meta : <EOL> meta_attrs = self . meta . __dict__ . copy ( ) <EOL> for name in self . meta . __dict__ : <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> del meta_attrs [ name ] <EOL> for attr_name in DEFAULT_NAMES : <EOL> if attr_name in meta_attrs : <EOL> setattr ( self , attr_name , meta_attrs . pop ( attr_name ) ) <EOL> elif hasattr ( self . meta , attr_name ) : <EOL> setattr ( self , attr_name , getattr ( self . meta , attr_name ) ) <EOL> ut = meta_attrs . pop ( '<STR_LIT>' , getattr ( self , '<STR_LIT>' ) ) <EOL> if ut and not isinstance ( ut [ <NUM_LIT:0> ] , ( tuple , list ) ) : <EOL> ut = ( ut , ) <EOL> setattr ( self , '<STR_LIT>' , ut ) <EOL> setattr ( self , '<STR_LIT>' , meta_attrs . pop ( '<STR_LIT>' , string_concat ( self . verbose_name , '<STR_LIT:s>' ) ) ) <EOL> if meta_attrs != { } : <EOL> raise TypeError , "<STR_LIT>" % '<STR_LIT:U+002C>' . join ( meta_attrs . keys ( ) ) <EOL> else : <EOL> self . verbose_name_plural = string_concat ( self . verbose_name , '<STR_LIT:s>' ) <EOL> del self . meta <EOL> if not self . db_table : <EOL> self . db_table = "<STR_LIT>" % ( self . app_label , self . module_name ) <EOL> self . db_table = truncate_name ( self . db_table , connection . ops . max_name_length ( ) ) <EOL> def _prepare ( self , model ) : <EOL> if self . order_with_respect_to : <EOL> self . order_with_respect_to = self . get_field ( self . order_with_respect_to ) <EOL> self . ordering = ( '<STR_LIT>' , ) <EOL> else : <EOL> self . order_with_respect_to = None <EOL> if self . pk is None : <EOL> if self . parents : <EOL> field = self . parents . value_for_index ( <NUM_LIT:0> ) <EOL> field . primary_key = True <EOL> self . setup_pk ( field ) <EOL> else : <EOL> auto = AutoField ( verbose_name = '<STR_LIT>' , primary_key = True , <EOL> auto_created = True ) <EOL> model . add_to_class ( '<STR_LIT:id>' , auto ) <EOL> collections = { } <EOL> for column , target in self . duplicate_targets . iteritems ( ) : <EOL> try : <EOL> collections [ target ] . add ( column ) <EOL> except KeyError : <EOL> collections [ target ] = set ( [ column ] ) <EOL> self . duplicate_targets = { } <EOL> for elt in collections . itervalues ( ) : <EOL> if len ( elt ) == <NUM_LIT:1> : <EOL> continue <EOL> for column in elt : <EOL> self . duplicate_targets [ column ] = elt . difference ( set ( [ column ] ) ) <EOL> def add_field ( self , field ) : <EOL> if field . rel and isinstance ( field . rel , ManyToManyRel ) : <EOL> self . local_many_to_many . insert ( bisect ( self . local_many_to_many , field ) , field ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _m2m_cache <EOL> else : <EOL> self . local_fields . insert ( bisect ( self . local_fields , field ) , field ) <EOL> self . setup_pk ( field ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _field_cache <EOL> del self . _field_name_cache <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _name_map <EOL> def add_virtual_field ( self , field ) : <EOL> self . virtual_fields . append ( field ) <EOL> def setup_pk ( self , field ) : <EOL> if not self . pk and field . primary_key : <EOL> self . pk = field <EOL> field . serialize = False <EOL> def setup_proxy ( self , target ) : <EOL> """<STR_LIT>""" <EOL> self . pk = target . _meta . pk <EOL> self . proxy_for_model = target <EOL> self . db_table = target . _meta . db_table <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . object_name <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( smart_str ( self . app_label ) , smart_str ( self . module_name ) ) <EOL> def verbose_name_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> lang = get_language ( ) <EOL> deactivate_all ( ) <EOL> raw = force_unicode ( self . verbose_name ) <EOL> activate ( lang ) <EOL> return raw <EOL> verbose_name_raw = property ( verbose_name_raw ) <EOL> def _fields ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _field_name_cache <EOL> except AttributeError : <EOL> self . _fill_fields_cache ( ) <EOL> return self . _field_name_cache <EOL> fields = property ( _fields ) <EOL> def get_fields_with_model ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _field_cache <EOL> except AttributeError : <EOL> self . _fill_fields_cache ( ) <EOL> return self . _field_cache <EOL> def _fill_fields_cache ( self ) : <EOL> cache = [ ] <EOL> for parent in self . parents : <EOL> for field , model in parent . _meta . get_fields_with_model ( ) : <EOL> if model : <EOL> cache . append ( ( field , model ) ) <EOL> else : <EOL> cache . append ( ( field , parent ) ) <EOL> cache . extend ( [ ( f , None ) for f in self . local_fields ] ) <EOL> self . _field_cache = tuple ( cache ) <EOL> self . _field_name_cache = [ x for x , _ in cache ] <EOL> def _many_to_many ( self ) : <EOL> try : <EOL> self . _m2m_cache <EOL> except AttributeError : <EOL> self . _fill_m2m_cache ( ) <EOL> return self . _m2m_cache . keys ( ) <EOL> many_to_many = property ( _many_to_many ) <EOL> def get_m2m_with_model ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _m2m_cache <EOL> except AttributeError : <EOL> self . _fill_m2m_cache ( ) <EOL> return self . _m2m_cache . items ( ) <EOL> def _fill_m2m_cache ( self ) : <EOL> cache = SortedDict ( ) <EOL> for parent in self . parents : <EOL> for field , model in parent . _meta . get_m2m_with_model ( ) : <EOL> if model : <EOL> cache [ field ] = model <EOL> else : <EOL> cache [ field ] = parent <EOL> for field in self . local_many_to_many : <EOL> cache [ field ] = None <EOL> self . _m2m_cache = cache <EOL> def get_field ( self , name , many_to_many = True ) : <EOL> """<STR_LIT>""" <EOL> to_search = many_to_many and ( self . fields + self . many_to_many ) or self . fields <EOL> for f in to_search : <EOL> if f . name == name : <EOL> return f <EOL> raise FieldDoesNotExist , '<STR_LIT>' % ( self . object_name , name ) <EOL> def get_field_by_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> return self . _name_map [ name ] <EOL> except AttributeError : <EOL> cache = self . init_name_map ( ) <EOL> return cache [ name ] <EOL> except KeyError : <EOL> raise FieldDoesNotExist ( '<STR_LIT>' <EOL> % ( self . object_name , name ) ) <EOL> def get_all_field_names ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> cache = self . _name_map <EOL> except AttributeError : <EOL> cache = self . init_name_map ( ) <EOL> names = cache . keys ( ) <EOL> names . sort ( ) <EOL> return [ val for val in names if not val . endswith ( '<STR_LIT:+>' ) ] <EOL> def init_name_map ( self ) : <EOL> """<STR_LIT>""" <EOL> cache = { } <EOL> for f , model in self . get_all_related_m2m_objects_with_model ( ) : <EOL> cache [ f . field . related_query_name ( ) ] = ( f , model , False , True ) <EOL> for f , model in self . get_all_related_objects_with_model ( ) : <EOL> cache [ f . field . related_query_name ( ) ] = ( f , model , False , False ) <EOL> for f , model in self . get_m2m_with_model ( ) : <EOL> cache [ f . name ] = ( f , model , True , True ) <EOL> for f , model in self . get_fields_with_model ( ) : <EOL> cache [ f . name ] = ( f , model , True , False ) <EOL> if self . order_with_respect_to : <EOL> cache [ '<STR_LIT>' ] = OrderWrt ( ) , None , True , False <EOL> if app_cache_ready ( ) : <EOL> self . _name_map = cache <EOL> return cache <EOL> def get_add_permission ( self ) : <EOL> return '<STR_LIT>' % self . object_name . lower ( ) <EOL> def get_change_permission ( self ) : <EOL> return '<STR_LIT>' % self . object_name . lower ( ) <EOL> def get_delete_permission ( self ) : <EOL> return '<STR_LIT>' % self . object_name . lower ( ) <EOL> def get_all_related_objects ( self , local_only = False ) : <EOL> try : <EOL> self . _related_objects_cache <EOL> except AttributeError : <EOL> self . _fill_related_objects_cache ( ) <EOL> if local_only : <EOL> return [ k for k , v in self . _related_objects_cache . items ( ) if not v ] <EOL> return self . _related_objects_cache . keys ( ) <EOL> def get_all_related_objects_with_model ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _related_objects_cache <EOL> except AttributeError : <EOL> self . _fill_related_objects_cache ( ) <EOL> return self . _related_objects_cache . items ( ) <EOL> def _fill_related_objects_cache ( self ) : <EOL> cache = SortedDict ( ) <EOL> parent_list = self . get_parent_list ( ) <EOL> for parent in self . parents : <EOL> for obj , model in parent . _meta . get_all_related_objects_with_model ( ) : <EOL> if ( obj . field . creation_counter < <NUM_LIT:0> or obj . field . rel . parent_link ) and obj . model not in parent_list : <EOL> continue <EOL> if not model : <EOL> cache [ obj ] = parent <EOL> else : <EOL> cache [ obj ] = model <EOL> for klass in get_models ( ) : <EOL> for f in klass . _meta . local_fields : <EOL> if f . rel and not isinstance ( f . rel . to , str ) and self == f . rel . to . _meta : <EOL> cache [ RelatedObject ( f . rel . to , klass , f ) ] = None <EOL> self . _related_objects_cache = cache <EOL> def get_all_related_many_to_many_objects ( self , local_only = False ) : <EOL> try : <EOL> cache = self . _related_many_to_many_cache <EOL> except AttributeError : <EOL> cache = self . _fill_related_many_to_many_cache ( ) <EOL> if local_only : <EOL> return [ k for k , v in cache . items ( ) if not v ] <EOL> return cache . keys ( ) <EOL> def get_all_related_m2m_objects_with_model ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> cache = self . _related_many_to_many_cache <EOL> except AttributeError : <EOL> cache = self . _fill_related_many_to_many_cache ( ) <EOL> return cache . items ( ) <EOL> def _fill_related_many_to_many_cache ( self ) : <EOL> cache = SortedDict ( ) <EOL> parent_list = self . get_parent_list ( ) <EOL> for parent in self . parents : <EOL> for obj , model in parent . _meta . get_all_related_m2m_objects_with_model ( ) : <EOL> if obj . field . creation_counter < <NUM_LIT:0> and obj . model not in parent_list : <EOL> continue <EOL> if not model : <EOL> cache [ obj ] = parent <EOL> else : <EOL> cache [ obj ] = model <EOL> for klass in get_models ( ) : <EOL> for f in klass . _meta . local_many_to_many : <EOL> if f . rel and not isinstance ( f . rel . to , str ) and self == f . rel . to . _meta : <EOL> cache [ RelatedObject ( f . rel . to , klass , f ) ] = None <EOL> if app_cache_ready ( ) : <EOL> self . _related_many_to_many_cache = cache <EOL> return cache <EOL> def get_base_chain ( self , model ) : <EOL> """<STR_LIT>""" <EOL> if not self . parents : <EOL> return <EOL> if model in self . parents : <EOL> return [ model ] <EOL> for parent in self . parents : <EOL> res = parent . _meta . get_base_chain ( model ) <EOL> if res : <EOL> res . insert ( <NUM_LIT:0> , parent ) <EOL> return res <EOL> raise TypeError ( '<STR_LIT>' <EOL> % model . _meta . module_name ) <EOL> def get_parent_list ( self ) : <EOL> """<STR_LIT>""" <EOL> result = set ( ) <EOL> for parent in self . parents : <EOL> result . add ( parent ) <EOL> result . update ( parent . _meta . get_parent_list ( ) ) <EOL> return result <EOL> def get_ancestor_link ( self , ancestor ) : <EOL> """<STR_LIT>""" <EOL> if ancestor in self . parents : <EOL> return self . parents [ ancestor ] <EOL> for parent in self . parents : <EOL> parent_link = parent . _meta . get_ancestor_link ( ancestor ) <EOL> if parent_link : <EOL> return self . parents [ parent ] or parent_link <EOL> def get_ordered_objects ( self ) : <EOL> "<STR_LIT>" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> objects = [ ] <EOL> self . _ordered_objects = objects <EOL> return self . _ordered_objects <EOL> def pk_index ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fields . index ( self . pk ) </s>
