def get(self, filters, limit=0): """pass filters as: {"key": "val", "key": ["!=", "val"], "key": ["in", "val"], "key": ["not in", "val"], "key": "^val"}""" out = [] for doc in self: d = isinstance(getattr(doc, "fields", None), dict) and doc.fields or doc add = True for f in filters: fval = filters[f] if not isinstance(fval, list): if isinstance(fval, basestring) and fval.startswith("^"): fval = ["^", fval[1:]] else: fval = ["=", fval] if not webnotes.compare(d.get(f), fval[0], fval[1]): add = False break if add: out.append(doc) if limit and (len(out) - 1) == limit: break return DocList(out)
def get(self, filters, limit=0): """pass filters as: {"key": "val", "key": ["!=", "val"], "key": ["in", "val"], "key": ["not in", "val"], "key": "^val", "key" : True (exists), "key": False (does not exist) }""" out = [] for doc in self: d = isinstance(getattr(doc, "fields", None), dict) and doc.fields or doc add = True for f in filters: fval = filters[f] if fval is True: fval = ["not None", fval] elif fval is False: fval = ["None", fval] elif not isinstance(fval, list): if isinstance(fval, basestring) and fval.startswith("^"): fval = ["^", fval[1:]] else: fval = ["=", fval] if not webnotes.compare(d.get(f), fval[0], fval[1]): add = False break if add: out.append(doc) if limit and (len(out)-1)==limit: break return DocList(out)
def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None): """check that value of fieldname should be 'condition' val2 else throw exception""" if not doc: doc = self.doc df = self.meta.get_field(fieldname, parent=doc.doctype) val1 = doc.fields.get(fieldname) if df.fieldtype in ("Currency", "Float"): val1 = flt(val1, self.precision(df.fieldname, doc.parentfield or None)) val2 = flt(val2, self.precision(df.fieldname, doc.parentfield or None)) elif df.fieldtype in ("Int", "Check"): val1 = cint(val1) val2 = cint(val2) if not webnotes.compare(val1, condition, val2): msg = _("Error") + ": " if doc.parentfield: msg += _("Row") + (" # %d: " % doc.idx) msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \ + " " + error_condition_map.get(condition, "") + " " + cstr(val2) # raise passed exception or True msgprint(msg, raise_exception=raise_exception or True)
def validate_value(self, fieldname, condition, val2, doc=None, raise_exception=None): """check that value of fieldname should be 'condition' val2 else throw exception""" error_condition_map = { "=": "!=", "!=": "=", "<": ">=", ">": "<=", ">=": "<", "<=": ">", "in": _("not in"), "not in": _("in"), "^": _("cannot start with"), } if not doc: doc = self.doc df = self.meta.get_field(fieldname, parent=doc.doctype) val1 = doc.fields.get(fieldname) if df.fieldtype in ("Currency", "Float"): val1 = flt(val1, self.precision(df.fieldname, doc.parentfield or None)) val2 = flt(val2, self.precision(df.fieldname, doc.parentfield or None)) elif df.fieldtype in ("Int", "Check"): val1 = cint(val1) val2 = cint(val2) elif df.fieldtype in ("Data", "Text", "Small Text", "Long Text", "Text Editor", "Select", "Link"): val1 = cstr(val1) val2 = cstr(val2) if not webnotes.compare(val1, condition, val2): msg = _("Error") + ": " if doc.parentfield: msg += _("Row") + (" # %d: " % doc.idx) msg += _(self.meta.get_label(fieldname, parent=doc.doctype)) \ + " " + error_condition_map.get(condition, "") + " " + cstr(val2) # raise passed exception or True msgprint(msg, raise_exception=raise_exception or True)