Skip to content

Commit 48db30a

Browse files
committed
Refactor
1 parent e548777 commit 48db30a

File tree

3 files changed

+53
-50
lines changed

3 files changed

+53
-50
lines changed

django_mongodb_backend/fields/embedded_model_array.py

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,55 +66,56 @@ class EMFArrayExact(EMFExact):
6666
def as_mql(self, compiler, connection):
6767
lhs_mql = process_lhs(self, compiler, connection)
6868
value = process_rhs(self, compiler, connection)
69-
if isinstance(self.lhs, Col | KeyTransform):
70-
if isinstance(self.lhs, Col):
71-
inner_lhs_mql = "$$item"
72-
else:
73-
lhs_mql, inner_lhs_mql = lhs_mql
74-
if isinstance(value, models.Model):
75-
value, emf_data = self.model_to_dict(value)
76-
# Get conditions for any nested EmbeddedModelFields.
77-
conditions = self.get_conditions({inner_lhs_mql: (value, emf_data)})
78-
return {
79-
"$anyElementTrue": {
80-
"$ifNull": [
81-
{
82-
"$map": {
83-
"input": lhs_mql,
84-
"as": "item",
85-
"in": {"$and": conditions},
86-
}
87-
},
88-
[],
89-
]
90-
}
91-
}
69+
if isinstance(self.lhs, KeyTransform):
70+
lhs_mql, inner_lhs_mql = lhs_mql
71+
else:
72+
inner_lhs_mql = "$$item"
73+
if isinstance(value, models.Model):
74+
value, emf_data = self.model_to_dict(value)
75+
# Get conditions for any nested EmbeddedModelFields.
76+
conditions = self.get_conditions({inner_lhs_mql: (value, emf_data)})
9277
return {
9378
"$anyElementTrue": {
9479
"$ifNull": [
9580
{
9681
"$map": {
9782
"input": lhs_mql,
9883
"as": "item",
99-
"in": {"$eq": [inner_lhs_mql, value]},
84+
"in": {"$and": conditions},
10085
}
10186
},
10287
[],
10388
]
10489
}
10590
}
106-
return connection.mongo_operators[self.lookup_name](lhs_mql, value)
91+
return {
92+
"$anyElementTrue": {
93+
"$ifNull": [
94+
{
95+
"$map": {
96+
"input": lhs_mql,
97+
"as": "item",
98+
"in": {"$eq": [inner_lhs_mql, value]},
99+
}
100+
},
101+
[],
102+
]
103+
}
104+
}
107105

108106

109107
class KeyTransform(Transform):
110108
# it should be different class than EMF keytransform even most of the methods are equal.
111109
def __init__(self, key_name, base_field, *args, **kwargs):
112110
super().__init__(*args, **kwargs)
113111
self.base_field = base_field
114-
# TODO: Need to create a column, will refactor this thing.
112+
self.key_name = key_name
113+
# The iteration items begins from the base_field, a virtual column with
114+
# base field output type is created.
115115
column_target = base_field.clone()
116-
column_target.db_column = f"$item.{key_name}"
117-
column_target.set_attributes_from_name(f"$item.{key_name}")
116+
column_name = f"$item.{key_name}"
117+
column_target.db_column = column_name
118+
column_target.set_attributes_from_name(column_name)
118119
self._lhs = Col(None, column_target)
119120
self._sub_transform = None
120121

@@ -125,19 +126,8 @@ def __call__(self, this, *args, **kwargs):
125126
def get_lookup(self, name):
126127
return self.output_field.get_lookup(name)
127128

128-
def get_transform(self, name):
129-
"""
130-
Validate that `name` is either a field of an embedded model or a
131-
lookup on an embedded model's field.
132-
"""
133-
if isinstance(self._lhs, Transform):
134-
transform = self._lhs.get_transform(name)
135-
else:
136-
transform = self.base_field.get_transform(name)
137-
if transform:
138-
self._sub_transform = transform
139-
return self
140-
suggested_lookups = difflib.get_close_matches(name, self.base_field.get_lookups())
129+
def _get_missing_field_or_lookup_exception(self, lhs, name):
130+
suggested_lookups = difflib.get_close_matches(name, lhs.get_lookups())
141131
if suggested_lookups:
142132
suggested_lookups = " or ".join(suggested_lookups)
143133
suggestion = f", perhaps you meant {suggested_lookups}?"
@@ -149,6 +139,25 @@ def get_transform(self, name):
149139
f"{suggestion}"
150140
)
151141

142+
def get_transform(self, name):
143+
"""
144+
Validate that `name` is either a field of an embedded model or a
145+
lookup on an embedded model's field.
146+
"""
147+
# Once the sub lhs is a transform, all the filter are applied over it.
148+
149+
transform = (
150+
self._lhs.get_transform(name)
151+
if isinstance(self._lhs, Transform)
152+
else self.base_field.get_transform(name)
153+
)
154+
if transform:
155+
self._sub_transform = transform
156+
return self
157+
raise self._get_missing_field_or_lookup_exception(
158+
self._lhs if isinstance(self._lhs, Transform) else self.base_field, name
159+
)
160+
152161
def as_mql(self, compiler, connection):
153162
inner_lhs_mql = self._lhs.as_mql(compiler, connection)
154163
lhs_mql = process_lhs(self, compiler, connection)

tests/model_fields_/models.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,12 @@ class ExhibitSection(EmbeddedModel):
188188
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
189189

190190

191-
class ExhibitMeta(EmbeddedModel):
192-
curator_name = models.CharField(max_length=255)
193-
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
194-
195-
196191
class MuseumExhibit(models.Model):
197192
"""An exhibit in the museum, composed of multiple sections."""
198193

199194
exhibit_name = models.CharField(max_length=255)
200195
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
201-
meta = EmbeddedModelField(ExhibitMeta, null=True)
196+
main_section = EmbeddedModelField(ExhibitSection, null=True)
202197

203198
def __str__(self):
204199
return self.exhibit_name

tests/model_fields_/test_embedded_model.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
Author,
2424
Book,
2525
Data,
26-
ExhibitMeta,
2726
ExhibitSection,
2827
Holder,
2928
Library,
@@ -202,8 +201,8 @@ def setUpTestData(cls):
202201
)
203202
cls.lost_empires = MuseumExhibit.objects.create(
204203
exhibit_name="Lost Empires",
205-
meta=ExhibitMeta(
206-
curator_name="Dr. Amina Hale",
204+
main_section=ExhibitSection(
205+
section_number=3,
207206
artifacts=[
208207
ArtifactDetail(
209208
name="Bronze Statue",

0 commit comments

Comments
 (0)