Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

115 рядки
4.0 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 DocListController(object):
  39. def __init__(self, doc, doclist):
  40. self.doc, self.doclist = doc, doclist
  41. if hasattr(self, "setup"):
  42. self.setup()
  43. @property
  44. def meta(self):
  45. if not hasattr(self, "_meta"):
  46. self._meta = webnotes.get_doctype(self.doc.doctype)
  47. return self._meta
  48. def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None):
  49. """check that value of fieldname should be 'condition' val2
  50. else throw exception"""
  51. if not doc:
  52. doc = self.doc
  53. df = self.meta.get_field(fieldname, parent=doc.doctype)
  54. val1 = doc.fields.get(fieldname)
  55. if df.fieldtype in ("Currency", "Float"):
  56. val1 = flt(val1)
  57. elif df.fieldtype in ("Int", "Check"):
  58. val1 = cint(val1)
  59. if not webnotes.compare(val1, condition, val2):
  60. msg = _("Error: ")
  61. if doc.parentfield:
  62. msg += _("Row") + (" # %d: " % doc.idx)
  63. msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \
  64. + " " + error_condition_map.get(condition, "") + " " + cstr(val2)
  65. # raise passed exception or True
  66. msgprint(msg, raise_exception=raise_exception or True)
  67. def round_floats_in(self, doc, fieldnames=None):
  68. if not fieldnames:
  69. fieldnames = [df.fieldname for df in self.meta.get({"doctype": "DocField", "parent": doc.doctype,
  70. "fieldtype": ["in", ["Currency", "Float"]]})]
  71. for fieldname in fieldnames:
  72. doc.fields[fieldname] = flt(doc.fields.get(fieldname), self.precision(fieldname, doc.parentfield))
  73. def _process(self, parentfield):
  74. from webnotes.model.doc import Document
  75. if isinstance(parentfield, Document):
  76. parentfield = parentfield.parentfield
  77. elif isinstance(parentfield, dict):
  78. parentfield = parentfield.get("parentfield")
  79. return parentfield
  80. def precision(self, fieldname, parentfield=None):
  81. parentfield = self._process(parentfield)
  82. if not hasattr(self, "_precision"):
  83. self._precision = webnotes._dict({
  84. "default": cint(webnotes.conn.get_default("float_precision")) or 6,
  85. "options": {}
  86. })
  87. if self._precision.setdefault(parentfield or "main", {}).get(fieldname) is None:
  88. df = self.meta.get_field(fieldname, parentfield=parentfield)
  89. if df.fieldtype == "Currency" and df.options and not self._precision.options.get(df.options):
  90. self._precision.options[df.options] = get_field_precision(df, self.doc)
  91. self._precision[parentfield or "main"][fieldname] = cint(self._precision.options.get(df.options)) or \
  92. self._precision.default
  93. return self._precision[parentfield or "main"][fieldname]