diff --git a/apis/main/get_tips.py b/apis/main/get_tips.py index 47abea8..dc4c1dc 100644 --- a/apis/main/get_tips.py +++ b/apis/main/get_tips.py @@ -2,17 +2,23 @@ import secrets from defs import app from models.services.tip import TipAction -from typing import Dict, Optional +from typing import Dict, Optional, List system_random = secrets.SystemRandom() -async def get_random_tips() -> Optional[Dict]: +async def get_random_tips() -> List[Optional[Dict]]: tips = await TipAction.get_tips() if not tips: return None - num = system_random.randint(0, len(tips) - 1) - return tips[num].dict_tip() + if len(tips) < 4: + return [tip.dict_tip() for tip in tips] + data = [] + while len(data) < 4: + num = system_random.randint(0, len(tips) - 1) + if tips[num] not in data: + data.append(tips[num]) + return [tip.dict_tip() for tip in data] @app.get("/get_tips") diff --git a/defs.py b/defs.py index 035bacc..96cbe9e 100644 --- a/defs.py +++ b/defs.py @@ -2,6 +2,7 @@ import asyncio from fastapi import FastAPI from starlette.middleware.trustedhost import TrustedHostMiddleware +from fastapi.middleware.cors import CORSMiddleware from models.sqlite import Sqlite @@ -10,6 +11,9 @@ app = FastAPI() app.add_middleware( TrustedHostMiddleware, allowed_hosts=["127.0.0.1"] ) +app.add_middleware( + CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"] +) sqlite = Sqlite() need_auth_routes = [] need_auth_uid_only_routes = []