2025-04-02 19:32:39 -03:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from functions.database import (
|
2025-04-22 18:11:32 -03:00
|
|
|
Base, Militante, CotaMensal, TipoComprovante, Comprovante,
|
2025-04-02 19:32:39 -03:00
|
|
|
MaterialVendido, TipoMaterial, VendaJornalAvulso, AssinaturaAnual,
|
2025-04-04 09:24:45 -03:00
|
|
|
RelatorioCotasMensais, RelatorioVendasMateriais, engine, SessionLocal,
|
2025-04-09 09:59:12 -03:00
|
|
|
Setor, ComiteCentral, Usuario, Role, EmailMilitante, Endereco,
|
2025-04-22 18:11:32 -03:00
|
|
|
ComiteRegional, Celula, EstadoMilitante, get_db_connection,
|
|
|
|
|
init_database
|
2025-04-02 19:32:39 -03:00
|
|
|
)
|
|
|
|
|
import random
|
|
|
|
|
from faker import Faker
|
2025-04-04 09:24:45 -03:00
|
|
|
import time
|
2025-04-09 09:59:12 -03:00
|
|
|
from werkzeug.security import generate_password_hash
|
2025-04-02 19:32:39 -03:00
|
|
|
|
|
|
|
|
fake = Faker('pt_BR')
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
def criar_estrutura_organizacional(session):
|
|
|
|
|
"""Cria a estrutura organizacional básica"""
|
|
|
|
|
print("\nCriando estrutura organizacional...")
|
|
|
|
|
|
|
|
|
|
# Criar Comitê Central
|
|
|
|
|
cc = ComiteCentral(nome="Comitê Central SP")
|
|
|
|
|
session.add(cc)
|
|
|
|
|
session.flush()
|
|
|
|
|
|
|
|
|
|
# Criar Comitês Regionais
|
|
|
|
|
crs = []
|
|
|
|
|
for nome in ["CR São Paulo", "CR ABC", "CR Campinas"]:
|
|
|
|
|
cr = ComiteRegional(nome=nome)
|
|
|
|
|
session.add(cr)
|
|
|
|
|
session.flush()
|
|
|
|
|
crs.append(cr)
|
|
|
|
|
|
|
|
|
|
# Criar Setores para cada CR
|
|
|
|
|
setores = []
|
|
|
|
|
for cr in crs:
|
|
|
|
|
for i in range(2): # 2 setores por CR
|
|
|
|
|
setor = Setor(
|
|
|
|
|
nome=f"Setor {i+1} - {cr.nome}",
|
|
|
|
|
cr_id=cr.id
|
|
|
|
|
)
|
|
|
|
|
session.add(setor)
|
|
|
|
|
session.flush()
|
|
|
|
|
setores.append(setor)
|
|
|
|
|
|
|
|
|
|
# Criar Células para cada Setor
|
|
|
|
|
for setor in setores:
|
|
|
|
|
for i in range(2): # 2 células por setor
|
|
|
|
|
celula = Celula(
|
|
|
|
|
nome=f"Célula {i+1} - {setor.nome}",
|
|
|
|
|
setor_id=setor.id
|
|
|
|
|
)
|
|
|
|
|
session.add(celula)
|
|
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
return crs, setores
|
|
|
|
|
|
2025-04-22 18:11:32 -03:00
|
|
|
def criar_tipos_comprovante(session):
|
|
|
|
|
"""Cria tipos de comprovante padrão"""
|
|
|
|
|
print("\nCriando tipos de comprovante...")
|
2025-04-02 19:32:39 -03:00
|
|
|
tipos = [
|
2025-04-22 18:11:32 -03:00
|
|
|
"Comprovante Padrão",
|
|
|
|
|
"Comprovante Especial",
|
|
|
|
|
"Comprovante Extraordinário",
|
|
|
|
|
"Jornal Avulso",
|
|
|
|
|
"Assinatura de Jornal",
|
|
|
|
|
"Campanha Financeira"
|
2025-04-02 19:32:39 -03:00
|
|
|
]
|
2025-04-22 18:11:32 -03:00
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
for tipo in tipos:
|
2025-04-22 18:11:32 -03:00
|
|
|
if not session.query(TipoComprovante).filter_by(descricao=tipo).first():
|
|
|
|
|
session.add(TipoComprovante(descricao=tipo))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
session.commit()
|
|
|
|
|
print("Tipos de comprovante criados com sucesso!")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
session.rollback()
|
|
|
|
|
print(f"Erro ao criar tipos de comprovante: {e}")
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-04 09:24:45 -03:00
|
|
|
def criar_tipos_material(session):
|
2025-04-02 19:32:39 -03:00
|
|
|
"""Cria tipos de material padrão"""
|
2025-04-09 09:59:12 -03:00
|
|
|
print("\nCriando tipos de material...")
|
2025-04-02 19:32:39 -03:00
|
|
|
tipos = [
|
|
|
|
|
"Jornal",
|
|
|
|
|
"Revista",
|
|
|
|
|
"Livro",
|
|
|
|
|
"Panfleto",
|
|
|
|
|
"Cartilha"
|
|
|
|
|
]
|
|
|
|
|
for tipo in tipos:
|
2025-04-04 09:24:45 -03:00
|
|
|
if not session.query(TipoMaterial).filter_by(descricao=tipo).first():
|
|
|
|
|
session.add(TipoMaterial(descricao=tipo))
|
|
|
|
|
session.commit()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
def criar_militantes(session, num_militantes, setores):
|
|
|
|
|
"""Cria militantes com todos os dados necessários"""
|
2025-04-02 21:20:48 -03:00
|
|
|
print(f"\nCriando {num_militantes} militantes...")
|
2025-04-02 19:32:39 -03:00
|
|
|
militantes = []
|
2025-04-04 09:24:45 -03:00
|
|
|
emails_usados = set()
|
|
|
|
|
|
2025-04-02 21:20:48 -03:00
|
|
|
for i in range(num_militantes):
|
2025-04-04 09:24:45 -03:00
|
|
|
try:
|
2025-04-09 09:59:12 -03:00
|
|
|
# Dados básicos
|
2025-04-04 09:24:45 -03:00
|
|
|
nome = fake.name()
|
|
|
|
|
cpf = fake.cpf()
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
# Email único
|
2025-04-04 09:24:45 -03:00
|
|
|
while True:
|
|
|
|
|
email = fake.email()
|
|
|
|
|
if email not in emails_usados:
|
|
|
|
|
emails_usados.add(email)
|
|
|
|
|
break
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
# Criar endereço
|
2025-04-04 09:24:45 -03:00
|
|
|
endereco = Endereco(
|
2025-04-09 09:59:12 -03:00
|
|
|
cep=fake.postcode(),
|
2025-04-04 09:24:45 -03:00
|
|
|
estado=fake.estado_sigla(),
|
|
|
|
|
cidade=fake.city(),
|
|
|
|
|
bairro=fake.bairro(),
|
|
|
|
|
rua=fake.street_name(),
|
|
|
|
|
numero=str(random.randint(1, 999)),
|
2025-04-09 09:59:12 -03:00
|
|
|
complemento=f"Bloco {random.randint(1, 10)}, Apto {random.randint(1, 999)}" if random.random() < 0.3 else None
|
2025-04-04 09:24:45 -03:00
|
|
|
)
|
|
|
|
|
session.add(endereco)
|
|
|
|
|
session.flush()
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
# Selecionar setor e célula aleatórios
|
|
|
|
|
setor = random.choice(setores)
|
|
|
|
|
celula = random.choice(session.query(Celula).filter_by(setor_id=setor.id).all())
|
|
|
|
|
|
|
|
|
|
# Definir responsabilidades
|
|
|
|
|
responsabilidades = 0
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser Responsável de Finanças
|
|
|
|
|
responsabilidades |= Militante.RESPONSAVEL_FINANCAS
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser Responsável de Imprensa
|
|
|
|
|
responsabilidades |= Militante.RESPONSAVEL_IMPRENSA
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser Quadro-Orientador
|
|
|
|
|
responsabilidades |= Militante.QUADRO_ORIENTADOR
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser Secretário
|
|
|
|
|
responsabilidades |= Militante.SECRETARIO
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser MPS
|
|
|
|
|
responsabilidades |= Militante.MPS
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser Tesoureiro
|
|
|
|
|
responsabilidades |= Militante.TESOUREIRO
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser MNS
|
|
|
|
|
responsabilidades |= Militante.MNS
|
|
|
|
|
if random.random() < 0.2: # 20% chance de ser da Juventude
|
|
|
|
|
responsabilidades |= Militante.JUVENTUDE
|
|
|
|
|
if random.random() < 0.3: # 30% chance de ser Aspirante
|
|
|
|
|
responsabilidades |= Militante.ASPIRANTE
|
|
|
|
|
|
|
|
|
|
print(f"Criando militante {i+1}: {nome}")
|
2025-04-04 09:24:45 -03:00
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
# Criar militante com todos os dados
|
2025-04-04 09:24:45 -03:00
|
|
|
militante = Militante(
|
|
|
|
|
nome=nome,
|
|
|
|
|
cpf=cpf,
|
|
|
|
|
titulo_eleitoral=str(random.randint(100000000000, 999999999999)),
|
|
|
|
|
data_nascimento=fake.date_of_birth(minimum_age=18, maximum_age=65),
|
|
|
|
|
data_entrada_oci=fake.date_between(start_date='-5y', end_date='today'),
|
|
|
|
|
data_efetivacao_oci=fake.date_between(start_date='-4y', end_date='today'),
|
|
|
|
|
telefone1=fake.phone_number(),
|
|
|
|
|
telefone2=fake.phone_number() if random.random() < 0.3 else None,
|
|
|
|
|
profissao=fake.job(),
|
|
|
|
|
regime_trabalho=random.choice(['CLT', 'PJ', 'Estatutário', 'Autônomo']),
|
|
|
|
|
empresa=fake.company(),
|
|
|
|
|
contratante=fake.company() if random.random() < 0.2 else None,
|
|
|
|
|
instituicao_ensino=fake.company() if random.random() < 0.4 else None,
|
|
|
|
|
tipo_instituicao=random.choice(['Federal', 'Estadual', 'Municipal', 'Privada']) if random.random() < 0.4 else None,
|
|
|
|
|
sindicato=fake.company() if random.random() < 0.6 else None,
|
|
|
|
|
cargo_sindical=random.choice(['Diretor', 'Delegado', 'Conselheiro']) if random.random() < 0.3 else None,
|
|
|
|
|
dirigente_sindical=random.random() < 0.2,
|
|
|
|
|
central_sindical=random.choice(['CUT', 'CSP-Conlutas', 'CTB', 'Força Sindical']) if random.random() < 0.4 else None,
|
|
|
|
|
endereco_id=endereco.id,
|
2025-04-09 09:59:12 -03:00
|
|
|
celula_id=celula.id,
|
|
|
|
|
responsabilidades=responsabilidades,
|
|
|
|
|
estado=random.choice(list(EstadoMilitante))
|
2025-04-04 09:24:45 -03:00
|
|
|
)
|
|
|
|
|
session.add(militante)
|
|
|
|
|
session.flush()
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
# Criar email do militante
|
2025-04-04 09:24:45 -03:00
|
|
|
email_militante = EmailMilitante(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
endereco_email=email
|
|
|
|
|
)
|
|
|
|
|
session.add(email_militante)
|
|
|
|
|
|
|
|
|
|
militantes.append(militante)
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Erro ao criar militante {i+1}: {e}")
|
|
|
|
|
session.rollback()
|
|
|
|
|
continue
|
2025-04-02 21:20:48 -03:00
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
return militantes
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
def criar_cotas(session, militantes):
|
|
|
|
|
"""Cria cotas mensais para os militantes"""
|
|
|
|
|
print("\nCriando cotas mensais...")
|
2025-04-02 19:32:39 -03:00
|
|
|
for militante in militantes:
|
2025-04-04 09:24:45 -03:00
|
|
|
try:
|
2025-04-09 09:59:12 -03:00
|
|
|
# Criar 12 cotas (1 ano) para cada militante
|
|
|
|
|
for i in range(12):
|
2025-04-04 09:24:45 -03:00
|
|
|
data_base = datetime.now() - timedelta(days=30 * i)
|
|
|
|
|
valor = random.uniform(50, 200)
|
|
|
|
|
cota = CotaMensal(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
valor_antigo=valor,
|
|
|
|
|
valor_novo=valor * 1.1,
|
|
|
|
|
data_alteracao=data_base,
|
|
|
|
|
data_vencimento=data_base + timedelta(days=30),
|
|
|
|
|
pago=random.choice([True, False])
|
|
|
|
|
)
|
|
|
|
|
session.add(cota)
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Erro ao criar cotas para militante {militante.nome}: {e}")
|
|
|
|
|
session.rollback()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-22 18:11:32 -03:00
|
|
|
def criar_comprovantes(session, militantes):
|
|
|
|
|
"""Cria comprovantes para os militantes"""
|
|
|
|
|
print("\nCriando comprovantes...")
|
|
|
|
|
tipos_comprovante = session.query(TipoComprovante).all()
|
2025-04-09 09:59:12 -03:00
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
for militante in militantes:
|
2025-04-09 09:59:12 -03:00
|
|
|
try:
|
2025-04-22 18:11:32 -03:00
|
|
|
# Criar entre 3 e 8 comprovantes por militante
|
2025-04-09 09:59:12 -03:00
|
|
|
for _ in range(random.randint(3, 8)):
|
2025-04-22 18:11:32 -03:00
|
|
|
tipo = random.choice(tipos_comprovante)
|
|
|
|
|
comprovante = Comprovante(
|
2025-04-09 09:59:12 -03:00
|
|
|
militante_id=militante.id,
|
2025-04-22 18:11:32 -03:00
|
|
|
tipo_comprovante=tipo.descricao, # Usando a descrição do tipo
|
|
|
|
|
valor=random.uniform(10, 1000),
|
|
|
|
|
data_comprovante=fake.date_between(start_date='-1y', end_date='today')
|
2025-04-09 09:59:12 -03:00
|
|
|
)
|
2025-04-22 18:11:32 -03:00
|
|
|
session.add(comprovante)
|
2025-04-09 09:59:12 -03:00
|
|
|
session.commit()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
session.rollback()
|
2025-04-22 18:11:32 -03:00
|
|
|
print(f"Erro ao criar comprovantes para militante {militante.nome}: {e}")
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
def criar_materiais_vendidos(session, militantes):
|
|
|
|
|
"""Cria registros de materiais vendidos"""
|
|
|
|
|
print("\nCriando materiais vendidos...")
|
|
|
|
|
tipos_material = session.query(TipoMaterial).all()
|
|
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
for militante in militantes:
|
2025-04-09 09:59:12 -03:00
|
|
|
try:
|
|
|
|
|
# Criar entre 2 e 5 materiais vendidos por militante
|
|
|
|
|
for _ in range(random.randint(2, 5)):
|
|
|
|
|
material = MaterialVendido(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
tipo_material_id=random.choice(tipos_material).id,
|
|
|
|
|
descricao=fake.sentence(),
|
|
|
|
|
valor=random.uniform(20, 100),
|
|
|
|
|
data_venda=fake.date_time_between(start_date='-1y', end_date='now')
|
|
|
|
|
)
|
|
|
|
|
session.add(material)
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Erro ao criar materiais vendidos para militante {militante.nome}: {e}")
|
|
|
|
|
session.rollback()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
def criar_vendas_jornal(session, militantes):
|
|
|
|
|
"""Cria vendas de jornal avulso"""
|
|
|
|
|
print("\nCriando vendas de jornal...")
|
2025-04-02 19:32:39 -03:00
|
|
|
for militante in militantes:
|
2025-04-09 09:59:12 -03:00
|
|
|
try:
|
|
|
|
|
# Criar entre 2 e 6 vendas de jornal por militante
|
|
|
|
|
for _ in range(random.randint(2, 6)):
|
|
|
|
|
quantidade = random.randint(1, 10)
|
|
|
|
|
valor_unitario = random.uniform(5, 15)
|
|
|
|
|
venda = VendaJornalAvulso(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
quantidade=quantidade,
|
|
|
|
|
valor_total=quantidade * valor_unitario,
|
|
|
|
|
data_venda=fake.date_time_between(start_date='-1y', end_date='now')
|
|
|
|
|
)
|
|
|
|
|
session.add(venda)
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Erro ao criar vendas de jornal para militante {militante.nome}: {e}")
|
|
|
|
|
session.rollback()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
def criar_assinaturas(session, militantes):
|
|
|
|
|
"""Cria assinaturas anuais"""
|
|
|
|
|
print("\nCriando assinaturas anuais...")
|
|
|
|
|
tipos_material = session.query(TipoMaterial).all()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
for militante in militantes:
|
|
|
|
|
try:
|
|
|
|
|
# 30% de chance de ter assinatura
|
|
|
|
|
if random.random() < 0.3:
|
|
|
|
|
data_inicio = fake.date_time_between(start_date='-1y', end_date='now')
|
|
|
|
|
assinatura = AssinaturaAnual(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
tipo_material_id=random.choice(tipos_material).id,
|
|
|
|
|
quantidade=random.randint(1, 3),
|
|
|
|
|
valor_total=random.uniform(100, 500),
|
|
|
|
|
data_inicio=data_inicio,
|
|
|
|
|
data_fim=data_inicio + timedelta(days=365)
|
|
|
|
|
)
|
|
|
|
|
session.add(assinatura)
|
|
|
|
|
session.commit()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Erro ao criar assinatura para militante {militante.nome}: {e}")
|
|
|
|
|
session.rollback()
|
2025-04-02 21:20:48 -03:00
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
def seed_database():
|
2025-04-09 09:59:12 -03:00
|
|
|
"""Função principal para popular o banco de dados"""
|
2025-04-22 18:11:32 -03:00
|
|
|
session = get_db_connection()
|
2025-04-04 09:24:45 -03:00
|
|
|
try:
|
2025-04-09 09:59:12 -03:00
|
|
|
print("Iniciando população do banco de dados...")
|
|
|
|
|
|
|
|
|
|
# Criar estrutura organizacional
|
|
|
|
|
crs, setores = criar_estrutura_organizacional(session)
|
|
|
|
|
|
|
|
|
|
# Criar tipos básicos
|
2025-04-22 18:11:32 -03:00
|
|
|
criar_tipos_comprovante(session)
|
2025-04-04 09:24:45 -03:00
|
|
|
criar_tipos_material(session)
|
|
|
|
|
|
2025-04-09 09:59:12 -03:00
|
|
|
# Criar militantes (30 militantes para teste)
|
|
|
|
|
militantes = criar_militantes(session, 30, setores)
|
|
|
|
|
|
|
|
|
|
# Criar dados financeiros e materiais
|
|
|
|
|
criar_cotas(session, militantes)
|
2025-04-22 18:11:32 -03:00
|
|
|
criar_comprovantes(session, militantes)
|
2025-04-09 09:59:12 -03:00
|
|
|
criar_materiais_vendidos(session, militantes)
|
|
|
|
|
criar_vendas_jornal(session, militantes)
|
|
|
|
|
criar_assinaturas(session, militantes)
|
|
|
|
|
|
|
|
|
|
print("\nBanco de dados populado com sucesso!")
|
|
|
|
|
|
2025-04-04 09:24:45 -03:00
|
|
|
except Exception as e:
|
2025-04-09 09:59:12 -03:00
|
|
|
print(f"Erro durante a população do banco: {e}")
|
2025-04-04 09:24:45 -03:00
|
|
|
session.rollback()
|
|
|
|
|
finally:
|
|
|
|
|
session.close()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
seed_database()
|