Refactorizacion de subir logo4.5
This commit is contained in:
@@ -1,47 +1,7 @@
|
|||||||
|
|
||||||
# ============= LOGO CONFIGURABLE =============
|
# ============= LOGO CONFIGURABLE =============
|
||||||
from fastapi import FastAPI, File, UploadFile, Form
|
|
||||||
app = FastAPI()
|
|
||||||
|
|
||||||
@app.post("/api/config/logo", response_model=dict)
|
from fastapi import FastAPI, File, UploadFile, Form, Depends, HTTPException, status
|
||||||
async def upload_logo(
|
|
||||||
file: UploadFile = File(...),
|
|
||||||
db: Session = Depends(get_db),
|
|
||||||
current_user: models.User = Depends(get_current_user)
|
|
||||||
):
|
|
||||||
"""Sube un logo y lo guarda en MinIO, actualiza la configuración."""
|
|
||||||
if current_user.role != "admin":
|
|
||||||
raise HTTPException(status_code=403, detail="Solo administradores pueden cambiar el logo")
|
|
||||||
|
|
||||||
# Subir imagen a MinIO
|
|
||||||
file_extension = file.filename.split(".")[-1]
|
|
||||||
now = datetime.now()
|
|
||||||
folder = f"logo"
|
|
||||||
file_name = f"logo_{now.strftime('%Y%m%d_%H%M%S')}.{file_extension}"
|
|
||||||
s3_key = f"{folder}/{file_name}"
|
|
||||||
s3_client.upload_fileobj(file.file, S3_IMAGE_BUCKET, s3_key, ExtraArgs={"ContentType": file.content_type})
|
|
||||||
logo_url = f"{S3_ENDPOINT}/{S3_IMAGE_BUCKET}/{s3_key}"
|
|
||||||
|
|
||||||
# Guardar en configuración (puedes tener una tabla Config o usar AIConfiguration)
|
|
||||||
config = db.query(models.AIConfiguration).filter(models.AIConfiguration.is_active == True).first()
|
|
||||||
if config:
|
|
||||||
config.logo_url = logo_url
|
|
||||||
db.commit()
|
|
||||||
db.refresh(config)
|
|
||||||
# Si no hay config, solo retorna la url
|
|
||||||
return {"logo_url": logo_url}
|
|
||||||
|
|
||||||
# Endpoint para obtener el logo
|
|
||||||
@app.get("/api/config/logo", response_model=dict)
|
|
||||||
def get_logo_url(
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
|
||||||
config = db.query(models.AIConfiguration).filter(models.AIConfiguration.is_active == True).first()
|
|
||||||
if config and getattr(config, "logo_url", None):
|
|
||||||
return {"logo_url": config.logo_url}
|
|
||||||
# Default logo (puedes poner una url por defecto)
|
|
||||||
return {"logo_url": f"{S3_ENDPOINT}/{S3_IMAGE_BUCKET}/logo/default_logo.png"}
|
|
||||||
from fastapi import FastAPI, Depends, HTTPException, status, UploadFile, File
|
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||||
from sqlalchemy.orm import Session, joinedload
|
from sqlalchemy.orm import Session, joinedload
|
||||||
@@ -52,6 +12,16 @@ import boto3
|
|||||||
from botocore.client import Config
|
from botocore.client import Config
|
||||||
import uuid
|
import uuid
|
||||||
from app.core import config as app_config
|
from app.core import config as app_config
|
||||||
|
from app.core.database import engine, get_db, Base
|
||||||
|
from app.core.security import verify_password, get_password_hash, create_access_token, decode_access_token
|
||||||
|
from app import models, schemas
|
||||||
|
import shutil
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
import sys
|
||||||
|
|
||||||
|
BACKEND_VERSION = "1.0.25"
|
||||||
|
app = FastAPI(title="Checklist Inteligente API", version=BACKEND_VERSION)
|
||||||
|
|
||||||
# S3/MinIO configuration
|
# S3/MinIO configuration
|
||||||
S3_ENDPOINT = app_config.MINIO_ENDPOINT
|
S3_ENDPOINT = app_config.MINIO_ENDPOINT
|
||||||
S3_ACCESS_KEY = app_config.MINIO_ACCESS_KEY
|
S3_ACCESS_KEY = app_config.MINIO_ACCESS_KEY
|
||||||
@@ -67,22 +37,11 @@ s3_client = boto3.client(
|
|||||||
config=Config(signature_version='s3v4'),
|
config=Config(signature_version='s3v4'),
|
||||||
region_name='us-east-1'
|
region_name='us-east-1'
|
||||||
)
|
)
|
||||||
import shutil
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
|
|
||||||
from app.core.database import engine, get_db, Base
|
|
||||||
from app.core.security import verify_password, get_password_hash, create_access_token, decode_access_token
|
|
||||||
from app import models, schemas
|
|
||||||
|
|
||||||
# Crear tablas
|
# Crear tablas
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
BACKEND_VERSION = "1.0.25"
|
|
||||||
app = FastAPI(title="Checklist Inteligente API", version=BACKEND_VERSION)
|
|
||||||
|
|
||||||
# Información visual al iniciar el backend
|
# Información visual al iniciar el backend
|
||||||
import sys
|
|
||||||
from app.core import config as app_config
|
|
||||||
print("\n================ BACKEND STARTUP INFO ================")
|
print("\n================ BACKEND STARTUP INFO ================")
|
||||||
print(f"Backend version: {BACKEND_VERSION}")
|
print(f"Backend version: {BACKEND_VERSION}")
|
||||||
print(f"Database URL: {app_config.settings.DATABASE_URL}")
|
print(f"Database URL: {app_config.settings.DATABASE_URL}")
|
||||||
@@ -154,6 +113,45 @@ def get_current_user(
|
|||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/api/config/logo", response_model=dict)
|
||||||
|
async def upload_logo(
|
||||||
|
file: UploadFile = File(...),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: models.User = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""Sube un logo y lo guarda en MinIO, actualiza la configuración."""
|
||||||
|
if current_user.role != "admin":
|
||||||
|
raise HTTPException(status_code=403, detail="Solo administradores pueden cambiar el logo")
|
||||||
|
|
||||||
|
# Subir imagen a MinIO
|
||||||
|
file_extension = file.filename.split(".")[-1]
|
||||||
|
now = datetime.now()
|
||||||
|
folder = f"logo"
|
||||||
|
file_name = f"logo_{now.strftime('%Y%m%d_%H%M%S')}.{file_extension}"
|
||||||
|
s3_key = f"{folder}/{file_name}"
|
||||||
|
s3_client.upload_fileobj(file.file, S3_IMAGE_BUCKET, s3_key, ExtraArgs={"ContentType": file.content_type})
|
||||||
|
logo_url = f"{S3_ENDPOINT}/{S3_IMAGE_BUCKET}/{s3_key}"
|
||||||
|
|
||||||
|
# Guardar en configuración (puedes tener una tabla Config o usar AIConfiguration)
|
||||||
|
config = db.query(models.AIConfiguration).filter(models.AIConfiguration.is_active == True).first()
|
||||||
|
if config:
|
||||||
|
config.logo_url = logo_url
|
||||||
|
db.commit()
|
||||||
|
db.refresh(config)
|
||||||
|
# Si no hay config, solo retorna la url
|
||||||
|
return {"logo_url": logo_url}
|
||||||
|
|
||||||
|
@app.get("/api/config/logo", response_model=dict)
|
||||||
|
def get_logo_url(
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
config = db.query(models.AIConfiguration).filter(models.AIConfiguration.is_active == True).first()
|
||||||
|
if config and getattr(config, "logo_url", None):
|
||||||
|
return {"logo_url": config.logo_url}
|
||||||
|
# Default logo (puedes poner una url por defecto)
|
||||||
|
return {"logo_url": f"{S3_ENDPOINT}/{S3_IMAGE_BUCKET}/logo/default_logo.png"}
|
||||||
|
|
||||||
|
|
||||||
# ============= AUTH ENDPOINTS =============
|
# ============= AUTH ENDPOINTS =============
|
||||||
@app.post("/api/auth/register", response_model=schemas.User)
|
@app.post("/api/auth/register", response_model=schemas.User)
|
||||||
def register(user: schemas.UserCreate, db: Session = Depends(get_db)):
|
def register(user: schemas.UserCreate, db: Session = Depends(get_db)):
|
||||||
|
|||||||
122
backend/main2.py
Normal file
122
backend/main2.py
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
from fastapi import FastAPI, File, UploadFile, Form, Depends, HTTPException, status
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||||
|
from sqlalchemy.orm import Session, joinedload
|
||||||
|
from sqlalchemy import func, case
|
||||||
|
from typing import List, Optional
|
||||||
|
import os
|
||||||
|
import boto3
|
||||||
|
from botocore.client import Config
|
||||||
|
import uuid
|
||||||
|
from app.core import config as app_config
|
||||||
|
from app.core.database import engine, get_db, Base
|
||||||
|
from app.core.security import verify_password, get_password_hash, create_access_token, decode_access_token
|
||||||
|
from app import models, schemas
|
||||||
|
import shutil
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
BACKEND_VERSION = "1.0.25"
|
||||||
|
app = FastAPI(title="Checklist Inteligente API", version=BACKEND_VERSION)
|
||||||
|
|
||||||
|
# S3/MinIO configuration
|
||||||
|
S3_ENDPOINT = app_config.MINIO_ENDPOINT
|
||||||
|
S3_ACCESS_KEY = app_config.MINIO_ACCESS_KEY
|
||||||
|
S3_SECRET_KEY = app_config.MINIO_SECRET_KEY
|
||||||
|
S3_IMAGE_BUCKET = app_config.MINIO_IMAGE_BUCKET
|
||||||
|
S3_PDF_BUCKET = app_config.MINIO_PDF_BUCKET
|
||||||
|
|
||||||
|
s3_client = boto3.client(
|
||||||
|
's3',
|
||||||
|
endpoint_url=S3_ENDPOINT,
|
||||||
|
aws_access_key_id=S3_ACCESS_KEY,
|
||||||
|
aws_secret_access_key=S3_SECRET_KEY,
|
||||||
|
config=Config(signature_version='s3v4'),
|
||||||
|
region_name='us-east-1'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Crear tablas
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
# Información visual al iniciar el backend
|
||||||
|
print("\n================ BACKEND STARTUP INFO ================")
|
||||||
|
print(f"Backend version: {BACKEND_VERSION}")
|
||||||
|
print(f"Database URL: {app_config.settings.DATABASE_URL}")
|
||||||
|
print(f"Environment: {app_config.settings.ENVIRONMENT}")
|
||||||
|
print(f"MinIO endpoint: {app_config.MINIO_ENDPOINT}")
|
||||||
|
print("====================================================\n", flush=True)
|
||||||
|
|
||||||
|
# CORS
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["http://localhost:5173", "http://localhost:3000"],
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Simulación de modelos y autenticación para ejemplo
|
||||||
|
class User:
|
||||||
|
def __init__(self, role):
|
||||||
|
self.role = role
|
||||||
|
|
||||||
|
class AIConfiguration:
|
||||||
|
is_active = True
|
||||||
|
logo_url = ""
|
||||||
|
|
||||||
|
class models:
|
||||||
|
User = User
|
||||||
|
AIConfiguration = AIConfiguration
|
||||||
|
|
||||||
|
# Simulación de get_db y get_current_user
|
||||||
|
|
||||||
|
def get_db():
|
||||||
|
# Aquí iría la lógica real de SQLAlchemy
|
||||||
|
class DummyDB:
|
||||||
|
def query(self, model):
|
||||||
|
return self
|
||||||
|
def filter(self, *args, **kwargs):
|
||||||
|
return self
|
||||||
|
def first(self):
|
||||||
|
return models.AIConfiguration()
|
||||||
|
def commit(self):
|
||||||
|
pass
|
||||||
|
def refresh(self, obj):
|
||||||
|
pass
|
||||||
|
return DummyDB()
|
||||||
|
|
||||||
|
def get_current_user():
|
||||||
|
# Aquí iría la lógica real de autenticación
|
||||||
|
return models.User(role="admin")
|
||||||
|
|
||||||
|
# Endpoint para subir el logo
|
||||||
|
@app.post("/api/config/logo", response_model=dict)
|
||||||
|
async def upload_logo(
|
||||||
|
file: UploadFile = File(...),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: models.User = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
if current_user.role != "admin":
|
||||||
|
raise HTTPException(status_code=403, detail="Solo administradores pueden cambiar el logo")
|
||||||
|
# Subir imagen a MinIO/S3
|
||||||
|
file_extension = file.filename.split(".")[-1]
|
||||||
|
now = datetime.now()
|
||||||
|
folder = "logo"
|
||||||
|
file_name = f"logo_{now.strftime('%Y%m%d_%H%M%S')}.{file_extension}"
|
||||||
|
s3_key = f"{folder}/{file_name}"
|
||||||
|
# s3_client.upload_fileobj(file.file, S3_IMAGE_BUCKET, s3_key, ExtraArgs={"ContentType": file.content_type})
|
||||||
|
logo_url = f"https://minio.example.com/bucket/{s3_key}" # Ajusta según tu config
|
||||||
|
# Actualiza la configuración en la base de datos
|
||||||
|
# config = db.query(models.AIConfiguration).filter(models.AIConfiguration.is_active == True).first()
|
||||||
|
# if config:
|
||||||
|
# config.logo_url = logo_url
|
||||||
|
# db.commit()
|
||||||
|
return {"logo_url": logo_url}
|
||||||
|
|
||||||
|
# Endpoint para obtener el logo
|
||||||
|
@app.get("/api/config/logo", response_model=dict)
|
||||||
|
def get_logo_url(db: Session = Depends(get_db)):
|
||||||
|
# config = db.query(models.AIConfiguration).filter(models.AIConfiguration.is_active == True).first()
|
||||||
|
# if config and getattr(config, "logo_url", None):
|
||||||
|
# return {"logo_url": config.logo_url}
|
||||||
|
# return {"logo_url": "https://minio.example.com/bucket/logo/default_logo.png"}
|
||||||
|
return {"logo_url": "https://minio.example.com/bucket/logo/default_logo.png"}
|
||||||
Reference in New Issue
Block a user