Пример #1
0
def savedocs(doc, action):
    """save / submit / update doclist"""
    try:
        doc = dataent.get_doc(json.loads(doc))
        set_local_name(doc)

        # action
        doc.docstatus = {
            "Save": 0,
            "Submit": 1,
            "Update": 1,
            "Cancel": 2
        }[action]

        if doc.docstatus == 1:
            doc.submit()
        else:
            try:
                doc.save()
            except dataent.NameError as e:
                doctype, name, original_exception = e if isinstance(
                    e, tuple) else (doc.doctype or "", doc.name or "", None)
                dataent.msgprint(
                    dataent._("{0} {1} already exists").format(doctype, name))
                raise

        # update recent documents
        run_onload(doc)
        dataent.get_user().update_recent(doc.doctype, doc.name)
        send_updated_docs(doc)
    except Exception:
        dataent.errprint(dataent.utils.get_traceback())
        raise
Пример #2
0
def apply_permissions(data):
	default_country = dataent.db.get_default("country")

	user = dataent.get_user()
	user.build_permissions()

	allowed_pages = get_allowed_pages()
	allowed_reports = get_allowed_reports()

	new_data = []
	for section in data:
		new_items = []

		for item in (section.get("items") or []):
			item = dataent._dict(item)

			if item.country and item.country!=default_country:
				continue

			if ((item.type=="doctype" and item.name in user.can_read)
				or (item.type=="page" and item.name in allowed_pages)
				or (item.type=="report" and item.name in allowed_reports)
				or item.type=="help"):

				new_items.append(item)

		if new_items:
			new_section = section.copy()
			new_section["items"] = new_items
			new_data.append(new_section)

	return new_data
Пример #3
0
def getdoc(doctype, name, user=None):
    """
	Loads a doclist for a given document. This method is called directly from the client.
	Requries "doctype", "name" as form variables.
	Will also call the "onload" method on the document.
	"""

    if not (doctype and name):
        raise Exception('doctype and name required!')

    if not name:
        name = doctype

    if not dataent.db.exists(doctype, name):
        return []

    try:
        doc = dataent.get_doc(doctype, name)
        run_onload(doc)

        if not doc.has_permission("read"):
            dataent.flags.error_message = _(
                'Insufficient Permission for {0}').format(
                    dataent.bold(doctype + ' ' + name))
            raise dataent.PermissionError(("read", doctype, name))

        doc.apply_fieldlevel_read_permissions()

        # add file list
        doc.add_viewed()
        get_docinfo(doc)

    except Exception:
        dataent.errprint(dataent.utils.get_traceback())
        raise

    if doc and not name.startswith('_'):
        dataent.get_user().update_recent(doctype, name)

    doc.add_seen()

    dataent.response.docs.append(doc)
Пример #4
0
def get_notifications_for_targets(config, notification_percent):
    """Notifications for doc targets"""
    can_read = dataent.get_user().get_can_read()
    doc_target_percents = {}

    # doc_target_percents = {
    # 	"Company": {
    # 		"Acme": 87,
    # 		"RobotsRUs": 50,
    # 	}, {}...
    # }

    for doctype in config.targets:
        if doctype in can_read:
            if doctype in notification_percent:
                doc_target_percents[doctype] = notification_percent[doctype]
            else:
                doc_target_percents[doctype] = {}
                d = config.targets[doctype]
                condition = d["filters"]
                target_field = d["target_field"]
                value_field = d["value_field"]
                try:
                    if isinstance(condition, dict):
                        doc_list = dataent.get_list(
                            doctype,
                            fields=["name", target_field, value_field],
                            filters=condition,
                            limit_page_length=100,
                            ignore_ifnull=True)

                except dataent.PermissionError:
                    dataent.clear_messages()
                    pass
                except Exception as e:
                    if e.args[0] not in (1412, 1684):
                        raise

                else:
                    for doc in doc_list:
                        value = doc[value_field]
                        target = doc[target_field]
                        doc_target_percents[doctype][doc.name] = (
                            value / target * 100) if value < target else 100

    return doc_target_percents
Пример #5
0
def get_notifications_for_doctypes(config, notification_count):
    """Notifications for DocTypes"""
    can_read = dataent.get_user().get_can_read()
    open_count_doctype = {}

    for d in config.for_doctype:
        if d in can_read:
            condition = config.for_doctype[d]

            if d in notification_count:
                open_count_doctype[d] = notification_count[d]
            else:
                try:
                    if isinstance(condition, dict):
                        result = len(
                            dataent.get_list(d,
                                             fields=["name"],
                                             filters=condition,
                                             limit_page_length=100,
                                             as_list=True,
                                             ignore_ifnull=True))
                    else:
                        result = dataent.get_attr(condition)()

                except dataent.PermissionError:
                    dataent.clear_messages()
                    pass
                    # dataent.msgprint("Permission Error in notifications for {0}".format(d))

                except Exception as e:
                    # OperationalError: (1412, 'Table definition has changed, please retry transaction')
                    # InternalError: (1684, 'Table definition is being modified by concurrent DDL statement')
                    if e.args[0] not in (1412, 1684):
                        raise

                else:
                    open_count_doctype[d] = result
                    dataent.cache().hset("notification_count:" + d,
                                         dataent.session.user, result)

    return open_count_doctype
Пример #6
0
def get_notification_info_for_boot():
    out = get_notifications()
    config = get_notification_config()
    can_read = dataent.get_user().get_can_read()
    conditions = {}
    module_doctypes = {}
    doctype_info = dict(
        dataent.db.sql("""select name, module from tabDocType"""))

    for d in list(set(can_read + list(config.for_doctype))):
        if d in config.for_doctype:
            conditions[d] = config.for_doctype[d]

        if d in doctype_info:
            module_doctypes.setdefault(doctype_info[d], []).append(d)

    out.update({
        "conditions": conditions,
        "module_doctypes": module_doctypes,
    })

    return out
Пример #7
0
def get_feed_match_conditions(user=None, force=True):
    if not user: user = dataent.session.user

    conditions = [
        '`tabCommunication`.owner="{user}" or `tabCommunication`.reference_owner="{user}"'
        .format(user=dataent.db.escape(user))
    ]

    user_permissions = dataent.permissions.get_user_permissions(user)
    can_read = dataent.get_user().get_can_read()

    can_read_doctypes = [
        '"{}"'.format(doctype)
        for doctype in list(set(can_read) - set(list(user_permissions)))
    ]

    if can_read_doctypes:
        conditions += [
            """(`tabCommunication`.reference_doctype is null
			or `tabCommunication`.reference_doctype = ''
			or `tabCommunication`.reference_doctype in ({}))""".format(
                ", ".join(can_read_doctypes))
        ]

        if user_permissions:
            can_read_docs = []
            for doctype, obj in user_permissions.items():
                for n in obj:
                    can_read_docs.append('"{}|{}"'.format(
                        doctype, dataent.db.escape(n.get('doc', ''))))

            if can_read_docs:
                conditions.append(
                    "concat_ws('|', `tabCommunication`.reference_doctype, `tabCommunication`.reference_name) in ({})"
                    .format(", ".join(can_read_docs)))

    return "(" + " or ".join(conditions) + ")"
Пример #8
0
def get_user(bootinfo):
    """get user info"""
    bootinfo.user = dataent.get_user().load_user()