Skip to content

Commit ac3deb1

Browse files
Continued implementing filters
1 parent e530675 commit ac3deb1

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

django_postgresql_dag/models.py

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,29 @@
2020
LIMITING_FK_EDGES_CLAUSE_1 = (
2121
"""AND second.{fk_field_name}_id = %(limiting_fk_edges_instance_id)s"""
2222
)
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+
2427
LIMITING_FK_NODES_CLAUSE_1 = """"""
2528
LIMITING_FK_NODES_CLAUSE_2 = """"""
2629

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
2831
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[])"""
3033
)
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
3236
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[])"""
4238
)
4339

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+
4446
ANCESTORS_QUERY = """
4547
WITH RECURSIVE traverse(id, depth) AS (
4648
SELECT first.parent_id, 1
@@ -96,21 +98,21 @@
9698
"""
9799

98100
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"""
100102
)
101103
PATH_LIMITING_FK_NODES_CLAUSE = """"""
102104

103105
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[])"""
105107
)
106108
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[])"""
108110
)
109111
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[])"""
111113
)
112114
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[])"""
114116
)
115117

116118
UPWARD_PATH_QUERY = """
@@ -290,7 +292,7 @@ def ancestors_ids(self, **kwargs):
290292
ancestors_clauses_2 += "\n" + EXCLUDED_UPWARD_NODES_CLAUSE_2.format(
291293
relationship_table=edge_model_table,
292294
)
293-
query_parameters["node_ids"] = str(
295+
query_parameters["excluded_upward_node_ids"] = str(
294296
set(excluded_nodes_queryset.values_list("id", flat=True))
295297
)
296298

@@ -378,7 +380,7 @@ def descendants_ids(self, **kwargs):
378380
descendants_clauses_2 += "\n" + EXCLUDED_DOWNWARD_NODES_CLAUSE_2.format(
379381
relationship_table=edge_model_table,
380382
)
381-
query_parameters["node_ids"] = str(
383+
query_parameters["excluded_downward_node_ids"] = str(
382384
set(excluded_nodes_queryset.values_list("id", flat=True))
383385
)
384386

@@ -526,10 +528,19 @@ def path_ids_list(
526528
pass # Not implemented yet
527529

528530
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
530538

531539
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+
)
533544

534545
if excluded_edges_queryset is not None:
535546
pass # Not implemented yet
@@ -551,6 +562,36 @@ def path_ids_list(
551562
)
552563
path = [row[0] + [target_node.id] for row in cursor.fetchall()]
553564
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+
554595
with connection.cursor() as cursor:
555596
cursor.execute(
556597
UPWARD_PATH_QUERY.format(
@@ -769,4 +810,3 @@ def save(self, *args, **kwargs):
769810
super(Edge, self).save(*args, **kwargs)
770811

771812
return Edge
772-

0 commit comments

Comments
 (0)