sqlalchemy_mptt package

Events

Base events

SQLAlchemy events extension

sqlalchemy_mptt.events.mptt_before_insert(mapper, connection, instance)[source]

Based on example https://bitbucket.org/zzzeek/sqlalchemy/src/73095b353124/examples/nested_sets/nested_sets.py?at=master

sqlalchemy_mptt.events.mptt_before_delete(mapper, connection, instance, delete=True)[source]
sqlalchemy_mptt.events.mptt_before_update(mapper, connection, instance)[source]

Based on this example: http://stackoverflow.com/questions/889527/move-node-in-nested-set

class sqlalchemy_mptt.events.TreesManager(base_class)[source]

Manages events dispatching for all subclasses of a given class.

Hidden method

sqlalchemy_mptt.events._insert_subtree(table, connection, node_size, node_pos_left, node_pos_right, parent_pos_left, parent_pos_right, subtree, parent_tree_id, parent_level, node_level, left_sibling, table_pk)[source]

Mixins

SQLAlchemy nested sets mixin

class sqlalchemy_mptt.mixins.BaseNestedSets[source]

Base mixin for MPTT model.

Example:

from sqlalchemy import Boolean, Column, create_engine, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from sqlalchemy_mptt.mixins import BaseNestedSets

Base = declarative_base()


class Tree(Base, BaseNestedSets):
    __tablename__ = "tree"

    id = Column(Integer, primary_key=True)
    visible = Column(Boolean)

    def __repr__(self):
        return "<Node (%s)>" % self.id
tree_id()

Represents a column in a database table.

parent_id
parent
left()

Represents a column in a database table.

right()

Represents a column in a database table.

level()

Represents a column in a database table.

drilldown_tree(session=None, json=False, json_fields=None)[source]

This method generate a branch from a tree, beginning with current node.

For example:

node7.drilldown_tree()
level           Nested sets example
1                    1(1)22       ---------------------
        _______________|_________|_________            |
       |               |         |         |           |
2    2(2)5           6(4)11      |      12(7)21        |
       |               ^         |         ^           |
3    3(3)4       7(5)8   9(6)10  | 13(8)16   17(10)20  |
                                 |    |          |     |
4                                | 14(9)15   18(11)19  |
                                 |                     |
                                  ---------------------

Example in tests:

  • sqlalchemy_mptt.tests.cases.get_tree.test_drilldown_tree

get_children(session=None)[source]

Returns a query containing the immediate children of this model instance, in tree order.

For example:

node7.get_children() #-> [Node(8), Node(10)]
level           Nested sets example

1                   1(1)22
        ______________|____________________
       |              |                    |
       |              |                    |
2    2(2)5          6(4)11              12(7)21
       |              ^                /       \             |
3    3(3)4      7(5)8   9(6)10        /         \            |
                                   13(8)16   17(10)20        |
                                      |         |            |
4                                  14(9)15   18(11)19        |
classmethod get_default_level()[source]

Compatibility with Django MPTT: level value for root node. See https://github.com/uralbash/sqlalchemy_mptt/issues/56

get_siblings(include_self=False, session=None)[source]

Creates a query containing siblings of this model instance. Root nodes are considered to be siblings of other root nodes.

For example:

node10.get_siblings() #-> [Node(8)]

Only one node is sibling of node10

level           Nested sets example

1                   1(1)22
        ______________|____________________
       |              |                    |
       |              |                    |
2    2(2)5          6(4)11              12(7)21
       |              ^                /       \            |
3    3(3)4      7(5)8   9(6)10        /         \           |
                                   13(8)16   17(10)20       |
                                      |         |           |
4                                  14(9)15   18(11)19       |
classmethod get_tree(session=None, json=False, json_fields=None, query=None)[source]

This method generate tree of current node table in dict or json format. You can make custom query with attribute query. By default it return all nodes in table.

Args:

session (sqlalchemy.orm.session.Session): SQLAlchemy session

Kwargs:

