Changeset 303
- Timestamp:
- 02/07/08 15:57:55 (5 years ago)
- Location:
- elixir/trunk
- Files:
-
- 5 modified
-
CHANGES (modified) (1 diff)
-
elixir/__init__.py (modified) (3 diffs)
-
elixir/fields.py (modified) (1 diff)
-
elixir/properties.py (modified) (4 diffs)
-
tests/test_properties.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/CHANGES
r302 r303 7 7 - Added support for custom base classes which inherit from another class (ie 8 8 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. 9 12 10 13 Changes: -
elixir/trunk/elixir/__init__.py
r296 r303 32 32 has_and_belongs_to_many, \ 33 33 ManyToOne, OneToOne, OneToMany, ManyToMany 34 from elixir.properties import has_property, GenericProperty, ColumnProperty 34 from elixir.properties import has_property, GenericProperty, ColumnProperty, \ 35 Synonym 35 36 from elixir.statements import Statement 36 37 … … 39 40 40 41 __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', 43 44 'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many', 44 45 'ManyToOne', 'OneToOne', 'OneToMany', 'ManyToMany', … … 46 47 'options_defaults', 'metadata', 'objectstore', 'session', 47 48 'create_all', 'drop_all', 48 'setup_all', 'cleanup_all', 49 'setup_all', 'cleanup_all', 49 50 'setup_entities', 'cleanup_entities'] + \ 50 51 sqlalchemy.types.__all__ -
elixir/trunk/elixir/fields.py
r267 r303 67 67 | ``synonym`` | Specify a synonym name for this field. The field will | 68 68 | | 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. | 70 72 +-------------------+---------------------------------------------------------+ 71 73 -
elixir/trunk/elixir/properties.py
r269 r303 4 4 properties such as fields and relationships (for those, please consult the 5 5 corresponding modules), but also provides some more specialized properties, 6 such as `ColumnProperty` . It also provides the GenericProperty class which7 allows you to wrap any SQLAlchemy property, and its DSL-syntax equivalent:8 has_property_.6 such as `ColumnProperty` and `Synonym`. It also provides the GenericProperty 7 class which allows you to wrap any SQLAlchemy property, and its DSL-syntax 8 equivalent: has_property_. 9 9 10 10 `has_property` … … 32 32 33 33 from elixir.statements import PropertyStatement 34 from sqlalchemy.orm import column_property 34 from sqlalchemy.orm import column_property, synonym 35 35 36 36 __doc_all__ = ['EntityBuilder', 'Property', 'GenericProperty', … … 150 150 def evaluate_property(self, prop): 151 151 return prop 152 152 153 153 154 class ColumnProperty(GenericProperty): … … 179 180 return column_property(prop.label(self.name)) 180 181 182 183 class 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 181 207 #class Composite(GenericProperty): 182 208 # def __init__(self, prop): -
elixir/trunk/tests/test_properties.py
r275 r303 162 162 assert p.name == 'Mr. X' 163 163 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
