Skip to content

Commit 8663c7d

Browse files
author
maxac
committed
fix: ограничить периоды истории сверху концом текущего дня
Односторонние фильтры (day/week/month30/month/year) использовали только created_at >= start, из-за чего записи с будущей датой (отредактированной вручную) протекали в выборку — в 'сегодня' попадал завтрашний день.
1 parent cc2551a commit 8663c7d

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

core/database/requests/_common.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ def apply_period_filter(
2828
elif now.tzinfo is not None:
2929
now = now.replace(tzinfo=None)
3030

31+
# Upper bound for "up to today" periods: end of the current day.
32+
# Without it, future-dated records (user-edited date) leak into the period.
33+
end_of_today = now.replace(
34+
hour=0, minute=0, second=0, microsecond=0
35+
) + timedelta(days=1)
36+
3137
if within == "day":
3238
start = now.replace(hour=0, minute=0, second=0, microsecond=0)
33-
query = query.where(Record.created_at >= start)
39+
query = query.where(Record.created_at >= start, Record.created_at < end_of_today)
3440
elif within == "yesterday":
3541
yesterday = now - timedelta(days=1)
3642
start = yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
@@ -40,15 +46,15 @@ def apply_period_filter(
4046
start = (now - timedelta(days=7)).replace(
4147
hour=0, minute=0, second=0, microsecond=0
4248
)
43-
query = query.where(Record.created_at >= start)
49+
query = query.where(Record.created_at >= start, Record.created_at < end_of_today)
4450
elif within == "month30":
4551
start = (now - timedelta(days=30)).replace(
4652
hour=0, minute=0, second=0, microsecond=0
4753
)
48-
query = query.where(Record.created_at >= start)
54+
query = query.where(Record.created_at >= start, Record.created_at < end_of_today)
4955
elif within == "month":
5056
start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
51-
query = query.where(Record.created_at >= start)
57+
query = query.where(Record.created_at >= start, Record.created_at < end_of_today)
5258
elif within == "prev_month":
5359
first_this_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
5460
last_prev_month = first_this_month - timedelta(days=1)
@@ -59,7 +65,7 @@ def apply_period_filter(
5965
query = query.where(Record.created_at.between(start, end))
6066
elif within == "year":
6167
start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
62-
query = query.where(Record.created_at >= start)
68+
query = query.where(Record.created_at >= start, Record.created_at < end_of_today)
6369
elif within == "date" and date_from:
6470
start = date_from.replace(
6571
hour=0, minute=0, second=0, microsecond=0, tzinfo=None

0 commit comments

Comments
 (0)