json (bool): if True return JSON jqTree format json_fields (function): append custom fields in JSON query (function): it takes sqlalchemy.orm.query.Query object as an argument, and returns in a modified form

def query(nodes):
    return nodes.filter(node.__class__.tree_id.is_(node.tree_id))

node.get_tree(session=session, json=True, query=query)

Example:

  • sqlalchemy_mptt.tests.cases.get_tree.test_get_tree

  • sqlalchemy_mptt.tests.cases.get_tree.test_get_json_tree

  • sqlalchemy_mptt.tests.cases.get_tree.test_get_json_tree_with_custom_field

is_ancestor_of(other, inclusive=False)[source]

class or instance level method which returns True if self is ancestor (closer to root) of other else False. Optional flag inclusive on whether or not to treat self as ancestor of self.

For example see:

  • sqlalchemy_mptt.tests.cases.integrity.test_hierarchy_structure

is_descendant_of(other, inclusive=False)[source]

class or instance level method which returns True if self is descendant (farther from root) of other else False. Optional flag inclusive on whether or not to treat self as descendant of self.

For example see:

  • sqlalchemy_mptt.tests.cases.integrity.test_hierarchy_structure

leftsibling_in_level()[source]

Node to the left of the current node at the same level

For example see sqlalchemy_mptt.tests.cases.get_tree.test_leftsibling_in_level

move_after(node_id)[source]

Moving one node of tree after another

For example see sqlalchemy_mptt.tests.cases.move_node.test_move_after_function

move_before(node_id)[source]

Moving one node of tree before another

For example see:

  • sqlalchemy_mptt.tests.cases.move_node.test_move_before_function

  • sqlalchemy_mptt.tests.cases.move_node.test_move_before_to_other_tree

  • sqlalchemy_mptt.tests.cases.move_node.test_move_before_to_top_level

move_inside(parent_id)[source]

Moving one node of tree inside another

For example see:

  • sqlalchemy_mptt.tests.cases.move_node.test_move_inside_function

  • sqlalchemy_mptt.tests.cases.move_node.test_move_inside_to_the_same_parent_function

path_to_root(session=None, order=<function desc>)[source]

Generate path from a leaf or intermediate node to the root.

For example:

node11.path_to_root()
level           Nested sets example

                 -----------------------------------------
1               |    1(1)22                               |
        ________|______|_____________________             |
       |        |      |                     |            |
       |         ------+---------            |            |
2    2(2)5           6(4)11      | --     12(7)21         |
       |               ^             |   /      \         |
3    3(3)4       7(5)8   9(6)10      ---/----    \        |
                                    13(8)16 |  17(10)20   |
                                       |    |     |       |
4                                   14(9)15 | 18(11)19    |
                                            |             |
                                             -------------
classmethod rebuild(session, tree_id=None)[source]

This function rebuild tree.

Args:

session (sqlalchemy.orm.session.Session): SQLAlchemy session

Kwargs:

tree_id (int or str): id of tree, default None

Example:

  • sqlalchemy_mptt.tests.TestTree.test_rebuild

classmethod rebuild_tree(session, tree_id)[source]

This method rebuild tree.

Args:

session (sqlalchemy.orm.session.Session): SQLAlchemy session tree_id (int or str): id of tree

Example:

  • sqlalchemy_mptt.tests.cases.get_tree.test_rebuild

Tests

test tree

class sqlalchemy_mptt.tests.test_events.Events(methodName='runTest')[source]

Bases: TestCase

test_register()[source]
test_register_and_remove()[source]
test_remove()[source]
class sqlalchemy_mptt.tests.test_events.InitialInsert(methodName='runTest')[source]

Bases: DatabaseSetupMixin, TestCase

Test case for initial insertion of node as specified in docs/initialize.rst

base

alias of Base

test_documented_initial_insert()[source]
class sqlalchemy_mptt.tests.test_events.TestTree(methodName='runTest')[source]

Bases: TreeTestingMixin, TestCase

base

alias of Base

model

alias of Tree

