prompts.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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,
  22. user=Depends(get_current_user)):
  23. if user.role != "admin":
  24. raise HTTPException(
  25. status_code=status.HTTP_401_UNAUTHORIZED,
  26. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  27. )
  28. prompt = Prompts.get_prompt_by_command(form_data.command)
  29. if prompt == None:
  30. prompt = Prompts.insert_new_prompt(user.id, form_data)
  31. if prompt:
  32. return prompt
  33. else:
  34. raise HTTPException(
  35. status_code=status.HTTP_401_UNAUTHORIZED,
  36. detail=ERROR_MESSAGES.DEFAULT(),
  37. )
  38. else:
  39. raise HTTPException(
  40. status_code=status.HTTP_400_BAD_REQUEST,
  41. detail=ERROR_MESSAGES.COMMAND_TAKEN,
  42. )
  43. ############################
  44. # GetPromptByCommand
  45. ############################
  46. @router.get("/{command}", response_model=Optional[PromptModel])
  47. async def get_prompt_by_command(command: str, user=Depends(get_current_user)):
  48. prompt = Prompts.get_prompt_by_command(f"/{command}")
  49. if prompt:
  50. return prompt
  51. else:
  52. raise HTTPException(
  53. status_code=status.HTTP_401_UNAUTHORIZED,
  54. detail=ERROR_MESSAGES.NOT_FOUND,
  55. )
  56. ############################
  57. # UpdatePromptByCommand
  58. ############################
  59. @router.post("/{command}/update", response_model=Optional[PromptModel])
  60. async def update_prompt_by_command(command: str,
  61. form_data: PromptForm,
  62. user=Depends(get_current_user)):
  63. if user.role != "admin":
  64. raise HTTPException(
  65. status_code=status.HTTP_401_UNAUTHORIZED,
  66. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  67. )
  68. prompt = Prompts.update_prompt_by_command(f"/{command}", form_data)
  69. if prompt:
  70. return prompt
  71. else:
  72. raise HTTPException(
  73. status_code=status.HTTP_401_UNAUTHORIZED,
  74. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  75. )
  76. ############################
  77. # DeletePromptByCommand
  78. ############################
  79. @router.delete("/{command}/delete", response_model=bool)
  80. async def delete_prompt_by_command(command: str,
  81. user=Depends(get_current_user)):
  82. if user.role != "admin":
  83. raise HTTPException(
  84. status_code=status.HTTP_401_UNAUTHORIZED,
  85. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  86. )
  87. result = Prompts.delete_prompt_by_command(f"/{command}")
  88. return result