def get_invalid_links(self, is_submittable=False):
        def get_msg(df, docname):
            if self.parentfield:
                return "{} #{}: {}: {}".format(_("Row"), self.idx, _(df.label),
                                               docname)
            else:
                return "{}: {}".format(_(df.label), docname)

        invalid_links = []
        cancelled_links = []
        for df in (self.meta.get_link_fields() +
                   self.meta.get("fields", {"fieldtype": "Dynamic Link"})):
            docname = self.get(df.fieldname)

            if docname:
                if df.fieldtype == "Link":
                    doctype = df.options
                    if not doctype:
                        frappe.throw(
                            _("Options not set for link field {0}").format(
                                df.fieldname))
                else:
                    doctype = self.get(df.options)
                    if not doctype:
                        frappe.throw(
                            _("{0} must be set first").format(
                                self.meta.get_label(df.options)))

                # MySQL is case insensitive. Preserve case of the original docname in the Link Field.
                value = frappe.db.get_value(doctype,
                                            docname,
                                            "name",
                                            cache=True)
                if frappe.get_meta(doctype).issingle:
                    value = doctype

                setattr(self, df.fieldname, value)

                notify_link_count(doctype, docname)

                if not value:
                    invalid_links.append(
                        (df.fieldname, docname, get_msg(df, docname)))

                elif (df.fieldname != "amended_from"
                      and (is_submittable or self.meta.is_submittable)
                      and frappe.get_meta(doctype).is_submittable and cint(
                          frappe.db.get_value(doctype, docname,
                                              "docstatus")) == 2):

                    cancelled_links.append(
                        (df.fieldname, docname, get_msg(df, docname)))

        return invalid_links, cancelled_links
Esempio n. 2
0
	def get_invalid_links(self, is_submittable=False):
		def get_msg(df, docname):
			if self.parentfield:
				return "{} #{}: {}: {}".format(_("Row"), self.idx, _(df.label), docname)
			else:
				return "{}: {}".format(_(df.label), docname)

		invalid_links = []
		cancelled_links = []
		for df in (self.meta.get_link_fields()
				 + self.meta.get("fields", {"fieldtype":"Dynamic Link"})):
			docname = self.get(df.fieldname)

			if docname:
				if df.fieldtype=="Link":
					doctype = df.options
					if not doctype:
						frappe.throw(_("Options not set for link field {0}").format(df.fieldname))
				else:
					doctype = self.get(df.options)
					if not doctype:
						frappe.throw(_("{0} must be set first").format(self.meta.get_label(df.options)))

				# MySQL is case insensitive. Preserve case of the original docname in the Link Field.
				value = frappe.db.get_value(doctype, docname, "name", cache=True)
				if frappe.get_meta(doctype).issingle:
					value = doctype

				setattr(self, df.fieldname, value)

				notify_link_count(doctype, docname)

				if not value:
					invalid_links.append((df.fieldname, docname, get_msg(df, docname)))

				elif (df.fieldname != "amended_from"
					and (is_submittable or self.meta.is_submittable) and frappe.get_meta(doctype).is_submittable
					and cint(frappe.db.get_value(doctype, docname, "docstatus"))==2):

					cancelled_links.append((df.fieldname, docname, get_msg(df, docname)))

		return invalid_links, cancelled_links
