Changeset 293
- Timestamp:
- 01/15/08 17:15:43 (5 years ago)
- Location:
- elixir/trunk
- Files:
-
- 2 modified
-
elixir/ext/list.py (modified) (7 diffs)
-
tests/test_acts_as_list.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/elixir/ext/list.py
r292 r293 6 6 Once you flag an entity with an `acts_as_list()` statement, a column will be 7 7 added 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: 8 managed for you by the plugin. You can pass an alternative column name to 9 the plugin using the `column_name` keyword argument. 10 11 In addition, your entity will get a series of new methods attached to it, 12 including: 10 13 11 14 +----------------------+------------------------------------------------------+ … … 43 46 return ToDo.owner_id == self.owner_id 44 47 45 acts_as_list(qualif y)48 acts_as_list(qualifier=qualify) 46 49 47 50 class Person(Entity): … … 95 98 class ListEntityBuilder(object): 96 99 97 def __init__(self, entity, qualifier _method=None):100 def __init__(self, entity, qualifier=None, column_name='position'): 98 101 self.entity = entity 99 self.qualifier_method = qualifier_method 102 self.qualifier_method = qualifier 103 self.column_name = column_name 100 104 101 105 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) 103 108 104 109 def after_table(self): 110 position_column = self.position_column 111 position_column_name = self.column_name 112 105 113 qualifier_method = self.qualifier_method 106 114 if not qualifier_method: 107 qualifier_method = lambda self: self.position==self.position115 qualifier_method = lambda self: None 108 116 109 117 @before_insert 110 118 def _init_position(self): 111 119 s = select( 112 [(func.max( self.table.c.position)+1).label('value')],120 [(func.max(position_column)+1).label('value')], 113 121 qualifier_method(self) 114 122 ).union( 115 123 select([literal(1).label('value')]) 116 124 ) 117 se lf.position = select([func.max(s.c.value)])125 setattr(self, position_column_name, select([func.max(s.c.value)])) 118 126 119 127 @before_delete … … 121 129 self.table.update( 122 130 and_( 123 self.table.c.position > self.position,131 position_column > getattr(self, position_column_name), 124 132 qualifier_method(self) 125 133 ), 126 134 values={ 127 self.table.c.position : self.table.c.position - 1128 } 129 ).execute() 130 135 position_column : position_column - 1 136 } 137 ).execute() 138 131 139 def move_to_bottom(self): 132 140 # move the items that were above this item up one 133 141 self.table.update( 134 142 and_( 135 self.table.c.position >= self.position,143 position_column >= getattr(self, position_column_name), 136 144 qualifier_method(self) 137 145 ), 138 146 values = { 139 self.table.c.position : self.table.c.position - 1147 position_column : position_column - 1 140 148 } 141 149 ).execute() … … 145 153 get_entity_where(self), 146 154 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], 149 157 qualifier_method(self) 150 158 ) … … 152 160 ).execute() 153 161 154 155 162 def move_to_top(self): 156 163 # move the items that were above this item down one 157 164 self.table.update( 158 165 and_( 159 self.table.c.position <= self.position,166 position_column <= getattr(self, position_column_name), 160 167 qualifier_method(self) 161 168 ), 162 169 values = { 163 self.table.c.position : self.table.c.position + 1170 position_column : position_column + 1 164 171 } 165 172 ).execute() 166 173 167 174 # 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 171 177 def move_to(self, position): 178 current_position = getattr(self, position_column_name) 179 172 180 # determine which direction we're moving 173 if position < self.position:181 if position < current_position: 174 182 where = and_( 175 position <= self.table.c.position,176 self.table.c.position < self.position,183 position <= position_column, 184 position_column < current_position, 177 185 qualifier_method(self) 178 186 ) 179 187 modifier = 1 180 elif position > self.position:188 elif position > current_position: 181 189 where = and_( 182 self.position < self.table.c.position,183 self.table.c.position <= position,190 current_position < position_column, 191 position_column <= position, 184 192 qualifier_method(self) 185 193 ) … … 188 196 # shift the items in between the current and new positions 189 197 self.table.update(where, values = { 190 self.table.c.position : self.table.c.position + modifier198 position_column : position_column + modifier 191 199 }).execute() 192 200 193 201 # 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 201 212 self.entity._init_position = _init_position 202 213 self.entity._shift_items = _shift_items -
elixir/trunk/tests/test_acts_as_list.py
r288 r293 11 11 def qualify(self): 12 12 return ToDo.owner_id == self.owner_id 13 14 acts_as_list(qualif y)13 14 acts_as_list(qualifier=qualify, column_name='position') 15 15 16 16 def __repr__(self):
