- inits centralizados, READMEs atualizados
- padronizando o nome de get_db_connection e session para get_db_session, para não confundir com session do Flask ou sessoes web
- corrigindo potenciais erros
-- has_permission nao consegue com lazy load carregar permission depois de load_user fechar a conexao, entao joinedLoad com Permission antes de fechar
-- db.rollback não existe caso db = get_db_session() apareça muito depois dentro do try, padronizando antes de try
--- comparar role por nivel (Role.SECRETARIO_GERAL) e nao por nome ("Secretario Geral")
- unificacao de get_otp_qr_code
- mudança de nowutc() para now(UTC) conforme novo padrão
This commit is contained in:
@@ -1,11 +1,8 @@
|
||||
from functions.database import get_db_connection, Usuario
|
||||
from functions.database import get_db_session, Usuario
|
||||
from flask_login import login_user, logout_user
|
||||
from datetime import datetime
|
||||
from typing import Dict, Optional
|
||||
import pyotp
|
||||
import qrcode
|
||||
import base64
|
||||
from io import BytesIO
|
||||
from services.otp_service import generate_qr_code_base64
|
||||
|
||||
class AuthService:
|
||||
"""Service para operações de autenticação"""
|
||||
@@ -13,7 +10,7 @@ class AuthService:
|
||||
@staticmethod
|
||||
def autenticar_usuario(email_or_username: str, password: str, otp: str = None) -> Dict:
|
||||
"""Autentica um usuário"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
# Tenta encontrar o usuário por email ou username
|
||||
user = db.query(Usuario).filter(
|
||||
@@ -64,7 +61,7 @@ class AuthService:
|
||||
@staticmethod
|
||||
def desautenticar_usuario(user) -> Dict:
|
||||
"""Desautentica um usuário"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
if user:
|
||||
user.logout()
|
||||
@@ -87,7 +84,7 @@ class AuthService:
|
||||
@staticmethod
|
||||
def alterar_senha(user_id: int, senha_atual: str, nova_senha: str) -> Dict:
|
||||
"""Altera a senha de um usuário"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
user = db.query(Usuario).get(user_id)
|
||||
if not user:
|
||||
@@ -122,20 +119,7 @@ class AuthService:
|
||||
@staticmethod
|
||||
def gerar_qr_code(user) -> str:
|
||||
"""Gera um QR code para o usuário"""
|
||||
if not user.otp_secret:
|
||||
user.otp_secret = pyotp.random_base32()
|
||||
|
||||
totp = pyotp.TOTP(user.otp_secret)
|
||||
qr = qrcode.QRCode(version=1, box_size=10, border=5)
|
||||
qr.add_data(totp.provisioning_uri(user.email, issuer_name="Sistema de Controles"))
|
||||
qr.make(fit=True)
|
||||
|
||||
img = qr.make_image(fill_color="black", back_color="white")
|
||||
buffer = BytesIO()
|
||||
img.save(buffer, format="PNG")
|
||||
qr_code = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
||||
|
||||
return qr_code
|
||||
return generate_qr_code_base64(user)
|
||||
|
||||
@staticmethod
|
||||
def verificar_sessao(user) -> Dict:
|
||||
@@ -154,4 +138,4 @@ class AuthService:
|
||||
|
||||
return {
|
||||
'valid': True
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from functions.database import get_db_connection, Militante, Pagamento, CotaMensal, MaterialVendido, AssinaturaAnual, TipoPagamento
|
||||
from functions.database import get_db_session, Militante, Pagamento, CotaMensal, MaterialVendido, AssinaturaAnual, TipoPagamento
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import joinedload
|
||||
from datetime import datetime, timedelta
|
||||
@@ -15,7 +15,7 @@ class DashboardService:
|
||||
@cached(expire=300, key_prefix="dashboard") # Cache for 5 minutes
|
||||
def get_dashboard_stats() -> Dict[str, Any]:
|
||||
"""Get dashboard statistics with caching"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
# Get cached stats first
|
||||
cache_key = CacheKeys.DASHBOARD_STATS
|
||||
@@ -146,7 +146,7 @@ class DashboardService:
|
||||
@cached(expire=600, key_prefix="dashboard") # Cache for 10 minutes
|
||||
def get_militante_stats() -> Dict[str, Any]:
|
||||
"""Get militante-specific statistics"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
# Militantes por estado
|
||||
estados = db.query(Militante.estado, func.count(Militante.id)).group_by(Militante.estado).all()
|
||||
@@ -175,7 +175,7 @@ class DashboardService:
|
||||
@cached(expire=300, key_prefix="dashboard")
|
||||
def get_financial_stats() -> Dict[str, Any]:
|
||||
"""Get financial statistics"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
# Total de pagamentos
|
||||
total_pagamentos = db.query(func.sum(Pagamento.valor)).scalar()
|
||||
@@ -218,7 +218,7 @@ class DashboardService:
|
||||
@staticmethod
|
||||
def obter_ultimos_militantes(limite: int = 5) -> List[Militante]:
|
||||
"""Obtém os últimos militantes cadastrados"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
return db.query(Militante).order_by(Militante.id.desc()).limit(limite).all()
|
||||
finally:
|
||||
@@ -227,7 +227,7 @@ class DashboardService:
|
||||
@staticmethod
|
||||
def obter_ultimos_pagamentos(limite: int = 5) -> List[Pagamento]:
|
||||
"""Obtém os últimos pagamentos realizados"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
return db.query(Pagamento).join(Militante).order_by(Pagamento.data_pagamento.desc()).limit(limite).all()
|
||||
finally:
|
||||
@@ -236,7 +236,7 @@ class DashboardService:
|
||||
@staticmethod
|
||||
def obter_tipos_pagamento() -> List[TipoPagamento]:
|
||||
"""Obtém todos os tipos de pagamento"""
|
||||
db = get_db_connection()
|
||||
db = get_db_session()
|
||||
try:
|
||||
return db.query(TipoPagamento).all()
|
||||
finally:
|
||||
|
||||
19
services/otp_service.py
Normal file
19
services/otp_service.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import base64
|
||||
import pyotp
|
||||
import qrcode
|
||||
from io import BytesIO
|
||||
|
||||
def generate_qr_code(user):
|
||||
"""Gera imagem PIL do QR code OTP para o usuário."""
|
||||
qr = qrcode.QRCode(version=1, box_size=10, border=5)
|
||||
qr.add_data(user.get_otp_uri())
|
||||
qr.make(fit=True)
|
||||
return qr.make_image(fill_color="black", back_color="white")
|
||||
|
||||
|
||||
def generate_qr_code_base64(user):
|
||||
"""Gera QR code OTP codificado em base64 (PNG)."""
|
||||
img = generate_qr_code(user)
|
||||
buffer = BytesIO()
|
||||
img.save(buffer, format="PNG")
|
||||
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
||||
Reference in New Issue
Block a user