Changeset 338

Show
Ignore:
Timestamp:
06/13/08 18:35:55 (7 years ago)
Author:
ged
Message:

* Added setattr method on Metaclass so that you can add properties

slightly more easily after class definition (but before setup_all):

class A(Entity):

pass

A.name = Field(String(32))

Location:
elixir/trunk
Files:
2 modified

Legend:

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

    r337 r338  
    726726            elixir.setup_all() 
    727727        return type.__call__(cls, *args, **kwargs) 
     728 
     729    def __setattr__(cls, key, value): 
     730        if isinstance(value, Property): 
     731            if hasattr(cls, '_setup_done'): 
     732                raise Exception('Cannot set attribute on a class after ' 
     733                                'setup_all') 
     734            else: 
     735                value.attach(cls, key) 
     736        else: 
     737            type.__setattr__(cls, key, value) 
    728738 
    729739 
  • elixir/trunk/tests/test_properties.py

    r327 r338  
    198198        assert p.primary_email == 'new@z.com' 
    199199 
     200    def test_setattr(self): 
     201        class A(Entity): 
     202            pass 
     203 
     204        A.name = Field(String(30)) 
     205 
     206        setup_all(True) 
     207 
     208        a1 = A(name='a1') 
     209 
     210        session.flush(); session.clear() 
     211 
     212        a = A.query.one() 
     213 
     214        assert a.name == 'a1' 
     215