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.
 
 
 
 
 
 

126 line
4.5 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. from webnotes import msgprint, _
  25. from webnotes.utils import flt, cint, cstr
  26. from webnotes.model.meta import get_field_precision
  27. error_condition_map = {
  28. "=": "!=",
  29. "!=": "=",
  30. "<": ">=",
  31. ">": "<=",
  32. ">=": "<",
  33. "<=": ">",
  34. "in": _("not in"),
  35. "not in": _("in"),
  36. "^": _("cannot start with"),
  37. }
  38. class EmptyTableError(webnotes.ValidationError): pass
  39. class DocListController(object):
  40. def __init__(self, doc, doclist):
  41. self.doc, self.doclist = doc, doclist
  42. if hasattr(self, "setup"):
  43. self.setup()
  44. @property
  45. def meta(self):
  46. if not hasattr(self, "_meta"):
  47. self._meta = webnotes.get_doctype(self.doc.doctype)
  48. return self._meta
  49. def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None):
  50. """check that value of fieldname should be 'condition' val2
  51. else throw exception"""
  52. if not doc:
  53. doc = self.doc
  54. df = self.meta.get_field(fieldname, parent=doc.doctype)
  55. val1 = doc.fields.get(fieldname)
  56. if df.fieldtype in ("Currency", "Float"):
  57. val1 = flt(val1, self.precision(df.fieldname, doc.parentfield or None))
  58. val2 = flt(val2, self.precision(df.fieldname, doc.parentfield or None))
  59. elif df.fieldtype in ("Int", "Check"):
  60. val1 = cint(val1)
  61. val2 = cint(val2)
  62. if not webnotes.compare(val1, condition, val2):
  63. msg = _("Error") + ": "
  64. if doc.parentfield:
  65. msg += _("Row") + (" # %d: " % doc.idx)
  66. msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \
  67. + " " + error_condition_map.get(condition, "") + " " + cstr(val2)
  68. # raise passed exception or True
  69. msgprint(msg, raise_exception=raise_exception or True)
  70. def validate_table_has_rows(self, parentfield, raise_exception=None):
  71. if not self.doclist.get({"parentfield": parentfield}):
  72. label = self.meta.get_label(parentfield)
  73. msgprint(_("Error") + ": " + _(label) + " " + _("cannot be empty"),
  74. raise_exception=raise_exception or EmptyTableError)
  75. def round_floats_in(self, doc, fieldnames=None):
  76. if not fieldnames:
  77. fieldnames = [df.fieldname for df in self.meta.get({"doctype": "DocField", "parent": doc.doctype,
  78. "fieldtype": ["in", ["Currency", "Float"]]})]
  79. for fieldname in fieldnames:
  80. doc.fields[fieldname] = flt(doc.fields.get(fieldname), self.precision(fieldname, doc.parentfield))
  81. def _process(self, parentfield):
  82. from webnotes.model.doc import Document
  83. if isinstance(parentfield, Document):
  84. parentfield = parentfield.parentfield
  85. elif isinstance(parentfield, dict):
  86. parentfield = parentfield.get("parentfield")
  87. return parentfield
  88. def precision(self, fieldname, parentfield=None):
  89. parentfield = self._process(parentfield)
  90. if not hasattr(self, "_precision"):
  91. self._precision = webnotes._dict({
  92. "default": cint(webnotes.conn.get_default("float_precision")) or 6,
  93. "options": {}
  94. })
  95. if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
  96. df = self.meta.get_field(fieldname, parentfield=parentfield)
  97. if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
  98. self._precision.options[df.options] = get_field_precision(df, self.doc)
  99. self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
  100. self._precision.default
  101. return self._precision[parentfield or "main"][fieldname]