Пример #1
0
def search(text, start=0, limit=20, doctype=""):
    """
	Search for given text in __global_search
	:param text: phrase to be searched
	:param start: start results at, default 0
	:param limit: number of results to return, default 20
	:return: Array of result objects
	"""
    results = []
    texts = text.split('&')
    for text in texts:
        text = "+" + text + "*"
        if not doctype:
            result = dataent.db.sql('''
				select
					doctype, name, content
				from
					__global_search
				where
					match(content) against (%s IN BOOLEAN MODE)
				limit {start}, {limit}'''.format(start=start, limit=limit),
                                    text + "*",
                                    as_dict=True)
        else:
            result = dataent.db.sql('''
				select
					doctype, name, content
				from
					__global_search
				where
					doctype = %s AND
					match(content) against (%s IN BOOLEAN MODE)
				limit {start}, {limit}'''.format(start=start, limit=limit),
                                    (doctype, text),
                                    as_dict=True)
        tmp_result = []
        for i in result:
            if i in results or not results:
                tmp_result.append(i)
        results = tmp_result

    for r in results:
        try:
            if dataent.get_meta(r.doctype).image_field:
                r.image = dataent.db.get_value(
                    r.doctype, r.name,
                    dataent.get_meta(r.doctype).image_field)
        except Exception:
            dataent.clear_messages()

    return results
Пример #2
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
Пример #3
0
def reset_password(user):
    if user == "Administrator":
        return 'not allowed'

    try:
        user = dataent.get_doc("User", user)
        if not user.enabled:
            return 'disabled'

        user.validate_reset_password()
        user.reset_password(send_email=True)

        return dataent.msgprint(
            _("Password reset instructions have been sent to your email"))

    except dataent.DoesNotExistError:
        dataent.clear_messages()
        return 'not found'
Пример #4
0
def get_notifications_for(notification_type, config, notification_count):
    open_count = {}
    notification_map = config.get(notification_type) or {}
    for m in notification_map:
        try:
            if m in notification_count:
                open_count[m] = notification_count[m]
            else:
                open_count[m] = dataent.get_attr(notification_map[m])()

                dataent.cache().hset("notification_count:" + m,
                                     dataent.session.user, open_count[m])
        except dataent.PermissionError:
            dataent.clear_messages()
            pass
            # dataent.msgprint("Permission Error in notifications for {0}".format(m))

    return open_count
Пример #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