Compare commits
10 Commits
d5588dd055
...
250d3f1f15
| Author | SHA1 | Date | |
|---|---|---|---|
| 250d3f1f15 | |||
| bf71979982 | |||
| 039b877241 | |||
| 7c35097c88 | |||
| 938f950646 | |||
| 842c127817 | |||
| 36da53a562 | |||
|
|
83389e0c10 | ||
|
|
99f611b3d0 | ||
|
|
1d3d74d9d6 |
196
auth/models.py
196
auth/models.py
@ -1,15 +1,25 @@
|
||||
from enum import Enum
|
||||
from backend.config import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES
|
||||
from backend.config import pwd_context, get_session_db
|
||||
import random
|
||||
import smtplib
|
||||
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 pydantic import BaseModel
|
||||
from fastapi import Depends, HTTPException
|
||||
from typing import Annotated, Optional
|
||||
from typing import Annotated
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from passlib.context import CryptContext
|
||||
import jwt
|
||||
from sqlmodel import SQLModel, Field, Session, select
|
||||
from pydantic.networks import EmailStr
|
||||
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
|
||||
|
||||
class Token(BaseModel):
|
||||
access_token: str
|
||||
@ -28,40 +38,62 @@ class Status(str, Enum):
|
||||
banned = "banned"
|
||||
suspended = "suspended"
|
||||
|
||||
### KULLANICI MODELLERİ ###
|
||||
class UserBase(SQLModel):
|
||||
username: Optional[str] = None
|
||||
user_id: Optional[int] = None
|
||||
role: Optional[Role] = None
|
||||
status: Optional[Status] = None
|
||||
### KULLANICI MODELLERİ ### sqlalchemy ve pydantic modelleri farklıdır
|
||||
class UserBase(BaseModel): #bu bir veri tabanı modeli değil !!!! lütfen dikkat et
|
||||
username: str | None = None #Option yerine Union kullanabilirsin
|
||||
role: Role | None = None
|
||||
status: Status | None = None
|
||||
|
||||
class UserInDb(UserBase):
|
||||
user_id: int | None = None
|
||||
email: EmailStr | None = None
|
||||
hashed_password: str | None = None
|
||||
|
||||
class UserPublic(UserBase):
|
||||
pass
|
||||
class UserPublic(BaseModel):
|
||||
username : str | None = None
|
||||
role : Role | None = None
|
||||
status : Status | None = None
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
username: Optional[str] = None
|
||||
role: Optional[Role] = None
|
||||
username: str | None = None
|
||||
role: Role | None = None
|
||||
email : EmailStr | None = None
|
||||
status: Optional[Status] = None
|
||||
status: Status | None = None
|
||||
password : str | None = None
|
||||
|
||||
|
||||
### VERİTABANI MODELİ ###
|
||||
class DBUser(SQLModel, table=True):
|
||||
__tablename__ = "users" # opsiyonel, sqlmodel bunu otomatik de atar
|
||||
user_id: Optional[int] = Field(default=None, primary_key=True)
|
||||
username: str = Field(index=True, nullable=False)
|
||||
hashed_password: str = Field(nullable=False)
|
||||
role: Role = Field(default=Role.user)
|
||||
status: Status = Field(default=Status.active)
|
||||
class DBUser(Base):
|
||||
__tablename__ = "users_table"
|
||||
|
||||
user_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
|
||||
username : Mapped[str] = mapped_column(unique=True, index=True, nullable=False)
|
||||
email : Mapped[str] = mapped_column(unique=True, index=True, nullable=False)
|
||||
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 ###
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
||||
|
||||
|
||||
|
||||
### SERVİSLER ###
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
@ -74,21 +106,20 @@ def authenticate_user(
|
||||
password: str
|
||||
) -> UserInDb | None:
|
||||
|
||||
statement = select(DBUser).where(DBUser.username == username)
|
||||
result = session.exec(statement).first()
|
||||
if not result or not verify_password(password, result.hashed_password):
|
||||
user = session.query(DBUser).filter(DBUser.username == username).first()
|
||||
if user is None or not verify_password(password, user.hashed_password): #sqlalchemy'de bu şekilde kontrol ediliyor None ile
|
||||
return None
|
||||
return result
|
||||
return user
|
||||
|
||||
|
||||
def create_access_token(
|
||||
data: dict,
|
||||
expires_delta: Optional[timedelta] = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
|
||||
expires_delta: Annotated[timedelta, None] = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
|
||||
) -> str:
|
||||
to_encode = data.copy()
|
||||
expire = datetime.now(timezone.utc) + expires_delta
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm="HS256")
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
@ -97,9 +128,8 @@ async def get_user(
|
||||
username: str
|
||||
) -> UserInDb | None:
|
||||
|
||||
statement = select(DBUser).where(DBUser.username == username)
|
||||
result = session.exec(statement).first()
|
||||
return result
|
||||
user = session.query(DBUser).filter(DBUser.username == username).first()
|
||||
return user
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
@ -109,14 +139,17 @@ async def get_current_user(
|
||||
|
||||
credentials_exception = HTTPException(
|
||||
status_code=401,
|
||||
detail="Invalid credentials",
|
||||
detail="Invalid credentials currently",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
|
||||
username: Optional[str] = payload.get("sub")
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
username : str | None = payload.get("sub")
|
||||
user = UserInDb.model_validate(payload)
|
||||
|
||||
if username is None:
|
||||
raise credentials_exception
|
||||
|
||||
except jwt.PyJWTError:
|
||||
raise credentials_exception
|
||||
|
||||
@ -133,3 +166,94 @@ async def get_current_active_user(
|
||||
if current_user.status == Status.banned:
|
||||
raise HTTPException(status_code=400, detail="Inactive 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,14 +1,11 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from .models import Token, UserPublic
|
||||
from .models import authenticate_user, create_access_token
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
|
||||
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 datetime import timedelta
|
||||
from ..auth.models import get_password_hash, verify_password
|
||||
from typing import Annotated
|
||||
from sqlmodel import Session
|
||||
from ..config import get_session_db
|
||||
from fastapi import Depends
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from .models import UserCreate, DBUser
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic.networks import EmailStr
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
@ -38,21 +35,41 @@ async def login_for_access_token(
|
||||
return Token(access_token=access_token, token_type="bearer")
|
||||
|
||||
|
||||
@router.post('/register', response_model=UserPublic)
|
||||
@router.post('/register', response_model=UserPublic) #userPublic güvenli bir model
|
||||
async def create_user(
|
||||
session : Annotated[Session, Depends(get_session_db)],
|
||||
user : Annotated[UserCreate, Depends()]
|
||||
):
|
||||
user_dict = user.dict()
|
||||
print(user.password)
|
||||
user_dict['hashed_password'] = get_password_hash(user.password)
|
||||
print (user_dict['hashed_password'])
|
||||
|
||||
return register_user(session, user)
|
||||
|
||||
if not verify_password(user.password, user_dict['hashed_password']):
|
||||
raise HTTPException(status_code=400, detail="Password hashing failed")
|
||||
@router.post('/password_reset')
|
||||
async def password_reset(
|
||||
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)
|
||||
|
||||
db_user = DBUser.model_validate(user_dict)
|
||||
session.add(db_user)
|
||||
session.commit()
|
||||
session.refresh(db_user)
|
||||
return db_user
|
||||
return {"message": "New password has been sent to your email."}
|
||||
|
||||
@router.post('/update_password')
|
||||
async def update_password(
|
||||
user: Annotated[str, Depends(get_current_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."}
|
||||
|
||||
|
||||
|
||||
173
collectionObj/models.py
Normal file
173
collectionObj/models.py
Normal file
@ -0,0 +1,173 @@
|
||||
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
|
||||
60
collectionObj/router.py
Normal file
60
collectionObj/router.py
Normal file
@ -0,0 +1,60 @@
|
||||
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
|
||||
63
config.py
63
config.py
@ -1,32 +1,61 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy import Table, Column, Integer, String, Float, Boolean, ForeignKey
|
||||
from passlib.context import CryptContext
|
||||
from sqlmodel import SQLModel, Field, Session
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# 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')}"
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
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)
|
||||
# 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():
|
||||
#Base.metadata.drop_all(engine) # Veritabanını her başlangıcta siler burayada dikkat !!!!!!!!
|
||||
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),
|
||||
)
|
||||
|
||||
engine = create_engine(DATABASE_URL, echo=False)
|
||||
def init_db():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
def get_session_db():
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
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)
|
||||
)
|
||||
|
||||
|
||||
|
||||
### SECRET KEY ###
|
||||
@ -39,7 +68,7 @@ origins = [
|
||||
|
||||
app = FastAPI()
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
def startup_event():
|
||||
init_db()
|
||||
|
||||
app.add_middleware(
|
||||
|
||||
@ -1,12 +1,78 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from ..auth.models import UserBase
|
||||
from typing import Annotated
|
||||
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
|
||||
created_date : datetime | None = None
|
||||
collections : list[str] | None = None
|
||||
items :list[str] | None = None
|
||||
# collection : 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,5 +1,7 @@
|
||||
from .models import UserProfile
|
||||
from .models import ItemCreate, UserProfileBase, UserProfileID, UserProfilePrivate, UserProfilePublic
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from ..config import get_session_db
|
||||
from typing import Annotated
|
||||
from ..auth.models import get_current_active_user
|
||||
|
||||
@ -10,9 +12,8 @@ router = APIRouter(
|
||||
dependencies=[],
|
||||
)
|
||||
|
||||
@router.get('/profile', response_model=UserProfile)
|
||||
async def get_user_profile(
|
||||
current_user: Annotated[UserProfile, Depends(get_current_active_user)]
|
||||
) -> UserProfile:
|
||||
|
||||
return current_user
|
||||
#tüm crud işlemleri yeni veri tabanı modeli ile yapılacak
|
||||
|
||||
|
||||
|
||||
|
||||
2
main.py
2
main.py
@ -1,6 +1,8 @@
|
||||
from .config import app
|
||||
from .auth.router import router as auth_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(collections_router)
|
||||
app.include_router(items_router)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
annotated-types==0.7.0
|
||||
anyio==4.9.0
|
||||
bcrypt==4.3.0
|
||||
certifi==2025.4.26
|
||||
click==8.1.8
|
||||
dnspython==2.7.0
|
||||
@ -39,3 +40,5 @@ uvicorn==0.34.2
|
||||
uvloop==0.21.0
|
||||
watchfiles==1.0.5
|
||||
websockets==15.0.1
|
||||
passlib[bcrypt]==1.7.4
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user