|
@@ -1,4 +1,3 @@
|
|
|
-from fastapi import Response
|
|
|
from fastapi import Depends, FastAPI, HTTPException, status
|
|
|
from datetime import datetime, timedelta
|
|
|
from typing import List, Union, Optional
|
|
@@ -16,9 +15,7 @@ from apps.web.models.modelfiles import (
|
|
|
ModelfileResponse,
|
|
|
)
|
|
|
|
|
|
-from utils.utils import (
|
|
|
- bearer_scheme,
|
|
|
-)
|
|
|
+from utils.utils import bearer_scheme, get_current_user
|
|
|
from constants import ERROR_MESSAGES
|
|
|
|
|
|
router = APIRouter()
|
|
@@ -30,16 +27,7 @@ router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[ModelfileResponse])
|
|
|
async def get_modelfiles(skip: int = 0, limit: int = 50, cred=Depends(bearer_scheme)):
|
|
|
- token = cred.credentials
|
|
|
- user = Users.get_user_by_token(token)
|
|
|
-
|
|
|
- if user:
|
|
|
- return Modelfiles.get_modelfiles(skip, limit)
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.INVALID_TOKEN,
|
|
|
- )
|
|
|
+ return Modelfiles.get_modelfiles(skip, limit)
|
|
|
|
|
|
|
|
|
############################
|
|
@@ -48,36 +36,28 @@ async def get_modelfiles(skip: int = 0, limit: int = 50, cred=Depends(bearer_sch
|
|
|
|
|
|
|
|
|
@router.post("/create", response_model=Optional[ModelfileResponse])
|
|
|
-async def create_new_modelfile(form_data: ModelfileForm, cred=Depends(bearer_scheme)):
|
|
|
- token = cred.credentials
|
|
|
- user = Users.get_user_by_token(token)
|
|
|
-
|
|
|
- if user:
|
|
|
- # Admin Only
|
|
|
- if user.role == "admin":
|
|
|
- modelfile = Modelfiles.insert_new_modelfile(user.id, form_data)
|
|
|
-
|
|
|
- if modelfile:
|
|
|
- return ModelfileResponse(
|
|
|
- **{
|
|
|
- **modelfile.model_dump(),
|
|
|
- "modelfile": json.loads(modelfile.modelfile),
|
|
|
- }
|
|
|
- )
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.DEFAULT(),
|
|
|
- )
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
- )
|
|
|
+async def create_new_modelfile(
|
|
|
+ form_data: ModelfileForm, user=Depends(get_current_user)
|
|
|
+):
|
|
|
+ if user.role != "admin":
|
|
|
+ raise HTTPException(
|
|
|
+ status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
+ detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
+ )
|
|
|
+
|
|
|
+ modelfile = Modelfiles.insert_new_modelfile(user.id, form_data)
|
|
|
+
|
|
|
+ if modelfile:
|
|
|
+ return ModelfileResponse(
|
|
|
+ **{
|
|
|
+ **modelfile.model_dump(),
|
|
|
+ "modelfile": json.loads(modelfile.modelfile),
|
|
|
+ }
|
|
|
+ )
|
|
|
else:
|
|
|
raise HTTPException(
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.INVALID_TOKEN,
|
|
|
+ detail=ERROR_MESSAGES.DEFAULT(),
|
|
|
)
|
|
|
|
|
|
|
|
@@ -87,31 +67,20 @@ async def create_new_modelfile(form_data: ModelfileForm, cred=Depends(bearer_sch
|
|
|
|
|
|
|
|
|
@router.post("/", response_model=Optional[ModelfileResponse])
|
|
|
-async def get_modelfile_by_tag_name(
|
|
|
- form_data: ModelfileTagNameForm, cred=Depends(bearer_scheme)
|
|
|
-):
|
|
|
- token = cred.credentials
|
|
|
- user = Users.get_user_by_token(token)
|
|
|
-
|
|
|
- if user:
|
|
|
- modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
|
|
|
-
|
|
|
- if modelfile:
|
|
|
- return ModelfileResponse(
|
|
|
- **{
|
|
|
- **modelfile.model_dump(),
|
|
|
- "modelfile": json.loads(modelfile.modelfile),
|
|
|
- }
|
|
|
- )
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
- )
|
|
|
+async def get_modelfile_by_tag_name(form_data: ModelfileTagNameForm):
|
|
|
+ modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
|
|
|
+
|
|
|
+ if modelfile:
|
|
|
+ return ModelfileResponse(
|
|
|
+ **{
|
|
|
+ **modelfile.model_dump(),
|
|
|
+ "modelfile": json.loads(modelfile.modelfile),
|
|
|
+ }
|
|
|
+ )
|
|
|
else:
|
|
|
raise HTTPException(
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.INVALID_TOKEN,
|
|
|
+ detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
)
|
|
|
|
|
|
|
|
@@ -122,44 +91,34 @@ async def get_modelfile_by_tag_name(
|
|
|
|
|
|
@router.post("/update", response_model=Optional[ModelfileResponse])
|
|
|
async def update_modelfile_by_tag_name(
|
|
|
- form_data: ModelfileUpdateForm, cred=Depends(bearer_scheme)
|
|
|
+ form_data: ModelfileUpdateForm, user=Depends(get_current_user)
|
|
|
):
|
|
|
- token = cred.credentials
|
|
|
- user = Users.get_user_by_token(token)
|
|
|
-
|
|
|
- if user:
|
|
|
- if user.role == "admin":
|
|
|
- modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
|
|
|
- if modelfile:
|
|
|
- updated_modelfile = {
|
|
|
- **json.loads(modelfile.modelfile),
|
|
|
- **form_data.modelfile,
|
|
|
- }
|
|
|
-
|
|
|
- modelfile = Modelfiles.update_modelfile_by_tag_name(
|
|
|
- form_data.tag_name, updated_modelfile
|
|
|
- )
|
|
|
-
|
|
|
- return ModelfileResponse(
|
|
|
- **{
|
|
|
- **modelfile.model_dump(),
|
|
|
- "modelfile": json.loads(modelfile.modelfile),
|
|
|
- }
|
|
|
- )
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
- )
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
- )
|
|
|
+ if user.role != "admin":
|
|
|
+ raise HTTPException(
|
|
|
+ status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
+ detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
+ )
|
|
|
+ modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
|
|
|
+ if modelfile:
|
|
|
+ updated_modelfile = {
|
|
|
+ **json.loads(modelfile.modelfile),
|
|
|
+ **form_data.modelfile,
|
|
|
+ }
|
|
|
+
|
|
|
+ modelfile = Modelfiles.update_modelfile_by_tag_name(
|
|
|
+ form_data.tag_name, updated_modelfile
|
|
|
+ )
|
|
|
+
|
|
|
+ return ModelfileResponse(
|
|
|
+ **{
|
|
|
+ **modelfile.model_dump(),
|
|
|
+ "modelfile": json.loads(modelfile.modelfile),
|
|
|
+ }
|
|
|
+ )
|
|
|
else:
|
|
|
raise HTTPException(
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.INVALID_TOKEN,
|
|
|
+ detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
)
|
|
|
|
|
|
|
|
@@ -170,22 +129,13 @@ async def update_modelfile_by_tag_name(
|
|
|
|
|
|
@router.delete("/delete", response_model=bool)
|
|
|
async def delete_modelfile_by_tag_name(
|
|
|
- form_data: ModelfileTagNameForm, cred=Depends(bearer_scheme)
|
|
|
+ form_data: ModelfileTagNameForm, user=Depends(get_current_user)
|
|
|
):
|
|
|
- token = cred.credentials
|
|
|
- user = Users.get_user_by_token(token)
|
|
|
-
|
|
|
- if user:
|
|
|
- if user.role == "admin":
|
|
|
- result = Modelfiles.delete_modelfile_by_tag_name(form_data.tag_name)
|
|
|
- return result
|
|
|
- else:
|
|
|
- raise HTTPException(
|
|
|
- status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
- )
|
|
|
- else:
|
|
|
+ if user.role != "admin":
|
|
|
raise HTTPException(
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
- detail=ERROR_MESSAGES.INVALID_TOKEN,
|
|
|
+ detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
|
)
|
|
|
+
|
|
|
+ result = Modelfiles.delete_modelfile_by_tag_name(form_data.tag_name)
|
|
|
+ return result
|