Skip to content

Commit

Permalink
Merge pull request #24684 from barredterra/label-in-link-to-form
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Feb 9, 2024
2 parents 6fc898a + 0184aa7 commit b3b02c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions frappe/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
get_time,
get_timedelta,
get_timespan_date_range,
get_url_to_form,
get_year_ending,
getdate,
now_datetime,
Expand Down Expand Up @@ -1009,6 +1010,10 @@ def test_get_site_info(self):
self.assertIn("frappe", installed_apps)
self.assertGreaterEqual(len(info["users"]), 1)

def test_get_url_to_form(self):
self.assertTrue(get_url_to_form("System Settings").endswith("/app/system-settings"))
self.assertTrue(get_url_to_form("User", "Test User").endswith("/app/user/Test%20User"))

def test_safe_json_load(self):
self.assertEqual(safe_json_loads("{}"), {})
self.assertEqual(safe_json_loads("{ /}"), "{ /}")
Expand Down
15 changes: 11 additions & 4 deletions frappe/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,14 +1808,16 @@ def get_host_name() -> str:
return get_url().rsplit("//", 1)[-1]


def get_link_to_form(doctype: str, name: str, label: str | None = None) -> str:
def get_link_to_form(doctype: str, name: str | None = None, label: str | None = None) -> str:
"""Return the HTML link to the given document's form view.
e.g. get_link_to_form("Sales Invoice", "INV-0001", "Link Label") returns:
'<a href="https://frappe.io/app/sales-invoice/INV-0001">Link Label</a>'.
"""
from frappe import _

if not label:
label = name
label = name or _(doctype)

return f"""<a href="{get_url_to_form(doctype, name)}">{label}</a>"""

Expand Down Expand Up @@ -1863,13 +1865,18 @@ def get_absolute_url(doctype: str, name: str) -> str:
return f"/app/{quoted(slug(doctype))}/{quoted(name)}"


def get_url_to_form(doctype: str, name: str) -> str:
def get_url_to_form(doctype: str, name: str | None = None) -> str:
"""Return the absolute URL for the form view of the given document in the desk.
e.g. when doctype="Sales Invoice" and your site URL is "https://frappe.io",
returns 'https://frappe.io/app/sales-invoice/INV-00001'
"""
return get_url(uri=f"/app/{quoted(slug(doctype))}/{quoted(name)}")
if not name:
uri = f"/app/{quoted(slug(doctype))}"
else:
uri = f"/app/{quoted(slug(doctype))}/{quoted(name)}"

return get_url(uri=uri)


def get_url_to_list(doctype: str) -> str:
Expand Down

0 comments on commit b3b02c8

Please sign in to comment.