final
This commit is contained in:
parent
265c335b0f
commit
7ecba57e85
@ -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")
|
||||
|
4
defs.py
4
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 = []
|
||||
|
Loading…
Reference in New Issue
Block a user