Skip to content

Commit 9e4887b

Browse files
Merge pull request #15 from maxsonferovante/feature/tech-floripa
Otimizar a geração de certificados ao baixar recursos compartilhados …
2 parents 7c1abd1 + 4ace86f commit 9e4887b

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

certified_builder/certified_builder.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,29 @@ def build_certificates(self, participants: List[Participant]):
2626
logger.info(f"Iniciando geração de {len(participants)} certificados")
2727
results = []
2828

29+
# Cache for background and logo if they are the same for all participants
30+
certificate_template = None
31+
logo = None
32+
33+
# Check if all participants share the same background and logo
34+
if participants:
35+
first_participant = participants[0]
36+
all_same_background = all(p.certificate.background == first_participant.certificate.background for p in participants)
37+
all_same_logo = all(p.certificate.logo == first_participant.certificate.logo for p in participants)
38+
39+
# Download shared resources once if they are the same for all
40+
if all_same_background:
41+
certificate_template = self._download_image(first_participant.certificate.background)
42+
if all_same_logo:
43+
logo = self._download_image(first_participant.certificate.logo)
44+
2945
for participant in participants:
3046
try:
31-
# Download template and logo with error handling
32-
certificate_template = self._download_image(participant.certificate.background)
33-
logo = self._download_image(participant.certificate.logo)
47+
# Download template and logo only if they are not shared
48+
if not all_same_background:
49+
certificate_template = self._download_image(participant.certificate.background)
50+
if not all_same_logo:
51+
logo = self._download_image(participant.certificate.logo)
3452

3553
# Generate and save certificate
3654
certificate_generated = self.generate_certificate(participant, certificate_template, logo)
@@ -43,7 +61,6 @@ def build_certificates(self, participants: List[Participant]):
4361
"success": True
4462
})
4563

46-
4764
logger.info(f"Certificado gerado para {participant.name_completed()} com codigo de validação {participant.formated_validation_code()}")
4865
except Exception as e:
4966
logger.error(f"Erro ao gerar certificado para {participant.name_completed()}: {str(e)}")

0 commit comments

Comments
 (0)