<s> '''<STR_LIT>''' <EOL> from elixir . statements import Statement <EOL> import sqlalchemy as sa <EOL> __doc_all__ = [ '<STR_LIT>' ] <EOL> def associable ( assoc_entity , plural_name = None , lazy = True ) : <EOL> '''<STR_LIT>''' <EOL> interface_name = assoc_entity . _descriptor . tablename <EOL> able_name = interface_name + '<STR_LIT>' <EOL> if plural_name : <EOL> attr_name = "<STR_LIT>" % plural_name <EOL> else : <EOL> plural_name = interface_name <EOL> attr_name = "<STR_LIT>" % interface_name <EOL> class GenericAssoc ( object ) : <EOL> def __init__ ( self , tablename ) : <EOL> self . type = tablename <EOL> class Associable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , entity , name = None , uselist = True , lazy = True ) : <EOL> self . entity = entity <EOL> self . lazy = lazy <EOL> self . uselist = uselist <EOL> if name is None : <EOL> self . name = plural_name <EOL> else : <EOL> self . name = name <EOL> def after_table ( self ) : <EOL> col = sa . Column ( '<STR_LIT>' % interface_name , sa . Integer , <EOL> sa . ForeignKey ( '<STR_LIT>' % able_name ) ) <EOL> self . entity . _descriptor . add_column ( col ) <EOL> if not hasattr ( assoc_entity , '<STR_LIT>' ) : <EOL> metadata = assoc_entity . _descriptor . metadata <EOL> association_table = sa . Table ( "<STR_LIT:%s>" % able_name , metadata , <EOL> sa . Column ( '<STR_LIT:id>' , sa . Integer , primary_key = True ) , <EOL> sa . Column ( '<STR_LIT:type>' , sa . String ( <NUM_LIT> ) , nullable = False ) , <EOL> ) <EOL> tablename = "<STR_LIT>" % ( able_name , interface_name ) <EOL> association_to_table = sa . Table ( tablename , metadata , <EOL> sa . Column ( '<STR_LIT>' , sa . Integer , <EOL> sa . ForeignKey ( association_table . c . id , <EOL> ondelete = "<STR_LIT>" ) , <EOL> primary_key = True ) , <EOL> sa . Column ( '<STR_LIT>' % interface_name , sa . Integer , <EOL> sa . ForeignKey ( assoc_entity . table . c . id , <EOL> ondelete = "<STR_LIT>" ) , <EOL> primary_key = True ) , <EOL> ) <EOL> assoc_entity . _assoc_table = association_table <EOL> assoc_entity . _assoc_to_table = association_to_table <EOL> def after_mapper ( self ) : <EOL> if not hasattr ( assoc_entity , '<STR_LIT>' ) : <EOL> assoc_entity . _assoc_mapper = sa . orm . mapper ( <EOL> GenericAssoc , assoc_entity . _assoc_table , properties = { <EOL> '<STR_LIT>' : sa . orm . relation ( <EOL> assoc_entity , <EOL> secondary = assoc_entity . _assoc_to_table , <EOL> lazy = lazy , backref = '<STR_LIT>' , <EOL> order_by = assoc_entity . mapper . order_by ) <EOL> } ) <EOL> entity = self . entity <EOL> entity . mapper . add_property ( <EOL> attr_name , <EOL> sa . orm . relation ( GenericAssoc , lazy = self . lazy , <EOL> backref = '<STR_LIT>' % entity . table . name ) <EOL> ) <EOL> if self . uselist : <EOL> def get ( self ) : <EOL> if getattr ( self , attr_name ) is None : <EOL> setattr ( self , attr_name , <EOL> GenericAssoc ( entity . table . name ) ) <EOL> return getattr ( self , attr_name ) . targets <EOL> setattr ( entity , self . name , property ( get ) ) <EOL> else : <EOL> def get ( self ) : <EOL> attr = getattr ( self , attr_name ) <EOL> if attr is not None : <EOL> return attr . targets [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def set ( self , value ) : <EOL> if getattr ( self , attr_name ) is None : <EOL> setattr ( self , attr_name , <EOL> GenericAssoc ( entity . table . name ) ) <EOL> getattr ( self , attr_name ) . targets = [ value ] <EOL> setattr ( entity , self . name , property ( get , set ) ) <EOL> entity . mapper . add_property ( self . name , sa . orm . synonym ( attr_name ) ) <EOL> def select_by ( cls , ** kwargs ) : <EOL> return cls . query . join ( attr_name , '<STR_LIT>' ) . filter_by ( ** kwargs ) . all ( ) <EOL> setattr ( entity , '<STR_LIT>' % self . name , classmethod ( select_by ) ) <EOL> def select ( cls , * args , ** kwargs ) : <EOL> return cls . query . join ( attr_name , '<STR_LIT>' ) . filter ( * args , ** kwargs ) . all ( ) <EOL> setattr ( entity , '<STR_LIT>' % self . name , classmethod ( select ) ) <EOL> return Statement ( Associable ) </s>