class sqlalchemy_mptt.tests.test_events.TestTreeWithCustomId(methodName='runTest')[source]

Bases: TreeTestingMixin, TestCase

base

alias of Base

model

alias of TreeWithCustomId

class sqlalchemy_mptt.tests.test_events.TestTreeWithCustomLevel(methodName='runTest')[source]

Bases: TreeTestingMixin, TestCase

base

alias of Base

model

alias of TreeWithCustomLevel

class sqlalchemy_mptt.tests.test_events.Tree(**kwargs)[source]

Bases: Base, BaseNestedSets

id
left
level
parent
parent_id
right
tree_id
visible
class sqlalchemy_mptt.tests.test_events.Tree0Id(methodName='runTest')[source]

Bases: DatabaseSetupMixin, TestCase

Test case where node id is provided and starts with 0

See comments in https://github.com/uralbash/sqlalchemy_mptt/issues/57

base

alias of Base

test()[source]
class sqlalchemy_mptt.tests.test_events.TreeWithCustomId(**kwargs)[source]

Bases: Base, BaseNestedSets

left
level
parent
parent_id
ppk
right
sqlalchemy_mptt_pk_name = 'ppk'
tree_id
visible
class sqlalchemy_mptt.tests.test_events.TreeWithCustomLevel(**kwargs)[source]

Bases: Base, BaseNestedSets

id
left
level
parent
parent_id
right
sqlalchemy_mptt_default_level = 0
tree_id
visible
class sqlalchemy_mptt.tests.test_inheritance.BaseInheritance(**kwargs)[source]

Bases: Base

ppk
type
visible
class sqlalchemy_mptt.tests.test_inheritance.GenericTree(**kwargs)[source]

Bases: Base, BaseNestedSets

left
level
parent
parent_id
ppk
right
sqlalchemy_mptt_pk_name = 'ppk'
tree_id
type
visible
class sqlalchemy_mptt.tests.test_inheritance.InheritanceTree(**kwargs)[source]

Bases: BaseInheritance, BaseNestedSets

left
level
parent
parent_id
ppk
right
sqlalchemy_mptt_pk_name = 'ppk'
tree_id
type
visible
class sqlalchemy_mptt.tests.test_inheritance.SpecializedTree(**kwargs)[source]

Bases: GenericTree

left
level
parent
parent_id
ppk
right
tree_id
type
visible
class sqlalchemy_mptt.tests.test_inheritance.TestGenericTree(methodName='runTest')[source]

Bases: TreeTestingMixin, TestCase

base

alias of Base

model

alias of GenericTree

class sqlalchemy_mptt.tests.test_inheritance.TestInheritanceTree(methodName='runTest')[source]

Bases: TreeTestingMixin, TestCase

base

alias of Base

model

alias of InheritanceTree

test_rebuild()[source]

Rebuild tree with tree_id==1

level      Nested sets w/o left & right (or broken left & right)
    1                     (1)
            _______________|___________________
           |               |                   |
    2     (2)             (4)                 (7)
           |               ^                   ^
    3     (3)          (5)   (6)           (8)   (10)
                                            |      |
    4                                      (9)   (11)


    level           Nested sets after rebuild
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
class sqlalchemy_mptt.tests.test_inheritance.TestSpecializedTree(methodName='runTest')[source]

Bases: TreeTestingMixin, TestCase

base

alias of Base

model

alias of SpecializedTree

test_rebuild()[source]

Rebuild tree with tree_id==1

level      Nested sets w/o left & right (or broken left & right)
    1                     (1)
            _______________|___________________
           |               |                   |
    2     (2)             (4)                 (7)
           |               ^                   ^
    3     (3)          (5)   (6)           (8)   (10)
                                            |      |
    4                                      (9)   (11)


    level           Nested sets after rebuild
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
class sqlalchemy_mptt.tests.test_inheritance.TestTree(methodName='runTest')[source]

Bases: DatabaseSetupMixin, TestCase

base

alias of Base

test_create_delete()[source]
test_create_generic()[source]
test_create_spec()[source]

