Compare commits
No commits in common. "250d3f1f15624f937fbfce0c2f48c4add2332bac" and "d5588dd055e06cc812af3dc02d2e2d190bce97b4" have entirely different histories.
250d3f1f15
...
d5588dd055
196
auth/models.py
196
auth/models.py
@ -1,25 +1,15 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
import random
|
from backend.config import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES
|
||||||
import smtplib
|
from backend.config import pwd_context, get_session_db
|
||||||
from backend.config import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES ,pwd_context, get_session_db, Base, user_collection
|
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from fastapi import Depends, HTTPException
|
from fastapi import Depends, HTTPException
|
||||||
from typing import Annotated
|
from typing import Annotated, Optional
|
||||||
from fastapi.security import OAuth2PasswordBearer
|
from fastapi.security import OAuth2PasswordBearer
|
||||||
from pydantic.networks import EmailStr
|
from passlib.context import CryptContext
|
||||||
from sqlalchemy import Integer, DateTime, ForeignKey
|
|
||||||
from sqlalchemy.orm import Session, relationship, mapped_column, Mapped
|
|
||||||
from sqlalchemy.dialects.postgresql import ARRAY
|
|
||||||
from email.message import EmailMessage
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from ..collectionObj.models import CollectionsDB #iç içe import döngüsünü önlemek için TYPE_CHECKING kullanıyoruz
|
|
||||||
|
|
||||||
|
|
||||||
import jwt
|
import jwt
|
||||||
|
from sqlmodel import SQLModel, Field, Session, select
|
||||||
|
from pydantic.networks import EmailStr
|
||||||
|
|
||||||
class Token(BaseModel):
|
class Token(BaseModel):
|
||||||
access_token: str
|
access_token: str
|
||||||
@ -38,62 +28,40 @@ class Status(str, Enum):
|
|||||||
banned = "banned"
|
banned = "banned"
|
||||||
suspended = "suspended"
|
suspended = "suspended"
|
||||||
|
|
||||||
### KULLANICI MODELLERİ ### sqlalchemy ve pydantic modelleri farklıdır
|
### KULLANICI MODELLERİ ###
|
||||||
class UserBase(BaseModel): #bu bir veri tabanı modeli değil !!!! lütfen dikkat et
|
class UserBase(SQLModel):
|
||||||
username: str | None = None #Option yerine Union kullanabilirsin
|
username: Optional[str] = None
|
||||||
role: Role | None = None
|
user_id: Optional[int] = None
|
||||||
status: Status | None = None
|
role: Optional[Role] = None
|
||||||
|
status: Optional[Status] = None
|
||||||
|
|
||||||
class UserInDb(UserBase):
|
class UserInDb(UserBase):
|
||||||
user_id: int | None = None
|
|
||||||
email: EmailStr | None = None
|
|
||||||
hashed_password: str | None = None
|
hashed_password: str | None = None
|
||||||
|
|
||||||
class UserPublic(BaseModel):
|
class UserPublic(UserBase):
|
||||||
username : str | None = None
|
pass
|
||||||
role : Role | None = None
|
|
||||||
status : Status | None = None
|
|
||||||
|
|
||||||
class UserCreate(BaseModel):
|
class UserCreate(BaseModel):
|
||||||
username: str | None = None
|
username: Optional[str] = None
|
||||||
role: Role | None = None
|
role: Optional[Role] = None
|
||||||
email : EmailStr | None = None
|
email : EmailStr | None = None
|
||||||
status: Status | None = None
|
status: Optional[Status] = None
|
||||||
password : str | None = None
|
password : str | None = None
|
||||||
|
|
||||||
|
|
||||||
### VERİTABANI MODELİ ###
|
### VERİTABANI MODELİ ###
|
||||||
class DBUser(Base):
|
class DBUser(SQLModel, table=True):
|
||||||
__tablename__ = "users_table"
|
__tablename__ = "users" # opsiyonel, sqlmodel bunu otomatik de atar
|
||||||
|
user_id: Optional[int] = Field(default=None, primary_key=True)
|
||||||
user_id: Mapped[int] = mapped_column(primary_key=True, index=True, autoincrement=True)
|
username: str = Field(index=True, nullable=False)
|
||||||
#collection_id : Mapped[list[int]] = mapped_column(Integer, ForeignKey("collections_table.collection_id"), nullable=True) # collection_id ile ilişki
|
hashed_password: str = Field(nullable=False)
|
||||||
username : Mapped[str] = mapped_column(unique=True, index=True, nullable=False)
|
role: Role = Field(default=Role.user)
|
||||||
email : Mapped[str] = mapped_column(unique=True, index=True, nullable=False)
|
status: Status = Field(default=Status.active)
|
||||||
hashed_password : Mapped[str] = mapped_column(nullable=False)
|
|
||||||
role : Mapped[Role] = mapped_column(default=Role.user)
|
|
||||||
status : Mapped[Status] = mapped_column(default=Status.active)
|
|
||||||
created_date : Mapped[datetime] = mapped_column(DateTime, default=datetime.now()) #datetime.datetime -> python, DateTime -> sqlalchemy
|
|
||||||
bio : Mapped[str] = mapped_column(default="No bio")
|
|
||||||
follow_users : Mapped[list[int]] = mapped_column(ARRAY(Integer), default=[]) # takip edilen kullanıcılar
|
|
||||||
# -> buralar diğer tablolar ile olan ilişkiler
|
|
||||||
#items : Mapped[list['Items']] = relationship("Items", back_populates="user", cascade="all, delete-orphan") items'e direk değil collection üzerinden erişiyoruz
|
|
||||||
collections : Mapped[list['CollectionsDB']] = relationship(
|
|
||||||
"CollectionsDB",
|
|
||||||
secondary=user_collection,
|
|
||||||
back_populates="users",
|
|
||||||
lazy='select'
|
|
||||||
) # collection'lar ile olan ilişki
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### AUTH ###
|
### AUTH ###
|
||||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### SERVİSLER ###
|
|
||||||
|
|
||||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||||
return pwd_context.verify(plain_password, hashed_password)
|
return pwd_context.verify(plain_password, hashed_password)
|
||||||
|
|
||||||
@ -106,20 +74,21 @@ def authenticate_user(
|
|||||||
password: str
|
password: str
|
||||||
) -> UserInDb | None:
|
) -> UserInDb | None:
|
||||||
|
|
||||||
user = session.query(DBUser).filter(DBUser.username == username).first()
|
statement = select(DBUser).where(DBUser.username == username)
|
||||||
if user is None or not verify_password(password, user.hashed_password): #sqlalchemy'de bu şekilde kontrol ediliyor None ile
|
result = session.exec(statement).first()
|
||||||
|
if not result or not verify_password(password, result.hashed_password):
|
||||||
return None
|
return None
|
||||||
return user
|
return result
|
||||||
|
|
||||||
|
|
||||||
def create_access_token(
|
def create_access_token(
|
||||||
data: dict,
|
data: dict,
|
||||||
expires_delta: Annotated[timedelta, None] = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
|
expires_delta: Optional[timedelta] = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
|
||||||
) -> str:
|
) -> str:
|
||||||
to_encode = data.copy()
|
to_encode = data.copy()
|
||||||
expire = datetime.now(timezone.utc) + expires_delta
|
expire = datetime.now(timezone.utc) + expires_delta
|
||||||
to_encode.update({"exp": expire})
|
to_encode.update({"exp": expire})
|
||||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm="HS256")
|
||||||
return encoded_jwt
|
return encoded_jwt
|
||||||
|
|
||||||
|
|
||||||
@ -128,8 +97,9 @@ async def get_user(
|
|||||||
username: str
|
username: str
|
||||||
) -> UserInDb | None:
|
) -> UserInDb | None:
|
||||||
|
|
||||||
user = session.query(DBUser).filter(DBUser.username == username).first()
|
statement = select(DBUser).where(DBUser.username == username)
|
||||||
return user
|
result = session.exec(statement).first()
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
async def get_current_user(
|
async def get_current_user(
|
||||||
@ -139,17 +109,14 @@ async def get_current_user(
|
|||||||
|
|
||||||
credentials_exception = HTTPException(
|
credentials_exception = HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="Invalid credentials currently",
|
detail="Invalid credentials",
|
||||||
headers={"WWW-Authenticate": "Bearer"},
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
||||||
username : str | None = payload.get("sub")
|
username: Optional[str] = payload.get("sub")
|
||||||
user = UserInDb.model_validate(payload)
|
|
||||||
|
|
||||||
if username is None:
|
if username is None:
|
||||||
raise credentials_exception
|
raise credentials_exception
|
||||||
|
|
||||||
except jwt.PyJWTError:
|
except jwt.PyJWTError:
|
||||||
raise credentials_exception
|
raise credentials_exception
|
||||||
|
|
||||||
@ -166,94 +133,3 @@ async def get_current_active_user(
|
|||||||
if current_user.status == Status.banned:
|
if current_user.status == Status.banned:
|
||||||
raise HTTPException(status_code=400, detail="Inactive user")
|
raise HTTPException(status_code=400, detail="Inactive user")
|
||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
|
|
||||||
### Kullanıcı kaydı
|
|
||||||
def register_user(
|
|
||||||
session: Annotated[Session, Depends(get_session_db)],
|
|
||||||
user: Annotated[UserCreate, Depends()]
|
|
||||||
) -> UserPublic:
|
|
||||||
|
|
||||||
user_dict = user.dict() # kullanıcıdan gelen verileri alıyoruz çunku şifreyi hashleyeceğiz
|
|
||||||
user_dict['hashed_password'] = get_password_hash(user.password) # şifreyi hashliyoruz
|
|
||||||
|
|
||||||
if not verify_password(user.password, user_dict['hashed_password']):
|
|
||||||
raise HTTPException(status_code=400, detail="Password hashing failed") # şifre hashleme işlemi başarısız oldu
|
|
||||||
|
|
||||||
# Kullanıcı adı ve e-posta adresinin benzersiz olduğunu kontrol et
|
|
||||||
existing_user = session.query(DBUser).filter(
|
|
||||||
(DBUser.username == user.username) | (DBUser.email == user.email)
|
|
||||||
).first()
|
|
||||||
|
|
||||||
if existing_user:
|
|
||||||
raise HTTPException(status_code=400, detail="Username or email already registered")
|
|
||||||
|
|
||||||
user_dict['created_date'] = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S") # kullanıcı oluşturulma tarihi
|
|
||||||
user_dict.pop('password') ##password'u veri tabanına eklemiyoruz zaten sınıfımızda tanımlı değil hata verir
|
|
||||||
db_user = DBUser(**user_dict) #alchemy ile pydantic modelleri farklıdır bir birine
|
|
||||||
session.add(db_user) # donuşum yaparken dikkat et
|
|
||||||
session.commit()
|
|
||||||
session.refresh(db_user)
|
|
||||||
return db_user
|
|
||||||
|
|
||||||
|
|
||||||
def find_user_w_email(
|
|
||||||
session: Annotated[Session, Depends(get_session_db)],
|
|
||||||
email: EmailStr | None = None,
|
|
||||||
):
|
|
||||||
|
|
||||||
exist_user = session.query(DBUser).filter(DBUser.email == email).first() #email ile kullanıcıyı bul
|
|
||||||
if exist_user is None:
|
|
||||||
raise HTTPException(status_code=400, detail="User not found")
|
|
||||||
|
|
||||||
if exist_user.status == Status.banned:
|
|
||||||
raise HTTPException(status_code=400, detail="Inactive user")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def send_password_to_email(
|
|
||||||
session: Annotated[Session, Depends(get_session_db)],
|
|
||||||
email: EmailStr | None = None,
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
msg = EmailMessage() #obje oluştur
|
|
||||||
msg['Subject'] = 'Password Reset'
|
|
||||||
msg['From'] = 'hansneiumann@gmail.com'
|
|
||||||
msg['To'] = email
|
|
||||||
veritification_code = generate_password_reset_number()
|
|
||||||
msg.set_content(veritification_code)
|
|
||||||
|
|
||||||
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
|
|
||||||
smtp.login("hansneiumann@gmail.com", "rwaq mbil lzut dgja")
|
|
||||||
smtp.send_message(msg)
|
|
||||||
|
|
||||||
update_password_w_email(session, email=email, password=veritification_code) #şifreyi güncelle
|
|
||||||
|
|
||||||
|
|
||||||
def generate_password_reset_number() -> str:
|
|
||||||
return str(random.randint(10000000, 99999999)) # 8 haneli rastgele bir sayı döndür
|
|
||||||
|
|
||||||
|
|
||||||
def update_password_w_email(
|
|
||||||
session: Annotated[Session, Depends(get_session_db)],
|
|
||||||
password: str | None = None,
|
|
||||||
email: EmailStr | None = None,
|
|
||||||
) -> dict:
|
|
||||||
|
|
||||||
hashed_password = get_password_hash(password)
|
|
||||||
session.query(DBUser).filter(DBUser.email == email).update({"hashed_password": hashed_password})
|
|
||||||
session.commit()
|
|
||||||
|
|
||||||
return {"message": "Password updated successfully"}
|
|
||||||
|
|
||||||
def update_password_w_user(
|
|
||||||
session: Annotated[Session, Depends(get_session_db)],
|
|
||||||
user: Annotated[DBUser , None],
|
|
||||||
password: str | None = None,
|
|
||||||
|
|
||||||
) -> any:
|
|
||||||
|
|
||||||
hashed_password = get_password_hash(password)
|
|
||||||
session.query(DBUser).filter(DBUser.user_id == user.user_id).update({"hashed_password": hashed_password})
|
|
||||||
session.commit()
|
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from .models import Token, UserPublic, authenticate_user, create_access_token, UserCreate, find_user_w_email, get_current_user, register_user, send_password_to_email, update_password_w_user
|
from .models import Token, UserPublic
|
||||||
|
from .models import authenticate_user, create_access_token
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from ..auth.models import get_password_hash, verify_password
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
from sqlmodel import Session
|
||||||
from ..config import get_session_db
|
from ..config import get_session_db
|
||||||
|
from fastapi import Depends
|
||||||
from fastapi.security import OAuth2PasswordRequestForm
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
from sqlalchemy.orm import Session
|
from .models import UserCreate, DBUser
|
||||||
from pydantic.networks import EmailStr
|
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
@ -35,41 +38,21 @@ async def login_for_access_token(
|
|||||||
return Token(access_token=access_token, token_type="bearer")
|
return Token(access_token=access_token, token_type="bearer")
|
||||||
|
|
||||||
|
|
||||||
@router.post('/register', response_model=UserPublic) #userPublic güvenli bir model
|
@router.post('/register', response_model=UserPublic)
|
||||||
async def create_user(
|
async def create_user(
|
||||||
session : Annotated[Session, Depends(get_session_db)],
|
session : Annotated[Session, Depends(get_session_db)],
|
||||||
user : Annotated[UserCreate, Depends()]
|
user : Annotated[UserCreate, Depends()]
|
||||||
):
|
):
|
||||||
|
user_dict = user.dict()
|
||||||
return register_user(session, user)
|
print(user.password)
|
||||||
|
user_dict['hashed_password'] = get_password_hash(user.password)
|
||||||
|
print (user_dict['hashed_password'])
|
||||||
|
|
||||||
@router.post('/password_reset')
|
if not verify_password(user.password, user_dict['hashed_password']):
|
||||||
async def password_reset(
|
raise HTTPException(status_code=400, detail="Password hashing failed")
|
||||||
session : Annotated[Session, Depends(get_session_db)],
|
|
||||||
email : Annotated[EmailStr, None] = None,
|
|
||||||
task: Annotated[BackgroundTasks, None] = None, # BackgroundTasks, task'ı arka planda çalıştırmak için kullanıyoruz
|
|
||||||
):
|
|
||||||
|
|
||||||
if not find_user_w_email(session, email):
|
|
||||||
return HTTPException(
|
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
|
||||||
detail="bad request",
|
|
||||||
)
|
|
||||||
|
|
||||||
task.add_task(send_password_to_email, session, email)
|
|
||||||
|
|
||||||
return {"message": "New password has been sent to your email."}
|
db_user = DBUser.model_validate(user_dict)
|
||||||
|
session.add(db_user)
|
||||||
@router.post('/update_password')
|
session.commit()
|
||||||
async def update_password(
|
session.refresh(db_user)
|
||||||
user: Annotated[str, Depends(get_current_user)],
|
return db_user
|
||||||
session: Annotated[Session, Depends(get_session_db)],
|
|
||||||
new_password: Annotated[str, None] = None,
|
|
||||||
) -> dict:
|
|
||||||
|
|
||||||
update_password_w_user(session, user, new_password)
|
|
||||||
|
|
||||||
return {"message": "Password updated successfully."}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1,173 +0,0 @@
|
|||||||
from fastapi import HTTPException, Depends
|
|
||||||
from sqlalchemy import Integer, String, Boolean
|
|
||||||
from pydantic import BaseModel
|
|
||||||
from sqlalchemy.orm import Session, relationship, mapped_column, Mapped
|
|
||||||
from ..config import Base, get_session_db, user_collection, collection_item
|
|
||||||
from ..auth.models import DBUser
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from ..items.models import Items, Item
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
###### SCHEMAS #########
|
|
||||||
|
|
||||||
class CollectionBase(BaseModel):
|
|
||||||
collection_name : str | None = None
|
|
||||||
collection_description : str | None = None
|
|
||||||
visibility : bool | None = None
|
|
||||||
|
|
||||||
class CollectionCreate(CollectionBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class CollectionPublic(CollectionBase):
|
|
||||||
collection_id : int | None = None
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
from_attributes = True #sqlalchemy ile pydantic arasında geçiş yapabilmek için kullanılır
|
|
||||||
|
|
||||||
class CollectionUpdate(CollectionBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
##### veri tabanı modelleri #####
|
|
||||||
class CollectionsDB(Base):
|
|
||||||
__tablename__ = "collections_table"
|
|
||||||
|
|
||||||
collection_id : Mapped[int] = mapped_column(Integer, primary_key=True, index=True, autoincrement=True)
|
|
||||||
#user_id : Mapped[int] = mapped_column(Integer, ForeignKey("users_table.user_id"), nullable=False) # user_id ile ilişki
|
|
||||||
#item_id : Mapped[list[int]] = mapped_column(Integer, ForeignKey("items_table.item_id"), nullable=False) # item_id ile ilişki
|
|
||||||
visibility : Mapped[bool] = mapped_column(Boolean, default=True)
|
|
||||||
collection_name : Mapped[str] = mapped_column(String, nullable=False)
|
|
||||||
collection_description : Mapped[str] = mapped_column(String, default="No description")
|
|
||||||
|
|
||||||
# ilişkiler
|
|
||||||
users : Mapped[list['DBUser']] = relationship(
|
|
||||||
"DBUser",
|
|
||||||
secondary=user_collection,
|
|
||||||
back_populates="collections",
|
|
||||||
lazy='select'
|
|
||||||
) #back_populates karşı tarafın ismi
|
|
||||||
|
|
||||||
items : Mapped[list['Items']] = relationship(
|
|
||||||
"Items",
|
|
||||||
secondary=collection_item,
|
|
||||||
back_populates="collections" ,
|
|
||||||
lazy='select'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
#### collection bir item listesi birde kullanıcı listesi tutacak
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create_colletion(
|
|
||||||
collection: CollectionCreate | None = None,
|
|
||||||
user_id : int | None = None
|
|
||||||
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection oluşturma fonksiyonu
|
|
||||||
"""
|
|
||||||
if collection is None:
|
|
||||||
raise HTTPException(status_code=400, detail="Collection is None returned")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
user = session.query(DBUser).filter(DBUser.user_id == user_id).first()
|
|
||||||
if user is None:
|
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
|
||||||
new_collection = CollectionsDB(
|
|
||||||
collection_name=collection.collection_name,
|
|
||||||
collection_description=collection.collection_description,
|
|
||||||
visibility=collection.visibility
|
|
||||||
)
|
|
||||||
|
|
||||||
new_collection.users.append(user)
|
|
||||||
session.add(new_collection)
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error creating collection: {e}")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def get_collections(
|
|
||||||
user_id : int | None = None
|
|
||||||
) -> list[CollectionPublic] | None:
|
|
||||||
"""
|
|
||||||
Kullanıcının collectionlarını döndürür
|
|
||||||
"""
|
|
||||||
if user_id is None:
|
|
||||||
raise HTTPException(status_code=400, detail="User id is None")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
collections = session.query(CollectionsDB).filter(CollectionsDB.users.any(user_id=user_id)).all()
|
|
||||||
|
|
||||||
if collections is None:
|
|
||||||
raise HTTPException(status_code=404, detail="No collections found")
|
|
||||||
|
|
||||||
return collections
|
|
||||||
|
|
||||||
def update_collection(
|
|
||||||
collection: CollectionUpdate | None = None,
|
|
||||||
user_id : int | None = None,
|
|
||||||
collection_id : int | None = None
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection güncelleme fonksiyonu
|
|
||||||
"""
|
|
||||||
if collection is None:
|
|
||||||
raise HTTPException(status_code=400, detail="Collection is None returned")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
user = session.query(DBUser).filter(DBUser.user_id == user_id).first()
|
|
||||||
if user is None:
|
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
|
||||||
|
|
||||||
collection_to_update = session.query(CollectionsDB).filter(CollectionsDB.collection_id == collection_id).first()
|
|
||||||
if collection_to_update is None:
|
|
||||||
raise HTTPException(status_code=404, detail="Collection not found")
|
|
||||||
|
|
||||||
try:
|
|
||||||
collection_to_update.collection_name = collection.collection_name
|
|
||||||
collection_to_update.collection_description = collection.collection_description
|
|
||||||
collection_to_update.visibility = collection.visibility
|
|
||||||
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error updating collection: {e}")
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def delete_collection(
|
|
||||||
user_id : int | None = None,
|
|
||||||
collection_id : int | None = None
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection silme fonksiyonu
|
|
||||||
"""
|
|
||||||
if user_id is None or collection_id is None:
|
|
||||||
raise HTTPException(status_code=400, detail="User id or collection id is None")
|
|
||||||
|
|
||||||
session = next(get_session_db()) # -> get_session_db() fonksiyonu daima generator döndürür next ile çağırmalısın
|
|
||||||
user = session.query(DBUser).filter(DBUser.user_id == user_id).first()
|
|
||||||
if user is None:
|
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
|
||||||
|
|
||||||
collection_to_delete = session.query(CollectionsDB).filter(CollectionsDB.collection_id == collection_id).first()
|
|
||||||
if collection_to_delete is None:
|
|
||||||
raise HTTPException(status_code=404, detail="Collection not found")
|
|
||||||
|
|
||||||
try:
|
|
||||||
session.delete(collection_to_delete)
|
|
||||||
session.commit()
|
|
||||||
except Exception as e:
|
|
||||||
raise HTTPException(status_code=500, detail=f"Error deleting collection: {e}")
|
|
||||||
|
|
||||||
return True
|
|
||||||
@ -1,60 +0,0 @@
|
|||||||
from fastapi import FastAPI, APIRouter
|
|
||||||
from .models import CollectionPublic, CollectionCreate, CollectionUpdate
|
|
||||||
from .models import get_collections, create_colletion, update_collection, delete_collection
|
|
||||||
|
|
||||||
router = APIRouter(
|
|
||||||
prefix="/collections",
|
|
||||||
tags=["collections"],
|
|
||||||
responses={404: {"description": "Not found"}},
|
|
||||||
dependencies=[],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{user_id}")
|
|
||||||
async def get_collections_api(user_id: int) -> list[CollectionPublic]:
|
|
||||||
"""
|
|
||||||
Kullanıcının collectionlarını döndürür
|
|
||||||
"""
|
|
||||||
|
|
||||||
_collections : list[CollectionPublic] = get_collections(user_id=user_id)
|
|
||||||
|
|
||||||
return _collections
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{user_id}")
|
|
||||||
async def create_collection(
|
|
||||||
user_id: int,
|
|
||||||
collection: CollectionCreate
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection oluşturma fonksiyonu
|
|
||||||
"""
|
|
||||||
_result = create_colletion(user_id=user_id, collection=collection)
|
|
||||||
return _result
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{user_id}/{collection_id}")
|
|
||||||
async def update_collection_api(
|
|
||||||
user_id: int,
|
|
||||||
collection_id : int,
|
|
||||||
collection: CollectionUpdate
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection güncelleme fonksiyonu
|
|
||||||
"""
|
|
||||||
_result = update_collection(user_id=user_id, collection_id=collection_id, collection=collection)
|
|
||||||
return _result
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{user_id}/{collection_id}")
|
|
||||||
async def delete_collection_api(
|
|
||||||
user_id: int,
|
|
||||||
collection_id : int
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Collection silme fonksiyonu
|
|
||||||
"""
|
|
||||||
_result = delete_collection(user_id=user_id, collection_id=collection_id)
|
|
||||||
return _result
|
|
||||||
61
config.py
61
config.py
@ -1,61 +1,32 @@
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from sqlalchemy import Table, Column, Integer, String, Float, Boolean, ForeignKey
|
|
||||||
from passlib.context import CryptContext
|
from passlib.context import CryptContext
|
||||||
|
from sqlmodel import SQLModel, Field, Session
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import os
|
import os
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
|
||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
# Veritabanı URL'sini oluştur
|
||||||
|
DATABASE_URL = (
|
||||||
|
f"postgresql://{os.getenv('USERNAME_DB')}:"
|
||||||
|
f"{os.getenv('PASSWORD_DB')}@"
|
||||||
|
f"{os.getenv('HOST_DB')}:"
|
||||||
|
f"{os.getenv('PORT_DB')}/"
|
||||||
|
f"{os.getenv('NAME_DB')}"
|
||||||
|
)
|
||||||
|
|
||||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
|
||||||
ALGORITHM = os.getenv("ALGORITHM")
|
|
||||||
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 30))
|
|
||||||
|
|
||||||
DATABASE_URL = os.getenv("DATABASE_URL")
|
|
||||||
# Engine oluştur
|
|
||||||
engine = create_engine(DATABASE_URL, echo=False)
|
engine = create_engine(DATABASE_URL, echo=False)
|
||||||
# Session factory oluştur
|
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
||||||
#Base = declarative_base() #sqlalchemy için bu sınıfı kullanıyoruz 'class DBUser(Base)' şeklinde tanımlıyoruz
|
|
||||||
|
|
||||||
class Base(DeclarativeBase):
|
|
||||||
pass #yeni sqlalchemy sürümünde bu sınıfı kullanıyoruz
|
|
||||||
|
|
||||||
|
|
||||||
#models te içe aktarmayı unutma
|
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
#Base.metadata.drop_all(engine) # Veritabanını her başlangıcta siler burayada dikkat !!!!!!!!
|
SQLModel.metadata.create_all(engine)
|
||||||
Base.metadata.create_all(bind=engine) # Veritabanını oluşturur
|
|
||||||
|
|
||||||
# Session dependency (FastAPI için)
|
|
||||||
def get_session_db() -> 'Generator[Session, None]':
|
|
||||||
db = SessionLocal()
|
|
||||||
try:
|
|
||||||
yield db
|
|
||||||
finally:
|
|
||||||
db.close()
|
|
||||||
|
|
||||||
user_collection = Table( # user -> collection
|
|
||||||
"user_collection",
|
|
||||||
Base.metadata,
|
|
||||||
Column("user_id", Integer, ForeignKey("users_table.user_id"), primary_key=True),
|
|
||||||
Column("collection_id", Integer, ForeignKey("collections_table.collection_id"), primary_key=True),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
collection_item = Table( # collection -> item
|
|
||||||
"collection_item",
|
|
||||||
Base.metadata,
|
|
||||||
Column("collection_id", ForeignKey("collections_table.collection_id"), primary_key=True),
|
|
||||||
Column("item_id", ForeignKey("items_table.item_id"), primary_key=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
def get_session_db():
|
||||||
|
with Session(engine) as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
|
||||||
### SECRET KEY ###
|
### SECRET KEY ###
|
||||||
@ -68,7 +39,7 @@ origins = [
|
|||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
def startup_event():
|
def on_startup():
|
||||||
init_db()
|
init_db()
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
|
|||||||
@ -1,78 +1,12 @@
|
|||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Annotated
|
from ..auth.models import UserBase
|
||||||
from sqlalchemy import DateTime
|
|
||||||
from pydantic import BaseModel
|
|
||||||
from fastapi import Depends
|
|
||||||
from sqlalchemy.orm import Session, relationship, mapped_column, Mapped
|
|
||||||
from sqlalchemy import String, Float, Integer, ForeignKey
|
|
||||||
from sqlalchemy.dialects.postgresql import ARRAY
|
|
||||||
from ..config import Base, get_session_db, collection_item
|
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
from ..auth.models import Role, Status, UserBase
|
|
||||||
from ..collectionObj.models import CollectionsDB
|
|
||||||
|
|
||||||
|
class UserProfile(UserBase):
|
||||||
class UserProfileBase(UserBase):
|
|
||||||
bio : str | None = None
|
bio : str | None = None
|
||||||
created_date : datetime | None = None
|
created_date : datetime | None = None
|
||||||
# collection : list[str] | None = None
|
collections : list[str] | None = None
|
||||||
|
items :list[str] | None = None
|
||||||
class UserProfileID(UserProfileBase):
|
|
||||||
user_id : int | None = None
|
|
||||||
|
|
||||||
|
|
||||||
class UserProfilePublic(UserProfileBase):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class UserProfilePrivate(UserProfilePublic):
|
|
||||||
#collection : list[str] | None = None
|
|
||||||
role : Role | None = None
|
|
||||||
status : Status | None = None
|
|
||||||
follow_user : list[int] | None = None
|
|
||||||
items : list['Item'] | None = None
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
######## ITEMS ######
|
|
||||||
|
|
||||||
class BaseItem(BaseModel):
|
|
||||||
item_created_date : datetime | None = None
|
|
||||||
item_location : str | None = None
|
|
||||||
item_type : str | None = None
|
|
||||||
item_content : str | None = None
|
|
||||||
|
|
||||||
class ItemCreate(BaseItem): # item oluşturma için ekstra bir ihtiyaci olmaz
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Item(BaseItem):
|
|
||||||
item_id : int | None = None
|
|
||||||
user_id : int | None = None
|
|
||||||
item_score : float | None = None
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
from_attributes = True #sqlalchemy ile pydantic arasında geçiş yapabilmek için kullanılır
|
|
||||||
|
|
||||||
##### VERİTABANI MODELİ #####
|
|
||||||
# Tüm modeller AUTH'da veri tabanına işlendi yukardaki
|
|
||||||
#modeller veri tabanında mevcuttur. Değiştirmek için AUTH'daki
|
|
||||||
# DBUser modelini değiştirip tekrar veri tabanına işleyebilirsin
|
|
||||||
|
|
||||||
class Items(Base):
|
|
||||||
__tablename__ = "items_table"
|
|
||||||
|
|
||||||
item_id : Mapped[int] = mapped_column(primary_key=True, index=True, autoincrement=True)
|
|
||||||
#collection_id : Mapped[list[int]] = mapped_column(Integer, ForeignKey("collections_table.collection_id"), nullable=True) # collection_id ile ilişki
|
|
||||||
item_created_date : Mapped[datetime] = mapped_column(DateTime, default=datetime.now())
|
|
||||||
item_location: Mapped[str] = mapped_column(String, default="No location")
|
|
||||||
item_type: Mapped[str] = mapped_column(String, default="No type")
|
|
||||||
item_content: Mapped[str] = mapped_column(String, default="No content")
|
|
||||||
item_score: Mapped[float] = mapped_column(Float, default=0.0)
|
|
||||||
|
|
||||||
# ilişkiler
|
|
||||||
collections : Mapped[list['CollectionsDB']]= relationship(
|
|
||||||
"CollectionsDB",
|
|
||||||
secondary=collection_item,
|
|
||||||
back_populates="items",
|
|
||||||
lazy='select'
|
|
||||||
) #back_populates karşı tarafın ismi
|
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
from .models import ItemCreate, UserProfileBase, UserProfileID, UserProfilePrivate, UserProfilePublic
|
from .models import UserProfile
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
from ..config import get_session_db
|
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
from ..auth.models import get_current_active_user
|
from ..auth.models import get_current_active_user
|
||||||
|
|
||||||
@ -12,8 +10,9 @@ router = APIRouter(
|
|||||||
dependencies=[],
|
dependencies=[],
|
||||||
)
|
)
|
||||||
|
|
||||||
#tüm crud işlemleri yeni veri tabanı modeli ile yapılacak
|
@router.get('/profile', response_model=UserProfile)
|
||||||
|
async def get_user_profile(
|
||||||
|
current_user: Annotated[UserProfile, Depends(get_current_active_user)]
|
||||||
|
) -> UserProfile:
|
||||||
|
|
||||||
|
return current_user
|
||||||
2
main.py
2
main.py
@ -1,8 +1,6 @@
|
|||||||
from .config import app
|
from .config import app
|
||||||
from .auth.router import router as auth_router
|
from .auth.router import router as auth_router
|
||||||
from .items.router import router as items_router
|
from .items.router import router as items_router
|
||||||
from .collectionObj.router import router as collections_router
|
|
||||||
|
|
||||||
app.include_router(auth_router)
|
app.include_router(auth_router)
|
||||||
app.include_router(collections_router)
|
|
||||||
app.include_router(items_router)
|
app.include_router(items_router)
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
annotated-types==0.7.0
|
annotated-types==0.7.0
|
||||||
anyio==4.9.0
|
anyio==4.9.0
|
||||||
bcrypt==4.3.0
|
|
||||||
certifi==2025.4.26
|
certifi==2025.4.26
|
||||||
click==8.1.8
|
click==8.1.8
|
||||||
dnspython==2.7.0
|
dnspython==2.7.0
|
||||||
@ -40,5 +39,3 @@ uvicorn==0.34.2
|
|||||||
uvloop==0.21.0
|
uvloop==0.21.0
|
||||||
watchfiles==1.0.5
|
watchfiles==1.0.5
|
||||||
websockets==15.0.1
|
websockets==15.0.1
|
||||||
passlib[bcrypt]==1.7.4
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user