8000 fix: Chart date format on x-axis is inaccurate by shariquerik · Pull Request #18191 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: Chart date format on x-axis is inaccurate #18191

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

Merged
merged 6 commits into from
Sep 21, 2022
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
2 changes: 1 addition & 1 deletion frappe/desk/doctype/dashboard_chart/dashboard_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def get_chart_config(chart, filters, timespan, timegrain, from_date, to_date):

return {
"labels": [
format_date(get_period(r[0], timegrain))
format_date(get_period(r[0], timegrain), parse_day_first=True)
if timegrain in ("Daily", "Weekly")
else get_period(r[0], timegrain)
for r in result
Expand Down
10 changes: 5 additions & 5 deletions frappe/desk/doctype/dashboard_chart/test_dashboard_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_daily_dashboard_chart(self):
self.assertEqual(result.get("datasets")[0].get("values"), [200.0, 400.0, 300.0, 0.0, 100.0, 0.0])
self.assertEqual(
result.get("labels"),
["06-01-2019", "07-01-2019", "08-01-2019", "09-01-2019", "10-01-2019", "11-01-2019"],
["01-06-2019", "01-07-2019", "01-08-2019", "01-09-2019", "01-10-2019", "01-11-2019"],
)

def test_weekly_dashboard_chart(self):
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_weekly_dashboard_chart(self):
result = get(chart_name="Test Weekly Dashboard Chart", refresh=1)

self.assertEqual(result.get("datasets")[0].get("values"), [50.0, 300.0, 800.0, 0.0])
self.assertEqual(result.get("labels"), ["12-30-2018", "06-01-2019", "01-13-2019", "01-20-2019"])
self.assertEqual(result.get("labels"), ["12-30-2018", "01-06-2019", "01-13-2019", "01-20-2019"])

def test_avg_dashboard_chart(self):
insert_test_records()
Expand All @@ -230,7 +230,7 @@ def test_avg_dashboard_chart(self):

with patch.object(frappe.utils.data, "get_first_day_of_the_week", return_value="Monday"):
result = get(chart_name="Test Average Dashboard Chart", refresh=1)
self.assertEqual(result.get("labels"), ["12-30-2018", "06-01-2019", "01-13-2019", "01-20-2019"])
self.assertEqual(result.get("labels"), ["12-30-2018", "01-06-2019", "01-13-2019", "01-20-2019"])
self.assertEqual(result.get("datasets")[0].get("values"), [50.0, 150.0, 266.6666666666667, 0.0])

def test_user_date_label_dashboard_chart(self):
Expand All @@ -255,13 +255,13 @@ def test_user_date_label_dashboard_chart(self):
with patch.object(frappe.utils.data, "get_user_date_format", return_value="dd.mm.yyyy"):
result = get(chart_name="Test Dashboard Chart Date Label")
self.assertEqual(
sorted(result.get("labels")), sorted(["01.05.2019", "01.12.2019", "19.01.2019"])
sorted(result.get("labels")), sorted(["05.01.2019", "12.01.2019", "19.01.2019"])
)

with patch.object(frappe.utils.data, "get_user_date_format", return_value="mm-dd-yyyy"):
result = get(chart_name="Test Dashboard Chart Date Label")
self.assertEqual(
sorted(result.get("labels")), sorted(["01-19-2019", "05-01-2019", "12-01-2019"])
sorted(result.get("labels")), sorted(["01-19-2019", "01-05-2019", "01-12-2019"])
)


Expand Down
12 changes: 8 additions & 4 deletions frappe/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def is_invalid_date_string(date_string: str) -> bool:
)


def getdate(string_date: Optional["DateTimeLikeObject"] = None) -> datetime.date | None:
def getdate(
string_date: Optional["DateTimeLikeObject"] = None, parse_day_first: bool = False
) -> datetime.date | None:
"""
Converts string date (yyyy-mm-dd) to datetime.date object.
If no input is provided, current date is returned.
Expand All @@ -91,7 +93,7 @@ def getdate(string_date: Optional["DateTimeLikeObject"] = None) -> datetime.date
if is_invalid_date_string(string_date):
return None
try:
return parser.parse(string_date).date()
return parser.parse(string_date, dayfirst=parse_day_first).date()
except ParserError:
frappe.throw(
frappe._("{} is not a valid date string.").format(frappe.bold(string_date)),
Expand Down Expand Up @@ -548,7 +550,9 @@ def get_user_time_format() -> str:
return frappe.local.user_time_format or "HH:mm:ss"


def format_date(string_date=None, format_string: str | None = None) -> str:
def format_date(
string_date=None, format_string: str | None = None, parse_day_first: bool = False
) -> str:
"""Converts the given string date to :data:`user_date_format`
User format specified in defaults

Expand All @@ -564,7 +568,7 @@ def format_date(string_date=None, format_string: str | None = None) -> str:
if not string_date:
return ""

date = getdate(string_date)
date = getdate(string_date, parse_day_first)
if not format_string:
format_string = get_user_date_format()
format_string = format_string.replace("mm", "MM").replace("Y", "y")
Expand Down
0