1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-10 15:47:32 +00:00

Added Additional Date Filters

This commit is contained in:
shashikanth 2021-01-06 19:33:39 +05:30
parent 6e313cecd4
commit bcf74d74d6
4 changed files with 319 additions and 0 deletions
backend/src/baserow/contrib/database
web-frontend/modules/database

View file

@ -68,6 +68,8 @@ class DatabaseConfig(AppConfig):
from .views.view_filters import (
EqualViewFilterType, NotEqualViewFilterType, EmptyViewFilterType,
NotEmptyViewFilterType, DateEqualViewFilterType, DateNotEqualViewFilterType,
DateEqualTodayViewFilterType,DateInNearFutureViewFilterType,DateInNearPastViewFilterType,
DateInThisMonthViewFilterType,DateInThisYearViewFilterType,
HigherThanViewFilterType, LowerThanViewFilterType, ContainsViewFilterType,
ContainsNotViewFilterType, BooleanViewFilterType
)
@ -78,6 +80,11 @@ class DatabaseConfig(AppConfig):
view_filter_type_registry.register(HigherThanViewFilterType())
view_filter_type_registry.register(LowerThanViewFilterType())
view_filter_type_registry.register(DateEqualViewFilterType())
view_filter_type_registry.register(DateEqualTodayViewFilterType())
view_filter_type_registry.register(DateInNearFutureViewFilterType())
view_filter_type_registry.register(DateInNearPastViewFilterType())
view_filter_type_registry.register(DateInThisMonthViewFilterType())
view_filter_type_registry.register(DateInThisYearViewFilterType())
view_filter_type_registry.register(DateNotEqualViewFilterType())
view_filter_type_registry.register(BooleanViewFilterType())
view_filter_type_registry.register(EmptyViewFilterType())

View file

