Changeset 403

Show
Ignore:
Timestamp:
09/15/08 15:39:23 (5 years ago)
Author:
ged
Message:

- Moved all methods of the "Entity" base class, to the "EntityBase" class, so

that people who want to provide their own base class but don't want to loose
all the methods provided by "Entity" can simply inherit from EntityBase
instead of copy-pasting the code for all its methods.

- Removed unused imports
- Fixed bug which broke the "identity" option

Files:
1 modified

Legend:

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

    r400 r403  
    44''' 
    55 
    6 from py23compat import set, rsplit, sorted 
     6from py23compat import sorted 
    77 
    88import sys 
    99import inspect 
    10 import warnings 
    1110from copy import copy 
    1211 
     
    1413from sqlalchemy     import Table, Column, Integer, desc, ForeignKey, and_, \ 
    1514                           ForeignKeyConstraint 
    16 from sqlalchemy.orm import Query, MapperExtension, mapper, object_session, \ 
     15from sqlalchemy.orm import MapperExtension, mapper, object_session, \ 
    1716                           EXT_CONTINUE, polymorphic_union, ScopedSession, \ 
    1817                           ColumnProperty 
     
    131130                #TODO: include module name 
    132131                self.identity = entity.__name__.lower() 
    133         elif 'polymorphic_identity' in kwargs: 
     132        elif 'polymorphic_identity' in self.mapper_options: 
    134133            raise Exception('You cannot use the "identity" option and the ' 
    135134                            'polymorphic_identity mapper option at the same ' 
     
    424423        else: 
    425424            raise Exception("Failed to map entity '%s' with its table or " 
    426                             "selectable" % self.entity.__name__) 
     425                            "selectable. You can only bind an Entity to a " 
     426                            "ScopedSession object or None." 
     427                            % self.entity.__name__) 
    427428 
    428429    def after_mapper(self): 
     
    875876        desc.properties = {} 
    876877 
    877  
    878 class Entity(object): 
    879     ''' 
    880     The base class for all entities 
    881  
    882     All Elixir model objects should inherit from this class. Statements can 
    883     appear within the body of the definition of an entity to define its 
    884     fields, relationships, and other options. 
    885  
    886     Here is an example: 
     878class EntityBase(object): 
     879    """ 
     880    This class holds all methods of the "Entity" base class, but does not act 
     881    as a base class itself (it does not use the EntityMeta metaclass), but 
     882    rather as a parent class for Entity. This is meant so that people who want 
     883    to provide their own base class but don't want to loose or copy-paste all 
     884    the methods of Entity can do so by inheriting from EntityBase: 
    887885 
    888886    .. sourcecode:: python 
    889887 
    890         class Person(Entity): 
    891             name = Field(Unicode(128)) 
    892             birthdate = Field(DateTime, default=datetime.now) 
    893  
    894     Please note, that if you don't specify any primary keys, Elixir will 
    895     automatically create one called ``id``. 
    896  
    897     For further information, please refer to the provided examples or 
    898     tutorial. 
    899     ''' 
    900     __metaclass__ = EntityMeta 
     888        class MyBase(EntityBase): 
     889            __metaclass__ = EntityMeta 
     890 
     891            def myCustomMethod(self): 
     892                # do something great 
     893    """ 
    901894 
    902895    def __init__(self, **kwargs): 
     
    10321025    get = classmethod(get) 
    10331026 
     1027 
     1028class Entity(EntityBase): 
     1029    ''' 
     1030    The base class for all entities 
     1031 
     1032    All Elixir model objects should inherit from this class. Statements can 
     1033    appear within the body of the definition of an entity to define its 
     1034    fields, relationships, and other options. 
     1035 
     1036    Here is an example: 
     1037 
     1038    .. sourcecode:: python 
     1039 
     1040        class Person(Entity): 
     1041            name = Field(Unicode(128)) 
     1042            birthdate = Field(DateTime, default=datetime.now) 
     1043 
     1044    Please note, that if you don't specify any primary keys, Elixir will 
     1045    automatically create one called ``id``. 
     1046 
     1047    For further information, please refer to the provided examples or 
     1048    tutorial. 
     1049    ''' 
     1050    __metaclass__ = EntityMeta 
     1051 
     1052