This file is indexed.

/usr/share/pyshared/elixir/ext/list.py is in python-elixir 0.7.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'''
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:

.. sourcecode:: python

    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:

.. sourcecode:: python

    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.
'''

from elixir.statements import Statement
from elixir.events import before_insert, before_delete
from sqlalchemy import Column, Integer, select, func, literal, and_
import warnings

__all__ = ['acts_as_list']
__doc_all__ = []


def get_entity_where(instance):
    clauses = []
    for column in instance.table.primary_key.columns:
        instance_value = getattr(instance, column.name)
        clauses.append(column == instance_value)
    return and_(*clauses)


class ListEntityBuilder(object):

    def __init__(self, entity, qualifier=None, column_name='position'):
        warnings.warn("The act_as_list extension is deprecated. Please use "
                      "SQLAlchemy's orderinglist extension instead",
                      DeprecationWarning, stacklevel=6)
        self.entity = entity
        self.qualifier_method = qualifier
        self.column_name = column_name

    def create_non_pk_cols(self):
        if self.entity._descriptor.autoload:
            for c in self.entity.table.c:
                if c.name == self.column_name:
                    self.position_column = c
            if not hasattr(self, 'position_column'):
                raise Exception(
                    "Could not find column '%s' in autoloaded table '%s', "
                    "needed by entity '%s'." % (self.column_name,
                        self.entity.table.name, self.entity.__name__))
        else:
            self.position_column = Column(self.column_name, Integer)
            self.entity._descriptor.add_column(self.position_column)

    def after_table(self):
        position_column = self.position_column
        position_column_name = self.column_name

        qualifier_method = self.qualifier_method
        if not qualifier_method:
            qualifier_method = lambda self: None

        def _init_position(self):
            s = select(
                [(func.max(position_column)+1).label('value')],
                qualifier_method(self)
            ).union(
                select([literal(1).label('value')])
            )
            a = s.alias()
            # we use a second func.max to get the maximum between 1 and the
            # real max position if any exist
            setattr(self, position_column_name, select([func.max(a.c.value)]))

            # Note that this method could be rewritten more simply like below,
            # but because this extension is going to be deprecated anyway,
            # I don't want to risk breaking something I don't want to maintain.
#            setattr(self, position_column_name, select(
#                [func.coalesce(func.max(position_column), 0) + 1],
#                qualifier_method(self)
#            ))
        _init_position = before_insert(_init_position)

        def _shift_items(self):
            self.table.update(
                and_(
                    position_column > getattr(self, position_column_name),
                    qualifier_method(self)
                ),
                values={
                    position_column : position_column - 1
                }
            ).execute()
        _shift_items = before_delete(_shift_items)

        def move_to_bottom(self):
            # move the items that were above this item up one
            self.table.update(
                and_(
                    position_column >= getattr(self, position_column_name),
                    qualifier_method(self)
                ),
                values = {
                    position_column : position_column - 1
                }
            ).execute()

            # move this item to the max position
            # MySQL does not support the correlated subquery, so we need to
            # execute the query (through scalar()). See ticket #34.
            self.table.update(
                get_entity_where(self),
                values={
                    position_column : select(
                        [func.max(position_column) + 1],
                        qualifier_method(self)
                    ).scalar()
                }
            ).execute()

        def move_to_top(self):
            self.move_to(1)

        def move_to(self, position):
            current_position = getattr(self, position_column_name)

            # determine which direction we're moving
            if position < current_position:
                where = and_(
                    position <= position_column,
                    position_column < current_position,
                    qualifier_method(self)
                )
                modifier = 1
            elif position > current_position:
                where = and_(
                    current_position < position_column,
                    position_column <= position,
                    qualifier_method(self)
                )
                modifier = -1

            # shift the items in between the current and new positions
            self.table.update(where, values = {
                position_column : position_column + modifier
            }).execute()

            # update this item's position to the desired position
            self.table.update(get_entity_where(self)) \
                      .execute(**{position_column_name: position})

        def move_lower(self):
            # replace for ex.: p.todos.insert(x + 1, p.todos.pop(x))
            self.move_to(getattr(self, position_column_name) + 1)

        def move_higher(self):
            self.move_to(getattr(self, position_column_name) - 1)


        # attach new methods to entity
        self.entity._init_position = _init_position
        self.entity._shift_items = _shift_items
        self.entity.move_lower = move_lower
        self.entity.move_higher = move_higher
        self.entity.move_to_bottom = move_to_bottom
        self.entity.move_to_top = move_to_top
        self.entity.move_to = move_to


acts_as_list = Statement(ListEntityBuilder)