feat: creating FastAPI routes for working with cards
Added 3 routes: - add_card (add a card via POST request with AddCardBody scheme) - get_card (get a specific card by card_id) - get_random_cards (get 10 random cards)
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from dirty_words import dirty_words_set
|
||||||
|
|
||||||
|
def is_not_empty(text: str) -> bool:
|
||||||
|
"""Checks that the text is not empty."""
|
||||||
|
return bool(text.strip())
|
||||||
|
|
||||||
|
def has_no_links(text: str) -> bool:
|
||||||
|
"""Checks that there are no links in the text."""
|
||||||
|
url_pattern = re.compile(r'https?://\S+|www\.\S+')
|
||||||
|
return not bool(url_pattern.search(text))
|
||||||
|
|
||||||
|
def has_no_dirty_words(text: str) -> bool:
|
||||||
|
"""Checks that there are no forbidden words in the text."""
|
||||||
|
words = set(re.findall(r'\w+', text.lower()))
|
||||||
|
return not bool(words & dirty_words_set)
|
||||||
|
|
||||||
|
def moderate_text(text: str) -> bool:
|
||||||
|
return is_not_empty(text) and has_no_links(text) and has_no_dirty_words(text)
|
||||||
File diff suppressed because one or more lines are too long
+36
@@ -3,6 +3,7 @@ import asyncio
|
|||||||
|
|
||||||
from schemas.api_schemas import *
|
from schemas.api_schemas import *
|
||||||
from mongo_worker import MongoWorker
|
from mongo_worker import MongoWorker
|
||||||
|
from base_moderation import moderate_text
|
||||||
|
|
||||||
from fastapi import FastAPI, Depends, Response, status
|
from fastapi import FastAPI, Depends, Response, status
|
||||||
|
|
||||||
@@ -42,6 +43,41 @@ async def add_user(new_user: AddUserBody,
|
|||||||
return BaseResponse(result="User already exist", error=True)
|
return BaseResponse(result="User already exist", error=True)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/get_card", status_code=200)
|
||||||
|
async def get_card(card_id: NonNegativeInt,
|
||||||
|
response: Response,
|
||||||
|
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
|
||||||
|
card = mongo.get_card(card_id)
|
||||||
|
if card:
|
||||||
|
return BaseResponse(result=card)
|
||||||
|
else:
|
||||||
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
|
return BaseResponse(result="There is no card with this card_id", error=True)
|
||||||
|
|
||||||
|
@app.get("/get_random_cards", status_code=200)
|
||||||
|
async def get_random_cards(response: Response,
|
||||||
|
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
|
||||||
|
result = mongo.get_random_cards(10, True)
|
||||||
|
if result:
|
||||||
|
return BaseResponse(result=result)
|
||||||
|
else:
|
||||||
|
response.status_code = status.HTTP_404_NOT_FOUND
|
||||||
|
return BaseResponse(result="No active cards", error=True)
|
||||||
|
|
||||||
|
@app.post("/add_card", status_code=201)
|
||||||
|
async def add_card(new_card: AddCardBody,
|
||||||
|
response: Response,
|
||||||
|
mongo: MongoWorker = Depends(MongoWorker)) -> BaseResponse:
|
||||||
|
if moderate_text(new_card.choice_A) and moderate_text(new_card.choice_B):
|
||||||
|
card = mongo.add_card(new_card.choice_A,
|
||||||
|
new_card.choice_B,
|
||||||
|
new_card.author_id)
|
||||||
|
return BaseResponse(result=card)
|
||||||
|
else:
|
||||||
|
response.status_code = status.HTTP_400_BAD_REQUEST
|
||||||
|
return BaseResponse(result="Card has not passed base moderation", error=True)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
config = uvicorn.Config("main:app", port=5000, log_level="info")
|
config = uvicorn.Config("main:app", port=5000, log_level="info")
|
||||||
server = uvicorn.Server(config)
|
server = uvicorn.Server(config)
|
||||||
|
|||||||
@@ -14,3 +14,9 @@ class AddUserBody(BaseModel):
|
|||||||
first_name: str
|
first_name: str
|
||||||
last_name: str
|
last_name: str
|
||||||
photo_url: str
|
photo_url: str
|
||||||
|
|
||||||
|
class AddCardBody(BaseModel):
|
||||||
|
choice_A: str
|
||||||
|
choice_B: str
|
||||||
|
|
||||||
|
author_id: NonNegativeInt
|
||||||
Reference in New Issue
Block a user