1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-15 01:28:30 +00:00

Implementing the missing 'in this week' date filter.

This commit is contained in:
Peter Evans 2022-07-14 09:03:14 +00:00 committed by Bram Wiepjes
parent 1ca4649669
commit deeda763bf
9 changed files with 186 additions and 3 deletions
backend
src/baserow
config/settings
contrib/database
tests/baserow/contrib/database/view
changelog.md
web-frontend
locales
modules/database
test/unit/database

View file

@ -373,6 +373,7 @@ SPECTACULAR_SETTINGS = {
"date_not_equal",
"date_equals_today",
"date_equals_days_ago",
"date_equals_week",
"date_equals_month",
"date_equals_day_of_month",
"date_equals_year",

View file

@ -235,6 +235,7 @@ class DatabaseConfig(AppConfig):
DateEqualsDaysAgoViewFilterType,
DateEqualsMonthsAgoViewFilterType,
DateEqualsYearsAgoViewFilterType,
DateEqualsCurrentWeekViewFilterType,
DateEqualsCurrentMonthViewFilterType,
DateEqualsCurrentYearViewFilterType,
HigherThanViewFilterType,
@ -273,6 +274,7 @@ class DatabaseConfig(AppConfig):
view_filter_type_registry.register(DateEqualsDaysAgoViewFilterType())
view_filter_type_registry.register(DateEqualsMonthsAgoViewFilterType())
view_filter_type_registry.register(DateEqualsYearsAgoViewFilterType())
view_filter_type_registry.register(DateEqualsCurrentWeekViewFilterType())
view_filter_type_registry.register(DateEqualsCurrentMonthViewFilterType())
view_filter_type_registry.register(DateEqualsDayOfMonthViewFilterType())
view_filter_type_registry.register(DateEqualsCurrentYearViewFilterType())

View file

@ -481,6 +481,9 @@ class DateEqualsTodayViewFilterType(ViewFilterType):
query_dict[f"{query_field_name}__year"] = now.year
if "month" in self.query_for:
query_dict[f"{query_field_name}__month"] = now.month
if "week" in self.query_for:
week = now.isocalendar()[1]
query_dict[f"{query_field_name}__week"] = week
if "day" in self.query_for:
query_dict[f"{query_field_name}__day"] = now.day
@ -620,6 +623,16 @@ class DateEqualsYearsAgoViewFilterType(DateEqualsXAgoViewFilterType):
return now + relativedelta(years=-x_units_ago)
class DateEqualsCurrentWeekViewFilterType(DateEqualsTodayViewFilterType):
"""
The current week filter works as a subset of today filter and checks if the
field value falls into current week.
"""
type = "date_equals_week"
query_for = ["year", "week"]
class DateEqualsCurrentMonthViewFilterType(DateEqualsTodayViewFilterType):
"""
The current month filter works as a subset of today filter and checks if the

View file

@ -2385,7 +2385,7 @@ def test_last_modified_year_filter_type(data_fixture):
@pytest.mark.django_db
def test_date_day_month_year_filter_type(data_fixture):
def test_date_day_week_month_year_filter_type(data_fixture):
user = data_fixture.create_user()
table = data_fixture.create_database_table(user=user)
grid_view = data_fixture.create_grid_view(table=table)
@ -2409,6 +2409,11 @@ def test_date_day_month_year_filter_type(data_fixture):
f"field_{date_field.id}": date(2021, 1, 2),
}
)
row_4 = model.objects.create(
**{
f"field_{date_field.id}": date(2021, 1, 4),
}
)
model.objects.create(
**{
f"field_{date_field.id}": None,
@ -2431,7 +2436,7 @@ def test_date_day_month_year_filter_type(data_fixture):
assert len(ids) == 1
assert row.id not in ids
view_filter.type = "date_equals_month"
view_filter.type = "date_equals_week"
view_filter.save()
ids = [
@ -2441,16 +2446,28 @@ def test_date_day_month_year_filter_type(data_fixture):
assert row_2.id in ids
assert row_3.id in ids
view_filter.type = "date_equals_year"
view_filter.type = "date_equals_month"
view_filter.save()
ids = [
r.id for r in handler.apply_filters(grid_view, model.objects.all()).all()
]
assert len(ids) == 3
assert row_2.id in ids
assert row_3.id in ids
assert row_4.id in ids
view_filter.type = "date_equals_year"
view_filter.save()
ids = [
r.id for r in handler.apply_filters(grid_view, model.objects.all()).all()
]
assert len(ids) == 4
assert row.id in ids
assert row_2.id in ids
assert row_3.id in ids
assert row_4.id in ids
view_filter.type = "date_equals_today"
view_filter.save()

View file

@ -11,6 +11,7 @@ For example:
### New Features
* Added `in this week` filter [#569](https://gitlab.com/bramw/baserow/-/issues/954).
* Added a new "is months ago filter". [#1018](https://gitlab.com/bramw/baserow/-/issues/1018)
* Added a new "is years ago filter". [#1019](https://gitlab.com/bramw/baserow/-/issues/1019)
@ -99,6 +100,8 @@ For example:
* Fixed bad request displayed with webhook endpoints that redirects
* **breaking change** The API endpoint `/api/templates/install/<group_id>/<template_id>/`
is now a POST request instead of GET.
## Released (2022-10-05 1.10.0)
* Prevent the Airtable import from failing hard when an invalid date is provided.
* Increased the max decimal places of a number field to 10.
* Fix formula autocomplete for fields with multiple quotes

View file

@ -136,6 +136,7 @@
"isDaysAgo": "is days ago",
"isMonthsAgo": "is months ago",
"isYearsAgo": "is years ago",
"inThisWeek": "in this week",
"inThisMonth": "in this month",
"inThisYear": "in this year",
"lowerThan": "lower than",

View file

@ -44,6 +44,7 @@ import {
DateEqualsDaysAgoViewFilterType,
DateEqualsMonthsAgoViewFilterType,
DateEqualsYearsAgoViewFilterType,
DateEqualsCurrentWeekViewFilterType,
DateEqualsCurrentMonthViewFilterType,
DateEqualsCurrentYearViewFilterType,
DateBeforeViewFilterType,
@ -241,6 +242,10 @@ export default (context) => {
'viewFilter',
new DateEqualsYearsAgoViewFilterType(context)
)
app.$registry.register(
'viewFilter',
new DateEqualsCurrentWeekViewFilterType(context)
)
app.$registry.register(
'viewFilter',
new DateEqualsCurrentMonthViewFilterType(context)

View file

@ -802,6 +802,36 @@ export class DateEqualsYearsAgoViewFilterType extends DateEqualsXAgoViewFilterTy
}
}
export class DateEqualsCurrentWeekViewFilterType extends DateEqualsTodayViewFilterType {
static getType() {
return 'date_equals_week'
}
getName() {
const { i18n } = this.app
return i18n.t('viewFilter.inThisWeek')
}
matches(rowValue, filterValue, field) {
if (rowValue === null) {
rowValue = ''
}
// If the field.timezone property is set (last modified and created on),
// the original value is in UTC0 and must be converted to the timezone of the filter.
// If the field.timezone property is not set (date+time), the original value is already
// in the right timezone because it's naive, so it doesn't have to be converted,
// but it must be marked as the same timezone.
rowValue = moment.utc(rowValue).tz(filterValue, !field.timezone)
const today = moment().tz(filterValue)
const firstDay = today.clone().startOf('isoWeek')
const lastDay = today.clone().endOf('isoWeek')
return rowValue.isBetween(firstDay, lastDay, null, '[]')
}
}
export class DateEqualsCurrentMonthViewFilterType extends DateEqualsTodayViewFilterType {
static getType() {
return 'date_equals_month'

View file

@ -15,6 +15,9 @@ import {
LengthIsLowerThanViewFilterType,
LinkRowContainsFilterType,
LinkRowNotContainsFilterType,
DateEqualsCurrentWeekViewFilterType,
DateEqualsCurrentMonthViewFilterType,
DateEqualsCurrentYearViewFilterType,
} from '@baserow/modules/database/viewFilters'
const dateBeforeCasesWithTimezone = [
@ -299,6 +302,65 @@ const dateToday = [
},
]
const dateInThisWeek = [
{
rowValue: '2022-05-29',
filterValue: 'Europe/Berlin',
expected: false,
},
{
rowValue: '2022-05-29T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: false,
},
{
rowValue: '2022-05-30',
filterValue: 'Europe/Berlin',
expected: true,
},
{
rowValue: '2022-05-30T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: true,
},
{
rowValue: '2022-06-05T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: true,
},
{
rowValue: '2022-06-06T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: false,
},
]
const dateInThisMonth = [
{
rowValue: '2022-05-01T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: false,
},
{
rowValue: '2022-06-01T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: true,
},
]
const dateInThisYear = [
{
rowValue: '2021-06-01T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: false,
},
{
rowValue: '2022-06-01T12:00:00.000000Z',
filterValue: 'Europe/Berlin',
expected: true,
},
]
const dateDaysAgo = [
{
rowValue: moment().tz('Europe/Berlin').subtract(1, 'days').format(),
@ -510,6 +572,55 @@ const lengthIsLowerThanCases = [
},
]
describe('Date in this week, month and year tests', () => {
let testApp = null
let dateNowSpy
beforeAll(() => {
testApp = new TestApp()
// Wed Jun 01 2022 00:00:00
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => 1654038000000)
})
afterAll(() => {
dateNowSpy.mockRestore()
})
afterEach(() => {
testApp.afterEach()
})
test.each(dateInThisWeek)('DateInThisWeek with timezone.', (values) => {
const result = new DateEqualsCurrentWeekViewFilterType({
app: testApp,
}).matches(values.rowValue, values.filterValue, {
timezone: values.timezone,
})
expect(result).toBe(values.expected)
})
test.each(dateInThisWeek)('DateInThisWeek without timezone.', (values) => {
const result = new DateEqualsCurrentWeekViewFilterType({
app: testApp,
}).matches(values.rowValue, values.filterValue, {})
expect(result).toBe(values.expected)
})
test.each(dateInThisMonth)('DateInThisMonth', (values) => {
const result = new DateEqualsCurrentMonthViewFilterType({
app: testApp,
}).matches(values.rowValue, values.filterValue, {})
expect(result).toBe(values.expected)
})
test.each(dateInThisYear)('DateInThisYear', (values) => {
const result = new DateEqualsCurrentYearViewFilterType({
app: testApp,
}).matches(values.rowValue, values.filterValue, {})
expect(result).toBe(values.expected)
})
})
const linkRowContainsCases = [
{
rowValue: [{ value: 'bill' }],