From 17631304c24b2a7f7304f5dc90d883ec00380ec2 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 15 Dec 2023 12:18:44 -0800 Subject: [PATCH] models: Extract zerver.models.lookups. Signed-off-by: Anders Kaseorg --- zerver/models/__init__.py | 34 +--------------------------------- zerver/models/lookups.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 zerver/models/lookups.py diff --git a/zerver/models/__init__.py b/zerver/models/__init__.py index 8de3b242bc..ce60472b55 100644 --- a/zerver/models/__init__.py +++ b/zerver/models/__init__.py @@ -1,10 +1,4 @@ -from typing import List, Tuple, Union - -from django.db import models -from django.db.backends.base.base import BaseDatabaseWrapper -from django.db.models.sql.compiler import SQLCompiler -from typing_extensions import override - +from zerver.models import lookups as lookups from zerver.models.alert_words import AlertWord as AlertWord from zerver.models.bots import BotConfigData as BotConfigData from zerver.models.bots import BotStorageData as BotStorageData @@ -71,29 +65,3 @@ from zerver.models.user_topics import UserTopic as UserTopic from zerver.models.users import RealmUserDefault as RealmUserDefault from zerver.models.users import UserBaseSettings as UserBaseSettings from zerver.models.users import UserProfile as UserProfile - - -@models.Field.register_lookup -class AndZero(models.Lookup[int]): - lookup_name = "andz" - - @override - def as_sql( - self, compiler: SQLCompiler, connection: BaseDatabaseWrapper - ) -> Tuple[str, List[Union[str, int]]]: # nocoverage # currently only used in migrations - lhs, lhs_params = self.process_lhs(compiler, connection) - rhs, rhs_params = self.process_rhs(compiler, connection) - return f"{lhs} & {rhs} = 0", lhs_params + rhs_params - - -@models.Field.register_lookup -class AndNonZero(models.Lookup[int]): - lookup_name = "andnz" - - @override - def as_sql( - self, compiler: SQLCompiler, connection: BaseDatabaseWrapper - ) -> Tuple[str, List[Union[str, int]]]: # nocoverage # currently only used in migrations - lhs, lhs_params = self.process_lhs(compiler, connection) - rhs, rhs_params = self.process_rhs(compiler, connection) - return f"{lhs} & {rhs} != 0", lhs_params + rhs_params diff --git a/zerver/models/lookups.py b/zerver/models/lookups.py new file mode 100644 index 0000000000..484cc889ae --- /dev/null +++ b/zerver/models/lookups.py @@ -0,0 +1,32 @@ +from typing import List, Tuple, Union + +from django.db import models +from django.db.backends.base.base import BaseDatabaseWrapper +from django.db.models.sql.compiler import SQLCompiler +from typing_extensions import override + + +@models.Field.register_lookup +class AndZero(models.Lookup[int]): + lookup_name = "andz" + + @override + def as_sql( + self, compiler: SQLCompiler, connection: BaseDatabaseWrapper + ) -> Tuple[str, List[Union[str, int]]]: # nocoverage # currently only used in migrations + lhs, lhs_params = self.process_lhs(compiler, connection) + rhs, rhs_params = self.process_rhs(compiler, connection) + return f"{lhs} & {rhs} = 0", lhs_params + rhs_params + + +@models.Field.register_lookup +class AndNonZero(models.Lookup[int]): + lookup_name = "andnz" + + @override + def as_sql( + self, compiler: SQLCompiler, connection: BaseDatabaseWrapper + ) -> Tuple[str, List[Union[str, int]]]: # nocoverage # currently only used in migrations + lhs, lhs_params = self.process_lhs(compiler, connection) + rhs, rhs_params = self.process_rhs(compiler, connection) + return f"{lhs} & {rhs} != 0", lhs_params + rhs_params