Changeset 96

Show
Ignore:
Timestamp:
03/27/07 11:15:20 (6 years ago)
Author:
ged
Message:

- The tablename option can now be given a callable so that people can provide

their own function to get the table name for an entity. The tablename option
can now also be set globally (using the options_defaults dictionary). Of
course, this only makes sense for the callable usecase.

Location:
elixir/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/CHANGES

    r95 r96  
    3131  This shouldn't change anything to how it's used but allowed me to factor 
    3232  some code with has_and_belongs_to_many relationships. 
     33- The tablename option can now be given a callable so that people can provide 
     34  their own function to get the table name for an entity. The tablename option 
     35  can now also be set globally (using the options_defaults dictionary). Of 
     36  course, this only makes sense for the callable usecase. 
     37 
    3338- Fixed bug preventing having entities without any statement. 
    3439- Fixed documentation for belongs_to relationships (the arguemnt is "required", 
     
    4045- Actually make the code python 2.3 compatible (Robin's patch was based on 
    4146  0.1.0 while I had introduced more decorators in the trunk in the mean time). 
     47 
    4248- Made some PEP8 tweaks in many places. Used the pep8 script provided with  
    4349  Cheesecake. 
  • elixir/trunk/elixir/entity.py

    r94 r96  
    6565        # set default value for options 
    6666        self.order_by = None 
    67         self.tablename = None 
    6867        self.table_args = list() 
    6968        self.metadata = getattr(self.module, 'metadata', elixir.metadata) 
    7069 
    7170        for option in ('inheritance',  
    72                        'autoload', 'shortnames', 'auto_primarykey', 
     71                       'autoload', 'tablename', 'shortnames',  
     72                       'auto_primarykey', 
    7373                       'version_id_col'): 
    7474            setattr(self, option, options_defaults[option]) 
     
    9393                tablename = "%s_%s" % (modulename, entity.__name__) 
    9494                self.tablename = tablename.lower() 
     95        elif callable(self.tablename): 
     96            self.tablename = self.tablename(entity) 
    9597     
    9698    def setup(self): 
  • elixir/trunk/elixir/options.py

    r83 r96  
    4545|                     | entities.                                             | 
    4646+---------------------+-------------------------------------------------------+ 
    47 | ``tablename``       | Specify a custom tablename                            | 
     47| ``tablename``       | Specify a custom tablename. You can either provide a  | 
     48|                     | plain string or a callable. The callable will be      | 
     49|                     | given the entity (ie class) as argument and must      | 
     50|                     | return a string representing the name of the table    | 
     51|                     | for that entity.                                      | 
    4852+---------------------+-------------------------------------------------------+ 
    4953| ``shortnames``      | Usually tablenames include the full module-path       | 
     
    117121    autoload=None, 
    118122    shortnames=False, 
     123    tablename=None, 
    119124    auto_primarykey=True, 
    120125    version_id_col=False, 
  • elixir/trunk/tests/test_options.py

    r83 r96  
    5454            pass 
    5555 
     56    def test_tablename_func(self): 
     57        import re 
     58 
     59        def camel_to_underscore(entity): 
     60            return re.sub(r'(.+?)([A-Z])+?', r'\1_\2', entity.__name__).lower() 
     61 
     62        options_defaults['tablename'] = camel_to_underscore 
     63 
     64        class MyEntity(Entity): 
     65            has_field('name', Unicode(30)) 
     66 
     67        class MySuperTestEntity(Entity): 
     68            has_field('name', Unicode(30)) 
     69 
     70        assert MyEntity.table.name == 'my_entity' 
     71        assert MySuperTestEntity.table.name == 'my_super_test_entity' 
     72 
     73        options_defaults['tablename'] = None 
     74 
    5675    def teardown(self): 
    5776        cleanup_all()