Changeset 303

Show
Ignore:
Timestamp:
02/07/08 15:57:55 (5 years ago)
Author:
ged
Message:
  • Added an alternate (nicer) syntax to define synonym properties. This syntax
    has a more limited scope, except that it can refer to properties defined in
    a parent entity. This is based on a patch from Alexandre da Silva.
Location:
elixir/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/CHANGES

    r302 r303  
    77- Added support for custom base classes which inherit from another class (ie 
    88  not directly from object). 
     9- Added an alternate (nicer) syntax to define synonym properties.  This syntax 
     10  has a more limited scope, except that it can refer to properties defined in  
     11  a parent entity. This is based on a patch from Alexandre da Silva. 
    912 
    1013Changes: 
  • elixir/trunk/elixir/__init__.py

    r296 r303  
    3232                                 has_and_belongs_to_many, \ 
    3333                                 ManyToOne, OneToOne, OneToMany, ManyToMany 
    34 from elixir.properties import has_property, GenericProperty, ColumnProperty 
     34from elixir.properties import has_property, GenericProperty, ColumnProperty, \ 
     35                              Synonym 
    3536from elixir.statements import Statement 
    3637 
     
    3940 
    4041__all__ = ['Entity', 'EntityMeta', 
    41            'Field', 'has_field', 'with_fields', 
    42            'has_property', 'GenericProperty', 'ColumnProperty', 
     42           'Field', 'has_field', 'with_fields',  
     43           'has_property', 'GenericProperty', 'ColumnProperty', 'Synonym', 
    4344           'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many', 
    4445           'ManyToOne', 'OneToOne', 'OneToMany', 'ManyToMany', 
     
    4647           'options_defaults', 'metadata', 'objectstore', 'session', 
    4748           'create_all', 'drop_all', 
    48            'setup_all', 'cleanup_all',  
     49           'setup_all', 'cleanup_all', 
    4950           'setup_entities', 'cleanup_entities'] + \ 
    5051           sqlalchemy.types.__all__ 
  • elixir/trunk/elixir/fields.py

    r267 r303  
    6767| ``synonym``       | Specify a synonym name for this field. The field will   | 
    6868|                   | also be usable under that name in keyword-based Query   | 
    69 |                   | functions such as filter_by.                            | 
     69|                   | functions such as filter_by. The Synonym class (see the | 
     70|                   | `properties` module) provides a similar functionality   | 
     71|                   | with an (arguably) nicer syntax, but a limited scope.   | 
    7072+-------------------+---------------------------------------------------------+ 
    7173 
  • elixir/trunk/elixir/properties.py

    r269 r303  
    44properties such as fields and relationships (for those, please consult the  
    55corresponding modules), but also provides some more specialized properties,  
    6 such as `ColumnProperty`. It also provides the GenericProperty class which  
    7 allows you to wrap any SQLAlchemy property, and its DSL-syntax equivalent:  
    8 has_property_. 
     6such as `ColumnProperty` and `Synonym`. It also provides the GenericProperty  
     7class which allows you to wrap any SQLAlchemy property, and its DSL-syntax  
     8equivalent: has_property_. 
    99 
    1010`has_property` 
     
    3232 
    3333from elixir.statements import PropertyStatement 
    34 from sqlalchemy.orm import column_property 
     34from sqlalchemy.orm import column_property, synonym 
    3535 
    3636__doc_all__ = ['EntityBuilder', 'Property', 'GenericProperty',  
     
    150150    def evaluate_property(self, prop): 
    151151        return prop 
     152 
    152153 
    153154class ColumnProperty(GenericProperty): 
     
    179180        return column_property(prop.label(self.name)) 
    180181 
     182 
     183class Synonym(GenericProperty): 
     184    ''' 
     185    This class represents a synonym property of another property (column, ...) 
     186    of an entity.  As opposed to the `synonym` kwarg to the Field class (which  
     187    share the same goal), this class can be used to define a synonym of a  
     188    property defined in a parent class (of the current class). On the other 
     189    hand, it cannot define a synonym for the purpose of using a standard python  
     190    property in queries. See the Field class for details on that usage. 
     191 
     192    .. sourcecode:: python 
     193 
     194    class Person(Entity): 
     195        name = Field(String(30)) 
     196        primary_email = Field(String(100)) 
     197        email_address = Synonym('primary_email') 
     198 
     199    class User(Person): 
     200        user_name = Synonym('name') 
     201        password = Field(String(20)) 
     202    ''' 
     203 
     204    def evaluate_property(self, prop): 
     205        return synonym(prop) 
     206 
    181207#class Composite(GenericProperty): 
    182208#    def __init__(self, prop): 
  • elixir/trunk/tests/test_properties.py

    r275 r303  
    162162        assert p.name == 'Mr. X' 
    163163 
     164    def test_synonym_class(self): 
     165        class Person(Entity): 
     166            name = Field(String(30)) 
     167            primary_email = Field(String(100)) 
     168            email_address = Synonym('primary_email') 
     169 
     170        class User(Person): 
     171            user_name = Synonym('name') 
     172            password = Field(String(20)) 
     173 
     174        setup_all(True) 
     175 
     176        alexandre = Person( 
     177            name = u'Alexandre da Silva', 
     178            email_address = u'x@y.com' 
     179        ) 
     180        johann = User( 
     181            name = 'Johann Felipe Voigt', 
     182            email_address = 'y@z.com', 
     183            password = 'unencrypted' 
     184        ) 
     185 
     186        session.flush(); session.clear() 
     187 
     188        p = Person.get_by(name='Alexandre da Silva') 
     189        assert p.primary_email == 'x@y.com' 
     190 
     191        u = User.get_by(user_name='Johann Felipe Voigt') 
     192        assert u.email_address == 'y@z.com' 
     193 
     194        u.email_address = 'new@z.com' 
     195        session.flush(); session.clear() 
     196 
     197        p = Person.get_by(name='Johann Felipe Voigt') 
     198        assert p.primary_email == 'new@z.com' 
     199