Esempio n. 3
0
    def get_invalid_links(self, is_submittable=False):
        '''Returns list of invalid links and also updates fetch values if not set'''
        def get_msg(df, docname):
            if self.parentfield:
                return "{} #{}: {}: {}".format(_("Row"), self.idx, _(df.label),
                                               docname)
            else:
                return "{}: {}".format(_(df.label), docname)

        invalid_links = []
        cancelled_links = []

        for df in (
                self.meta.get_link_fields() +
                self.meta.get("fields", {"fieldtype": ('=', "Dynamic Link")})):
            docname = self.get(df.fieldname)

            if docname:
                if df.fieldtype == "Link":
                    doctype = df.options
                    if not doctype:
                        frappe.throw(
                            _("Options not set for link field {0}").format(
                                df.fieldname))
                else:
                    doctype = self.get(df.options)
                    if not doctype:
                        frappe.throw(
                            _("{0} must be set first").format(
                                self.meta.get_label(df.options)))

                # MySQL is case insensitive. Preserve case of the original docname in the Link Field.

                # get a map of values ot fetch along with this link query
                # that are mapped as link_fieldname.source_fieldname in Options of
                # Readonly or Data or Text type fields
                fields_to_fetch = [
                    _df for _df in self.meta.get_fields_to_fetch(df.fieldname)
                    if not self.get(_df.fieldname)
                ]

                if not fields_to_fetch:
                    # cache a single value type
                    values = frappe._dict(name=frappe.db.get_value(
                        doctype, docname, 'name', cache=True))
                else:
                    values_to_fetch = ['name'] + [
                        _df.options.split('.')[-1] for _df in fields_to_fetch
                    ]

                    # don't cache if fetching other values too
                    values = frappe.db.get_value(doctype,
                                                 docname,
                                                 values_to_fetch,
                                                 as_dict=True)

                if frappe.get_meta(doctype).issingle:
                    values.name = doctype

                setattr(self, df.fieldname, values.name)

                for _df in fields_to_fetch:
                    setattr(self, _df.fieldname,
                            values[_df.options.split('.')[-1]])

                notify_link_count(doctype, docname)

                if not values.name:
                    invalid_links.append(
                        (df.fieldname, docname, get_msg(df, docname)))

                elif (df.fieldname != "amended_from"
                      and (is_submittable or self.meta.is_submittable)
                      and frappe.get_meta(doctype).is_submittable and cint(
                          frappe.db.get_value(doctype, docname,
                                              "docstatus")) == 2):

                    cancelled_links.append(
                        (df.fieldname, docname, get_msg(df, docname)))

        return invalid_links, cancelled_links
Esempio n. 4
0
	def get_invalid_links(self, is_submittable=False):
		'''Returns list of invalid links and also updates fetch values if not set'''
		def get_msg(df, docname):
			if self.parentfield:
				return "{} #{}: {}: {}".format(_("Row"), self.idx, _(df.label), docname)
			else:
				return "{}: {}".format(_(df.label), docname)

		invalid_links = []
		cancelled_links = []

		for df in (self.meta.get_link_fields()
				+ self.meta.get("fields", {"fieldtype": ('=', "Dynamic Link")})):
			docname = self.get(df.fieldname)

			if docname:
				if df.fieldtype=="Link":
					doctype = df.options
					if not doctype:
						frappe.throw(_("Options not set for link field {0}").format(df.fieldname))
				else:
					doctype = self.get(df.options)
					if not doctype:
						frappe.throw(_("{0} must be set first").format(self.meta.get_label(df.options)))

				# MySQL is case insensitive. Preserve case of the original docname in the Link Field.

				# get a map of values ot fetch along with this link query
				# that are mapped as link_fieldname.source_fieldname in Options of
				# Readonly or Data or Text type fields

				fields_to_fetch = [
					_df for _df in self.meta.get_fields_to_fetch(df.fieldname)
					if
						not _df.get('fetch_if_empty')
						or (_df.get('fetch_if_empty') and not self.get(_df.fieldname))
				]

				if not fields_to_fetch:
					# cache a single value type
					values = frappe._dict(name=frappe.db.get_value(doctype, docname,
						'name', cache=True))
				else:
					values_to_fetch = ['name'] + [_df.fetch_from.split('.')[-1]
						for _df in fields_to_fetch]

					# don't cache if fetching other values too
					values = frappe.db.get_value(doctype, docname,
						values_to_fetch, as_dict=True)

				if frappe.get_meta(doctype).issingle:
					values.name = doctype

				if values:
					setattr(self, df.fieldname, values.name)

					for _df in fields_to_fetch:
						if self.is_new() or self.docstatus != 1 or _df.allow_on_submit:
							setattr(self, _df.fieldname, values[_df.fetch_from.split('.')[-1]])

					notify_link_count(doctype, docname)

					if not values.name:
						invalid_links.append((df.fieldname, docname, get_msg(df, docname)))

					elif (df.fieldname != "amended_from"
						and (is_submittable or self.meta.is_submittable) and frappe.get_meta(doctype).is_submittable
						and cint(frappe.db.get_value(doctype, docname, "docstatus"))==2):

						cancelled_links.append((df.fieldname, docname, get_msg(df, docname)))

		return invalid_links, cancelled_links