mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
create list_dataset api and tests (#1138)
### What problem does this PR solve? This PR have completed both HTTP API and Python SDK for 'list_dataset". In addition, there are tests for it. ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -46,7 +46,7 @@ from api.contants import NAME_LENGTH_LIMIT
|
||||
|
||||
# ------------------------------ create a dataset ---------------------------------------
|
||||
@manager.route('/', methods=['POST'])
|
||||
@login_required # use login
|
||||
@login_required # use login
|
||||
@validate_request("name") # check name key
|
||||
def create_dataset():
|
||||
# Check if Authorization header is present
|
||||
@ -111,10 +111,27 @@ def create_dataset():
|
||||
if not KnowledgebaseService.save(**request_body):
|
||||
# failed to create new dataset
|
||||
return construct_result()
|
||||
return construct_json_result(data={"dataset_id": request_body["id"]})
|
||||
return construct_json_result(data={"dataset_name": request_body["name"]})
|
||||
except Exception as e:
|
||||
return construct_error_response(e)
|
||||
|
||||
# -----------------------------list datasets-------------------------------------------------------
|
||||
@manager.route('/', methods=['GET'])
|
||||
@login_required
|
||||
def list_datasets():
|
||||
offset = request.args.get("offset", 0)
|
||||
count = request.args.get("count", -1)
|
||||
orderby = request.args.get("orderby", "create_time")
|
||||
desc = request.args.get("desc", True)
|
||||
try:
|
||||
tenants = TenantService.get_joined_tenants_by_user_id(current_user.id)
|
||||
kbs = KnowledgebaseService.get_by_tenant_ids(
|
||||
[m["tenant_id"] for m in tenants], current_user.id, int(offset), int(count), orderby, desc)
|
||||
return construct_json_result(data=kbs, code=RetCode.DATA_ERROR, message=f"attempt to list datasets")
|
||||
except Exception as e:
|
||||
return construct_error_response(e)
|
||||
|
||||
# ---------------------------------delete a dataset ----------------------------
|
||||
|
||||
@manager.route('/<dataset_id>', methods=['DELETE'])
|
||||
@login_required
|
||||
@ -135,8 +152,5 @@ def get_dataset(dataset_id):
|
||||
return construct_json_result(code=RetCode.DATA_ERROR, message=f"attempt to get detail of dataset: {dataset_id}")
|
||||
|
||||
|
||||
@manager.route('/', methods=['GET'])
|
||||
@login_required
|
||||
def list_datasets():
|
||||
return construct_json_result(code=RetCode.DATA_ERROR, message=f"attempt to list datasets")
|
||||
|
||||
|
||||
|
||||
@ -40,6 +40,29 @@ class KnowledgebaseService(CommonService):
|
||||
|
||||
return list(kbs.dicts())
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
|
||||
offset, count, orderby, desc):
|
||||
kbs = cls.model.select().where(
|
||||
((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
|
||||
TenantPermission.TEAM.value)) | (
|
||||
cls.model.tenant_id == user_id))
|
||||
& (cls.model.status == StatusEnum.VALID.value)
|
||||
)
|
||||
if desc:
|
||||
kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
|
||||
else:
|
||||
kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
|
||||
|
||||
kbs = list(kbs.dicts())
|
||||
|
||||
kbs_length = len(kbs)
|
||||
if offset < 0 or offset > kbs_length:
|
||||
raise IndexError("Offset is out of the valid range.")
|
||||
|
||||
return kbs[offset:offset+count]
|
||||
|
||||
@classmethod
|
||||
@DB.connection_context()
|
||||
def get_detail(cls, kb_id):
|
||||
|
||||
Reference in New Issue
Block a user