prompts.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from fastapi import Depends, FastAPI, HTTPException, status
  2. from datetime import datetime, timedelta
  3. from typing import List, Union, Optional
  4. from fastapi import APIRouter
  5. from pydantic import BaseModel
  6. import json
  7. from apps.web.models.prompts import Prompts, PromptForm, PromptModel
  8. from utils.utils import get_current_user
  9. from constants import ERROR_MESSAGES
  10. router = APIRouter()
  11. ############################
  12. # GetPrompts
  13. ############################
  14. @router.get("/", response_model=List[PromptModel])
  15. async def get_prompts(user=Depends(get_current_user)):
  16. return Prompts.get_prompts()
  17. ############################
  18. # CreateNewPrompt
  19. ############################
  20. @router.post("/create", response_model=Optional[PromptModel])
  21. async def create_new_prompt(form_data: PromptForm, user=Depends(get_current_user)):
  22. if user.role != "admin":
  23. raise HTTPException(
  24. status_code=status.HTTP_401_UNAUTHORIZED,
  25. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  26. )
  27. prompt = Prompts.get_prompt_by_command(form_data.command)
  28. if prompt == None:
  29. prompt = Prompts.insert_new_prompt(user.id, form_data)
  30. if prompt:
  31. return prompt
  32. else:
  33. raise HTTPException(
  34. status_code=status.HTTP_401_UNAUTHORIZED,
  35. detail=ERROR_MESSAGES.DEFAULT(),
  36. )
  37. else:
  38. raise HTTPException(
  39. status_code=status.HTTP_400_BAD_REQUEST,
  40. detail=ERROR_MESSAGES.COMMAND_TAKEN,
  41. )
  42. ############################
  43. # GetPromptByCommand
  44. ############################
  45. @router.get("/{command}", response_model=Optional[PromptModel])
  46. async def get_prompt_by_command(command: str, user=Depends(get_current_user)):
  47. prompt = Prompts.get_prompt_by_command(f"/{command}")
  48. if prompt:
  49. return prompt
  50. else:
  51. raise HTTPException(
  52. status_code=status.HTTP_401_UNAUTHORIZED,
  53. detail=ERROR_MESSAGES.NOT_FOUND,
  54. )
  55. ############################
  56. # UpdatePromptByCommand
  57. ############################
  58. @router.post("/{command}/update", response_model=Optional[PromptModel])
  59. async def update_prompt_by_command(
  60. command: str, form_data: PromptForm, user=Depends(get_current_user)
  61. ):
  62. if user.role != "admin":
  63. raise HTTPException(
  64. status_code=status.HTTP_401_UNAUTHORIZED,
  65. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  66. )
  67. prompt = Prompts.update_prompt_by_command(f"/{command}", form_data)
  68. if prompt:
  69. return prompt
  70. else:
  71. raise HTTPException(
  72. status_code=status.HTTP_401_UNAUTHORIZED,
  73. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  74. )
  75. ############################
  76. # DeletePromptByCommand
  77. ############################
  78. @router.delete("/{command}/delete", response_model=bool)
  79. async def delete_prompt_by_command(command: str, user=Depends(get_current_user)):
  80. if user.role != "admin":
  81. raise HTTPException(
  82. status_code=status.HTTP_401_UNAUTHORIZED,
  83. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  84. )
  85. result = Prompts.delete_prompt_by_command(f"/{command}")
  86. return result