def list(name: text = None, hash: text = None, tags: comma_separated_list = None, offset: number = 0, limit: number = 25): """ Search a file using query filters (tags + hash or name). Support pagination. :param name: lookup name :param hash: lookup hash value :param tags: lookup tag list :param offset: start index in matching results :param limit: max number of results :rtype: dict of 'total': int, 'page': int, 'per_page': int, 'items': list of file(s) found :return: on success 'items' contains a list of files found on error 'msg' gives reason message """ session = db.session if name is not None: name = decode_utf8(name) log.debug("name %s h_value %s tags %s", name, hash, tags) if name is not None and hash is not None: raise ValueError("Can't find using both name and hash") if name is not None: base_query = FileExt.query_find_by_name(name, tags, session) elif hash is not None: h_type = guess_hash_type(hash) if h_type is None: raise ValueError("Hash not supported") base_query = FileExt.query_find_by_hash( h_type, hash, tags, session) else: # FIXME this is just a temporary way to output # all files, need a dedicated # file route and controller base_query = FileExt.query_find_by_name("", tags, session) # TODO: Find a way to move pagination as a BaseQuery like in # flask_sqlalchemy. # https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L422 items = base_query.limit(limit).offset(offset).all() if offset == 0 and len(items) < limit: total = len(items) else: total = base_query.count() log.debug("Found %s results", total) return { 'total': total, 'offset': offset, 'limit': limit, 'items': file_ext_schema_lite.dump(items, many=True).data, }
def test_find_by_name(self, m_Tag, m_File): m_session = MagicMock() name = "something" tag = MagicMock() tag.id = randint(0, 10) tags = [tag.id] FileExt.query_find_by_name(name, tags, m_session) m_Tag.find_by_id.assert_called_once_with(tag.id, m_session)
def test_find_by_name(self, m_Tag, m_File): m_session = MagicMock() name = "something" tag = MagicMock() tag.id = randint(0, 10) tags = [tag.id] FileExt.query_find_by_name(name, tags, m_session) m_session.query.called_with(m_Tag) m_session.query().filter_by.assert_called_once_with(id=tag.id) m_session.query().filter_by().one.assert_called_once()