test tree

class sqlalchemy_mptt.tests.test_mixins.TestMixin(methodName='runTest')[source]

Bases: TestCase

test_mixin_parent_id()[source]
class sqlalchemy_mptt.tests.test_mixins.Tree2(**kwargs)[source]

Bases: Base, BaseNestedSets

id
left
level
parent
parent_id
right
tree_id

Cases tests

class sqlalchemy_mptt.tests.cases.edit_node.Changes[source]

Bases: object

test_delete_node()[source]

Delete node(4) initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Test delete node
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19
level         Delete node == 4
1                    1(1)16
        _______________|_____
       |                     |
2    2(2)5                 6(7)15
       |                     ^
3    3(3)4            7(8)10   11(10)14
                        |          |
4                     8(9)9    12(11)13
test_insert_after_node()[source]
test_insert_node()[source]

Insert node with parent==6 initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19
level     Insert node with parent_id == 6
1                    1(1)24
        _______________|_________________
       |               |                 |
2    2(2)5           6(4)13           14(7)23
       |           ____|____          ___|____
       |          |         |        |        |
3    3(3)4      7(5)8    9(6)12  15(8)18   19(10)22
                           |        |         |
4                      10(23)11  16(9)17  20(11)21
test_insert_node_near_subtree()[source]

Insert node with parent==4 initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19
level     Insert node with parent_id == 4
1                    1(1)24
        _______________|_____________________
       |               |                     |
2    2(2)5           6(4)13               14(7)23
       |         ______|________           __|______
       |        |      |        |         |         |
3    3(3)4    7(5)8  9(6)10  11(23)12  15(8)18   19(10)22
                                          |         |
4                                      16(9)17   20(11)21
test_rebuild()[source]

Rebuild tree with tree_id==1

level      Nested sets w/o left & right (or broken left & right)
    1                     (1)
            _______________|___________________
           |               |                   |
    2     (2)             (4)                 (7)
           |               ^                   ^
    3     (3)          (5)   (6)           (8)   (10)
                                            |      |
    4                                      (9)   (11)


    level           Nested sets after rebuild
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
test_update_node()[source]

Set parent_id==5 for node(8) initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Test update node
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
level               Move 8 - > 5
    1                     1(1)22
             _______________|__________________
            |               |                  |
    2     2(2)5           6(4)15            16(7)21
            |               ^                  |
    3     3(3)4      7(5)12   13(6)14      17(10)20
                       |                        |
    4                8(8)11                18(11)19
                       |
    5                9(9)10
test_update_wo_move()[source]

Update node w/o move initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19
test_update_wo_move_like_sacrud_save()[source]

Just change attr from node w/o move initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19
class sqlalchemy_mptt.tests.cases.get_node.GetNodes[source]

Bases: object

test_get_children()[source]

Get children of node

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
test_get_siblings()[source]

Get siblings of node

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22                              (12)
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
class sqlalchemy_mptt.tests.cases.get_tree.Tree[source]

Bases: object

test_drilldown_tree()[source]
level           Nested sets example
1                    1(1)22       ---------------------
        _______________|_________|_________            |
       |               |         |         |           |
2    2(2)5           6(4)11      |      12(7)21        |
       |               ^         |         ^           |
3    3(3)4       7(5)8   9(6)10  | 13(8)16   17(10)20  |
                                 |    |          |     |
4                                | 14(9)15   18(11)19  |
                                 |                     |
                                  ---------------------
test_drilldown_tree_without_session()[source]
test_get_empty_tree()[source]

No rows in database.

test_get_empty_tree_with_custom_query()[source]

No rows with id < 0.

test_get_json_tree()[source]

Note

See [source] for full example

Return tree as JSON of jqTree format

tree = Tree.get_tree(self.session, json=True)
test_get_json_tree_with_custom_field()[source]

Note

See [source] for full example

Return tree as JSON of jqTree format with additional field

