|
| 1 | +from flask_login import current_user # type: ignore # type: ignore |
| 2 | +from flask_restful import Resource, marshal_with, reqparse # type: ignore |
| 3 | +from werkzeug.exceptions import NotFound |
| 4 | + |
| 5 | +from controllers.console import api |
| 6 | +from controllers.console.wraps import account_initialization_required, enterprise_license_required, setup_required |
| 7 | +from fields.dataset_fields import dataset_metadata_fields |
| 8 | +from libs.login import login_required |
| 9 | +from services.dataset_service import DatasetService |
| 10 | +from services.entities.knowledge_entities.knowledge_entities import ( |
| 11 | + MetadataArgs, |
| 12 | + MetadataOperationData, |
| 13 | +) |
| 14 | +from services.metadata_service import MetadataService |
| 15 | + |
| 16 | + |
| 17 | +def _validate_name(name): |
| 18 | + if not name or len(name) < 1 or len(name) > 40: |
| 19 | + raise ValueError("Name must be between 1 to 40 characters.") |
| 20 | + return name |
| 21 | + |
| 22 | + |
| 23 | +def _validate_description_length(description): |
| 24 | + if len(description) > 400: |
| 25 | + raise ValueError("Description cannot exceed 400 characters.") |
| 26 | + return description |
| 27 | + |
| 28 | + |
| 29 | +class DatasetMetadataCreateApi(Resource): |
| 30 | + @setup_required |
| 31 | + @login_required |
| 32 | + @account_initialization_required |
| 33 | + @enterprise_license_required |
| 34 | + @marshal_with(dataset_metadata_fields) |
| 35 | + def post(self, dataset_id): |
| 36 | + parser = reqparse.RequestParser() |
| 37 | + parser.add_argument("type", type=str, required=True, nullable=True, location="json") |
| 38 | + parser.add_argument("name", type=str, required=True, nullable=True, location="json") |
| 39 | + args = parser.parse_args() |
| 40 | + metadata_args = MetadataArgs(**args) |
| 41 | + |
| 42 | + dataset_id_str = str(dataset_id) |
| 43 | + dataset = DatasetService.get_dataset(dataset_id_str) |
| 44 | + if dataset is None: |
| 45 | + raise NotFound("Dataset not found.") |
| 46 | + DatasetService.check_dataset_permission(dataset, current_user) |
| 47 | + |
| 48 | + metadata = MetadataService.create_metadata(dataset_id_str, metadata_args) |
| 49 | + return metadata, 201 |
| 50 | + |
| 51 | + @setup_required |
| 52 | + @login_required |
| 53 | + @account_initialization_required |
| 54 | + @enterprise_license_required |
| 55 | + def get(self, dataset_id): |
| 56 | + dataset_id_str = str(dataset_id) |
| 57 | + dataset = DatasetService.get_dataset(dataset_id_str) |
| 58 | + if dataset is None: |
| 59 | + raise NotFound("Dataset not found.") |
| 60 | + return MetadataService.get_dataset_metadatas(dataset), 200 |
| 61 | + |
| 62 | + |
| 63 | +class DatasetMetadataApi(Resource): |
| 64 | + @setup_required |
| 65 | + @login_required |
| 66 | + @account_initialization_required |
| 67 | + @enterprise_license_required |
| 68 | + @marshal_with(dataset_metadata_fields) |
| 69 | + def patch(self, dataset_id, metadata_id): |
| 70 | + parser = reqparse.RequestParser() |
| 71 | + parser.add_argument("name", type=str, required=True, nullable=True, location="json") |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + dataset_id_str = str(dataset_id) |
| 75 | + metadata_id_str = str(metadata_id) |
| 76 | + dataset = DatasetService.get_dataset(dataset_id_str) |
| 77 | + if dataset is None: |
| 78 | + raise NotFound("Dataset not found.") |
| 79 | + DatasetService.check_dataset_permission(dataset, current_user) |
| 80 | + |
| 81 | + metadata = MetadataService.update_metadata_name(dataset_id_str, metadata_id_str, args.get("name")) |
| 82 | + return metadata, 200 |
| 83 | + |
| 84 | + @setup_required |
| 85 | + @login_required |
| 86 | + @account_initialization_required |
| 87 | + @enterprise_license_required |
| 88 | + def delete(self, dataset_id, metadata_id): |
| 89 | + dataset_id_str = str(dataset_id) |
| 90 | + metadata_id_str = str(metadata_id) |
| 91 | + dataset = DatasetService.get_dataset(dataset_id_str) |
| 92 | + if dataset is None: |
| 93 | + raise NotFound("Dataset not found.") |
| 94 | + DatasetService.check_dataset_permission(dataset, current_user) |
| 95 | + |
| 96 | + MetadataService.delete_metadata(dataset_id_str, metadata_id_str) |
| 97 | + return 200 |
| 98 | + |
| 99 | + |
| 100 | +class DatasetMetadataBuiltInFieldApi(Resource): |
| 101 | + @setup_required |
| 102 | + @login_required |
| 103 | + @account_initialization_required |
| 104 | + @enterprise_license_required |
| 105 | + def get(self): |
| 106 | + built_in_fields = MetadataService.get_built_in_fields() |
| 107 | + return {"fields": built_in_fields}, 200 |
| 108 | + |
| 109 | + |
| 110 | +class DatasetMetadataBuiltInFieldActionApi(Resource): |
| 111 | + @setup_required |
| 112 | + @login_required |
| 113 | + @account_initialization_required |
| 114 | + @enterprise_license_required |
| 115 | + def post(self, dataset_id, action): |
| 116 | + dataset_id_str = str(dataset_id) |
| 117 | + dataset = DatasetService.get_dataset(dataset_id_str) |
| 118 | + if dataset is None: |
| 119 | + raise NotFound("Dataset not found.") |
| 120 | + DatasetService.check_dataset_permission(dataset, current_user) |
| 121 | + |
| 122 | + if action == "enable": |
| 123 | + MetadataService.enable_built_in_field(dataset) |
| 124 | + elif action == "disable": |
| 125 | + MetadataService.disable_built_in_field(dataset) |
| 126 | + return 200 |
| 127 | + |
| 128 | + |
| 129 | +class DocumentMetadataEditApi(Resource): |
| 130 | + @setup_required |
| 131 | + @login_required |
| 132 | + @account_initialization_required |
| 133 | + @enterprise_license_required |
| 134 | + def post(self, dataset_id): |
| 135 | + dataset_id_str = str(dataset_id) |
| 136 | + dataset = DatasetService.get_dataset(dataset_id_str) |
| 137 | + if dataset is None: |
| 138 | + raise NotFound("Dataset not found.") |
| 139 | + DatasetService.check_dataset_permission(dataset, current_user) |
| 140 | + |
| 141 | + parser = reqparse.RequestParser() |
| 142 | + parser.add_argument("operation_data", type=list, required=True, nullable=True, location="json") |
| 143 | + args = parser.parse_args() |
| 144 | + metadata_args = MetadataOperationData(**args) |
| 145 | + |
| 146 | + MetadataService.update_documents_metadata(dataset, metadata_args) |
| 147 | + |
| 148 | + return 200 |
| 149 | + |
| 150 | + |
| 151 | +api.add_resource(DatasetMetadataCreateApi, "/datasets/<uuid:dataset_id>/metadata") |
| 152 | +api.add_resource(DatasetMetadataApi, "/datasets/<uuid:dataset_id>/metadata/<uuid:metadata_id>") |
| 153 | +api.add_resource(DatasetMetadataBuiltInFieldApi, "/datasets/metadata/built-in") |
| 154 | +api.add_resource(DatasetMetadataBuiltInFieldActionApi, "/datasets/<uuid:dataset_id>/metadata/built-in/<string:action>") |
| 155 | +api.add_resource(DocumentMetadataEditApi, "/datasets/<uuid:dataset_id>/documents/metadata") |
0 commit comments