20
20
LIMITING_FK_EDGES_CLAUSE_1 = (
21
21
"""AND second.{fk_field_name}_id = %(limiting_fk_edges_instance_id)s"""
22
22
)
23
- LIMITING_FK_EDGES_CLAUSE_2 = """AND {relationship_table}.{fk_field_name}_id = %(limiting_fk_edges_instance_id)s"""
23
+ LIMITING_FK_EDGES_CLAUSE_2 = (
24
+ """AND {relationship_table}.{fk_field_name}_id = %(limiting_fk_edges_instance_id)s"""
25
+ )
26
+
24
27
LIMITING_FK_NODES_CLAUSE_1 = """"""
25
28
LIMITING_FK_NODES_CLAUSE_2 = """"""
26
29
27
- EXCLUDED_UPWARD_NODES_CLAUSE_1 = """AND second.child_id <> ALL(%(node_ids )s::int[])""" # Used for ancestors and upward path
30
+ EXCLUDED_UPWARD_NODES_CLAUSE_1 = """AND second.child_id <> ALL(%(excluded_upward_node_ids )s::int[])""" # Used for ancestors and upward path
28
31
EXCLUDED_UPWARD_NODES_CLAUSE_2 = (
29
- """AND {relationship_table}.child_id <> ALL(%(node_ids )s::int[])"""
32
+ """AND {relationship_table}.child_id <> ALL(%(excluded_upward_node_ids )s::int[])"""
30
33
)
31
- EXCLUDED_DOWNWARD_NODES_CLAUSE_1 = """AND second.parent_id <> ALL(%(node_ids)s::int[])""" # Used for descendants and downward path
34
+
35
+ EXCLUDED_DOWNWARD_NODES_CLAUSE_1 = """AND second.parent_id <> ALL(%(excluded_downward_node_ids)s::int[])""" # Used for descendants and downward path
32
36
EXCLUDED_DOWNWARD_NODES_CLAUSE_2 = (
33
- """AND {relationship_table}.parent_id <> ALL(%(node_ids)s::int[])"""
34
- )
35
- REQUIRED_UPWARD_NODES_CLAUSE_1 = """AND second.child_id = ALL(%(node_ids)s::int[])""" # Used for ancestors and upward path
36
- REQUIRED_UPWARD_NODES_CLAUSE_2 = (
37
- """AND {relationship_table}.child_id = ALL(%(node_ids)s::int[])"""
38
- )
39
- REQUIRED_DOWNWARD_NODES_CLAUSE_1 = """AND second.parent_id = ALL(%(node_ids)s::int[])""" # Used for descendants and downward path
40
- REQUIRED_DOWNWARD_NODES_CLAUSE_2 = (
41
- """AND {relationship_table}.parent_id = ALL(%(node_ids)s::int[])"""
37
+ """AND {relationship_table}.parent_id <> ALL(%(excluded_downward_node_ids)s::int[])"""
42
38
)
43
39
40
+ REQUIRED_UPWARD_NODES_CLAUSE_1 = """""" # Used for ancestors and upward path
41
+ REQUIRED_UPWARD_NODES_CLAUSE_2 = """"""
42
+
43
+ REQUIRED_DOWNWARD_NODES_CLAUSE_1 = """""" # Used for descendants and downward path
44
+ REQUIRED_DOWNWARD_NODES_CLAUSE_2 = """"""
45
+
44
46
ANCESTORS_QUERY = """
45
47
WITH RECURSIVE traverse(id, depth) AS (
46
48
SELECT first.parent_id, 1
96
98
"""
97
99
98
100
PATH_LIMITING_FK_EDGES_CLAUSE = (
99
- """AND first.{fk_field_name}_id = { limiting_fk_edges_instance_id} """
101
+ """AND first.{fk_field_name}_id = %( limiting_fk_edges_instance_id)s """
100
102
)
101
103
PATH_LIMITING_FK_NODES_CLAUSE = """"""
102
104
103
105
EXCLUDED_UPWARD_PATH_NODES_CLAUSE = (
104
- """AND second.parent_id <> ALL('{node_ids }'::int[])"""
106
+ """AND second.parent_id <> ALL('{excluded_path_node_ids }'::int[])"""
105
107
)
106
108
EXCLUDED_DOWNWARD_PATH_NODES_CLAUSE = (
107
- """AND second.child_id <> ALL('{node_ids }'::int[])"""
109
+ """AND second.child_id <> ALL('{excluded_path_node_ids }'::int[])"""
108
110
)
109
111
REQUIRED_UPWARD_PATH_NODES_CLAUSE = (
110
- """AND second.parent_id = ALL('{node_ids }'::int[])"""
112
+ """AND second.parent_id = ALL('{required_path_node_ids }'::int[])"""
111
113
)
112
114
REQUIRED_DOWNWARD_PATH_NODES_CLAUSE = (
113
- """AND second.child_id = ALL('{node_ids }'::int[])"""
115
+ """AND second.child_id = ALL('{required_path_node_ids }'::int[])"""
114
116
)
115
117
116
118
UPWARD_PATH_QUERY = """
@@ -290,7 +292,7 @@ def ancestors_ids(self, **kwargs):
290
292
ancestors_clauses_2 += "\n " + EXCLUDED_UPWARD_NODES_CLAUSE_2 .format (
291
293
relationship_table = edge_model_table ,
292
294
)
293
- query_parameters ["node_ids " ] = str (
295
+ query_parameters ["excluded_upward_node_ids " ] = str (
294
296
set (excluded_nodes_queryset .values_list ("id" , flat = True ))
295
297
)
296
298
@@ -378,7 +380,7 @@ def descendants_ids(self, **kwargs):
378
380
descendants_clauses_2 += "\n " + EXCLUDED_DOWNWARD_NODES_CLAUSE_2 .format (
379
381
relationship_table = edge_model_table ,
380
382
)
381
- query_parameters ["node_ids " ] = str (
383
+ query_parameters ["excluded_downward_node_ids " ] = str (
382
384
set (excluded_nodes_queryset .values_list ("id" , flat = True ))
383
385
)
384
386
@@ -526,10 +528,19 @@ def path_ids_list(
526
528
pass # Not implemented yet
527
529
528
530
if limiting_fk_edges_instance is not None :
529
- pass # Not implemented yet
531
+ fk_field_name = get_foreign_key_field (limiting_fk_edges_instance )
532
+ if fk_field_name is not None :
533
+ downward_clauses += "\n " + PATH_LIMITING_FK_EDGES_CLAUSE .format (
534
+ relationship_table = edge_model_table ,
535
+ fk_field_name = fk_field_name ,
536
+ )
537
+ query_parameters ["limiting_fk_edges_instance_id" ] = limiting_fk_edges_instance .id
530
538
531
539
if excluded_nodes_queryset is not None :
532
- pass # Not implemented yet
540
+ downward_clauses += "\n " + EXCLUDED_DOWNWARD_PATH_NODES_CLAUSE
541
+ query_parameters ["excluded_path_node_ids" ] = str (
542
+ set (excluded_nodes_queryset .values_list ("id" , flat = True ))
543
+ )
533
544
534
545
if excluded_edges_queryset is not None :
535
546
pass # Not implemented yet
@@ -551,6 +562,36 @@ def path_ids_list(
551
562
)
552
563
path = [row [0 ] + [target_node .id ] for row in cursor .fetchall ()]
553
564
if not path and not directional :
565
+
566
+ if limiting_fk_nodes_instance is not None :
567
+ pass # Not implemented yet
568
+
569
+ if limiting_fk_edges_instance is not None :
570
+ pass # Not implemented yet
571
+
572
+
573
+ if limiting_fk_edges_instance is not None :
574
+ if 'fk_field_name' in locals ():
575
+ upward_clauses += "\n " + PATH_LIMITING_FK_EDGES_CLAUSE .format (
576
+ relationship_table = edge_model_table ,
577
+ fk_field_name = fk_field_name ,
578
+ )
579
+
580
+ if excluded_nodes_queryset is not None :
581
+ upward_clauses += "\n " + EXCLUDED_UPWARD_PATH_NODES_CLAUSE
582
+ query_parameters ["excluded_path_node_ids" ] = str (
583
+ set (excluded_nodes_queryset .values_list ("id" , flat = True ))
584
+ )
585
+
586
+ if excluded_edges_queryset is not None :
587
+ pass # Not implemented yet
588
+
589
+ if required_nodes_queryset is not None :
590
+ pass # Not implemented yet
591
+
592
+ if required_edges_queryset is not None :
593
+ pass # Not implemented yet
594
+
554
595
with connection .cursor () as cursor :
555
596
cursor .execute (
556
597
UPWARD_PATH_QUERY .format (
@@ -769,4 +810,3 @@ def save(self, *args, **kwargs):
769
810
super (Edge , self ).save (* args , ** kwargs )
770
811
771
812
return Edge
772
-
0 commit comments