Example #1
0
def _filter(data, filters, limit=None):
	"""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 d in data:
		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, (tuple, list)):
				if isinstance(fval, basestring) and fval.startswith("^"):
					fval = ("^", fval[1:])
				else:
					fval = ("=", fval)

			if not frappe.compare(getattr(d, f, None), fval[0], fval[1]):
				add = False
				break

		if add:
			out.append(d)
			if limit and (len(out)-1)==limit:
				break

	return out
Example #2
0
    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": _("one of"),
            "not in": _("none of"),
            "^": _("beginning with"),
        }

        if not doc:
            doc = self

        val1 = doc.get_value(fieldname)

        df = doc.meta.get_field(fieldname)
        val2 = doc.cast(val2, df)

        if not frappe.compare(val1, condition, val2):
            label = doc.meta.get_label(fieldname)
            condition_str = error_condition_map.get(condition, condition)
            if doc.parentfield:
                msg = _(
                    "Incorrect value in row {0}: {1} must be {2} {3}".format(
                        doc.idx, label, condition_str, val2))
            else:
                msg = _("Incorrect value: {0} must be {1} {2}".format(
                    label, condition_str, val2))

            # raise passed exception or True
            msgprint(msg, raise_exception=raise_exception or True)
Example #3
0
	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": _("one of"),
			"not in": _("none of"),
			"^": _("beginning with"),
		}

		if not doc:
			doc = self

		val1 = doc.get_value(fieldname)

		df = doc.meta.get_field(fieldname)
		val2 = doc.cast(val2, df)

		if not frappe.compare(val1, condition, val2):
			label = doc.meta.get_label(fieldname)
			condition_str = error_condition_map.get(condition, condition)
			if doc.parentfield:
				msg = _("Incorrect value in row {0}: {1} must be {2} {3}".format(doc.idx, label, condition_str, val2))
			else:
				msg = _("Incorrect value: {0} must be {1} {2}".format(label, condition_str, val2))

			# raise passed exception or True
			msgprint(msg, raise_exception=raise_exception or True)
Example #4
0
    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, (tuple, list)):
                    if isinstance(fval, basestring) and fval.startswith("^"):
                        fval = ["^", fval[1:]]
                    else:
                        fval = ["=", fval]

                if not frappe.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 _filter(data, filters, limit=None):
    """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 d in data:
        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, (tuple, list)):
                if isinstance(fval, basestring) and fval.startswith("^"):
                    fval = ("^", fval[1:])
                else:
                    fval = ("=", fval)

            if not frappe.compare(getattr(d, f, None), fval[0], fval[1]):
                add = False
                break

        if add:
            out.append(d)
            if limit and (len(out) - 1) == limit:
                break

    return out
Example #6
0
    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 frappe.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)
Example #7
0
    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": _("one of"),
            "not in": _("none of"),
            "^": _("beginning with"),
        }

        if not doc:
            doc = self

        df = doc.meta.get_field(fieldname)

        val1 = doc.get(fieldname)

        if df.fieldtype in ("Currency", "Float", "Percent"):
            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", "Dynamic Link"):
            val1 = cstr(val1)
            val2 = cstr(val2)

        if not frappe.compare(val1, condition, val2):
            label = doc.meta.get_label(fieldname)
            condition_str = error_condition_map.get(condition, condition)
            if doc.parentfield:
                msg = _(
                    "Incorrect value in row {0}: {1} must be {2} {3}".format(
                        doc.idx, label, condition_str, val2))
            else:
                msg = _("Incorrect value: {0} must be {1} {2}".format(
                    label, condition_str, val2))

            # raise passed exception or True
            msgprint(msg, raise_exception=raise_exception or True)
Example #8
0
def _filter(data, filters, limit=None):
	"""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, _filters = [], {}

	if not data:
		return out

	# setup filters as tuples
	if filters:
		for f in filters:
			fval = filters[f]

			if not isinstance(fval, (tuple, list)):
				if fval is True:
					fval = ("not None", fval)
				elif fval is False:
					fval = ("None", fval)
				elif isinstance(fval, string_types) and fval.startswith("^"):
					fval = ("^", fval[1:])
				else:
					fval = ("=", fval)

			_filters[f] = fval

	for d in data:
		for f, fval in _filters.items():
			if not frappe.compare(getattr(d, f, None), fval[0], fval[1]):
				break
		else:
			out.append(d)
			if limit and len(out) >= limit:
				break

	return out
Example #9
0
	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": _("one of"),
			"not in": _("none of"),
			"^": _("beginning with"),
		}

		if not doc:
			doc = self

		df = doc.meta.get_field(fieldname)

		val1 = doc.get(fieldname)

		if df.fieldtype in ("Currency", "Float", "Percent"):
			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", "Dynamic Link"):
				val1 = cstr(val1)
				val2 = cstr(val2)

		if not frappe.compare(val1, condition, val2):
			label = doc.meta.get_label(fieldname)
			condition_str = error_condition_map.get(condition, condition)
			if doc.parentfield:
				msg = _("Incorrect value in row {0}: {1} must be {2} {3}".format(doc.idx, label, condition_str, val2))
			else:
				msg = _("Incorrect value: {0} must be {1} {2}".format(label, condition_str, val2))

			# raise passed exception or True
			msgprint(msg, raise_exception=raise_exception or True)