-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
100 lines (76 loc) · 2.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi import Request
from deta import Deta
from typing import Union
from pydantic import BaseModel
from dotenv import load_dotenv
import os
import requests
# dto
class FormData(BaseModel):
firstname: str
lastname: str
email: str
captcha_id: str
captcha_text: str
# fast api setup
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="static")
# db setup
load_dotenv()
PROJECT_KEY = os.getenv('PROJECT_KEY')
deta = Deta(PROJECT_KEY)
forms_db = deta.Base("forms_db")
captcha_db = deta.Base("captcha_db")
# api
@app.post("/submit")
async def post(formData: FormData):
captcha = get_captcha(formData.captcha_id)
if captcha is None:
raise HTTPException(status_code=404, detail="Captcha not found")
if captcha["text_of_captcha"] == formData.captcha_text:
if not captcha["is_used"]:
captcha_db.update({
"is_used": True,
}, captcha["key"])
forms_db.insert({
"firstname": formData.firstname,
"lastname": formData.lastname,
"email": formData.email
})
return "OK"
else:
return "USED_CAPTCHA"
else:
return "NOT_OK"
@app.get("form/{id}")
async def get(id):
form = forms_db.get(id)
return form
# html routes
@app.get('/favicon.ico', include_in_schema=False)
async def favicon():
return FileResponse("static/favicon.ico")
@app.get('/', include_in_schema=False)
async def root(request: Request):
captcha_id = create_captcha()
return templates.TemplateResponse("index.html", {"request": request, "captcha_id": captcha_id})
def create_captcha():
url = "https://privatesimplecaptchaapi.deta.dev/create-random-captcha?number_of_words=1"
response = requests.get(url)
data = response.json()
captcha_db.insert({
"image_url": data["image_url"],
"audio_url": data["audio_url"],
"text_of_captcha": data["text_of_captcha"],
"audio_captcha_numbers": data["audio_captcha_numbers"],
"how_many_times_accessed": data["how_many_times_accessed"],
"is_used": False
}, data["captcha_id"])
return data["captcha_id"]
def get_captcha(captcha_id):
return captcha_db.get(captcha_id)