def send_ballot_receipt_emails_to_adjudicators(ballots, debate): messages = [] round_name = _("%(tournament)s %(round)s @ %(room)s") % { 'tournament': str(debate.round.tournament), 'round': debate.round.name, 'room': debate.venue.name } subject = Template(debate.round.tournament.pref('ballot_email_subject')) message = Template(debate.round.tournament.pref('ballot_email_message')) context = {'DEBATE': round_name} use_codes = use_team_code_names(debate.round.tournament, False) for ballot in ballots: if 'adjudicator' in ballot: judge = ballot['adjudicator'] else: judge = debate.debateadjudicator_set.get( type=DebateAdjudicator.TYPE_CHAIR).adjudicator if judge.email is None: continue scores = "" for team in ballot['teams']: team_name = team['team'].code_name if use_codes else team[ 'team'].short_name scores += _("(%(side)s) %(team)s\n") % { 'side': team['side'], 'team': team_name } for speaker in team['speakers']: scores += _("- %(debater)s: %(score)s\n") % { 'debater': speaker['speaker'], 'score': speaker['score'] } context['USER'] = judge.name context['SCORES'] = scores messages.append( TournamentEmailMessage( subject, message, debate.round.tournament, debate.round, SentMessageRecord.EVENT_TYPE_BALLOT_CONFIRMED, judge, context)) try: get_connection().send_messages(messages) except SMTPException: logger.exception("Failed to send ballot receipt e-mails") raise except ConnectionError: logger.exception("Connection error sending ballot receipt e-mails") raise else: SentMessageRecord.objects.bulk_create( [message.as_sent_record() for message in messages])