This commit is contained in:
Valeria Fadeeva 2025-06-08 05:05:38 +05:00
parent 98fd31c44f
commit 50462c7729
2214 changed files with 308 additions and 210 deletions

View File

@ -1,4 +1,4 @@
from datetime import datetime # from datetime import datetime
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
@ -14,11 +14,13 @@ class Model(DeclarativeBase):
class DatingOrm(Model): class DatingOrm(Model):
__tablename__ = "datings" __tablename__ = "datings"
id: Mapped[int] = mapped_column(primary_key=True) id: Mapped[int] = mapped_column(primary_key=True, unique=True, autoincrement=True)
sessionkey: Mapped[str] = mapped_column(unique=True)
theme_name: Mapped[str] theme_name: Mapped[str]
sender_name: Mapped[str] sender_name: Mapped[str]
sender_sex: Mapped[str] sender_sex: Mapped[str]
sender_age: Mapped[int] sender_age: Mapped[int]
sender_city: Mapped[str]
telegram_name: Mapped[str] telegram_name: Mapped[str]
interests_in_school_university: Mapped[str] interests_in_school_university: Mapped[str]
interests_in_sports: Mapped[str] interests_in_sports: Mapped[str]

View File

@ -1,10 +1,15 @@
from contextlib import asynccontextmanager # import pathlib
from fastapi import FastAPI, Request
from fastapi import FastAPI from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
import uuid
from contextlib import asynccontextmanager
from .database import create_tables, delete_tables from .database import create_tables, delete_tables
from .router import router as datings_router from .router import router as datings_router
from .validity import is_valid_uuid
@asynccontextmanager @asynccontextmanager
@ -17,8 +22,22 @@ async def lifespan(app: FastAPI):
pass pass
app = FastAPI(lifespan=lifespan) # app = FastAPI(lifespan=lifespan)
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None)
templates = Jinja2Templates(directory="templates")
app.include_router(datings_router) app.include_router(datings_router)
app.mount("/", StaticFiles(directory="public", html=True)) app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
response = templates.TemplateResponse("index.html", {"request": request})
sessionKey = request.cookies.get("sessionkey")
if isinstance(sessionKey, str) and is_valid_uuid(sessionKey):
pass
else:
response.set_cookie(key="sessionkey", value=str(uuid.uuid4()))
return response

View File

@ -1,13 +1,22 @@
from fastapi import APIRouter, FastAPI, Depends, Body, Form, Request, HTTPException # from json import JSONDecodeError, dumps, loads
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi import (
APIRouter,
Body,
Cookie,
Depends,
FastAPI,
Form,
Header,
HTTPException,
Request,
)
from fastapi.responses import HTMLResponse, JSONResponse
from starlette.datastructures import FormData from starlette.datastructures import FormData
from json import JSONDecodeError, dumps, loads
import uuid
from .repository import DatingRepository from .repository import DatingRepository
from .schemas import SDating, SDatingAdd, SDatingId from .schemas import SDating, SDatingAdd, SDatingId
# async def get_body(request: Request): # async def get_body(request: Request):
# content_type = request.headers.get('Content-Type') # content_type = request.headers.get('Content-Type')
# if content_type is None: # if content_type is None:
@ -43,11 +52,11 @@ router = APIRouter(
# new_dating_id = await DatingRepository.add_dating(dating) # new_dating_id = await DatingRepository.add_dating(dating)
# return {"id": new_dating_id} # type: ignore # return {"id": new_dating_id} # type: ignore
async def add_dating(dating: SDatingAdd = Body()) -> JSONResponse: async def add_dating(dating: SDatingAdd = Body()) -> JSONResponse:
new_dating_id = await DatingRepository.add_dating(dating) new_dating_id = await DatingRepository.add_dating(dating)
content = {"id": new_dating_id} # type: ignore content = {"id": new_dating_id} # type: ignore
response = JSONResponse(content=content) response = JSONResponse(content=content)
response.set_cookie(key="fakesession", value=str(uuid.uuid4()))
return response return response
@ -56,45 +65,10 @@ async def get_datings() -> list[SDating]:
datings = await DatingRepository.get_datings() datings = await DatingRepository.get_datings()
return datings return datings
# async def get_datings() -> JSONResponse: # async def get_datings() -> JSONResponse:
# datings = await DatingRepository.get_datings() # datings = await DatingRepository.get_datings()
# content = dumps(datings, default=lambda o: o.__dict__) # type: ignore # content = dumps(datings, default=lambda o: o.__dict__) # type: ignore
# response = JSONResponse(content=content) # response = JSONResponse(content=content)
# response.set_cookie(key="fakesession", value="fake-cookie-session-value") # response.set_cookie(key="fakesession", value="fake-cookie-session-value")
# return response # return response
@router.post("/message")
async def message_data(
theme_name: str = Form(...),
sender_name: str = Form(...),
sender_sex: str = Form(...),
sender_age: int = Form(...),
telegram_name: str = Form(...),
interests_in_school_university: str = Form(...),
interests_in_sports: str = Form(...),
interests_in_work: str = Form(...),
interests_in_your_free_time: str = Form(...),
pets_plants_fish: str = Form(...),
relationships_with_relatives_and_friends: str = Form(...),
past_relationships: str = Form(...),
life_principles_values: str = Form(...),
goals_in_life: str = Form(...),
message_text: str = Form(...),
):
return {
"theme_name": theme_name,
"sender_name": sender_name,
"sender_sex": sender_sex,
"sender_age": sender_age,
"telegram_name": telegram_name,
"interests_in_school_university": interests_in_school_university,
"interests_in_sports": interests_in_sports,
"interests_in_work": interests_in_work,
"interests_in_your_free_time": interests_in_your_free_time,
"pets_plants_fish": pets_plants_fish,
"relationships_with_relatives_and_friends": relationships_with_relatives_and_friends,
"past_relationships": past_relationships,
"life_principles_values": life_principles_values,
"goals_in_life": goals_in_life,
"message_text": message_text,
}

View File

@ -1,12 +1,14 @@
from datetime import datetime # from datetime import datetime
from pydantic import BaseModel, ConfigDict from pydantic import BaseModel, ConfigDict
class SDatingAdd(BaseModel): class SDatingAdd(BaseModel):
sessionkey: str
theme_name: str theme_name: str
sender_name: str sender_name: str
sender_sex: str sender_sex: str
sender_age: int sender_age: int
sender_city: str
telegram_name: str telegram_name: str
interests_in_school_university: str interests_in_school_university: str
interests_in_sports: str interests_in_sports: str
@ -22,7 +24,6 @@ class SDatingAdd(BaseModel):
class SDating(SDatingAdd): class SDating(SDatingAdd):
id: int id: int
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)

