def search_list(kb,
                    from_=None,
                    match_type=None,
                    page=None,
                    per_page=None,
                    unique=False):
        """Search "mapping from" for knowledge."""
        # init
        page = page or 1
        per_page = per_page or 10

        if kb.kbtype == models.KnwKB.KNWKB_TYPES['written_as']:
            # get the base query
            query = api.query_kb_mappings(kbid=kb.id,
                                          key=from_ or '',
                                          match_type=match_type
                                          or 's').with_entities(
                                              models.KnwKBRVAL.m_key)
            # if you want a 'unique' list
            if unique:
                query = query.distinct()
            # run query and paginate
            return [
                item.m_key for item in pagination.RestfulSQLAlchemyPagination(
                    query, page=page or 1, per_page=per_page or 10).items
            ]
        return []
    def search_list(kb,
                    value=None,
                    match_type=None,
                    page=None,
                    per_page=None,
                    unique=False):
        """Search "mappings to" for knowledge."""
        # init
        page = page or 1
        per_page = per_page or 10

        if kb.kbtype == models.KnwKB.KNWKB_TYPES['written_as']:
            # get the base query
            query = api.query_kb_mappings(kbid=kb.id,
                                          value=value or '',
                                          match_type=match_type
                                          or 's').with_entities(
                                              models.KnwKBRVAL.m_value)
            # if you want a 'unique' list
            if unique:
                query = query.distinct()
            # run query and paginate
            return [
                item.m_value
                for item in pagination.RestfulSQLAlchemyPagination(
                    query, page=page or 1, per_page=per_page or 10).items
            ]
        elif kb.kbtype == models.KnwKB.KNWKB_TYPES['dynamic']:
            items = api.get_kbd_values(kb.name, value)
            return pagination.RestfulPagination(
                page=page, per_page=per_page,
                total_count=len(items)).slice(items)
        return []
Пример #3
0
            def get(self):
                import json
                from flask import make_response
                from invenio_ext.restful.errors import (RestfulError,
                                                        InvalidPageError)
                from invenio_ext.restful import pagination

                response = None
                try:
                    endpoint = request.endpoint
                    args = request.args
                    page = int(args.get('page', 1))
                    per_page = int(args.get('per_page', 2))
                    # check values arguments and raise exceptions if any errors
                    if per_page < 0:
                        raise RestfulError(
                            error_msg="Invalid per_page: {}".format(per_page),
                            status_code=400)
                    if page < 0:
                        raise InvalidPageError(
                            error_msg="Invalid page: {}".format(page),
                            status_code=400)

                    # need to sort by id
                    # also assuming only one user so no need to filter
                    # user's id
                    tags_q = WtgTAGPaginationMokup()
                    p = pagination.RestfulSQLAlchemyPagination(
                        query=tags_q, page=page, per_page=per_page)
                    if page > p.pages:
                        raise InvalidPageError(
                            error_msg="Invalid page: {}".format(page),
                            status_code=400)
                    tags_to_return = map(
                        lambda x: TagRepresenation(x).marshal(), p.items)

                    kwargs = {}
                    kwargs['endpoint'] = endpoint
                    kwargs['args'] = request.args
                    link_header = p.link_header(**kwargs)
                    response = make_response(json.dumps(tags_to_return))
                    response.headers[link_header[0]] = link_header[1]
                    response.headers['Content-Type'] = request.headers[
                        'Content-Type']
                except (RestfulError, InvalidPageError) as e:
                    exception = {}
                    exception['message'] = e.error_msg
                    exception['type'] = "{0}".format(type(e))
                    response = make_response(json.dumps(exception))
                return response
 def search_mappings(kb,
                     key=None,
                     value=None,
                     match_type=None,
                     sortby=None,
                     page=None,
                     per_page=None):
     """Search tags for knowledge."""
     if kb.kbtype == models.KnwKB.KNWKB_TYPES['written_as']:
         return pagination.RestfulSQLAlchemyPagination(
             api.query_kb_mappings(
                 kbid=kb.id,
                 key=key or '',
                 value=value or '',
                 match_type=match_type or 's',
                 sortby=sortby or 'to',
             ),
             page=page or 1,
             per_page=per_page or 10).items
     return []