You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

114 lines
3.7 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 webnotes
  5. from webnotes import msgprint, _
  6. from webnotes.utils import flt, cint, cstr
  7. from webnotes.model.meta import get_field_precision
  8. error_condition_map = {
  9. "=": "!=",
  10. "!=": "=",
  11. "<": ">=",
  12. ">": "<=",
  13. ">=": "<",
  14. "<=": ">",
  15. "in": _("not in"),
  16. "not in": _("in"),
  17. "^": _("cannot start with"),
  18. }
  19. class EmptyTableError(webnotes.ValidationError): pass
  20. class DocListController(object):
  21. def __init__(self, doc, doclist):
  22. self.doc, self.doclist = doc, doclist
  23. if hasattr(self, "setup"):
  24. self.setup()
  25. @property
  26. def meta(self):
  27. if not hasattr(self, "_meta"):
  28. self._meta = webnotes.get_doctype(self.doc.doctype)
  29. return self._meta
  30. def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None):
  31. """check that value of fieldname should be 'condition' val2
  32. else throw exception"""
  33. if not doc:
  34. doc = self.doc
  35. df = self.meta.get_field(fieldname, parent=doc.doctype)
  36. val1 = doc.fields.get(fieldname)
  37. if df.fieldtype in ("Currency", "Float"):
  38. val1 = flt(val1, self.precision(df.fieldname, doc.parentfield or None))
  39. val2 = flt(val2, self.precision(df.fieldname, doc.parentfield or None))
  40. elif df.fieldtype in ("Int", "Check"):
  41. val1 = cint(val1)
  42. val2 = cint(val2)
  43. elif df.fieldtype in ("Data", "Text", "Small Text", "Long Text",
  44. "Text Editor", "Select", "Link"):
  45. val1 = cstr(val1)
  46. val2 = cstr(val2)
  47. if not webnotes.compare(val1, condition, val2):
  48. msg = _("Error") + ": "
  49. if doc.parentfield:
  50. msg += _("Row") + (" # %d: " % doc.idx)
  51. msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \
  52. + " " + error_condition_map.get(condition, "") + " " + cstr(val2)
  53. # raise passed exception or True
  54. msgprint(msg, raise_exception=raise_exception or True)
  55. def validate_table_has_rows(self, parentfield, raise_exception=None):
  56. if not self.doclist.get({"parentfield": parentfield}):
  57. label = self.meta.get_label(parentfield)
  58. msgprint(_("Error") + ": " + _(label) + " " + _("cannot be empty"),
  59. raise_exception=raise_exception or EmptyTableError)
  60. def round_floats_in(self, doc, fieldnames=None):
  61. if not fieldnames:
  62. fieldnames = [df.fieldname for df in self.meta.get({"doctype": "DocField", "parent": doc.doctype,
  63. "fieldtype": ["in", ["Currency", "Float"]]})]
  64. for fieldname in fieldnames:
  65. doc.fields[fieldname] = flt(doc.fields.get(fieldname), self.precision(fieldname, doc.parentfield))
  66. def _process(self, parentfield):
  67. from webnotes.model.doc import Document
  68. if isinstance(parentfield, Document):
  69. parentfield = parentfield.parentfield
  70. elif isinstance(parentfield, dict):
  71. parentfield = parentfield.get("parentfield")
  72. return parentfield
  73. def precision(self, fieldname, parentfield=None):
  74. parentfield = self._process(parentfield)
  75. if not hasattr(self, "_precision"):
  76. self._precision = webnotes._dict({
  77. "default": cint(webnotes.conn.get_default("float_precision")) or 3,
  78. "options": {}
  79. })
  80. if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
  81. df = self.meta.get_field(fieldname, parentfield=parentfield)
  82. if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
  83. self._precision.options[df.options] = get_field_precision(df, self.doc)
  84. if df.fieldtype == "Currency":
  85. self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
  86. self._precision.default
  87. elif df.fieldtype == "Float":
  88. self._precision[parentfield or "main"][fieldname] = self._precision.default
  89. return self._precision[parentfield or "main"][fieldname]