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.
 
 
 
 
 
 

110 lines
3.6 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  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. if not webnotes.compare(val1, condition, val2):
  44. msg = _("Error") + ": "
  45. if doc.parentfield:
  46. msg += _("Row") + (" # %d: " % doc.idx)
  47. msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \
  48. + " " + error_condition_map.get(condition, "") + " " + cstr(val2)
  49. # raise passed exception or True
  50. msgprint(msg, raise_exception=raise_exception or True)
  51. def validate_table_has_rows(self, parentfield, raise_exception=None):
  52. if not self.doclist.get({"parentfield": parentfield}):
  53. label = self.meta.get_label(parentfield)
  54. msgprint(_("Error") + ": " + _(label) + " " + _("cannot be empty"),
  55. raise_exception=raise_exception or EmptyTableError)
  56. def round_floats_in(self, doc, fieldnames=None):
  57. if not fieldnames:
  58. fieldnames = [df.fieldname for df in self.meta.get({"doctype": "DocField", "parent": doc.doctype,
  59. "fieldtype": ["in", ["Currency", "Float"]]})]
  60. for fieldname in fieldnames:
  61. doc.fields[fieldname] = flt(doc.fields.get(fieldname), self.precision(fieldname, doc.parentfield))
  62. def _process(self, parentfield):
  63. from webnotes.model.doc import Document
  64. if isinstance(parentfield, Document):
  65. parentfield = parentfield.parentfield
  66. elif isinstance(parentfield, dict):
  67. parentfield = parentfield.get("parentfield")
  68. return parentfield
  69. def precision(self, fieldname, parentfield=None):
  70. parentfield = self._process(parentfield)
  71. if not hasattr(self, "_precision"):
  72. self._precision = webnotes._dict({
  73. "default": cint(webnotes.conn.get_default("float_precision")) or 3,
  74. "options": {}
  75. })
  76. if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
  77. df = self.meta.get_field(fieldname, parentfield=parentfield)
  78. if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
  79. self._precision.options[df.options] = get_field_precision(df, self.doc)
  80. if df.fieldtype == "Currency":
  81. self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
  82. self._precision.default
  83. elif df.fieldtype == "Float":
  84. self._precision[parentfield or "main"][fieldname] = self._precision.default
  85. return self._precision[parentfield or "main"][fieldname]