1def fields(node):
2    return {'visible': node.visible}
3
4tree = Tree.get_tree(self.session, json=True, json_fields=fields)
test_get_tree()[source]

Note

See [source] for full example

Return tree as list of dict

tree = Tree.get_tree(self.session)
test_get_tree_count_query()[source]

Count num of queries to the database. See https://github.com/uralbash/sqlalchemy_mptt/issues/39

test_leftsibling_in_level()[source]

Node to the left of the current node at the same level

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19

level1 = [1]
level2 = [2, 4, 7]
level3 = [3, 5, 6, 8, 10]
level4 = [9, 11]

leftsibling_in_level_of_node_3 = None
leftsibling_in_level_of_node_5 = 3
leftsibling_in_level_of_node_6 = 5
leftsibling_in_level_of_node_8 = 6
leftsibling_in_level_of_node_11 = 9
test_path_to_root()[source]

Generate path from a leaf or intermediate node to the root.

For example:

node11.path_to_root()

level           Nested sets example

                 -----------------------------------------
1               |    1(1)22                               |
        ________|______|_____________________             |
       |        |      |                     |            |
       |          -----+---------            |            |
2    2(2)5           6(4)11      | --     12(7)21         |
       |               ^             |    /     \         |
3    3(3)4       7(5)8   9(6)10      ---/----    \        |
                                    13(8)16 |  17(10)20   |
                                       |    |     |       |
4                                   14(9)15 | 18(11)19    |
                                            |             |
                                             -------------
sqlalchemy_mptt.tests.cases.get_tree.get_obj(session, model, id)[source]
class sqlalchemy_mptt.tests.cases.initialize.Initialize[source]

Bases: object

test_flush_with_transient_nodes_present()[source]

https://github.com/uralbash/sqlalchemy_mptt/issues/34

test_tree_initialize()[source]

Initial state of the trees

level               Tree 1
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19


level               Tree 2
1                    1(12)22
        _______________|___________________
       |               |                   |
2    2(13)5         6(15)11              12(18)21
       |               ^                    ^
3    3(14)4     7(16)8   9(17)10   13(19)16   17(21)20
                                       |          |
4                                  14(20)15   18(22)19
test_tree_orm_initialize()[source]
class sqlalchemy_mptt.tests.cases.integrity.DataIntegrity[source]

Bases: object

test_greatest_right_is_always_double_number_of_nodes()[source]

The greatest right key is always double the number of nodes.

The following example should match COUNT(id) * 2 equal MAX(right).

SELECT COUNT(id), MAX(right) FROM tree
test_hierarchy_structure()[source]

Nodes with left < self and right > self are considered ancestors, while nodes with left > self and right < self are considered descendants

test_left_and_right_always_unique_number()[source]

left and right always is unique.

test_left_is_always_less_than_right()[source]

The left key is always less than the right.

The following example should return an empty result.

SELECT id FROM tree WHERE left >= right
test_level_odd_when_left_odd_and_vice_versa()[source]

If the node number is odd then the left key is always an odd number, and the same goes for the even numbers.

The following example should return an empty result.

SELECT id, MOD((left - level + 2) / 2) AS modulo FROM tree
WHERE modulo = 1
test_lowest_left_is_always_1()[source]

The lowest left key is always 1.

The following example should return 1.

SELECT MIN(left) FROM tree
test_right_minus_left_always_odd()[source]

Difference between left and right keys are always an odd number.

The following example should return an empty result.

SELECT MOD((right - left) / 2) AS modulo
FROM tree WHERE modulo = 0
class sqlalchemy_mptt.tests.cases.move_node.MoveAfter[source]

Bases: object

test_move_after_between_tree()[source]

Move node(7) (big subtree) to top level after node(1) and before node(12)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level               Move 7 to toplevel
    1                     1(1)12             1(7)10
             _______________|              ____|____
            |               |             |         |
    2     2(2)5           6(4)11        2(8)5     6(10)9
            |               ^             |         |
    3     3(3)4       7(5)8   9(6)10    3(9)4     7(11)8

              id lft rgt lvl parent tree
