Esempio n. 1
0
def get_permission_filter(comm_id=None):
    """Get permission filter."""
    # check permission
    is_perm = search_permission.can()
    match = Q('match', publish_status='0')
    version = Q('match', relation_version_is_last='true')
    rng = Q('range', **{'publish_date': {'lte': 'now/d'}})
    term_list = []
    mst = []
    is_perm_paths = Indexes.get_browsing_tree_paths()
    search_type = request.values.get('search_type')

    if comm_id:
        if search_type == config.WEKO_SEARCH_TYPE_DICT['FULL_TEXT']:
            self_path = Indexes.get_self_path(comm_id)
            if self_path and self_path.path in is_perm_paths:
                term_list.append(self_path.path)

            path = term_list[0] + '*'
            should_path = []
            wildcard_path = Q("wildcard", path=path)
            should_path.append(wildcard_path)

            mst.append(match)
            mst.append(rng)
            terms = Q('bool', should=should_path)
        else:  # In case search_type is keyword or index
            self_path = Indexes.get_self_path(comm_id)
            if self_path and self_path.path in is_perm_paths:
                term_list.append(self_path.path)

            mst.append(match)
            mst.append(rng)
            terms = Q('terms', path=term_list)
    else:
        mst.append(match)
        mst.append(rng)
        terms = Q('terms', path=is_perm_paths)

    mut = []
    if is_perm:
        user_id, result = check_admin_user()
        if result:
            shuld = [
                Q('match', weko_creator_id=user_id),
                Q('match', weko_shared_id=user_id)
            ]
            shuld.append(Q('bool', must=mst))
            mut.append(Q('bool', should=shuld, must=[terms]))
            mut.append(Q('bool', must=version))
    else:
        mut = mst
        mut.append(terms)
        base_mut = [match, version]
        mut.append(Q('bool', must=base_mut))

    return mut
Esempio n. 2
0
File: admin.py Progetto: mhaya/weko
    def save_sort(self):
        """Save custom sort."""
        try:
            data = request.get_json()
            index_id = data.get("q_id")
            sort_data = data.get("sort")

            # save data to DB
            item_sort = {}
            for sort in sort_data:
                item_sort[sort.get('id')] = sort.get('custom_sort').get(
                    index_id)

            Indexes.set_item_sort_custom(index_id, item_sort)

            # update es
            fp = Indexes.get_self_path(index_id)
            Indexes.update_item_sort_custom_es(fp.path, sort_data)

            jfy = {}
            jfy['status'] = 200
            jfy['message'] = 'Data is successfully updated.'
            return make_response(jsonify(jfy), jfy['status'])
        except Exception as ex:
            jfy['status'] = 405
            jfy['message'] = 'Error'
            return make_response(jsonify(jfy), jfy['status'])
Esempio n. 3
0
File: query.py Progetto: mhaya/weko
def get_permission_filter(comm_id=None):
    """Get permission filter."""
    # check permission
    is_perm = search_permission.can()
    match = Q('match', publish_status='0')
    version = Q('match', relation_version_is_last='true')
    rng = Q('range', **{'publish_date': {'lte': 'now/d'}})
    term_list = []
    mst = []
    is_perm_paths = Indexes.get_browsing_tree_paths()
    if comm_id is not None:
        self_path = Indexes.get_self_path(comm_id)
        if self_path.path in is_perm_paths:
            term_list.append(self_path.path)

        mst.append(match)
        mst.append(rng)
        terms = Q('terms', path=term_list)
    else:
        mst.append(match)
        mst.append(rng)
        terms = Q('terms', path=is_perm_paths)

    mut = []
    if is_perm:
        user_id, result = check_admin_user()
        if result:
            shuld = [
                Q('match', weko_creator_id=user_id),
                Q('match', weko_shared_id=user_id)
            ]
            shuld.append(Q('bool', must=mst))
            mut.append(Q('bool', should=shuld, must=[terms]))
            mut.append(Q('bool', must=version))
    else:
        mut = mst
        mut.append(terms)
        base_mut = [match, version]
        mut.append(Q('bool', must=base_mut))

    return mut
