INIT Commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
class BaseEndpoint:
|
||||
def __init__(self, db):
|
||||
self.db = db
|
||||
@@ -0,0 +1,17 @@
|
||||
from sqlalchemy import select, desc
|
||||
|
||||
from database.models import Flight
|
||||
from endpoints.base import BaseEndpoint
|
||||
|
||||
|
||||
class FlightsEndpoint(BaseEndpoint):
|
||||
|
||||
async def resolve(self):
|
||||
self.db.add(Flight(name="test"))
|
||||
await self.db.flush()
|
||||
|
||||
data = await self.db.execute(select(Flight).order_by(desc(Flight.id)))
|
||||
model = data.scalars().first()
|
||||
return {
|
||||
"status": model
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
from database.models import User, Flight, Copilot, Airport, Aircraft
|
||||
from endpoints.base import BaseEndpoint
|
||||
|
||||
|
||||
class InitDataEndpoint(BaseEndpoint):
|
||||
|
||||
async def on_get(self):
|
||||
user_ids = []
|
||||
copilot_ids = []
|
||||
flight_ids = []
|
||||
|
||||
users = [
|
||||
User(avatar_image_url="", name="Karel Vomacka", email="a@test.cz", password_hashed="****"),
|
||||
User(avatar_image_url="", name="Karel Novak", email="b@test.cz", password_hashed="****"),
|
||||
User(avatar_image_url="", name="Franta Pavel", email="c@test.cz", password_hashed="****"),
|
||||
]
|
||||
|
||||
airport = Airport(name="Letiste Letnany", icao_code="LKLT")
|
||||
self.db.add(airport)
|
||||
|
||||
for user in users:
|
||||
self.db.add(user)
|
||||
await self.db.flush()
|
||||
user_ids.append(user.id)
|
||||
|
||||
aircraft = Aircraft(name="OK-AUR28", type="Bristell NG5", description="", created_by=user)
|
||||
self.db.add(aircraft)
|
||||
|
||||
copilot = None
|
||||
if user.name == 'Franta Pavel':
|
||||
copilot = Copilot(name="Copilot test", created_by_id=user.id)
|
||||
self.db.add(copilot)
|
||||
await self.db.flush()
|
||||
|
||||
copilot_ids.append(copilot.id)
|
||||
|
||||
flight = Flight(name="test flight", description="Testovaci popis", duration_total=65, duration_pic=65,
|
||||
takeoff_airport=airport, landing_airport=airport, aircraft=aircraft, created_by_id=user.id,
|
||||
copilot_id=copilot.id if copilot else None)
|
||||
self.db.add(flight)
|
||||
await self.db.flush()
|
||||
flight_ids.append(flight.id)
|
||||
|
||||
return {
|
||||
"user_ids": user_ids,
|
||||
"copilot_ids": copilot_ids,
|
||||
"flight_ids": flight_ids,
|
||||
'airport_id': airport.id
|
||||
}
|
||||
|
||||
#
|
||||
#
|
||||
# self.db.add(Flight(name="test"))
|
||||
# await self.db.flush()
|
||||
#
|
||||
# data = await self.db.execute(select(Flight).order_by(desc(Flight.id)))
|
||||
# model = data.scalars().first()
|
||||
# return {
|
||||
# "status": model
|
||||
# }
|
||||
@@ -0,0 +1,59 @@
|
||||
from fastapi import HTTPException
|
||||
from fastapi_jwt import JwtAuthorizationCredentials
|
||||
from fastapi_jwt.jwt import JwtAccess, JwtRefresh
|
||||
from passlib.hash import bcrypt
|
||||
from sqlalchemy import select
|
||||
from starlette.responses import Response
|
||||
|
||||
from database.models import User
|
||||
from endpoints.base import BaseEndpoint
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class LoginInput(BaseModel):
|
||||
email: str
|
||||
password: str
|
||||
|
||||
|
||||
class LoginEndpoint(BaseEndpoint):
|
||||
|
||||
def __init__(self, db, access_token: JwtAccess, refresh_token: JwtRefresh):
|
||||
super().__init__(db)
|
||||
self.access_security = access_token
|
||||
self.refresh_security = refresh_token
|
||||
|
||||
async def on_post(self, user_data: LoginInput, resp: Response) -> dict:
|
||||
query = select(User).filter_by(email=user_data.email)
|
||||
logged_user = (await self.db.scalars(query)).first()
|
||||
|
||||
if not logged_user:
|
||||
raise HTTPException(status_code=401, detail="Invalid user")
|
||||
|
||||
if not bcrypt.verify(user_data.password, logged_user.password_hashed):
|
||||
raise HTTPException(status_code=401, detail="Bad username or password")
|
||||
|
||||
subject = {"id": logged_user.id, "email": logged_user.email}
|
||||
access_token = self.access_security.create_access_token(subject=subject)
|
||||
refresh_token = self.refresh_security.create_refresh_token(subject=subject)
|
||||
|
||||
self.access_security.set_access_cookie(resp, access_token)
|
||||
self.refresh_security.set_refresh_cookie(resp, refresh_token)
|
||||
|
||||
return {
|
||||
"user": logged_user.as_dict(),
|
||||
"access_token": access_token,
|
||||
"refresh_token": refresh_token
|
||||
}
|
||||
|
||||
|
||||
class MeEndpoint(BaseEndpoint):
|
||||
|
||||
def __init__(self, db, credentials: JwtAuthorizationCredentials):
|
||||
super().__init__(db)
|
||||
self.credentials = credentials
|
||||
|
||||
async def on_get(self) -> dict:
|
||||
query = select(User).filter_by(id=self.credentials['id'])
|
||||
user = (await self.db.scalars(query)).first()
|
||||
|
||||
return user.as_dict()
|
||||
@@ -0,0 +1,45 @@
|
||||
import re
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, root_validator, Field
|
||||
from database.models import User
|
||||
from endpoints.base import BaseEndpoint
|
||||
from passlib.hash import bcrypt
|
||||
|
||||
|
||||
class RegistrationInput(BaseModel):
|
||||
email: str = Field(..., min_length=4)
|
||||
name: Optional[str]
|
||||
password: str
|
||||
|
||||
@root_validator()
|
||||
def validate_email(cls, values):
|
||||
email = values.get("email") or ""
|
||||
|
||||
if email and not re.match(r"(.+)@(.+)\..{2,6}", email):
|
||||
raise ValueError("Specified e-mail is not valid!")
|
||||
|
||||
return values
|
||||
|
||||
|
||||
class RegistrationEndpoint(BaseEndpoint):
|
||||
|
||||
async def on_post(self, user_data: RegistrationInput) -> User:
|
||||
query = select(User).filter_by(email=user_data.email)
|
||||
existing_user = (await self.db.scalars(query)).first()
|
||||
|
||||
if existing_user:
|
||||
raise HTTPException(status_code=422, detail="User already exists")
|
||||
|
||||
model = User(
|
||||
name=user_data.name,
|
||||
email=user_data.email,
|
||||
password_hashed=bcrypt.hash(user_data.password)
|
||||
)
|
||||
|
||||
self.db.add(model)
|
||||
await self.db.commit()
|
||||
|
||||
return model.as_dict()
|
||||
Reference in New Issue
Block a user