style: remove unnecessary whitespace in dataset.py for improved readability

pull/18148/head
ZeroZ_JQ 1 year ago
parent 024649adb9
commit 331ea65f68

@ -330,16 +330,16 @@ class DocumentStatusApi(DatasetApiResource):
def patch(self, tenant_id, dataset_id, action): def patch(self, tenant_id, dataset_id, action):
""" """
Batch update document status. Batch update document status.
Args: Args:
tenant_id: tenant id tenant_id: tenant id
dataset_id: dataset id dataset_id: dataset id
action: action to perform (enable, disable, archive, un_archive) action: action to perform (enable, disable, archive, un_archive)
Returns: Returns:
dict: A dictionary with a key 'result' and a value 'success' dict: A dictionary with a key 'result' and a value 'success'
int: HTTP status code 200 indicating that the operation was successful. int: HTTP status code 200 indicating that the operation was successful.
Raises: Raises:
NotFound: If the dataset with the given ID does not exist. NotFound: If the dataset with the given ID does not exist.
Forbidden: If the user does not have permission. Forbidden: If the user does not have permission.
@ -347,7 +347,7 @@ class DocumentStatusApi(DatasetApiResource):
""" """
dataset_id_str = str(dataset_id) dataset_id_str = str(dataset_id)
dataset = DatasetService.get_dataset(dataset_id_str) dataset = DatasetService.get_dataset(dataset_id_str)
if dataset is None: if dataset is None:
raise NotFound("Dataset not found.") raise NotFound("Dataset not found.")
@ -356,28 +356,28 @@ class DocumentStatusApi(DatasetApiResource):
DatasetService.check_dataset_permission(dataset, current_user) DatasetService.check_dataset_permission(dataset, current_user)
except services.errors.account.NoPermissionError as e: except services.errors.account.NoPermissionError as e:
raise Forbidden(str(e)) raise Forbidden(str(e))
# Check dataset model setting # Check dataset model setting
DatasetService.check_dataset_model_setting(dataset) DatasetService.check_dataset_model_setting(dataset)
# Get document IDs from request body # Get document IDs from request body
data = request.get_json() data = request.get_json()
document_ids = data.get('document_ids', []) document_ids = data.get("document_ids", [])
if not document_ids: if not document_ids:
return {"result": "success"}, 200 return {"result": "success"}, 200
for document_id in document_ids: for document_id in document_ids:
document = DocumentService.get_document(dataset_id_str, document_id) document = DocumentService.get_document(dataset_id_str, document_id)
if not document: if not document:
continue continue
indexing_cache_key = f"document_{document.id}_indexing" indexing_cache_key = f"document_{document.id}_indexing"
cache_result = redis_client.get(indexing_cache_key) cache_result = redis_client.get(indexing_cache_key)
if cache_result is not None: if cache_result is not None:
raise InvalidActionError(f"Document:{document.name} is being indexed, please try again later") raise InvalidActionError(f"Document:{document.name} is being indexed, please try again later")
if action == "enable": if action == "enable":
if document.enabled: if document.enabled:
continue continue
@ -386,45 +386,45 @@ class DocumentStatusApi(DatasetApiResource):
document.disabled_by = None document.disabled_by = None
document.updated_at = datetime.now(UTC).replace(tzinfo=None) document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit() db.session.commit()
# Set cache to prevent indexing the same document multiple times # Set cache to prevent indexing the same document multiple times
redis_client.setex(indexing_cache_key, 600, 1) redis_client.setex(indexing_cache_key, 600, 1)
add_document_to_index_task.delay(document_id) add_document_to_index_task.delay(document_id)
elif action == "disable": elif action == "disable":
if not document.completed_at or document.indexing_status != "completed": if not document.completed_at or document.indexing_status != "completed":
raise InvalidActionError(f"Document: {document.name} is not completed.") raise InvalidActionError(f"Document: {document.name} is not completed.")
if not document.enabled: if not document.enabled:
continue continue
document.enabled = False document.enabled = False
document.disabled_at = datetime.now(UTC).replace(tzinfo=None) document.disabled_at = datetime.now(UTC).replace(tzinfo=None)
document.disabled_by = current_user.id document.disabled_by = current_user.id
document.updated_at = datetime.now(UTC).replace(tzinfo=None) document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit() db.session.commit()
# Set cache to prevent indexing the same document multiple times # Set cache to prevent indexing the same document multiple times
redis_client.setex(indexing_cache_key, 600, 1) redis_client.setex(indexing_cache_key, 600, 1)
remove_document_from_index_task.delay(document_id) remove_document_from_index_task.delay(document_id)
elif action == "archive": elif action == "archive":
if document.archived: if document.archived:
continue continue
document.archived = True document.archived = True
document.archived_at = datetime.now(UTC).replace(tzinfo=None) document.archived_at = datetime.now(UTC).replace(tzinfo=None)
document.archived_by = current_user.id document.archived_by = current_user.id
document.updated_at = datetime.now(UTC).replace(tzinfo=None) document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit() db.session.commit()
if document.enabled: if document.enabled:
# Set cache to prevent indexing the same document multiple times # Set cache to prevent indexing the same document multiple times
redis_client.setex(indexing_cache_key, 600, 1) redis_client.setex(indexing_cache_key, 600, 1)
remove_document_from_index_task.delay(document_id) remove_document_from_index_task.delay(document_id)
elif action == "un_archive": elif action == "un_archive":
if not document.archived: if not document.archived:
continue continue
@ -433,15 +433,15 @@ class DocumentStatusApi(DatasetApiResource):
document.archived_by = None document.archived_by = None
document.updated_at = datetime.now(UTC).replace(tzinfo=None) document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit() db.session.commit()
# Set cache to prevent indexing the same document multiple times # Set cache to prevent indexing the same document multiple times
redis_client.setex(indexing_cache_key, 600, 1) redis_client.setex(indexing_cache_key, 600, 1)
add_document_to_index_task.delay(document_id) add_document_to_index_task.delay(document_id)
else: else:
raise InvalidActionError() raise InvalidActionError()
return {"result": "success"}, 200 return {"result": "success"}, 200

Loading…
Cancel
Save