Пример #1
0
	def get_defaults(self, key=None, parent="__default"):
		"""Get all defaults"""
		if key:
			defaults = frappe.defaults.get_defaults(parent)
			d = defaults.get(key, None)
			if(not d and key != frappe.scrub(key)):
				d = defaults.get(frappe.scrub(key), None)
			return d
		else:
			return frappe.defaults.get_defaults(parent)
Пример #2
0
 def get_defaults(key=None, parent="__default"):
     """Get all defaults"""
     if key:
         defaults = frappe.defaults.get_defaults(parent)
         d = defaults.get(key, None)
         if (not d and key != frappe.scrub(key)):
             d = defaults.get(frappe.scrub(key), None)
         return d
     else:
         return frappe.defaults.get_defaults(parent)
Пример #3
0
def get_new_doc(doctype, parent_doc=None, parentfield=None):
    doc = frappe.get_doc({
        "doctype": doctype,
        "__islocal": 1,
        "owner": frappe.session.user,
        "docstatus": 0
    })

    restrictions = frappe.defaults.get_restrictions()

    if parent_doc:
        doc.parent = parent_doc.name
        doc.parenttype = parent_doc.doctype

    if parentfield:
        doc.parentfield = parentfield

    defaults = frappe.defaults.get_defaults()

    for d in doc.meta.get("fields"):
        default = defaults.get(d.fieldname)

        if (d.fieldtype=="Link") and d.ignore_restrictions != 1 and (d.options in restrictions)\
         and len(restrictions[d.options])==1:
            doc.set(d.fieldname, restrictions[d.options][0])
        elif default:
            doc.set(d.fieldname, default)
        elif d.get("default"):
            if d.default == "__user":
                doc.set(d.fieldname, frappe.session.user)
            elif d.default == "Today":
                doc.set(d.fieldname, nowdate())

            elif d.default.startswith(":"):
                ref_doctype = d.default[1:]
                ref_fieldname = ref_doctype.lower().replace(" ", "_")
                if parent_doc:
                    ref_docname = parent_doc.get(ref_fieldname)
                else:
                    ref_docname = frappe.db.get_default(ref_fieldname)
                doc.set(
                    d.fieldname,
                    frappe.db.get_value(ref_doctype, ref_docname, d.fieldname))
            else:
                doc.set(d.fieldname, d.default)

            # convert type of default
            if d.fieldtype in ("Int", "Check"):
                doc.set(d.fieldname, cint(doc.get(d.fieldname)))
            elif d.fieldtype in ("Float", "Currency"):
                doc.set(d.fieldname, flt(doc.get(d.fieldname)))

        elif d.fieldtype == "Time":
            doc.set(d.fieldname, nowtime())

        elif (d.fieldtype == "Select" and d.options
              and d.options != "[Select]"):
            doc.set(d.fieldname, d.options.split("\n")[0])

    return doc
Пример #4
0
def get_default_value(df, defaults, user_permissions, parent_doc):
    user_permissions_exist = (df.fieldtype == "Link" and
                              not getattr(df, "ignore_user_permissions", False)
                              and df.options in (user_permissions or []))

    # don't set defaults for "User" link field using User Permissions!
    if df.fieldtype == "Link" and df.options != "User":
        # 1 - look in user permissions
        if user_permissions_exist and len(user_permissions[df.options]) == 1:
            return user_permissions[df.options][0]

        # 2 - Look in user defaults
        user_default = defaults.get(df.fieldname)
        is_allowed_user_default = user_default and (
            not user_permissions_exist or
            (user_default in user_permissions.get(df.options, [])))

        # is this user default also allowed as per user permissions?
        if is_allowed_user_default:
            return user_default

    # 3 - look in default of docfield
    if df.get("default"):
        if df.default == "__user":
            return frappe.session.user

        elif df.default == "Today":
            return nowdate()

        elif df.default.startswith(":"):
            # default value based on another document
            ref_doctype = df.default[1:]
            ref_fieldname = ref_doctype.lower().replace(" ", "_")
            ref_docname = parent_doc.get(
                ref_fieldname) if parent_doc else frappe.db.get_default(
                    ref_fieldname)

            default_value = frappe.db.get_value(ref_doctype, ref_docname,
                                                df.fieldname)
            is_allowed_default_value = (not user_permissions_exist or
                                        (default_value in user_permissions.get(
                                            df.options, [])))

            # is this allowed as per user permissions
            if is_allowed_default_value:
                return default_value

        # a static default value
        is_allowed_default_value = (not user_permissions_exist
                                    or (df.default in user_permissions.get(
                                        df.options, [])))
        if df.fieldtype != "Link" or df.options == "User" or is_allowed_default_value:
            return df.default

    elif df.fieldtype == "Time":
        return nowtime()

    elif (df.fieldtype == "Select" and df.options
          and df.options not in ("[Select]", "Loading...")):
        return df.options.split("\n")[0]
