Przykład
This commit is contained in:
BIN
backend/__pycache__/database.cpython-313.pyc
Normal file
BIN
backend/__pycache__/database.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/main.cpython-313.pyc
Normal file
BIN
backend/__pycache__/main.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/models.cpython-313.pyc
Normal file
BIN
backend/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/validation.cpython-313.pyc
Normal file
BIN
backend/__pycache__/validation.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/archivium.db
Normal file
BIN
backend/archivium.db
Normal file
Binary file not shown.
20
backend/database.py
Normal file
20
backend/database.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./archivium.db"
|
||||
|
||||
engine = create_engine(
|
||||
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
||||
)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
# Dependency do wstrzykiwania sesji w endpointach
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
19
backend/main.py
Normal file
19
backend/main.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from database import engine, Base
|
||||
from routers import auth, home
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI(title="Archivium API")
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(auth.router)
|
||||
app.include_router(home.router)
|
||||
50
backend/models.py
Normal file
50
backend/models.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Table
|
||||
from sqlalchemy.orm import relationship
|
||||
from database import Base
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class SecurityConfig(Base):
|
||||
__tablename__ = "security_config"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
password_hash = Column(String, nullable=False)
|
||||
recovery_key_hash = Column(String, nullable=False)
|
||||
|
||||
|
||||
document_tags = Table(
|
||||
"document_tags",
|
||||
Base.metadata,
|
||||
Column("document_id", Integer, ForeignKey("documents.id"), primary_key=True),
|
||||
Column("tag_id", Integer, ForeignKey("tags.id"), primary_key=True),
|
||||
)
|
||||
|
||||
# --- Modele pod Stronę Główną ---
|
||||
|
||||
|
||||
class Tag(Base):
|
||||
__tablename__ = "tags"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, unique=True, nullable=False)
|
||||
|
||||
|
||||
class Folder(Base):
|
||||
__tablename__ = "folders"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False)
|
||||
parent_id = Column(Integer, ForeignKey("folders.id"), nullable=True)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
documents = relationship("Document", back_populates="folder")
|
||||
|
||||
|
||||
class Document(Base):
|
||||
__tablename__ = "documents"
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
title = Column(String, nullable=False)
|
||||
file_path = Column(String, nullable=False)
|
||||
folder_id = Column(Integer, ForeignKey("folders.id"), nullable=True)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
folder = relationship("Folder", back_populates="documents")
|
||||
tags = relationship("Tag", secondary=document_tags, backref="documents")
|
||||
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,
|
||||
}
|
||||
68
backend/validation.py
Normal file
68
backend/validation.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class TagResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class DocumentResponse(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
updated_at: datetime
|
||||
tags: List[TagResponse] = []
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
# Istniejące modele Authorization
|
||||
class InitRequest(BaseModel):
|
||||
password: str
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
password: str
|
||||
is_recovery: bool = False
|
||||
|
||||
|
||||
# Modele Strony Głównej
|
||||
|
||||
|
||||
class DocumentResponse(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class FolderResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class FolderCreate(BaseModel):
|
||||
name: str
|
||||
parent_id: Optional[int] = None
|
||||
|
||||
|
||||
class DashboardResponse(BaseModel):
|
||||
folders: List[FolderResponse]
|
||||
recent_documents: List[DocumentResponse]
|
||||
|
||||
|
||||
class FolderContentResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
subfolders: List[FolderResponse]
|
||||
documents: List[DocumentResponse]
|
||||
Reference in New Issue
Block a user