Skip to content

Commit 048534f

Browse files
committed
Refactor mql form clean() method
1 parent 5c3e9b7 commit 048534f

File tree

1 file changed

+13
-12
lines changed
  • django_mongodb_extensions/mql_panel

1 file changed

+13
-12
lines changed

django_mongodb_extensions/mql_panel/forms.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
class MQLBaseForm(SQLSelectForm):
2121
def clean(self):
22-
# Explicitly call forms.Form.clean() to bypass SQLSelectForm.clean()
23-
# which has SQL-specific validation not needed for MQL queries.
22+
# Call forms.Form.clean() to bypass SQLSelectForm.clean() which has
23+
# SQL-specific validation
2424
cleaned_data = forms.Form.clean(self)
2525
request_id = cleaned_data.get("request_id")
26-
djdt_query_id = cleaned_data.get("djdt_query_id")
2726
if not request_id:
2827
raise ValidationError(_("Missing request ID."))
28+
djdt_query_id = cleaned_data.get("djdt_query_id")
2929
if not djdt_query_id:
3030
raise ValidationError(_("Missing query ID."))
3131
toolbar = DebugToolbar.fetch(request_id, panel_id=MQL_PANEL_ID)
@@ -35,18 +35,19 @@ def clean(self):
3535
stats = panel.get_stats()
3636
if not stats or "queries" not in stats:
3737
raise ValidationError(_("Query data is not available."))
38-
query = next(
39-
(
40-
_query
41-
for _query in stats["queries"]
42-
if isinstance(_query, dict)
38+
# Find query in stats using djdt_query_id
39+
query = None
40+
for _query in stats["queries"]:
41+
if (
42+
isinstance(_query, dict)
4343
and _query.get("djdt_query_id") == djdt_query_id
44-
),
45-
None,
46-
)
44+
):
45+
query = _query
46+
break
4747
if not query:
4848
raise ValidationError(_("Invalid query ID."))
49-
if not all(key in query for key in ["alias", "mql"]):
49+
# Ensure query contains required keys
50+
if not all(key in query for key in ("alias", "mql")):
5051
raise ValidationError(_("Query data is incomplete."))
5152
cleaned_data["query"] = query
5253
return cleaned_data

0 commit comments

Comments
 (0)