Module: list

This extension is DEPRECATED. Please use the orderinglist SQLAlchemy extension instead.

For details: http://www.sqlalchemy.org/docs/05/reference/ext/orderinglist.html

For an Elixir example: http://elixir.ematia.de/trac/wiki/Recipes/UsingEntityForOrderedList or http://elixir.ematia.de/trac/browser/elixir/0.7.0/tests/test_o2m.py#L155

An ordered-list plugin for Elixir to help you make an entity be able to be managed in a list-like way. Much inspiration comes from the Ruby on Rails acts_as_list plugin, which is currently more full-featured than this plugin.

Once you flag an entity with an acts_as_list() statement, a column will be added to the entity called position which will be an integer column that is managed for you by the plugin. You can pass an alternative column name to the plugin using the column_name keyword argument.

In addition, your entity will get a series of new methods attached to it, including:

Method Name Description
move_lower Move the item lower in the list
move_higher Move the item higher in the list
move_to_bottom Move the item to the bottom of the list
move_to_top Move the item to the top of the list
move_to Move the item to a specific position in the list

Sometimes, your entities that represent list items will be a part of different lists. To implement this behavior, simply pass the acts_as_list statement a callable that returns a "qualifier" SQLAlchemy expression. This expression will be added to the generated WHERE clauses used by the plugin.

Example model usage:

from elixir import *
from elixir.ext.list import acts_as_list

class ToDo(Entity):
    subject = Field(String(128))
    owner = ManyToOne('Person')

    def qualify(self):
        return ToDo.owner_id == self.owner_id

    acts_as_list(qualifier=qualify)

class Person(Entity):
    name = Field(String(64))
    todos = OneToMany('ToDo', order_by='position')

The above example can then be used to manage ordered todo lists for people. Note that you must set the order_by property on the Person.todo relation in order for the relation to respect the ordering. Here is an example of using this model in practice:

p = Person.query.filter_by(name='Jonathan').one()
p.todos.append(ToDo(subject='Three'))
p.todos.append(ToDo(subject='Two'))
p.todos.append(ToDo(subject='One'))
session.commit(); session.clear()

p = Person.query.filter_by(name='Jonathan').one()
p.todos[0].move_to_bottom()
p.todos[2].move_to_top()
session.commit(); session.clear()

p = Person.query.filter_by(name='Jonathan').one()
assert p.todos[0].subject == 'One'
assert p.todos[1].subject == 'Two'
assert p.todos[2].subject == 'Three'

For more examples, refer to the unit tests for this plugin.