Changeset 293

Show
Ignore:
Timestamp:
01/15/08 17:15:43 (5 years ago)
Author:
cleverdevil
Message:

Now you can specify a column name for the position column in acts_as_list.

Location:
elixir/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/ext/list.py

    r292 r293  
    66Once you flag an entity with an `acts_as_list()` statement, a column will be 
    77added to the entity called `position` which will be an integer column that is 
    8 managed for you by the plugin. In addition, your entity will get a series of new 
    9 methods attached to it, including: 
     8managed for you by the plugin.  You can pass an alternative column name to  
     9the plugin using the `column_name` keyword argument. 
     10 
     11In addition, your entity will get a series of new methods attached to it, 
     12including: 
    1013 
    1114+----------------------+------------------------------------------------------+ 
     
    4346            return ToDo.owner_id == self.owner_id 
    4447 
    45         acts_as_list(qualify) 
     48        acts_as_list(qualifier=qualify) 
    4649 
    4750    class Person(Entity): 
     
    9598class ListEntityBuilder(object): 
    9699     
    97     def __init__(self, entity, qualifier_method=None): 
     100    def __init__(self, entity, qualifier=None, column_name='position'): 
    98101        self.entity = entity 
    99         self.qualifier_method = qualifier_method 
     102        self.qualifier_method = qualifier 
     103        self.column_name = column_name 
    100104     
    101105    def create_non_pk_cols(self): 
    102         self.entity._descriptor.add_column(Column('position', Integer)) 
     106        self.position_column = Column(self.column_name, Integer) 
     107        self.entity._descriptor.add_column(self.position_column) 
    103108     
    104109    def after_table(self): 
     110        position_column = self.position_column 
     111        position_column_name = self.column_name 
     112         
    105113        qualifier_method = self.qualifier_method  
    106114        if not qualifier_method: 
    107             qualifier_method = lambda self: self.position==self.position 
     115            qualifier_method = lambda self: None 
    108116         
    109117        @before_insert 
    110118        def _init_position(self): 
    111119            s = select( 
    112                 [(func.max(self.table.c.position)+1).label('value')], 
     120                [(func.max(position_column)+1).label('value')], 
    113121                qualifier_method(self) 
    114122            ).union( 
    115123                select([literal(1).label('value')]) 
    116124            ) 
    117             self.position = select([func.max(s.c.value)]) 
     125            setattr(self, position_column_name, select([func.max(s.c.value)])) 
    118126         
    119127        @before_delete 
     
    121129            self.table.update( 
    122130                and_( 
    123                     self.table.c.position > self.position, 
     131                    position_column > getattr(self, position_column_name), 
    124132                    qualifier_method(self) 
    125133                ), 
    126134                values={ 
    127                     self.table.c.position : self.table.c.position - 1 
    128                 } 
    129             ).execute() 
    130              
     135                    position_column : position_column - 1 
     136                } 
     137            ).execute() 
     138         
    131139        def move_to_bottom(self):         
    132140            # move the items that were above this item up one 
    133141            self.table.update( 
    134142                and_( 
    135                     self.table.c.position >= self.position, 
     143                    position_column >= getattr(self, position_column_name), 
    136144                    qualifier_method(self) 
    137145                ), 
    138146                values = { 
    139                     self.table.c.position : self.table.c.position - 1 
     147                    position_column : position_column - 1 
    140148                } 
    141149            ).execute() 
     
    145153                get_entity_where(self), 
    146154                values={ 
    147                     self.table.c.position : select( 
    148                         [func.max(self.table.c.position)+1], 
     155                    position_column : select( 
     156                        [func.max(position_column) + 1], 
    149157                        qualifier_method(self) 
    150158                    ) 
     
    152160            ).execute() 
    153161             
    154              
    155162        def move_to_top(self): 
    156163            # move the items that were above this item down one 
    157164            self.table.update( 
    158165                and_( 
    159                     self.table.c.position <= self.position, 
     166                    position_column <= getattr(self, position_column_name), 
    160167                    qualifier_method(self) 
    161168                ), 
    162169                values = { 
    163                     self.table.c.position : self.table.c.position + 1 
     170                    position_column : position_column + 1 
    164171                } 
    165172            ).execute() 
    166173 
    167174            # move this item to the first position 
    168             self.table.update(get_entity_where(self)).execute(position=1) 
    169              
    170          
     175            self.table.update(get_entity_where(self)).execute(**{position_column_name:1}) 
     176             
    171177        def move_to(self, position): 
     178            current_position = getattr(self, position_column_name) 
     179             
    172180            # determine which direction we're moving 
    173             if position < self.position: 
     181            if position < current_position: 
    174182                where = and_( 
    175                     position <= self.table.c.position, 
    176                     self.table.c.position < self.position, 
     183                    position <= position_column, 
     184                    position_column < current_position, 
    177185                    qualifier_method(self) 
    178186                ) 
    179187                modifier = 1 
    180             elif position > self.position: 
     188            elif position > current_position: 
    181189                where = and_( 
    182                     self.position < self.table.c.position, 
    183                     self.table.c.position <= position, 
     190                    current_position < position_column, 
     191                    position_column <= position, 
    184192                    qualifier_method(self) 
    185193                ) 
     
    188196            # shift the items in between the current and new positions 
    189197            self.table.update(where, values = { 
    190                     self.table.c.position : self.table.c.position + modifier 
     198                position_column : position_column + modifier 
    191199            }).execute() 
    192200             
    193201            # update this item's position to the desired position 
    194             self.table.update(get_entity_where(self)).execute(position=position) 
    195          
    196          
    197         def move_lower(self): self.move_to(self.position+1) 
    198         def move_higher(self): self.move_to(self.position-1) 
    199          
    200          
     202            self.table.update(get_entity_where(self)).execute(**{position_column_name:position}) 
     203         
     204        def move_lower(self):  
     205            self.move_to(getattr(self, position_column_name)+1) 
     206         
     207        def move_higher(self):  
     208            self.move_to(getattr(self, position_column_name)-1) 
     209         
     210         
     211        # attach new methods to entity 
    201212        self.entity._init_position = _init_position 
    202213        self.entity._shift_items = _shift_items 
  • elixir/trunk/tests/test_acts_as_list.py

    r288 r293  
    1111        def qualify(self): 
    1212            return ToDo.owner_id == self.owner_id 
    13  
    14         acts_as_list(qualify) 
     13             
     14        acts_as_list(qualifier=qualify, column_name='position') 
    1515         
    1616        def __repr__(self):