Add filter class for boolean fields

This commit is contained in:
Denis Shatov
2021-09-06 16:56:55 +07:00
parent cc5f9b2670
commit 94f4adba4c
2 changed files with 22 additions and 2 deletions

View File

@ -2,7 +2,9 @@
## 1.0 ## 1.0
### 0.1.4 ### 1.0.4
- Add `widgets.filters.Boolean` class
### 1.0.3 ### 1.0.3

View File

@ -1,6 +1,6 @@
import abc import abc
from enum import Enum as EnumCLS from enum import Enum as EnumCLS
from typing import Any, Optional, Tuple, Type from typing import Any, List, Optional, Tuple, Type
import pendulum import pendulum
from starlette.requests import Request from starlette.requests import Request
@ -191,3 +191,21 @@ class DistinctColumn(Select):
async def get_values(self): async def get_values(self):
return await self.model.all().distinct().values_list(self.name) return await self.model.all().distinct().values_list(self.name)
class Boolean(Select):
async def get_options(self) -> List[Tuple[str, str]]:
"""Return list of possible values to select from."""
options = [
("TRUE", "true"),
("FALSE", "false"),
]
if self.context.get("null"):
options.insert(0, ("", ""))
return options
async def get_queryset(self, request: Request, value: str, qs: QuerySet[Model]) -> QuerySet[Model]:
"""Return filtered queryset."""
filters = {self.context.get("name"): (value == "true")}
return qs.filter(**filters)