Пример #5
0
def get_new_doc(doctype, parent_doc = None, parentfield = None):
	doc = frappe.get_doc({
		"doctype": doctype,
		"__islocal": 1,
		"owner": frappe.session.user,
		"docstatus": 0
	})

	restrictions = frappe.defaults.get_restrictions()

	if parent_doc:
		doc.parent = parent_doc.name
		doc.parenttype = parent_doc.doctype

	if parentfield:
		doc.parentfield = parentfield

	defaults = frappe.defaults.get_defaults()

	for d in doc.meta.get("fields"):
		default = defaults.get(d.fieldname)

		if (d.fieldtype=="Link") and d.ignore_restrictions != 1 and (d.options in restrictions)\
			and len(restrictions[d.options])==1:
			doc.set(d.fieldname, restrictions[d.options][0])
		elif default:
			doc.set(d.fieldname, default)
		elif d.get("default"):
			if d.default == "__user":
				doc.set(d.fieldname, frappe.session.user)
			elif d.default == "Today":
				doc.set(d.fieldname, nowdate())

			elif d.default.startswith(":"):
				ref_doctype =  d.default[1:]
				ref_fieldname = ref_doctype.lower().replace(" ", "_")
				if parent_doc:
					ref_docname = parent_doc.get(ref_fieldname)
				else:
					ref_docname = frappe.db.get_default(ref_fieldname)
				doc.set(d.fieldname, frappe.db.get_value(ref_doctype, ref_docname, d.fieldname))
			else:
				doc.set(d.fieldname, d.default)

			# convert type of default
			if d.fieldtype in ("Int", "Check"):
				doc.set(d.fieldname, cint(doc.get(d.fieldname)))
			elif d.fieldtype in ("Float", "Currency"):
				doc.set(d.fieldname, flt(doc.get(d.fieldname)))

		elif d.fieldtype == "Time":
			doc.set(d.fieldname, nowtime())

		elif (d.fieldtype == "Select" and d.options and not d.options.startswith("link:")
			and d.options != "[Select]"):
			doc.set(d.fieldname, d.options.split("\n")[0])

	return doc
Пример #6
0
	def create_onboarding_docs(self, args):
		defaults = frappe.defaults.get_defaults()
		for i in range(1, args.get('max_count')):
			customer = args.get('customer_name_' + str(i))
			if customer:
				try:
					doc = frappe.get_doc({
						'doctype': self.doctype,
						'customer_name': customer,
						'customer_type': 'Company',
						'customer_group': _('Commercial'),
						'territory': defaults.get('country'),
						'company': defaults.get('company')
					}).insert()

					if args.get('customer_email_' + str(i)):
						create_contact(customer, self.doctype,
							doc.name, args.get("customer_email_" + str(i)))
				except frappe.NameError:
					pass
Пример #7
0
def get_default_value(df, defaults, user_permissions, parent_doc):
	user_permissions_exist = (df.fieldtype=="Link"
		and not getattr(df, "ignore_user_permissions", False)
		and df.options in (user_permissions or []))

	# don't set defaults for "User" link field using User Permissions!
	if df.fieldtype == "Link" and df.options != "User":
		# 1 - look in user permissions
		if user_permissions_exist and len(user_permissions[df.options])==1:
			return user_permissions[df.options][0]

		# 2 - Look in user defaults
		user_default = defaults.get(df.fieldname)
		is_allowed_user_default = user_default and (not user_permissions_exist
			or (user_default in user_permissions.get(df.options, [])))

		# is this user default also allowed as per user permissions?
		if is_allowed_user_default:
			return user_default

	# 3 - look in default of docfield
	if df.get("default"):
		if df.default == "__user":
			return frappe.session.user

		elif df.default == "Today":
			return nowdate()

		elif df.default.startswith(":"):
			# default value based on another document
			ref_doctype =  df.default[1:]
			ref_fieldname = ref_doctype.lower().replace(" ", "_")
			ref_docname = parent_doc.get(ref_fieldname) if parent_doc else frappe.db.get_default(ref_fieldname)

			default_value = frappe.db.get_value(ref_doctype, ref_docname, df.fieldname)
			is_allowed_default_value = (not user_permissions_exist or
				(default_value in user_permissions.get(df.options, [])))

			# is this allowed as per user permissions
			if is_allowed_default_value:
				return default_value

		# a static default value
		is_allowed_default_value = (not user_permissions_exist or (df.default in user_permissions.get(df.options, [])))
		if df.fieldtype!="Link" or df.options=="User" or is_allowed_default_value:
			return df.default

	elif df.fieldtype == "Time":
		return nowtime()

	elif (df.fieldtype == "Select" and df.options and df.options not in ("[Select]", "Loading...")):
		return df.options.split("\n")[0]
Пример #8
0
def get_user_default_value(df, defaults, doctype_user_permissions, allowed_records, default_doc):
	# don't set defaults for "User" link field using User Permissions!
	if df.fieldtype == "Link" and df.options != "User":
		# If user permission has Is Default enabled or single-user permission has found against respective doctype.
		if (not df.ignore_user_permissions and default_doc):
			return default_doc

		# 2 - Look in user defaults
		user_default = defaults.get(df.fieldname)
		is_allowed_user_default = user_default and (not user_permissions_exist(df, doctype_user_permissions)
			or user_default in allowed_records)

		# is this user default also allowed as per user permissions?
		if is_allowed_user_default:
			return user_default
