Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix this_week filter [SDESK-7264] #1987

Merged
merged 9 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/planning/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ def get_start_of_next_week(date=None, start_of_week=0):
current_date = (date if date else utcnow()).replace(hour=0, minute=0, second=0, microsecond=0)
weekday = current_date.isoweekday()
weekDay = 0 if weekday == 7 else weekday
diff = start_of_week - weekDay if weekday < start_of_week else 7 - weekDay + start_of_week
diff = (start_of_week - weekDay) % 7
if diff == 0:
diff = 7
return current_date + timedelta(days=diff)


Expand Down
9 changes: 7 additions & 2 deletions server/planning/search/queries/combined.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, Any, List, Callable

from planning.search.queries import elastic, events, planning, common
from flask import current_app as app


def construct_combined_view_data_query(
Expand All @@ -22,8 +23,12 @@ def construct_combined_view_data_query(

query = elastic.ElasticQuery()

if len(search_filter["params"]):
search_dates(search_filter["params"], query)
filter_params = common.get_params_from_search_filter(search_filter)
if len(filter_params):
filter_params["time_zone"] = params.get("time_zone") or app.config.get("DEFAULT_TIMEZONE")
filter_params["start_of_week"] = params.get("start_of_week", app.config.get("START_OF_WEEK", 0))

search_dates(filter_params, query)

search_dates(params, query)

Expand Down
10 changes: 4 additions & 6 deletions server/planning/search/queries/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,19 @@ def __init__(


def start_of_this_week(start_of_week=0, date=None):
end = get_start_of_next_week(date, start_of_week) - timedelta(days=7)
start = end - timedelta(days=7)

return start.strftime("%Y-%m-%d") + "||/d"
start = get_start_of_next_week(date, start_of_week) - timedelta(days=7)
return start.strftime("%Y-%m-%d")


def start_of_next_week(start_of_week=0, date=None):
return get_start_of_next_week(date, start_of_week).strftime("%Y-%m-%d") + "||/d"
return get_start_of_next_week(date, start_of_week).strftime("%Y-%m-%d")


def end_of_next_week(start_of_week=0, date=None):
start = get_start_of_next_week(date, start_of_week)
end = start + timedelta(days=7)

return end.strftime("%Y-%m-%d") + "||/d"
return end.strftime("%Y-%m-%d")


def bool_or(conditions: List[Dict[str, Any]]):
Expand Down
39 changes: 33 additions & 6 deletions server/planning/tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,57 @@ def test_dates_same_and_different_time(self):


class TestDateRangeFunctions(TestCase):
def get_start_date_and_weekday(self, start_date_str):
start_date = datetime.strptime(start_date_str.split("||")[0], "%Y-%m-%d")
def get_weekday(self, start_date_str):
start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
return start_date.weekday()

def test_start_of_next_week(self):
# Test with default start_of_week
start_date_str = elastic.start_of_next_week()
expected_weekday = self.get_start_date_and_weekday(start_date_str)
expected_weekday = self.get_weekday(start_date_str)
self.assertEqual(expected_weekday, 6) # Sunday

# Test with default start_of_week = 1
start_date_str = elastic.start_of_next_week(start_of_week=1)
expected_weekday = self.get_start_date_and_weekday(start_date_str)
expected_weekday = self.get_weekday(start_date_str)
self.assertEqual(expected_weekday, 0) # Monday

def test_end_of_next_week(self):
# Test with default start_of_week
start_date = datetime(2024, 5, 6) # Assuming today is May 6, 2024 (Sunday)
end_date_str = elastic.end_of_next_week(date=start_date)
expected_weekday = self.get_start_date_and_weekday(end_date_str)
expected_weekday = self.get_weekday(end_date_str)
self.assertEqual(expected_weekday, 6) # Sunday

# Test with default start_of_week = 1
start_date = datetime(2024, 5, 6) # Assuming today is May 6, 2024 (Sunday)
end_date_str = elastic.end_of_next_week(date=start_date, start_of_week=1)
expected_weekday = self.get_start_date_and_weekday(end_date_str)
expected_weekday = self.get_weekday(end_date_str)
self.assertEqual(expected_weekday, 0) # Monday

def test_events_within_current_week(self):
# Test events that start and end within the current week
start_date = datetime(2024, 5, 15) # Assuming today is May 15, 2024 (Wed)
devketanpro marked this conversation as resolved.
Show resolved Hide resolved
start = elastic.start_of_this_week(date=start_date, start_of_week=1)
end = elastic.start_of_next_week(date=start_date, start_of_week=1)

self.assertEqual(start, "2024-05-13")
self.assertEqual(end, "2024-05-20")

def test_events_within_current_week_monday(self):
# Test case for Monday
start_date = datetime(2024, 5, 13) # May 13, 2024 is a Monday
start = elastic.start_of_this_week(date=start_date, start_of_week=1)
end = elastic.start_of_next_week(date=start_date, start_of_week=1)

self.assertEqual(start, "2024-05-13")
self.assertEqual(end, "2024-05-20")

def test_events_within_current_week_sunday(self):
# Test case for Sunday
start_date = datetime(2024, 5, 19) # May 19, 2024 is a Sunday
start = elastic.start_of_this_week(date=start_date, start_of_week=1)
end = elastic.start_of_next_week(date=start_date, start_of_week=1)

self.assertEqual(start, "2024-05-13")
self.assertEqual(end, "2024-05-20")
Loading