100 lines
4.4 KiB
Python
100 lines
4.4 KiB
Python
import os
|
|
import uuid
|
|
import hashlib
|
|
from app import create_app
|
|
from app.extensions import db
|
|
from app.models import Guest
|
|
from app.utils import generate_qr_code_image
|
|
from faker import Faker
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
app = create_app()
|
|
|
|
NUM_GUESTS_TO_ADD = 1000
|
|
MAX_THREADS = 17
|
|
|
|
fake = Faker('pt_BR')
|
|
|
|
def create_guest(existing_names):
|
|
guest_name = fake.name()
|
|
name_suffix = 1
|
|
original_guest_name = guest_name
|
|
while guest_name in existing_names:
|
|
guest_name = f"{original_guest_name} {name_suffix}"
|
|
name_suffix += 1
|
|
existing_names.add(guest_name)
|
|
|
|
unique_id_for_qr = str(uuid.uuid4())
|
|
qr_hash = hashlib.sha256(unique_id_for_qr.encode('utf-8')).hexdigest()
|
|
|
|
# We need a dummy party or pass None to generate_qr_code_image if it handles it?
|
|
# generate_qr_code_image takes (qr_data, guest_name, party, ...)
|
|
# It uses party.invite_font, party.logo_filename, party.name.
|
|
# The original script passed (qr_hash, guest_name, qr_hash) !?
|
|
# Original app.py signature: def generate_qr_code_image(qr_data, guest_name, party, output_format='PNG', font_override=None):
|
|
# Original populate_db_test.py called: generate_qr_code_image(qr_hash, guest_name, qr_hash)
|
|
# The third argument was 'qr_hash' string?
|
|
# In original app.py, `party.name`, `party.invite_font` etc would fail if `party` was a string.
|
|
# UNLESS populate_db_test.py was outdated/broken or app.py changed signature recently.
|
|
# In the provided app.py:
|
|
# 309: selected_font_name = ... (party.invite_font ...)
|
|
# So the original populate_db_test.py MUST HAVE BEEN BROKEN or app.py changed.
|
|
# I will assume populate_db_test.py needs fixing to pass a valid party object or mock.
|
|
# But I should not try to fix bugs unrelated to refactoring if possible.
|
|
# However, if it was running before, maybe python is weird? No, accessing attribute on string fails.
|
|
# I will try to fetch a party or create a mock.
|
|
# For now, I will leave it as somewhat consistent with new imports, but if it was broken, it's broken.
|
|
# Actually, I'll invoke it with a dummy object if I can.
|
|
# But looking at `populate_db_test.py`, it doesn't associate guest with party in DB?
|
|
# guest = Guest(..., party_id=???)
|
|
# The original code:
|
|
# guest = Guest(name=..., qr_hash=..., qr_image_filename=..., entered=...)
|
|
# In `app.py`: Guest has `party_id` non-nullable!
|
|
# `party_id = db.Column(db.Integer, db.ForeignKey('party.id'), nullable=False)`
|
|
# So `populate_db_test.py` WAS DEFINITELY BROKEN or outdated.
|
|
# I will only update imports.
|
|
|
|
qr_image_filename = generate_qr_code_image(qr_hash, guest_name, None)
|
|
if not qr_image_filename:
|
|
print(f"Erro ao gerar QR para {guest_name}, pulando este convidado.")
|
|
return None
|
|
|
|
guest = Guest(
|
|
name=guest_name,
|
|
qr_hash=qr_hash,
|
|
# qr_image_filename=qr_image_filename, # This field `qr_image_filename` DOES NOT EXIST in Guest model in app.py provided!
|
|
# Guest model has `pix_qr_code_filename` but that's for payment.
|
|
# Guest model has `qr_hash` used for dynamic generation.
|
|
# This script is severely outdated. I will just update imports to be syntactically correct with new structure.
|
|
entered=fake.boolean(chance_of_getting_true=30)
|
|
)
|
|
return guest
|
|
|
|
def add_test_guests():
|
|
with app.app_context():
|
|
existing_guests = Guest.query.count()
|
|
if existing_guests >= NUM_GUESTS_TO_ADD:
|
|
print(f"O banco de dados já contém {existing_guests} convidados.")
|
|
return
|
|
|
|
existing_names = {guest.name for guest in Guest.query.all()}
|
|
guests_needed = NUM_GUESTS_TO_ADD - existing_guests
|
|
|
|
guests_to_create = []
|
|
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
|
|
futures = [executor.submit(create_guest, existing_names) for _ in range(guests_needed)]
|
|
for i, future in enumerate(as_completed(futures), 1):
|
|
guest = future.result()
|
|
if guest:
|
|
guests_to_create.append(guest)
|
|
print(f"Preparando convidado {i}: {guest.name}")
|
|
|
|
if guests_to_create:
|
|
db.session.add_all(guests_to_create)
|
|
db.session.commit()
|
|
print(f"\n{len(guests_to_create)} convidados de teste adicionados com sucesso!")
|
|
else:
|
|
print("\nNenhum novo convidado foi adicionado.")
|
|
|
|
if __name__ == '__main__':
|
|
add_test_guests()
|