@ -4,6 +4,7 @@ from decimal import Decimal
from dateutil import parser
from dateutil.parser import ParserError
from datetime import datetime, timedelta
from django.db.models import Q, IntegerField, BooleanField
from django.db.models.fields.related import ManyToManyField
@ -158,6 +159,142 @@ class LowerThanViewFilterType(ViewFilterType):
return Q()
class DateEqualTodayViewFilterType(ViewFilterType):
"""
The date filter parses the provided value as date and checks if the field value is
the same date. It only works if a valid ISO date is provided as value and it is
only compatible with models.DateField and models.DateTimeField.
"""
type = 'date_equal_today'
compatible_field_types = [DateFieldType.type]
def get_filter(self, field_name, value, model_field):
"""
Parses the provided value string and converts it to an aware datetime object.
That object will used to make a comparison with today.
"""
try:
today = datetime.utcnow()
except (ParserError, ValueError):
return Q()
return Q(**{
f'{field_name}__year': today.year,
f'{field_name}__month': today.month,
f'{field_name}__day': today.day
})
class DateInThisMonthViewFilterType(ViewFilterType):
"""
The date filter parses the provided value as date and checks if the field value is
the same date. It only works if a valid ISO date is provided as value and it is
only compatible with models.DateField and models.DateTimeField.
"""
type = 'date_in_this_month'
compatible_field_types = [DateFieldType.type]
def get_filter(self, field_name, value, model_field):
"""
Parses the provided value string and converts it to an aware datetime object.
That object will used to make a comparison with today.
"""
try:
today = datetime.utcnow()
except (ParserError, ValueError):
return Q()
return Q(**{
f'{field_name}__year': today.year,
f'{field_name}__month': today.month
})
class DateInThisYearViewFilterType(ViewFilterType):
"""
The date filter parses the provided value as date and checks if the field value is
the same date. It only works if a valid ISO date is provided as value and it is
only compatible with models.DateField and models.DateTimeField.
"""
type = 'date_in_this_year'
compatible_field_types = [DateFieldType.type]
def get_filter(self, field_name, value, model_field):
"""
Parses the provided value string and converts it to an aware datetime object.
That object will used to make a comparison with today.
"""
try:
today = datetime.utcnow()
except (ParserError, ValueError):
return Q()
return Q(**{
f'{field_name}__year': today.year,
})
class DateInNearFutureViewFilterType(ViewFilterType):
"""
The date filter parses the provided value as date and checks if the field value is
the same date. It only works if a valid ISO date is provided as value and it is
only compatible with models.DateField and models.DateTimeField.
"""
type = 'date_in_near_future'
compatible_field_types = [DateFieldType.type]
def get_filter(self, field_name, value, model_field):
"""
Parses the provided value string and converts it to an aware datetime object.
That object will used to make a comparison with the provided field name.
"""
try:
value = int(value.strip())
except (ParserError, ValueError):
return Q()
if value == '':
return Q()
today = datetime.utcnow()
lastDay = today + timedelta(days=value)
return Q(**{f'{field_name}__lte': lastDay,f'{field_name}__gte': today})
class DateInNearPastViewFilterType(ViewFilterType):
"""
The date filter parses the provided value as date and checks if the field value is
the same date. It only works if a valid ISO date is provided as value and it is
only compatible with models.DateField and models.DateTimeField.
"""
type = 'date_in_near_past'
compatible_field_types = [DateFieldType.type]
def get_filter(self, field_name, value, model_field):
"""
Parses the provided value string and converts it to an aware datetime object.
That object will used to make a comparison with the provided field name.
"""
try:
value = int(value.strip())
except (ParserError, ValueError):
return Q()
if value == '':
return Q()
today = datetime.utcnow()
lastDay = today - timedelta(days=value)
return Q(**{f'{field_name}__lte': today,f'{field_name}__gte': lastDay})
class DateEqualViewFilterType(ViewFilterType):
"""
The date filter parses the provided value as date and checks if the field value is

View file

@ -15,6 +15,11 @@ import {
EqualViewFilterType,
NotEqualViewFilterType,
DateEqualViewFilterType,
DateEqualTodayViewFilterType,
DateInNearFutureViewFilterType,
DateInNearPastViewFilterType,
DateInThisMonthViewFilterType,
DateInThisYearViewFilterType,
DateNotEqualViewFilterType,
ContainsViewFilterType,
ContainsNotViewFilterType,
@ -46,6 +51,11 @@ export default ({ store, app }) => {
app.$registry.register('viewFilter', new EqualViewFilterType())
app.$registry.register('viewFilter', new NotEqualViewFilterType())
app.$registry.register('viewFilter', new DateEqualViewFilterType())
app.$registry.register('viewFilter', new DateEqualTodayViewFilterType())
app.$registry.register('viewFilter', new DateInNearFutureViewFilterType())
app.$registry.register('viewFilter', new DateInNearPastViewFilterType())
app.$registry.register('viewFilter', new DateInThisMonthViewFilterType())
app.$registry.register('viewFilter', new DateInThisYearViewFilterType())
app.$registry.register('viewFilter', new DateNotEqualViewFilterType())
app.$registry.register('viewFilter', new ContainsViewFilterType())
app.$registry.register('viewFilter', new ContainsNotViewFilterType())

View file

@ -220,6 +220,171 @@ export class DateEqualViewFilterType extends ViewFilterType {
}
}
export class DateInNearPastViewFilterType extends ViewFilterType {
static getType() {
return 'date_in_near_past'
}
getName() {
return 'is in last few days'
}
getInputComponent() {
return ViewFilterTypeNumber
}
getExample() {
return '1'
}
getCompatibleFieldTypes() {
return ['date']
}
matches(rowValue, filterValue) {
if (rowValue === null) {
rowValue = ''
}
const rowDate = new Date(rowValue)
const today = new Date()
const difference = rowDate - today
return !isNaN(filterValue) && filterValue <= difference
}
}
export class DateInNearFutureViewFilterType extends ViewFilterType {
static getType() {
return 'date_in_near_future'
}
getName() {
return 'is in coming days'
}
getInputComponent() {
return ViewFilterTypeNumber
}
getExample() {
return '1'
}
getCompatibleFieldTypes() {
return ['date']
}
matches(rowValue, filterValue) {
if (rowValue === null) {
rowValue = ''
}
const rowDate = new Date(rowValue)
const today = new Date()
const difference = today - rowDate
return !isNaN(filterValue) && difference <= filterValue
}
}
export class DateEqualTodayViewFilterType extends ViewFilterType {
static getType() {
return 'date_equal_today'
}
getName() {
return 'is today'
}
prepareValue(value) {
return new Date().toDateString()
}
getExample() {
return '2020-01-01'
}
getCompatibleFieldTypes() {
return ['date']
}
matches(rowValue, filterValue) {
if (rowValue === null) {
rowValue = ''
}
rowValue = rowValue.toString().toLowerCase().trim()
rowValue = rowValue.slice(0, 10)
return filterValue === '' || rowValue === filterValue
}
}
export class DateInThisMonthViewFilterType extends ViewFilterType {
static getType() {
return 'date_in_this_month'
}
getName() {
return 'is in this month'
}
prepareValue(value) {
return new Date()
}
getExample() {
return '2020-01-01'
}
getCompatibleFieldTypes() {
return ['date']
}
matches(rowValue, filterValue) {
if (rowValue === null) {
rowValue = ''
}
const sameYear = rowValue.getYear() === filterValue.getYear()
const sameMonth = rowValue.getMonth() === filterValue.getMonth()
return filterValue === '' || (sameYear && sameMonth)
}
}
export class DateInThisYearViewFilterType extends ViewFilterType {
static getType() {
return 'date_in_this_year'
}
getName() {
return 'is in this year'
}
prepareValue(value) {
return new Date()
}
getExample() {
return '2020-01-01'
}
getCompatibleFieldTypes() {
return ['date']
}
matches(rowValue, filterValue) {
if (rowValue === null) {
rowValue = ''
}
const sameYear = rowValue.getYear() === filterValue.getYear()
return filterValue === '' || sameYear
}
}
export class DateNotEqualViewFilterType extends ViewFilterType {
static getType() {
return 'date_not_equal'