Пример #9
0
def get_user_default_value(df, defaults, user_permissions):
	# don't set defaults for "User" link field using User Permissions!
	if df.fieldtype == "Link" and df.options != "User":
		# 1 - look in user permissions
		if user_permissions_exist(df, user_permissions) and len(user_permissions[df.options])==1:
			return user_permissions[df.options][0]

		# 2 - Look in user defaults
		user_default = defaults.get(df.fieldname)
		is_allowed_user_default = user_default and (not user_permissions_exist(df, user_permissions)
			or (user_default in user_permissions.get(df.options, [])))

		# is this user default also allowed as per user permissions?
		if is_allowed_user_default:
			return user_default
Пример #10
0
def get_user_default_value(df, defaults, doctype_user_permissions, allowed_records):
	# don't set defaults for "User" link field using User Permissions!
	if df.fieldtype == "Link" and df.options != "User":
		# 1 - look in user permissions only for document_type==Setup
		# We don't want to include permissions of transactions to be used for defaults.
		if frappe.get_meta(df.options).document_type=="Setup" and len(allowed_records)==1:
			return allowed_records[0]

		# 2 - Look in user defaults
		user_default = defaults.get(df.fieldname)
		is_allowed_user_default = user_default and (not user_permissions_exist(df, doctype_user_permissions)
			or user_default in allowed_records)

		# is this user default also allowed as per user permissions?
		if is_allowed_user_default:
			return user_default
Пример #11
0
def get_user_default_value(df, defaults, doctype_user_permissions, allowed_records, default_doc):
	# don't set defaults for "User" link field using User Permissions!
	if df.fieldtype == "Link" and df.options != "User":
		# 1 - look in user permissions only for document_type==Setup
		# We don't want to include permissions of transactions to be used for defaults.
		if (frappe.get_meta(df.options).document_type=="Setup"
			and not df.ignore_user_permissions and default_doc):
				return default_doc

		# 2 - Look in user defaults
		user_default = defaults.get(df.fieldname)
		is_allowed_user_default = user_default and (not user_permissions_exist(df, doctype_user_permissions)
			or user_default in allowed_records)

		# is this user default also allowed as per user permissions?
		if is_allowed_user_default:
			return user_default
Пример #12
0
def get_user_default_value(df, defaults, user_permissions):
	# don't set defaults for "User" link field using User Permissions!
	if df.fieldtype == "Link" and df.options != "User":
		# 1 - look in user permissions only for document_type==Setup
		# We don't want to include permissions of transactions to be used for defaults.
		if (frappe.get_meta(df.options).document_type=="Setup"
			and user_permissions_exist(df, user_permissions)
			and len(user_permissions.get(df.options))==1):
			return user_permissions.get(df.options)[0].get("doc")

		# 2 - Look in user defaults
		user_default = defaults.get(df.fieldname)
		is_allowed_user_default = user_default and (not user_permissions_exist(df, user_permissions)
			or (user_default in user_permissions.get(df.options, [])))

		# is this user default also allowed as per user permissions?
		if is_allowed_user_default:
			return user_default
Пример #13
0
    def create_onboarding_docs(self, args):
        defaults = frappe.defaults.get_defaults()
        for i in range(1, args.get('max_count')):
            supplier = args.get('supplier_name_' + str(i))
            if supplier:
                try:
                    doc = frappe.get_doc({
                        'doctype': self.doctype,
                        'supplier_name': supplier,
                        'supplier_group': _('Local'),
                        'company': defaults.get('company')
                    }).insert()

                    if args.get('supplier_email_' + str(i)):
                        from erpnext.selling.doctype.customer.customer import create_contact
                        create_contact(supplier, 'Supplier', doc.name,
                                       args.get('supplier_email_' + str(i)))
                except frappe.NameError:
                    pass
		frappe.defaults.set_default(key, val, parent, parenttype)

	def add_default(self, key, val, parent="__default", parenttype=None):
		"""Append a default value for a key, there can be multiple default values for a particular key."""
		frappe.defaults.add_default(key, val, parent, parenttype)

	def get_default(self, key, parent="__default"):
		"""Returns default value as a list if multiple or single"""
		d = self.get_defaults(key, parent)
		return isinstance(d, list) and d[0] or d

	def get_defaults(self, key=None, parent="__default"):
		"""Get all defaults"""
		if key:
			defaults = frappe.defaults.get_defaults(parent)
			d = defaults.get(key, None)
			if(not d and key != frappe.scrub(key)):
				d = defaults.get(frappe.scrub(key), None)
			return d
		else:
			return frappe.defaults.get_defaults(parent)

	def begin(self):
		self.sql("start transaction")

	def commit(self):
		"""Commit current transaction. Calls SQL `COMMIT`."""
		self.sql("commit")
		frappe.local.rollback_observers = []
		self.flush_realtime_log()
		self.enqueue_global_search()