Skip to content

Commit 02c2d46

Browse files
committed
Fixing errors uncovered by linting
1 parent cd6a300 commit 02c2d46

File tree

7 files changed

+39
-30
lines changed

7 files changed

+39
-30
lines changed

django_postgresql_dag/__init__.py

Whitespace-only changes.

django_postgresql_dag/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ class IncorrectUsageException(Exception):
2323
"""
2424

2525
pass
26+
27+
class IncorrectQuerysetTypeException(Exception):
28+
"""
29+
Exception for mismatch between helper method and queryset arg
30+
"""
31+
32+
pass

django_postgresql_dag/models.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
"""
77

88
from copy import deepcopy
9-
from django.apps import apps
10-
from django.db import models, connection
11-
from django.db.models import Case, When
9+
from django.db import models
1210
from django.core.exceptions import ValidationError
1311

1412
from .exceptions import NodeNotReachableException
1513
from .utils import _ordered_filter
16-
from .query_builders import AncestorQuery, DescendantQuery, UpwardPathQuery, DownwardPathQuery, ConnectedGraphQuery
14+
from .query_builders import (
15+
AncestorQuery,
16+
DescendantQuery,
17+
UpwardPathQuery,
18+
DownwardPathQuery,
19+
ConnectedGraphQuery
20+
)
1721

1822

1923
class NodeManager(models.Manager):
@@ -53,18 +57,6 @@ class Node(base_model):
5357
class Meta:
5458
abstract = True
5559

56-
def get_foreign_key_field(self, fk_instance=None):
57-
"""
58-
Provided a model instance, checks if the edge model has a ForeignKey field to the
59-
model class of that instance, and then returns the associated field name, else None.
60-
"""
61-
if fk_instance is not None:
62-
for field in edge_model._meta.get_fields():
63-
if field.related_model is fk_instance._meta.model:
64-
# Return the first field that matches
65-
return field.name
66-
return None
67-
6860
def get_pk_name(self):
6961
"""Sometimes we set a field other than 'pk' for the primary key.
7062
This method is used to get the correct primary key field name for the

django_postgresql_dag/query_builders.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import ABC, abstractmethod
22
from django.core.exceptions import ImproperlyConfigured
3-
from .utils import get_instance_characteristics, get_queryset_characteristics
3+
4+
from .utils import get_foreign_key_field, get_instance_characteristics
45

56

67
class BaseQuery(ABC):
@@ -198,7 +199,7 @@ def _limit_to_edges_set_fk(self):
198199
"""AND {relationship_table}.{fk_field_name}_id = %(limiting_edges_set_fk_pk)s"""
199200
)
200201

201-
fk_field_name = get_foreign_key_field(fk_instance=self.limiting_edges_set_fk)
202+
fk_field_name = get_foreign_key_field(self.edge_model, self.limiting_edges_set_fk)
202203
if fk_field_name is not None:
203204
self.where_clauses_part_1 += "\n" + LIMITING_EDGES_SET_FK_CLAUSE_1.format(
204205
relationship_table=self.edge_model_table,
@@ -316,7 +317,7 @@ def _limit_to_edges_set_fk(self):
316317
"""AND {relationship_table}.{fk_field_name}_id = %(limiting_edges_set_fk_pk)s"""
317318
)
318319

319-
fk_field_name = get_foreign_key_field(fk_instance=self.limiting_edges_set_fk)
320+
fk_field_name = get_foreign_key_field(self.edge_model, self.limiting_edges_set_fk)
320321
if fk_field_name is not None:
321322
self.where_clauses_part_1 += "\n" + LIMITING_EDGES_SET_FK_CLAUSE_1.format(
322323
relationship_table=self.edge_model_table,
@@ -482,7 +483,7 @@ def _limit_to_nodes_set_fk(self):
482483
def _limit_to_edges_set_fk(self):
483484
LIMITING_EDGES_SET_FK_CLAUSE = """AND first.{fk_field_name}_id = %(limiting_edges_set_fk_pk)s"""
484485

485-
fk_field_name = get_foreign_key_field(fk_instance=self.limiting_edges_set_fk)
486+
fk_field_name = get_foreign_key_field(self.edge_model, self.limiting_edges_set_fk)
486487
if fk_field_name is not None:
487488
self.where_clauses_part_2 += "\n" + LIMITING_EDGES_SET_FK_CLAUSE.format(
488489
relationship_table=self.edge_model_table,
@@ -584,7 +585,7 @@ def _limit_to_nodes_set_fk(self):
584585
def _limit_to_edges_set_fk(self):
585586
LIMITING_EDGES_SET_FK_CLAUSE = """AND first.{fk_field_name}_id = %(limiting_edges_set_fk_pk)s"""
586587

587-
fk_field_name = get_foreign_key_field(fk_instance=self.limiting_edges_set_fk)
588+
fk_field_name = get_foreign_key_field(self.edge_model, self.limiting_edges_set_fk)
588589
if fk_field_name is not None:
589590
self.where_clauses_part_2 += "\n" + LIMITING_EDGES_SET_FK_CLAUSE.format(
590591
relationship_table=self.edge_model_table,

django_postgresql_dag/transformations.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
"""
55

66
import networkx as nx
7-
import pandas as pd
87

98
from .utils import (
10-
_ordered_filter,
119
get_queryset_characteristics,
1210
model_to_dict,
1311
edges_from_nodes_queryset,

django_postgresql_dag/urls.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
from django.conf import settings
2-
from django.conf.urls.static import static
3-
from django.urls import include, path, re_path
4-
51
urlpatterns = []

django_postgresql_dag/utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
Functions for transforming RawQuerySet or other outputs of
33
django-postgresql-dag to alternate formats.
44
"""
5-
5+
import inspect
66
from itertools import chain
77

8-
98
from django.core.exceptions import FieldDoesNotExist
109
from django.db.models import Case, When
1110
from django.db.models.fields import DateTimeField, UUIDField
1211
from django.db.models.fields.files import FileField, ImageField
1312
from django.db.models.fields.related import ManyToManyField
1413

15-
from .exceptions import GraphModelsCannotBeParsedException, IncorrectUsageException
14+
from .exceptions import (
15+
GraphModelsCannotBeParsedException,
16+
IncorrectUsageException,
17+
IncorrectQuerysetTypeException
18+
)
1619

1720

1821
def _ordered_filter(queryset, field_names, values):
@@ -57,6 +60,18 @@ def get_instance_characteristics(instance):
5760
return (_NodeModel, _EdgeModel, instance_type)
5861

5962

63+
def get_foreign_key_field(edge_model, fk_instance):
64+
"""
65+
Provided a model instance, checks if the edge model has a ForeignKey field to the
66+
model class of that instance, and then returns the associated field name, else None.
67+
"""
68+
for field in edge_model._meta.get_fields():
69+
if field.related_model is fk_instance._meta.model:
70+
# Return the first field that matches
71+
return field.name
72+
return None
73+
74+
6075
def get_queryset_characteristics(queryset):
6176
"""
6277
Returns a tuple of the node & edge model classes and the queryset type

0 commit comments

Comments
 (0)