Esempio n. 4
0
    def _get_index_search_query():
        """Get index search query."""
        query_q = {
            "from": 0,
            "size": 10000,
            "_source": {
                "excludes": ["content", "_item_metadata"]
            },
            "query": {
                "bool": {
                    "must": [{
                        "match": {
                            "path.tree": "@index"
                        }
                    }, {
                        "match": {
                            "relation_version_is_last": "true"
                        }
                    }]
                }
            },
            "post_filter": {
                "bool": {
                    "must": [{
                        "match": {
                            "publish_status": "0"
                        }
                    }, {
                        "range": {
                            "publish_date": {
                                "lte": "now/d"
                            }
                        }
                    }]
                }
            }
        }

        q = str(index_id)
        if q != str(current_app.config.get("WEKO_ROOT_INDEX",
                                           WEKO_ROOT_INDEX)):
            post_filter = query_q['post_filter']

            if post_filter:
                list_path = Indexes.get_list_path_publish(index_id)
                post_filter['bool']['must'].append(
                    {"terms": {
                        "path": list_path
                    }})
            # create search query
            try:
                fp = Indexes.get_self_path(q)
                query_q = json.dumps(query_q).replace("@index", fp.path)
                query_q = json.loads(query_q)
            except BaseException:
                pass
        else:
            post_filter = query_q['post_filter']

            if post_filter:
                list_path = Indexes.get_list_path_publish(index_id)
                post_filter['bool']['must'].append(
                    {"terms": {
                        "path": list_path
                    }})
            wild_card = []
            child_list = Indexes.get_child_list(q)
            if child_list:
                for item in child_list:
                    wc = {"wildcard": {"path.tree": item.cid}}
                    wild_card.append(wc)
                query_q['query']['bool']['must'] = [{
                    "bool": {
                        "should": wild_card
                    }
                }, {
                    "match": {
                        "relation_version_is_last": "true"
                    }
                }]
        return query_q
Esempio n. 5
0
File: query.py Progetto: mhaya/weko
    def _get_index_earch_query():

        query_q = {
            "_source": {
                "excludes": ['content']
            },
            "query": {
                "bool": {
                    "must": [{
                        "match": {
                            "path.tree": "@index"
                        }
                    }, {
                        "match": {
                            "relation_version_is_last": "true"
                        }
                    }]
                }
            },
            "aggs": {
                "path": {
                    "terms": {
                        "field": "path.tree",
                        "include": "@index|@index/[^/]+",
                        "size": "@count"
                    },
                    "aggs": {
                        "date_range": {
                            "filter": {
                                "match": {
                                    "publish_status": "0"
                                }
                            },
                            "aggs": {
                                "available": {
                                    "range": {
                                        "field":
                                        "publish_date",
                                        "ranges": [{
                                            "from": "now+1d/d"
                                        }, {
                                            "to": "now+1d/d"
                                        }]
                                    },
                                }
                            }
                        },
                        "no_available": {
                            "filter": {
                                "bool": {
                                    "must_not": [{
                                        "match": {
                                            "publish_status": "0"
                                        }
                                    }]
                                }
                            }
                        }
                    }
                }
            },
            "post_filter": {}
        }

        # add item type aggs
        query_q['aggs']['path']['aggs']. \
            update(get_item_type_aggs(search._index[0]))

        q = request.values.get('q') if index_id is None else index_id
        if q:
            mut = get_permission_filter(q)
        else:
            mut = get_permission_filter()
        if mut:
            mut = list(map(lambda x: x.to_dict(), mut))
            post_filter = query_q['post_filter']
            if mut[0].get('bool'):
                post_filter['bool'] = mut[0]['bool']
            else:
                post_filter['bool'] = {'must': mut}

        # create search query
        if q:
            try:
                fp = Indexes.get_self_path(q)
                if fp:
                    query_q = json.dumps(query_q).replace("@index", fp.path)
                    query_q = json.loads(query_q)
            except BaseException:
                pass

        query_q = json.dumps(query_q).replace("@count",
                                              str(Indexes.get_index_count()))
        query_q = json.loads(query_q)

        return query_q