refactor: Split address render function (#22784)

This function can be used as utility where permssion checks might not be
required.
This commit is contained in:
Ankush Menat 2023-10-17 19:37:31 +05:30 committed by GitHub
parent 440612f3b9
commit 3e19fb36a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,6 @@
# Copyright (c) 2015, Frappe Technologies and contributors
# License: MIT. See LICENSE
from typing import Optional
from jinja2 import TemplateSyntaxError
import frappe
@ -166,18 +164,23 @@ def get_default_address(
@frappe.whitelist()
def get_address_display(address_dict: dict | str | None) -> str | None:
if not address_dict:
return render_address(address_dict)
def render_address(address: dict | str | None, ignore_permissions=False) -> str | None:
if not address:
return
if not isinstance(address_dict, dict):
address = frappe.get_cached_doc("Address", address_dict)
address.check_permission()
address_dict = address.as_dict()
if not isinstance(address, dict):
address = frappe.get_cached_doc("Address", address)
if not ignore_permissions:
address.check_permission()
address = address.as_dict()
name, template = get_address_templates(address_dict)
name, template = get_address_templates(address)
try:
return frappe.render_template(template, address_dict)
return frappe.render_template(template, address)
except TemplateSyntaxError:
frappe.throw(_("There is an error in your Address Template {0}").format(name))
@ -258,7 +261,7 @@ def get_company_address(company):
if company:
ret.company_address = get_default_address("Company", company)
ret.company_address_display = get_address_display(ret.company_address)
ret.company_address_display = render_address(ret.company_address, ignore_permissions=True)
return ret