2025-04-02 19:32:39 -03:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from functions.database import (
|
|
|
|
|
Base, Militante, CotaMensal, TipoPagamento, Pagamento,
|
|
|
|
|
MaterialVendido, TipoMaterial, VendaJornalAvulso, AssinaturaAnual,
|
2025-04-02 21:20:48 -03:00
|
|
|
RelatorioCotasMensais, RelatorioVendasMateriais, engine, get_db_connection,
|
2025-04-03 20:58:02 -03:00
|
|
|
Setor, ComiteCentral, Usuario, Role, EmailMilitante, Endereco
|
2025-04-02 19:32:39 -03:00
|
|
|
)
|
|
|
|
|
import random
|
|
|
|
|
from faker import Faker
|
|
|
|
|
|
|
|
|
|
fake = Faker('pt_BR')
|
|
|
|
|
db_session = get_db_connection()
|
|
|
|
|
|
|
|
|
|
def criar_tipos_pagamento():
|
|
|
|
|
"""Cria tipos de pagamento padrão"""
|
|
|
|
|
tipos = [
|
|
|
|
|
"Dinheiro",
|
|
|
|
|
"PIX",
|
|
|
|
|
"Cartão de Crédito",
|
|
|
|
|
"Cartão de Débito",
|
|
|
|
|
"Transferência Bancária"
|
|
|
|
|
]
|
|
|
|
|
for tipo in tipos:
|
|
|
|
|
if not db_session.query(TipoPagamento).filter_by(descricao=tipo).first():
|
|
|
|
|
db_session.add(TipoPagamento(descricao=tipo))
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_tipos_material():
|
|
|
|
|
"""Cria tipos de material padrão"""
|
|
|
|
|
tipos = [
|
|
|
|
|
"Jornal",
|
|
|
|
|
"Revista",
|
|
|
|
|
"Livro",
|
|
|
|
|
"Panfleto",
|
|
|
|
|
"Cartilha"
|
|
|
|
|
]
|
|
|
|
|
for tipo in tipos:
|
|
|
|
|
if not db_session.query(TipoMaterial).filter_by(descricao=tipo).first():
|
|
|
|
|
db_session.add(TipoMaterial(descricao=tipo))
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
2025-04-02 21:20:48 -03:00
|
|
|
def criar_militantes(num_militantes):
|
|
|
|
|
print(f"\nCriando {num_militantes} militantes...")
|
2025-04-02 19:32:39 -03:00
|
|
|
militantes = []
|
2025-04-02 21:20:48 -03:00
|
|
|
emails_usados = set() # Conjunto para rastrear emails já usados
|
|
|
|
|
|
|
|
|
|
for i in range(num_militantes):
|
|
|
|
|
nome = fake.name()
|
|
|
|
|
cpf = fake.cpf()
|
|
|
|
|
|
|
|
|
|
# Gerar email único
|
|
|
|
|
while True:
|
|
|
|
|
email = fake.email()
|
|
|
|
|
if email not in emails_usados:
|
|
|
|
|
emails_usados.add(email)
|
|
|
|
|
break
|
|
|
|
|
|
2025-04-03 20:58:02 -03:00
|
|
|
# Criar endereço
|
|
|
|
|
endereco = Endereco(
|
|
|
|
|
estado=fake.estado_sigla(),
|
|
|
|
|
cidade=fake.city(),
|
|
|
|
|
bairro=fake.bairro(),
|
|
|
|
|
rua=fake.street_name(),
|
|
|
|
|
numero=str(random.randint(1, 999)),
|
|
|
|
|
complemento=f"Bloco {random.randint(1, 10)}, Apto {random.randint(1, 999)}" if random.random() < 0.3 else None,
|
|
|
|
|
cep=fake.postcode()
|
|
|
|
|
)
|
|
|
|
|
db_session.add(endereco)
|
|
|
|
|
db_session.flush() # Para obter o ID do endereço
|
2025-04-02 21:20:48 -03:00
|
|
|
|
|
|
|
|
print(f"Criando militante {i+1}: {nome} (CPF: {cpf})")
|
|
|
|
|
|
2025-04-03 20:58:02 -03:00
|
|
|
# Criar militante
|
2025-04-02 19:32:39 -03:00
|
|
|
militante = Militante(
|
2025-04-02 21:20:48 -03:00
|
|
|
nome=nome,
|
|
|
|
|
cpf=cpf,
|
2025-04-03 20:58:02 -03:00
|
|
|
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,
|
|
|
|
|
responsabilidades=random.randint(0, 1023) # Valor aleatório para responsabilidades
|
|
|
|
|
)
|
|
|
|
|
db_session.add(militante)
|
|
|
|
|
db_session.flush() # Para obter o ID do militante
|
|
|
|
|
|
|
|
|
|
# Criar email do militante
|
|
|
|
|
email_militante = EmailMilitante(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
endereco_email=email
|
2025-04-02 19:32:39 -03:00
|
|
|
)
|
2025-04-03 20:58:02 -03:00
|
|
|
db_session.add(email_militante)
|
|
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
militantes.append(militante)
|
2025-04-02 21:20:48 -03:00
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
db_session.commit()
|
|
|
|
|
return militantes
|
|
|
|
|
|
|
|
|
|
def criar_cotas(militantes, quantidade_por_militante=3):
|
|
|
|
|
"""Cria cotas mensais fictícias"""
|
2025-04-02 21:20:48 -03:00
|
|
|
print(f"Criando {quantidade_por_militante} cotas para cada um dos {len(militantes)} militantes...")
|
2025-04-02 19:32:39 -03:00
|
|
|
for militante in militantes:
|
2025-04-02 21:20:48 -03:00
|
|
|
print(f"Criando cotas para militante {militante.nome}")
|
2025-04-02 19:32:39 -03:00
|
|
|
for i in range(quantidade_por_militante):
|
|
|
|
|
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])
|
|
|
|
|
)
|
|
|
|
|
db_session.add(cota)
|
2025-04-02 21:20:48 -03:00
|
|
|
print(f" Cota criada: valor={valor:.2f}, vencimento={data_base + timedelta(days=30)}")
|
2025-04-02 19:32:39 -03:00
|
|
|
db_session.commit()
|
2025-04-02 21:20:48 -03:00
|
|
|
print("Cotas criadas com sucesso!")
|
2025-04-02 19:32:39 -03:00
|
|
|
|
|
|
|
|
def criar_pagamentos(militantes):
|
|
|
|
|
"""Cria pagamentos fictícios"""
|
2025-04-03 20:58:02 -03:00
|
|
|
tipos_pagamento = ["Cota", "Jornal", "Assinatura", "Campanha Financeira"]
|
2025-04-02 19:32:39 -03:00
|
|
|
for militante in militantes:
|
|
|
|
|
for _ in range(random.randint(1, 5)):
|
|
|
|
|
pagamento = Pagamento(
|
|
|
|
|
militante_id=militante.id,
|
2025-04-03 20:58:02 -03:00
|
|
|
tipo_pagamento=random.choice(tipos_pagamento),
|
2025-04-02 19:32:39 -03:00
|
|
|
valor=random.uniform(50, 500),
|
2025-04-03 20:58:02 -03:00
|
|
|
data_pagamento=fake.date_between(start_date='-1y', end_date='today')
|
2025-04-02 19:32:39 -03:00
|
|
|
)
|
|
|
|
|
db_session.add(pagamento)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_materiais_vendidos(militantes):
|
|
|
|
|
"""Cria materiais vendidos fictícios"""
|
|
|
|
|
tipos_material = db_session.query(TipoMaterial).all()
|
|
|
|
|
for militante in militantes:
|
|
|
|
|
for _ in range(random.randint(1, 3)):
|
|
|
|
|
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')
|
|
|
|
|
)
|
|
|
|
|
db_session.add(material)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_vendas_jornal(militantes):
|
|
|
|
|
"""Cria vendas de jornal avulso fictícias"""
|
|
|
|
|
for militante in militantes:
|
|
|
|
|
for _ in range(random.randint(1, 4)):
|
|
|
|
|
venda = VendaJornalAvulso(
|
|
|
|
|
militante_id=militante.id,
|
|
|
|
|
quantidade=random.randint(1, 10),
|
|
|
|
|
valor_total=random.uniform(10, 100),
|
|
|
|
|
data_venda=fake.date_time_between(start_date='-1y', end_date='now')
|
|
|
|
|
)
|
|
|
|
|
db_session.add(venda)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_assinaturas(militantes):
|
|
|
|
|
"""Cria assinaturas anuais fictícias"""
|
|
|
|
|
tipos_material = db_session.query(TipoMaterial).all()
|
|
|
|
|
for militante in militantes:
|
|
|
|
|
if random.random() < 0.3: # 30% de chance de ter assinatura
|
|
|
|
|
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)
|
|
|
|
|
)
|
|
|
|
|
db_session.add(assinatura)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_relatorios():
|
|
|
|
|
"""Cria relatórios fictícios"""
|
|
|
|
|
for _ in range(12): # Um relatório por mês do último ano
|
|
|
|
|
data = fake.date_time_between(start_date='-1y', end_date='now')
|
|
|
|
|
|
|
|
|
|
relatorio_cotas = RelatorioCotasMensais(
|
|
|
|
|
setor_id=random.randint(1, 5),
|
|
|
|
|
comite_id=random.randint(1, 3),
|
|
|
|
|
total_cotas=random.uniform(1000, 5000),
|
|
|
|
|
data_relatorio=data
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
relatorio_vendas = RelatorioVendasMateriais(
|
|
|
|
|
setor_id=random.randint(1, 5),
|
|
|
|
|
comite_id=random.randint(1, 3),
|
|
|
|
|
total_vendas=random.uniform(500, 3000),
|
|
|
|
|
data_relatorio=data
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db_session.add(relatorio_cotas)
|
|
|
|
|
db_session.add(relatorio_vendas)
|
|
|
|
|
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
2025-04-02 21:20:48 -03:00
|
|
|
def criar_setores():
|
|
|
|
|
"""Cria setores padrão"""
|
|
|
|
|
setores = [
|
|
|
|
|
"Setor 1",
|
|
|
|
|
"Setor 2",
|
|
|
|
|
"Setor 3",
|
|
|
|
|
"Setor 4",
|
|
|
|
|
"Setor 5"
|
|
|
|
|
]
|
|
|
|
|
for setor in setores:
|
|
|
|
|
if not db_session.query(Setor).filter_by(nome=setor).first():
|
|
|
|
|
db_session.add(Setor(nome=setor))
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_comites():
|
|
|
|
|
"""Cria comitês padrão"""
|
|
|
|
|
comites = [
|
|
|
|
|
"Comitê 1",
|
|
|
|
|
"Comitê 2",
|
|
|
|
|
"Comitê 3"
|
|
|
|
|
]
|
|
|
|
|
for comite in comites:
|
|
|
|
|
if not db_session.query(ComiteCentral).filter_by(nome=comite).first():
|
|
|
|
|
db_session.add(ComiteCentral(nome=comite))
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_roles():
|
|
|
|
|
"""Cria roles padrão"""
|
|
|
|
|
roles = [
|
|
|
|
|
("admin", 1), # Nível 1: Administrador
|
|
|
|
|
("gestor", 2), # Nível 2: Gestor
|
|
|
|
|
("usuario", 3) # Nível 3: Usuário comum
|
|
|
|
|
]
|
|
|
|
|
for nome, nivel in roles:
|
|
|
|
|
if not db_session.query(Role).filter_by(nome=nome).first():
|
|
|
|
|
db_session.add(Role(nome=nome, nivel=nivel))
|
|
|
|
|
db_session.commit()
|
|
|
|
|
|
|
|
|
|
def criar_usuario_admin():
|
|
|
|
|
"""Cria usuário admin inicial"""
|
|
|
|
|
if not db_session.query(Usuario).filter_by(username='admin').first():
|
|
|
|
|
role_admin = db_session.query(Role).filter_by(nome='admin').first()
|
|
|
|
|
setor = db_session.query(Setor).first()
|
|
|
|
|
|
|
|
|
|
admin = Usuario(
|
|
|
|
|
username='admin',
|
|
|
|
|
email='admin@example.com',
|
|
|
|
|
is_admin=True,
|
|
|
|
|
ativo=True,
|
|
|
|
|
role_id=role_admin.id if role_admin else None,
|
|
|
|
|
setor_id=setor.id if setor else None
|
|
|
|
|
)
|
|
|
|
|
admin.set_password('admin123') # Método que deve existir na classe Usuario
|
|
|
|
|
db_session.add(admin)
|
|
|
|
|
db_session.commit()
|
|
|
|
|
print("Usuário admin criado com sucesso!")
|
|
|
|
|
|
2025-04-02 19:32:39 -03:00
|
|
|
def seed_database():
|
|
|
|
|
"""Função principal para popular o banco de dados com dados fictícios"""
|
2025-04-02 21:20:48 -03:00
|
|
|
print("Populando banco de dados com dados fictícios...")
|
2025-04-02 19:32:39 -03:00
|
|
|
|
|
|
|
|
criar_tipos_pagamento()
|
|
|
|
|
criar_tipos_material()
|
2025-04-02 21:20:48 -03:00
|
|
|
criar_setores()
|
|
|
|
|
criar_comites()
|
|
|
|
|
criar_roles()
|
2025-04-02 19:32:39 -03:00
|
|
|
|
|
|
|
|
militantes = criar_militantes(50)
|
|
|
|
|
criar_cotas(militantes)
|
|
|
|
|
criar_pagamentos(militantes)
|
|
|
|
|
criar_materiais_vendidos(militantes)
|
|
|
|
|
criar_vendas_jornal(militantes)
|
|
|
|
|
criar_assinaturas(militantes)
|
|
|
|
|
criar_relatorios()
|
|
|
|
|
|
2025-04-02 21:20:48 -03:00
|
|
|
print("Dados fictícios criados com sucesso!")
|
2025-04-02 19:32:39 -03:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
seed_database()
|