29
LeraFoxQueen/validity.py Normal file
View File

@ -0,0 +1,29 @@
import uuid
def is_valid_uuid(uuid_to_test, version=4):
"""
Check if uuid_to_test is a valid UUID.
Parameters
----------
uuid_to_test : str
version : {1, 2, 3, 4}
Returns
-------
`True` if uuid_to_test is a valid UUID, otherwise `False`.
Examples
--------
>>> is_valid_uuid('c9bf9e57-1685-4c89-bafb-ff5af830be8a')
True
>>> is_valid_uuid('c9bf9e58')
False
"""
try:
uuid_obj = uuid.UUID(uuid_to_test, version=version)
except ValueError:
return False
return str(uuid_obj) == uuid_to_test

View File

@ -27,6 +27,7 @@ dependencies = [
"h11 (>=0.16.0,<0.17.0)", "h11 (>=0.16.0,<0.17.0)",
"idna (>=3.10,<4.0)", "idna (>=3.10,<4.0)",
"sniffio (>=1.3.1,<2.0.0)", "sniffio (>=1.3.1,<2.0.0)",
"jinja2 (>=3.1.6,<4.0.0)",
] ]
package-mode = false package-mode = false

View File

@ -3,9 +3,10 @@
python -m venv .venv python -m venv .venv
pyenv local 3.13 pyenv local 3.13
poetry env activate poetry env activate
poetry lock
poetry install --no-root poetry install --no-root
#hypercorn LeraFoxQueen.main:app --reload --bind 0.0.0.0:8001 #hypercorn LeraFoxQueen.main:app --reload --bind 0.0.0.0:8001
#poetry run hypercorn LeraFoxQueen/main:app --reload --bind 0.0.0.0:8001 #poetry run hypercorn LeraFoxQueen/main:app --reload --bind 0.0.0.0:8001
#uvicorn LeraFoxQueen.main:app --reload --host 0.0.0.0 --port 8001 #uvicorn LeraFoxQueen.main:app --reload --host 0.0.0.0 --port 8001
uvicorn LeraFoxQueen.main:app --reload --host 127.0.0.1 --port 8001 uvicorn LeraFoxQueen.main:app --reload --host 127.0.0.1 --port 8001

View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 697 B

After

Width:  |  Height:  |  Size: 697 B

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

Some files were not shown because too many files have changed in this diff Show More