Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

123 righe
4.3 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)
  58. elif df.fieldtype in ("Int", "Check"):
  59. val1 = cint(val1)
  60. if not webnotes.compare(val1, condition, val2):
  61. msg = _("Error") + ": "
  62. if doc.parentfield:
  63. msg += _("Row") + (" # %d: " % doc.idx)
  64. msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \
  65. + " " + error_condition_map.get(condition, "") + " " + cstr(val2)
  66. # raise passed exception or True
  67. msgprint(msg, raise_exception=raise_exception or True)
  68. def validate_table_has_rows(self, parentfield, raise_exception=None):
  69. if not self.doclist.get({"parentfield": parentfield}):
  70. label = self.meta.get_label(parentfield)
  71. msgprint(_("Error") + ": " + _(label) + " " + _("cannot be empty"),
  72. raise_exception=raise_exception or EmptyTableError)
  73. def round_floats_in(self, doc, fieldnames=None):
  74. if not fieldnames:
  75. fieldnames = [df.fieldname for df in self.meta.get({"doctype": "DocField", "parent": doc.doctype,
  76. "fieldtype": ["in", ["Currency", "Float"]]})]
  77. for fieldname in fieldnames:
  78. doc.fields[fieldname] = flt(doc.fields.get(fieldname), self.precision(fieldname, doc.parentfield))
  79. def _process(self, parentfield):
  80. from webnotes.model.doc import Document
  81. if isinstance(parentfield, Document):
  82. parentfield = parentfield.parentfield
  83. elif isinstance(parentfield, dict):
  84. parentfield = parentfield.get("parentfield")
  85. return parentfield
  86. def precision(self, fieldname, parentfield=None):
  87. parentfield = self._process(parentfield)
  88. if not hasattr(self, "_precision"):
  89. self._precision = webnotes._dict({
  90. "default": cint(webnotes.conn.get_default("float_precision")) or 6,
  91. "options": {}
  92. })
  93. if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
  94. df = self.meta.get_field(fieldname, parentfield=parentfield)
  95. if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
  96. self._precision.options[df.options] = get_field_precision(df, self.doc)
  97. self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
  98. self._precision.default
  99. return self._precision[parentfield or "main"][fieldname]