32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import os
|
|
from pathlib import Path
|
|
|
|
from rest_framework.views import APIView
|
|
from common.auth import TokenAuth
|
|
from common.auth.authentication import has_permissions
|
|
from common.constants.permission_constants import RoleConstants
|
|
from common.exception.app_exception import AppApiException
|
|
from common.result import result
|
|
from maxkb.conf import PROJECT_DIR
|
|
from models_provider.local_imports import ModelImportStore, ImportError
|
|
|
|
|
|
class LocalModelImports(APIView):
|
|
authentication_classes = [TokenAuth]
|
|
|
|
@staticmethod
|
|
def store():
|
|
return ModelImportStore(os.environ.get('MAXKB_MODEL_IMPORT_DIR', str(Path(PROJECT_DIR) / 'model' / 'imports')))
|
|
|
|
@has_permissions(RoleConstants.ADMIN)
|
|
def get(self, request):
|
|
return result.success(self.store().list())
|
|
|
|
@has_permissions(RoleConstants.ADMIN)
|
|
def post(self, request):
|
|
try:
|
|
return result.success(self.store().create(request.user.id,
|
|
path=request.data.get('path'), upload=request.FILES.get('file')))
|
|
except ImportError as exc:
|
|
raise AppApiException(500, str(exc))
|