[minor] validate address template before saving
This commit is contained in:
parent
1106524121
commit
a064e3a200
@ -8,6 +8,7 @@ from frappe import throw, _
|
|||||||
from frappe.utils import cstr
|
from frappe.utils import cstr
|
||||||
|
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from jinja2 import TemplateSyntaxError
|
||||||
|
|
||||||
class Address(Document):
|
class Address(Document):
|
||||||
def __setup__(self):
|
def __setup__(self):
|
||||||
@ -85,16 +86,22 @@ def get_address_display(address_dict):
|
|||||||
if not isinstance(address_dict, dict):
|
if not isinstance(address_dict, dict):
|
||||||
address_dict = frappe.db.get_value("Address", address_dict, "*", as_dict=True) or {}
|
address_dict = frappe.db.get_value("Address", address_dict, "*", as_dict=True) or {}
|
||||||
|
|
||||||
template = frappe.db.get_value("Address Template", \
|
data = frappe.db.get_value("Address Template", \
|
||||||
{"country": address_dict.get("country")}, "template")
|
{"country": address_dict.get("country")}, ["name", "template"])
|
||||||
if not template:
|
if not data:
|
||||||
template = frappe.db.get_value("Address Template", \
|
data = frappe.db.get_value("Address Template", \
|
||||||
{"is_default": 1}, "template")
|
{"is_default": 1}, ["name", "template"])
|
||||||
|
|
||||||
if not template:
|
if not data:
|
||||||
frappe.throw(_("No default Address Template found. Please create a new one from Setup > Printing and Branding > Address Template."))
|
frappe.throw(_("No default Address Template found. Please create a new one from Setup > Printing and Branding > Address Template."))
|
||||||
|
|
||||||
return frappe.render_template(template, address_dict)
|
name, template = data
|
||||||
|
|
||||||
|
try:
|
||||||
|
return frappe.render_template(template, address_dict)
|
||||||
|
except TemplateSyntaxError:
|
||||||
|
frappe.throw(_("There is an error in your Address Template {0}").format(name))
|
||||||
|
|
||||||
|
|
||||||
def get_territory_from_address(address):
|
def get_territory_from_address(address):
|
||||||
"""Tries to match city, state and country of address to existing territory"""
|
"""Tries to match city, state and country of address to existing territory"""
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe
|
import frappe
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
from frappe.utils.jinja import validate_template
|
||||||
from frappe import _
|
from frappe import _
|
||||||
|
|
||||||
class AddressTemplate(Document):
|
class AddressTemplate(Document):
|
||||||
@ -14,6 +15,8 @@ class AddressTemplate(Document):
|
|||||||
self.is_default = 1
|
self.is_default = 1
|
||||||
frappe.msgprint(_("Setting this Address Template as default as there is no other default"))
|
frappe.msgprint(_("Setting this Address Template as default as there is no other default"))
|
||||||
|
|
||||||
|
validate_template(self.template)
|
||||||
|
|
||||||
def on_update(self):
|
def on_update(self):
|
||||||
if self.is_default and self.defaults:
|
if self.is_default and self.defaults:
|
||||||
for d in self.defaults:
|
for d in self.defaults:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user