Show
Ignore:
Timestamp:
02/12/07 15:27:25 (6 years ago)
Author:
ged
Message:

- Changed tests so that when one test fails, other unrelated ones don't.

- now options are initialized to their "global defaults" values instead
of hard coded values, so we can set a bunch of options on all entities.

- added a way to set custom column names for belongs_to relations.

- implemented a way to delay setup (to have a behaviour somewhat similar
to something non dynamic). This is to be used in conjunction with the
new "setup_all" function.

- made autoload work for belongs_to/has_one/has_many relations. The user
must provide column names in that case. It would be possible (and quite
easy, I think) to guess things based on foreign keys when there is only
one relation of the same type between two entities, but I haven't done
it yet, and don't plan to do it before the release. It doesn't work yet
for HasAndBelongsToMany relationships.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/entity.py

    r46 r47  
    77from elixir.statements              import Statement 
    88from elixir.fields                  import Field 
     9from elixir.options                 import options_defaults 
    910 
    1011import sys 
     
    1819DEFAULT_AUTO_PRIMARYKEY_NAME = "id" 
    1920DEFAULT_AUTO_PRIMARYKEY_TYPE = Integer 
    20  
    21 class Entity(object): 
    22     ''' 
    23     The base class for all entities 
    24      
    25     All Elixir model objects should inherit from this class. Statements can 
    26     appear within the body of the definition of an entity to define its 
    27     fields, relationships, and other options. 
    28      
    29     Here is an example: 
    30  
    31     :: 
    32      
    33         class Person(Entity): 
    34             has_field('name', Unicode(128)) 
    35             has_field('birthdate', DateTime, default=datetime.now) 
    36      
    37     Please note, that if you don't specify any primary keys, Elixir will 
    38     automatically create one called ``id``. 
    39      
    40     For further information, please refer to the provided examples or 
    41     tutorial. 
    42     ''' 
    43      
    44     class __metaclass__(type): 
    45         def __init__(cls, name, bases, dict_): 
    46             # only process subclasses of Entity, not Entity itself 
    47             if bases[0] is object: 
    48                 return 
    49              
    50             # create the entity descriptor 
    51             desc = cls._descriptor = EntityDescriptor(cls) 
    52             EntityDescriptor.current = desc 
    53              
    54             # process statements 
    55             Statement.process(cls) 
    56              
    57             # setup misc options here (like tablename etc.) 
    58             desc.setup_options() 
    59              
    60             # create table & assign (empty) mapper 
    61             desc.setup() 
    62              
    63             # try to setup all uninitialized relationships 
    64             EntityDescriptor.setup_relationships() 
    6521 
    6622 
     
    9046 
    9147        # set default value for options 
     48        self.order_by = None 
     49        self.tablename = None 
    9250        self.metadata = getattr(self.module, 'metadata', elixir.metadata) 
    93         self.autoload = None 
    94         self.tablename = None 
    95         self.shortnames = False 
    96         self.auto_primarykey = True 
    97         self.order_by = None 
    98         self.mapper_options = dict() 
    99         self.table_options = dict() 
     51 
     52        for option in ('autoload', 'shortnames', 'auto_primarykey'): 
     53            setattr(self, option, options_defaults[option]) 
     54 
     55        for option_dict in ('mapper_options', 'table_options'): 
     56            setattr(self, option_dict, options_defaults[option_dict].copy()) 
    10057     
    10158    def setup_options(self): 
     
    12279        ''' 
    12380         
     81        if elixir.delay_setup: 
     82            elixir.delayed_entities.add(self) 
     83            return 
     84 
     85        self.setup_table() 
    12486        self.setup_mapper() 
    12587        
     
    12890        EntityDescriptor.uninitialized_rels.update( 
    12991            self.relationships.values()) 
     92 
     93        # try to setup all uninitialized relationships 
     94        EntityDescriptor.setup_relationships() 
    13095     
    13196    def setup_mapper(self): 
     
    138103         
    139104        session = getattr(self.module, 'session', elixir.objectstore) 
    140         table = self.setup_table() 
    141105         
    142106        kwargs = self.mapper_options 
     
    144108            kwargs['order_by'] = self.translate_order_by(self.order_by) 
    145109         
    146         assign_mapper(session.context, self.entity, table, **kwargs) 
     110        assign_mapper(session.context, self.entity, self.entity.table,  
     111                      **kwargs) 
    147112        elixir.metadatas.add(self.metadata) 
    148113     
     
    157122                col = desc(col) 
    158123            order.append(col) 
    159              
    160124        return order 
    161125 
     
    183147            kwargs['autoload'] = True 
    184148         
    185         table = Table(self.tablename, self.metadata, *args, **kwargs) 
    186         self.entity.table = table 
    187         return table 
     149        self.entity.table = Table(self.tablename, self.metadata,  
     150                                  *args, **kwargs) 
    188151     
    189152    def create_auto_primary_key(self): 
     
    232195                    raise Exception( 
    233196                            "Several relations match as inverse of the '%s' " 
    234                             "relation in class '%s'. You should specify " 
     197                            "relation in entity '%s'. You should specify " 
    235198                            "inverse relations manually by using the inverse " 
    236199                            "keyword." 
     
    246209        return matching_rel 
    247210 
    248  
    249211    @classmethod 
    250212    def setup_relationships(cls): 
     
    253215                EntityDescriptor.uninitialized_rels.remove(relationship) 
    254216 
     217 
     218class Entity(object): 
     219    ''' 
     220    The base class for all entities 
     221     
     222    All Elixir model objects should inherit from this class. Statements can 
     223    appear within the body of the definition of an entity to define its 
     224    fields, relationships, and other options. 
     225     
     226    Here is an example: 
     227 
     228    :: 
     229     
     230        class Person(Entity): 
     231            has_field('name', Unicode(128)) 
     232            has_field('birthdate', DateTime, default=datetime.now) 
     233     
     234    Please note, that if you don't specify any primary keys, Elixir will 
     235    automatically create one called ``id``. 
     236     
     237    For further information, please refer to the provided examples or 
     238    tutorial. 
     239    ''' 
     240     
     241    class __metaclass__(type): 
     242        def __init__(cls, name, bases, dict_): 
     243            # only process subclasses of Entity, not Entity itself 
     244            if bases[0] is object: 
     245                return 
     246             
     247            # create the entity descriptor 
     248            desc = cls._descriptor = EntityDescriptor(cls) 
     249            EntityDescriptor.current = desc 
     250             
     251            # process statements 
     252            Statement.process(cls) 
     253             
     254            # setup misc options here (like tablename etc.) 
     255            desc.setup_options() 
     256             
     257            # create table & assign (empty) mapper 
     258            desc.setup() 
     259