Skip to content

Commit e4535ec

Browse files
committed
Address warnings
1 parent 48c8af8 commit e4535ec

5 files changed

Lines changed: 89 additions & 135 deletions

File tree

sqlalchemy_mptt/events.py

Lines changed: 68 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import weakref
1414

1515
# SQLAlchemy
16-
from sqlalchemy import and_, case, event, select, inspection
16+
from sqlalchemy import and_, event, inspection
1717
from sqlalchemy.orm import object_session
1818
from sqlalchemy.sql import func
1919
from sqlalchemy.orm.base import NO_VALUE
2020

21+
from sqlalchemy_mptt.sqlalchemy_compat import case, select
22+
2123

2224
def _insert_subtree(
2325
table,
@@ -41,9 +43,9 @@ def _insert_subtree(
4143
delta_rgt = delta_lft + node_size - 1
4244

4345
connection.execute(
44-
table.update(
45-
table_pk.in_(subtree)
46-
).values(
46+
table.update()
47+
.where(table_pk.in_(subtree))
48+
.values(
4749
lft=table.c.lft - node_pos_left + delta_lft,
4850
rgt=table.c.rgt - node_pos_right + delta_rgt,
4951
level=table.c.level - node_level + parent_level + 1,
@@ -53,21 +55,14 @@ def _insert_subtree(
5355

5456
# step 2: update key of right side
5557
connection.execute(
56-
table.update(
57-
and_(
58-
table.c.rgt > delta_lft - 1,
59-
table_pk.notin_(subtree),
60-
table.c.tree_id == parent_tree_id
61-
)
62-
).values(
58+
table.update()
59+
.where(table.c.rgt > delta_lft - 1)
60+
.where(table_pk.notin_(subtree))
61+
.where(table.c.tree_id == parent_tree_id)
62+
.values(
6363
rgt=table.c.rgt + node_size,
6464
lft=case(
65-
[
66-
(
67-
table.c.lft > left_sibling['lft'],
68-
table.c.lft + node_size
69-
)
70-
],
65+
(table.c.lft > left_sibling['lft'], table.c.lft + node_size),
7166
else_=table.c.lft
7267
)
7368
)
@@ -94,9 +89,7 @@ def mptt_before_insert(mapper, connection, instance):
9489
instance.level = instance.get_default_level()
9590
tree_id = connection.scalar(
9691
select(
97-
[
98-
func.max(table.c.tree_id) + 1
99-
]
92+
func.max(table.c.tree_id) + 1
10093
)
10194
) or 1
10295
instance.tree_id = tree_id
@@ -106,39 +99,27 @@ def mptt_before_insert(mapper, connection, instance):
10699
parent_tree_id,
107100
parent_level) = connection.execute(
108101
select(
109-
[
110-
table.c.lft,
111-
table.c.rgt,
112-
table.c.tree_id,
113-
table.c.level
114-
]
102+
table.c.lft,
103+
table.c.rgt,
104+
table.c.tree_id,
105+
table.c.level
115106
).where(
116107
table_pk == instance.parent_id
117108
)
118109
).fetchone()
119110

120111
# Update key of right side
121112
connection.execute(
122-
table.update(
123-
and_(table.c.rgt >= parent_pos_right,
124-
table.c.tree_id == parent_tree_id)
125-
).values(
113+
table.update()
114+
.where(table.c.rgt >= parent_pos_right)
115+
.where(table.c.tree_id == parent_tree_id)
116+
.values(
126117
lft=case(
127-
[
128-
(
129-
table.c.lft > parent_pos_right,
130-
table.c.lft + 2
131-
)
132-
],
118+
(table.c.lft > parent_pos_right, table.c.lft + 2),
133119
else_=table.c.lft
134120
),
135121
rgt=case(
136-
[
137-
(
138-
table.c.rgt >= parent_pos_right,
139-
table.c.rgt + 2
140-
)
141-
],
122+
(table.c.rgt >= parent_pos_right, table.c.rgt + 2),
142123
else_=table.c.rgt
143124
)
144125
)
@@ -158,10 +139,8 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
158139
table_pk = getattr(table.c, db_pk.name)
159140
lft, rgt = connection.execute(
160141
select(
161-
[
162-
table.c.lft,
163-
table.c.rgt
164-
]
142+
table.c.lft,
143+
table.c.rgt
165144
).where(
166145
table_pk == pk
167146
)
@@ -171,7 +150,7 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
171150
if delete:
172151
mapper.base_mapper.confirm_deleted_rows = False
173152
connection.execute(
174-
table.delete(
153+
table.delete().where(
175154
table_pk == pk
176155
)
177156
)
@@ -190,28 +169,16 @@ def mptt_before_delete(mapper, connection, instance, delete=True):
190169
END
191170
"""
192171
connection.execute(
193-
table.update(
194-
and_(
195-
table.c.rgt > rgt,
196-
table.c.tree_id == tree_id
197-
)
198-
).values(
172+
table.update()
173+
.where(table.c.rgt > rgt)
174+
.where(table.c.tree_id == tree_id)
175+
.values(
199176
lft=case(
200-
[
201-
(
202-
table.c.lft > lft,
203-
table.c.lft - delta
204-
)
205-
],
177+
(table.c.lft > lft, table.c.lft - delta),
206178
else_=table.c.lft
207179
),
208180
rgt=case(
209-
[
210-
(
211-
table.c.rgt >= rgt,
212-
table.c.rgt - delta
213-
)
214-
],
181+
(table.c.rgt >= rgt, table.c.rgt - delta),
215182
else_=table.c.rgt
216183
)
217184
)
@@ -243,25 +210,21 @@ def mptt_before_update(mapper, connection, instance):
243210
right_sibling_tree_id
244211
) = connection.execute(
245212
select(
246-
[
247-
table.c.lft,
248-
table.c.rgt,
249-
table.c.parent_id,
250-
table.c.level,
251-
table.c.tree_id
252-
]
213+
table.c.lft,
214+
table.c.rgt,
215+
table.c.parent_id,
216+
table.c.level,
217+
table.c.tree_id
253218
).where(
254219
table_pk == instance.mptt_move_before
255220
)
256221
).fetchone()
257222
current_lvl_nodes = connection.execute(
258223
select(
259-
[
260-
table.c.lft,
261-
table.c.rgt,
262-
table.c.parent_id,
263-
table.c.tree_id
264-
]
224+
table.c.lft,
225+
table.c.rgt,
226+
table.c.parent_id,
227+
table.c.tree_id
265228
).where(
266229
and_(
267230
table.c.level == right_sibling_level,
@@ -296,12 +259,10 @@ def mptt_before_update(mapper, connection, instance):
296259
left_sibling_tree_id
297260
) = connection.execute(
298261
select(
299-
[
300-
table.c.lft,
301-
table.c.rgt,
302-
table.c.parent_id,
303-
table.c.tree_id
304-
]
262+
table.c.lft,
263+
table.c.rgt,
264+
table.c.parent_id,
265+
table.c.tree_id
305266
).where(
306267
table_pk == instance.mptt_move_after
307268
)
@@ -320,7 +281,7 @@ def mptt_before_update(mapper, connection, instance):
320281
ORDER BY left_key
321282
"""
322283
subtree = connection.execute(
323-
select([table_pk])
284+
select(table_pk)
324285
.where(
325286
and_(
326287
table.c.lft >= instance.left,
@@ -345,13 +306,11 @@ def mptt_before_update(mapper, connection, instance):
345306
node_level
346307
) = connection.execute(
347308
select(
348-
[
349-
table.c.lft,
350-
table.c.rgt,
351-
table.c.tree_id,
352-
table.c.parent_id,
353-
table.c.level
354-
]
309+
table.c.lft,
310+
table.c.rgt,
311+
table.c.tree_id,
312+
table.c.parent_id,
313+
table.c.level
355314
).where(
356315
table_pk == node_id
357316
)
@@ -375,13 +334,11 @@ def mptt_before_update(mapper, connection, instance):
375334
parent_level
376335
) = connection.execute(
377336
select(
378-
[
379-
table_pk,
380-
table.c.rgt,
381-
table.c.lft,
382-
table.c.tree_id,
383-
table.c.level
384-
]
337+
table_pk,
338+
table.c.rgt,
339+
table.c.lft,
340+
table.c.tree_id,
341+
table.c.level
385342
).where(
386343
table_pk == instance.parent_id
387344
)
@@ -405,13 +362,11 @@ def mptt_before_update(mapper, connection, instance):
405362
parent_level
406363
) = connection.execute(
407364
select(
408-
[
409-
table_pk,
410-
table.c.rgt,
411-
table.c.lft,
412-
table.c.tree_id,
413-
table.c.level
414-
]
365+
table_pk,
366+
table.c.rgt,
367+
table.c.lft,
368+
table.c.tree_id,
369+
table.c.level
415370
).where(
416371
table_pk == instance.parent_id
417372
)
@@ -449,28 +404,24 @@ def mptt_before_update(mapper, connection, instance):
449404
if left_sibling_tree_id or left_sibling_tree_id == 0:
450405
tree_id = left_sibling_tree_id + 1
451406
connection.execute(
452-
table.update(
453-
table.c.tree_id > left_sibling_tree_id
454-
).values(
407+
table.update()
408+
.where(table.c.tree_id > left_sibling_tree_id)
409+
.values(
455410
tree_id=table.c.tree_id + 1
456411
)
457412
)
458413
# if just insert
459414
else:
460415
tree_id = connection.scalar(
461416
select(
462-
[
463-
func.max(table.c.tree_id) + 1
464-
]
417+
func.max(table.c.tree_id) + 1
465418
)
466419
)
467420

468421
connection.execute(
469-
table.update(
470-
table_pk.in_(
471-
subtree
472-
)
473-
).values(
422+
table.update()
423+
.where(table_pk.in_(subtree))
424+
.values(
474425
lft=table.c.lft - node_pos_left + 1,
475426
rgt=table.c.rgt - node_pos_left + 1,
476427
level=table.c.level - node_level + default_level,

sqlalchemy_mptt/tests/test_events.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
from sqlalchemy import Column, Boolean, Integer, create_engine
1616
from sqlalchemy.event import contains
17-
from sqlalchemy.ext.declarative import declarative_base
1817
from sqlalchemy.orm import sessionmaker
1918

2019
from sqlalchemy_mptt import mptt_sessionmaker
2120

22-
from . import TreeTestingMixin
23-
from ..mixins import BaseNestedSets
21+
from sqlalchemy_mptt.mixins import BaseNestedSets
22+
from sqlalchemy_mptt.sqlalchemy_compat import declarative_base
23+
from sqlalchemy_mptt.tests import TreeTestingMixin
24+
2425

2526
Base = declarative_base()
2627

0 commit comments

Comments
 (0)