Skip to content

Commit

Permalink
chore: update pyproject.toml + django 5.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
gersmann committed Apr 7, 2024
1 parent eb68891 commit 83a8452
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 340 deletions.
9 changes: 2 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ repos:

- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: "v0.1.1"
rev: "v0.3.5"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
language_version: python3.11
- id: ruff-format
4 changes: 2 additions & 2 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_distinct_clause(self):
{"$replaceRoot": {"newRoot": "$_id"}},
]

def as_operation(self, with_limits=True, with_col_aliases=False):
def as_operation(self, with_limits=True, with_col_aliases=False): # noqa: C901
combinator = self.query.combinator
extra_select, order_by, group_by = self.pre_sql_setup(
with_col_aliases=with_col_aliases or bool(combinator),
Expand Down Expand Up @@ -145,7 +145,7 @@ def execute_sql(
if result_type == CURSOR:
return cursor
if result_type == SINGLE:
cols = [col for col in self.select[0 : self.col_count]]
cols = list(self.select)
result = cursor.fetchone()
if result:
return (result.get(alias or col.target.attname) for col, _, alias in cols)
Expand Down
7 changes: 3 additions & 4 deletions django_mongodb/cursor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import logging

from pymongo import MongoClient
from pymongo.cursor import Cursor as MongoCursor
Expand All @@ -11,7 +11,7 @@

from django_mongodb.database import InterfaceError, NotSupportedError

DEBUG = os.environ.get("DEBUG", False)
logger = logging.getLogger(__name__)


class Cursor:
Expand Down Expand Up @@ -69,8 +69,7 @@ def lastrowid(self):
raise NotSupportedError

def execute(self, command, params=None):
if DEBUG:
print(command)
logger.info(command)
match command:
case {"op": "aggregate"}:
self.result = self.connection[command["collection"]].aggregate(command["pipeline"])
Expand Down
18 changes: 9 additions & 9 deletions django_mongodb/introspection.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import NamedTuple
from dataclasses import dataclass

from django.db.backends.base.introspection import BaseDatabaseIntrospection

TableInfo = NamedTuple(
"TableInfo",
[
("name", str),
("type", str),
],
)

@dataclass
class TableInfo:
name: str
type: str


class DatabaseIntrospection(BaseDatabaseIntrospection):
def get_table_list(self, cursor):
with self.connection.cursor() as conn:
return [TableInfo(name, "t") for name in conn.connection.list_collection_names()]
return [
TableInfo(name=name, type="t") for name in conn.connection.list_collection_names()
]
2 changes: 1 addition & 1 deletion django_mongodb/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DatabaseOperations(BaseDatabaseOperations):
def quote_name(self, name):
if name.startswith('"') and name.endswith('"'):
return name
return '"{}"'.format(name)
return f'"{name}"'

def sql_flush(self, style, tables, *, reset_sequences=False, allow_cascade=False):
return [{"op": "flush", "collection": table} for table in tables]
Expand Down
28 changes: 17 additions & 11 deletions django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
GreaterThan,
GreaterThanOrEqual,
In,
IntegerFieldExact,
LessThan,
LessThanOrEqual,
Lookup,
Expand Down Expand Up @@ -38,12 +39,10 @@ def requires_search(self) -> bool:
return False

@abc.abstractmethod
def get_mongo_query(self, compiler, connection, requires_search=...) -> dict:
...
def get_mongo_query(self, compiler, connection, requires_search=...) -> dict: ...

@abc.abstractmethod
def get_mongo_search(self, compiler, connection) -> dict:
...
def get_mongo_search(self, compiler, connection) -> dict: ...


class RawMongoQueryExpression(Node):
Expand Down Expand Up @@ -94,8 +93,7 @@ def get_mongo_search(self, compiler, connection) -> dict:
return self._get_mongo_search(compiler, connection)

@abc.abstractmethod
def _get_mongo_search(self, compiler, connection) -> dict:
...
def _get_mongo_search(self, compiler, connection) -> dict: ...


class MongoExact(MongoLookup):
Expand Down Expand Up @@ -148,7 +146,9 @@ class MongoEqualityComparison(MongoLookup):
filter_operator: str

def __init__(
self, operator: LessThan | LessThanOrEqual | GreaterThan | GreaterThanOrEqual, mongo_meta
self,
operator: LessThan | LessThanOrEqual | GreaterThan | GreaterThanOrEqual,
mongo_meta,
):
super().__init__(operator, mongo_meta)
self.filter_operator = {
Expand Down Expand Up @@ -186,8 +186,7 @@ def get_mongo_search(self, compiler, connection) -> dict:
return self._get_mongo_search(compiler, connection)

@abstractmethod
def _get_mongo_search(self, compiler, connection) -> dict:
...
def _get_mongo_search(self, compiler, connection) -> dict: ...


class MongoSearchLookup(SearchNode):
Expand Down Expand Up @@ -260,6 +259,7 @@ class MongoWhereNode:
node_map = {
NothingNode: MongoNothingNode,
Exact: MongoExact,
IntegerFieldExact: MongoExact,
RelatedIn: MongoRelatedIn,
RelatedExact: MongoExact,
In: MongoIn,
Expand Down Expand Up @@ -314,7 +314,10 @@ def get_mongo_query(self, compiler, connection, is_search=False) -> dict:

def get_mongo_search(self, compiler, connection) -> dict:
child_queries = list(
filter(bool, [child.get_mongo_search(compiler, connection) for child in self.children])
filter(
bool,
[child.get_mongo_search(compiler, connection) for child in self.children],
)
)
if len(child_queries) == 0:
return {}
Expand Down Expand Up @@ -355,7 +358,10 @@ def __init__(self, col: Count, alias: str | None, mongo_meta):
def get_mongo(self):
return {
"$group": {"_id": None, "_count": {"$sum": 1}},
"$project": {"_id": None, (self.alias or self.col.output_field.column): "$_count"},
"$project": {
"_id": None,
(self.alias or self.col.output_field.column): "$_count",
},
}


Expand Down
63 changes: 21 additions & 42 deletions django_mongodb/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,44 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def quote_value(self, value):
return value

def add_constraint(self, model, constraint):
...
def add_constraint(self, model, constraint): ...

def add_field(self, model, field):
...
def add_field(self, model, field): ...

def add_index(self, model, index):
...
def add_index(self, model, index): ...

def alter_db_table(self, model, old_db_table, new_db_table):
...
def alter_db_table(self, model, old_db_table, new_db_table): ...

def alter_db_table_comment(self, model, old_db_table_comment, new_db_table_comment):
...
def alter_db_table_comment(self, model, old_db_table_comment, new_db_table_comment): ...

def alter_db_tablespace(self, model, old_db_tablespace, new_db_tablespace):
...
def alter_db_tablespace(self, model, old_db_tablespace, new_db_tablespace): ...

def alter_field(self, model, old_field, new_field, strict=False):
...
def alter_field(self, model, old_field, new_field, strict=False): ...

def alter_index_together(self, model, old_index_together, new_index_together):
...
def alter_index_together(self, model, old_index_together, new_index_together): ...

def alter_unique_together(self, model, old_unique_together, new_unique_together):
...
def alter_unique_together(self, model, old_unique_together, new_unique_together): ...

def create_model(self, model):
...
def create_model(self, model): ...

def delete_model(self, model):
...
def delete_model(self, model): ...

def effective_default(self, field):
...
def effective_default(self, field): ...

def prepare_default(self, value):
...
def prepare_default(self, value): ...

def quote_name(self, name):
...
def quote_name(self, name): ...

def remove_constraint(self, model, constraint):
...
def remove_constraint(self, model, constraint): ...

def remove_field(self, model, field):
...
def remove_field(self, model, field): ...

def remove_index(self, model, index):
...
def remove_index(self, model, index): ...

def remove_procedure(self, procedure_name, param_types=()):
...
def remove_procedure(self, procedure_name, param_types=()): ...

def rename_index(self, model, old_index_name, new_index_name):
...
def rename_index(self, model, old_index_name, new_index_name): ...

def skip_default(self, field):
...
def skip_default(self, field): ...

def skip_default_on_alter(self, field):
...
def skip_default_on_alter(self, field): ...
1 change: 1 addition & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Loading

0 comments on commit 83a8452

Please sign in to comment.