models: Extract zerver.models.lookups.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-12-15 12:18:44 -08:00 committed by Alex Vandiver
parent cf14f27602
commit 17631304c2
2 changed files with 33 additions and 33 deletions

View File

@ -1,10 +1,4 @@
from typing import List, Tuple, Union from zerver.models import lookups as lookups
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.alert_words import AlertWord as AlertWord from zerver.models.alert_words import AlertWord as AlertWord
from zerver.models.bots import BotConfigData as BotConfigData from zerver.models.bots import BotConfigData as BotConfigData
from zerver.models.bots import BotStorageData as BotStorageData 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 RealmUserDefault as RealmUserDefault
from zerver.models.users import UserBaseSettings as UserBaseSettings from zerver.models.users import UserBaseSettings as UserBaseSettings
from zerver.models.users import UserProfile as UserProfile 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

32
zerver/models/lookups.py Normal file
View File

@ -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