25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 lines
3.3 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import msgprint, _
  6. from frappe.utils import flt, cint, cstr
  7. from frappe.model.meta import get_field_precision
  8. from frappe.model.document import Document
  9. class EmptyTableError(frappe.ValidationError): pass
  10. class DocListController(Document):
  11. def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None):
  12. """check that value of fieldname should be 'condition' val2
  13. else throw exception"""
  14. error_condition_map = {
  15. "=": "!=",
  16. "!=": "=",
  17. "<": ">=",
  18. ">": "<=",
  19. ">=": "<",
  20. "<=": ">",
  21. "in": _("not in"),
  22. "not in": _("in"),
  23. "^": _("cannot start with"),
  24. }
  25. if not doc:
  26. doc = self.doc
  27. df = self.meta.get_field(fieldname, parent=doc.doctype)
  28. val1 = doc.get(fieldname)
  29. if df.fieldtype in ("Currency", "Float"):
  30. val1 = flt(val1, self.precision(df.fieldname, doc.parentfield or None))
  31. val2 = flt(val2, self.precision(df.fieldname, doc.parentfield or None))
  32. elif df.fieldtype in ("Int", "Check"):
  33. val1 = cint(val1)
  34. val2 = cint(val2)
  35. elif df.fieldtype in ("Data", "Text", "Small Text", "Long Text",
  36. "Text Editor", "Select", "Link"):
  37. val1 = cstr(val1)
  38. val2 = cstr(val2)
  39. if not frappe.compare(val1, condition, val2):
  40. msg = _("Error") + ": "
  41. if doc.parentfield:
  42. msg += _("Row") + (" # %d: " % doc.idx)
  43. msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \
  44. + " " + error_condition_map.get(condition, "") + " " + cstr(val2)
  45. # raise passed exception or True
  46. msgprint(msg, raise_exception=raise_exception or True)
  47. def validate_table_has_rows(self, parentfield, raise_exception=None):
  48. if not self.get(parentfield):
  49. label = self.meta.get_label(parentfield)
  50. msgprint(_("Error") + ": " + _(label) + " " + _("cannot be empty"),
  51. raise_exception=raise_exception or EmptyTableError)
  52. def round_floats_in(self, doc, fieldnames=None):
  53. if not fieldnames:
  54. fieldnames = [df.fieldname for df in self.meta.get({"doctype": "DocField", "parent": doc.doctype,
  55. "fieldtype": ["in", ["Currency", "Float"]]})]
  56. for fieldname in fieldnames:
  57. doc.set(fieldname, flt(doc.get(fieldname), self.precision(fieldname, doc.parentfield)))
  58. def precision(self, fieldname, parentfield=None):
  59. if not isinstance(parentfield, basestring):
  60. parentfield = parentfield.parentfield
  61. if not hasattr(self, "_precision"):
  62. self._precision = frappe._dict({
  63. "default": cint(frappe.db.get_default("float_precision")) or 3,
  64. "options": {}
  65. })
  66. if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
  67. df = self.meta.get_field(fieldname, parentfield=parentfield)
  68. if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
  69. self._precision.options[df.options] = get_field_precision(df, self.doc)
  70. if df.fieldtype == "Currency":
  71. self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
  72. self._precision.default
  73. elif df.fieldtype == "Float":
  74. self._precision[parentfield or "main"][fieldname] = self._precision.default
  75. return self._precision[parentfield or "main"][fieldname]