test_move_after_function()[source]

For example move node(8) after node(5)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level               Initial state
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level               Move 8 after 5
    1                     1(1)22
            _______________|__________________
           |               |                  |
    2     2(2)5           6(4)15            16(7)21
            |               ^                  |
    3     3(3)4    7(5)8  9(8)12  13(6)14   17(10)20
                            |                  |
    4                    10(9)11            18(11)19
test_move_to_toplevel()[source]

Move node(8) to top level after node(1)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level               Move 8 after 1
    1                     1(1)18                     1(8)4
             _______________|______________            |
            |               |              |           |
    2     2(2)5           6(4)11        12(7)17      2(9)3
            |               ^              |
    3     3(3)4       7(5)8   9(6)10   13(10)16
                                           |
    4                                  14(11)15
test_move_to_toplevel2()[source]

Move node(8) to top level after node(1)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level               Move 8 after 1
    1                     1(1)18                     1(8)4
             _______________|______________            |
            |               |              |           |
    2     2(2)5           6(4)11        12(7)17      2(9)3
            |               ^              |
    3     3(3)4       7(5)8   9(6)10   13(10)16
                                           |
    4                                  14(11)15

              id lft rgt lvl parent tree
test_move_to_toplevel_big_subtree()[source]

Move node(7) (big subtree) to top level after node(1)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level               Move 7 to toplevel
    1                     1(1)12             1(7)10
             _______________|              ____|____
            |               |             |         |
    2     2(2)5           6(4)11        2(8)5     6(10)9
            |               ^             |         |
    3     3(3)4       7(5)8   9(6)10    3(9)4     7(11)8

              id lft rgt lvl parent tree
test_move_to_toplevel_where_much_trees_from_right_side()[source]

Move 20 after 1

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           tree_id = 1
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19

level           tree_id = 2
1                     1(15)6
                         ^
2                 2(16)3   4(17)5

level           tree_id = 3
1                    1(12)16
         _______________|
        |               |
2    2(13)5          6(18)15
        |               ^
3    3(14)4     7(19)10   11(21)14
                   |          |
4               8(20)9    12(22)13
class sqlalchemy_mptt.tests.cases.move_node.MoveBefore[source]

Bases: object

test_move_before_function()[source]

For example move node(8) before node(4)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19

level           move 8 before 4
1                    1(1)22
        _______________|___________________
       |        |            |             |
2    2(2)5    6(8)9       10(4)15       16(7)21
       |        |            ^             |
3    3(3)4    7(9)8   11(5)12 13(6)14  17(10)20
                                           |
4                                      18(11)19
test_move_before_to_other_tree()[source]

For example move node(8) before node(15)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Move 8 before 15
1                    1(1)18
         _______________|___________________
        |               |                   |
2     2(2)5           6(4)11             12(7)17
        |               ^                   |
3     3(3)4       7(5)8   9(6)10        13(10)16
                                            |
4                                       14(11)15

level
1                    1(12)26
         _______________|______________________________
        |         |               |                    |
2    2(13)5     6(8)9         10(15)15             16(18)25
        |         |               ^                    ^
3    3(14)4     7(9)8    11(16)12  13(17)14   17(19)20   21(21)24
                                                  |          |
4                                             18(20)19   22(22)23
test_move_before_to_top_level()[source]

For example move node(4) before node(1)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets example
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level            move 4 before 1
    1         1(4)6              1(1)16
                ^           _______|_______
          2(5)3   4(6)5    |               |
    2                    2(2)5           6(7)15
                           |               ^
    3                    3(3)4      7(8)10   11(10)14
                                      |          |
    4                               8(9)9    12(11)13
test_move_one_tree_before_another()[source]

For example move node(12) before node(1)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

                            <--------------------------------
                                                            |
