Przykład
This commit is contained in:
BIN
backend/routers/__pycache__/auth.cpython-313.pyc
Normal file
BIN
backend/routers/__pycache__/auth.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/routers/__pycache__/home.cpython-313.pyc
Normal file
BIN
backend/routers/__pycache__/home.cpython-313.pyc
Normal file
Binary file not shown.
74
backend/routers/auth.py
Normal file
74
backend/routers/auth.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from passlib.hash import argon2
|
||||
import secrets
|
||||
|
||||
from database import get_db
|
||||
import models, validation
|
||||
|
||||
router = APIRouter(prefix="/api/auth", tags=["Authentication"])
|
||||
|
||||
|
||||
@router.post("/init")
|
||||
def initialize_system(request: validation.InitRequest, db: Session = Depends(get_db)):
|
||||
if db.query(models.SecurityConfig).first():
|
||||
raise HTTPException(status_code=400, detail="System został już zainicjowany.")
|
||||
|
||||
recovery_key = secrets.token_hex(16)
|
||||
|
||||
new_config = models.SecurityConfig(
|
||||
password_hash=argon2.using(type="ID").hash(request.password),
|
||||
recovery_key_hash=argon2.using(type="ID").hash(recovery_key),
|
||||
)
|
||||
db.add(new_config)
|
||||
db.commit()
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "System zainicjowany pomyślnie.",
|
||||
"recovery_key": recovery_key,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/reset-password")
|
||||
def reset_password(request: validation.InitRequest, db: Session = Depends(get_db)):
|
||||
config = db.query(models.SecurityConfig).first()
|
||||
if not config:
|
||||
raise HTTPException(status_code=404, detail="System nie zainicjowany.")
|
||||
|
||||
config.password_hash = argon2.using(type="ID").hash(request.password)
|
||||
db.commit()
|
||||
return {"status": "success", "message": "Hasło zostało zmienione."}
|
||||
|
||||
|
||||
@router.delete("/account")
|
||||
def delete_account(db: Session = Depends(get_db)):
|
||||
db.query(models.SecurityConfig).delete()
|
||||
db.commit()
|
||||
return {"status": "success", "message": "Konto zostało usunięte."}
|
||||
|
||||
|
||||
@router.post("/login")
|
||||
def login(request: validation.LoginRequest, db: Session = Depends(get_db)):
|
||||
config = db.query(models.SecurityConfig).first()
|
||||
if not config:
|
||||
raise HTTPException(status_code=500, detail="Brak konfiguracji.")
|
||||
|
||||
if request.is_recovery:
|
||||
if not argon2.using(type="ID").verify(
|
||||
request.password, config.recovery_key_hash
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=401, detail="Nieprawidłowy klucz przywracania."
|
||||
)
|
||||
return {"status": "success", "message": "Zalogowano awaryjnie."}
|
||||
else:
|
||||
if not argon2.using(type="ID").verify(request.password, config.password_hash):
|
||||
raise HTTPException(status_code=401, detail="Nieprawidłowe hasło główne.")
|
||||
return {"status": "success", "message": "Autoryzacja pomyślna."}
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
def check_status(db: Session = Depends(get_db)):
|
||||
is_initialized = db.query(models.SecurityConfig).first() is not None
|
||||
return {"is_initialized": is_initialized}
|
||||
78
backend/routers/home.py
Normal file
78
backend/routers/home.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from database import get_db
|
||||
import models, validation
|
||||
|
||||
router = APIRouter(prefix="/api/home", tags=["Home Page"])
|
||||
|
||||
|
||||
@router.get("/dashboard", response_model=validation.DashboardResponse)
|
||||
def get_dashboard_data(db: Session = Depends(get_db)):
|
||||
|
||||
root_folders = db.query(models.Folder).filter(models.Folder.parent_id == None).all()
|
||||
|
||||
recent_docs = (
|
||||
db.query(models.Document)
|
||||
.order_by(models.Document.updated_at.desc())
|
||||
.limit(5)
|
||||
.all()
|
||||
)
|
||||
|
||||
return {"folders": root_folders, "recent_documents": recent_docs}
|
||||
|
||||
|
||||
@router.post("/folders", response_model=validation.FolderResponse)
|
||||
def create_folder(folder: validation.FolderCreate, db: Session = Depends(get_db)):
|
||||
new_folder = models.Folder(name=folder.name, parent_id=folder.parent_id)
|
||||
db.add(new_folder)
|
||||
db.commit()
|
||||
db.refresh(new_folder)
|
||||
return new_folder
|
||||
|
||||
|
||||
@router.get("/search", response_model=List[validation.DocumentResponse])
|
||||
def search_documents(
|
||||
q: str = Query(..., min_length=1, description="Fraza wyszukiwania"),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
|
||||
# Wyszukiwanie po tytule pliku
|
||||
by_title = (
|
||||
db.query(models.Document).filter(models.Document.title.ilike(f"%{q}%")).all()
|
||||
)
|
||||
|
||||
# Wyszukiwanie po tagach
|
||||
by_tag = (
|
||||
db.query(models.Document)
|
||||
.join(models.Document.tags)
|
||||
.filter(models.Tag.name.ilike(f"%{q}%"))
|
||||
.all()
|
||||
)
|
||||
|
||||
# Połączenie wyników i usunięcie duplikatów
|
||||
results = list({doc.id: doc for doc in (by_title + by_tag)}.values())
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@router.get("/folders/{folder_id}", response_model=validation.FolderContentResponse)
|
||||
def get_folder_content(folder_id: int, db: Session = Depends(get_db)):
|
||||
|
||||
folder = db.query(models.Folder).filter(models.Folder.id == folder_id).first()
|
||||
if not folder:
|
||||
raise HTTPException(status_code=404, detail="Folder nie istnieje")
|
||||
|
||||
subfolders = (
|
||||
db.query(models.Folder).filter(models.Folder.parent_id == folder_id).all()
|
||||
)
|
||||
documents = (
|
||||
db.query(models.Document).filter(models.Document.folder_id == folder_id).all()
|
||||
)
|
||||
|
||||
return {
|
||||
"id": folder.id,
|
||||
"name": folder.name,
|
||||
"subfolders": subfolders,
|
||||
"documents": documents,
|
||||
}
|
||||
Reference in New Issue
Block a user