level           Nested sets tree1                           |
    1                    1(1)22                             |
            _______________|___________________             |
           |               |                   |            |
    2    2(2)5           6(4)11             12(7)21         |
           |               ^                   ^            |
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20   |
                                          |          |      |
    4                                  14(9)15   18(11)19   |
                                                            |
    level           Nested sets tree2                       |
    1                    1(12)22 ----------------------------
            _______________|___________________
           |               |                   |
    2    2(13)5         6(15)11             12(18)21
           |               ^                    ^
    3    3(14)4     7(16)8   9(17)10   13(19)16   17(21)20
                                           |          |
    4                                  14(20)15   18(22)19
test_move_one_tree_before_other_tree()[source]
class sqlalchemy_mptt.tests.cases.move_node.MoveInside[source]

Bases: object

test_move_between_tree()[source]

Move node(4) to other tree inside node(15)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets tree1
1                    1(1)16
        _______________|_____________________
       |                                     |
2    2(2)5                                 6(7)15
       |                                     ^
3    3(3)4                            7(8)10   11(10)14
                                        |          |
4                                     8(9)9    12(11)13

level           Nested sets tree2
1                     1(12)28
         ________________|_______________________
        |                |                       |
2    2(13)5            6(15)17                18(18)27
       |                 ^                        ^
3    3(14)4    7(4)12 13(16)14  15(17)16  19(19)22  23(21)26
                 ^                            |         |
4          8(5)9  10(6)11                 20(20)21  24(22)25
test_move_inside_function()[source]

For example move node(4) inside node(15)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Nested sets tree1
1                    1(1)16
        _______________|_____________________
       |                                     |
2    2(2)5                                 6(7)15
       |                                     ^
3    3(3)4                            7(8)10   11(10)14
                                        |          |
4                                     8(9)9    12(11)13

level           Nested sets tree2
1                     1(12)28
         ________________|_______________________
        |                |                       |
2    2(13)5            6(15)17                18(18)27
       |                 ^                        ^
3    3(14)4    7(4)12 13(16)14  15(17)16  19(19)22  23(21)26
                 ^                            |         |
4          8(5)9  10(6)11                 20(20)21  24(22)25
test_move_inside_to_the_same_parent_function()[source]

For example move node(6) inside node(4)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level               Initial state
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19

level           move 6 inside 4
    1                    1(1)22
            _______________|___________________
           |               |                   |
    2    2(2)5           6(4)11             12(7)21
           |               ^                   ^
    3    3(3)4       7(6)8   9(5)10    13(8)16   17(10)20
                                          |          |
    4                                  14(9)15   18(11)19
test_move_tree_to_another_tree()[source]

Move tree(2) inside tree(1)

initial state of the tree sqlalchemy_mptt.tests.add_mptt_tree

level           Move tree2 to tree1
1                    1(1)44
         _______________|_________________________________
        |               |                                 |
2     2(2)5           6(4)11                            12(7)43
        |            ___|___                             __|_____________________________________
        |           |       |                           |                             |          |
3     3(3)4       7(5)8   9(6)10                    13(12)34                       35(8)38   39(10)42
                                        _______________|___________________           |          |
                                       |               |                   |       36(9)37   40(11)41
4                                   14(13)17        18(15)23             24(18)33
                                        |               ^                    ^
5                                   15(14)16   19(16)20   21(17)22   25(19)28  29(21)32
                                                                         |         |
6                                                                    26(20)27  30(22)31
test_tree_shorting()[source]

Try to move top level node(1) inside tree

level           Nested sets example
1                    1(1)22
        _______________|___________________
       |               |                   |
2    2(2)5           6(4)11             12(7)21
       |               ^                   ^
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20
                                      |          |
4                                  14(9)15   18(11)19

level           Nested sets example

                        __parent_id______________________
                       |                                 |
1                    1(1)22                              |
        _______________|___________________              |
       |               |                   |             |
2    2(2)5           6(4)11             12(7)21         (X)
       |               ^                   ^             |
3    3(3)4       7(5)8   9(6)10    13(8)16   17(10)20    |
                                      |          |       |
4                                  14(9)15   18(11)19    |
                                                ↑        |
                                                ↑________|