Repository: TabbycatDebate/tabbycat

Issue:
Email notifications should use team code names if applicable
Just making a note that currently the "draw released" emails sent to adjudicators use `debate.matchup`, which (always) uses real names. Ideally we'd want to use code names if code names are enabled for the public, and real names if they're not. (That is, we'd want to use the same names as are used in the public draw.)

Unfortunately there's currently no single convenience function for this _within_ `Debate` or `DebateTeam`, because it depends on a tournament preference. I think we'll need to construct the relevant string from within `draw/utils.py`, or do some refactoring. There _is_, however, a convenience function `use_team_code_names()` in `options/utils.py`, that interprets the preference cleanly, because there are five choices for various permutations.

Relevant bits of code (as of 531faa3056bd3892dcf3138a7bf86814a2666471):

https://github.com/TabbycatDebate/tabbycat/blob/531faa3056bd3892dcf3138a7bf86814a2666471/tabbycat/draw/utils.py#L60-L66

Example of existing code for choosing between code name and real name (this is the code used in the tables constructor):
https://github.com/TabbycatDebate/tabbycat/blob/531faa3056bd3892dcf3138a7bf86814a2666471/tabbycat/utils/tables.py#L197-L213


Discussion:
I also just realized similarly for ballot receipts (line 281):

https://github.com/TabbycatDebate/tabbycat/blob/531faa3056bd3892dcf3138a7bf86814a2666471/tabbycat/results/utils.py#L280-L284

Resolving patch:
diff --git a/tabbycat/draw/models.py b/tabbycat/draw/models.py
--- a/tabbycat/draw/models.py
+++ b/tabbycat/draw/models.py
@@ -100,6 +100,20 @@ def _teams_and_sides_display(self):
         return ", ".join(["%s (%s)" % (dt.team.short_name, dt.get_side_display())
                 for dt in self.debateteam_set.all()])
 
+    @property
+    def matchup_codes(self):
+        # Like matchup, but uses team codes. It is not as protected.
+        if not self.sides_confirmed:
+            teams_list = ", ".join([dt.team.code_name for dt in self.debateteam_set.all()])
+            return teams_list + gettext(" (sides not confirmed)")
+
+        try:
+            sides = self.round.tournament.sides
+            return gettext(" vs ").join(self.get_team(side).code_name for side in sides)
+        except (IndexError, ObjectDoesNotExist, MultipleObjectsReturned):
+            return ", ".join(["%s (%s)" % (dt.team.code_name, dt.get_side_display())
+                for dt in self.debateteam_set.all()])
+
     # --------------------------------------------------------------------------
     # Team properties
     # --------------------------------------------------------------------------
diff --git a/tabbycat/draw/utils.py b/tabbycat/draw/utils.py
--- a/tabbycat/draw/utils.py
+++ b/tabbycat/draw/utils.py
@@ -9,6 +9,7 @@
 from adjallocation.allocation import AdjudicatorAllocation
 from notifications.models import SentMessageRecord
 from notifications.utils import TournamentEmailMessage
+from options.utils import use_team_code_names
 from participants.models import Team
 from tournaments.models import Round
 
@@ -38,6 +39,7 @@ def annotate_npullups(teams, until):
 def send_mail_to_adjs(round):
     tournament = round.tournament
     draw = round.debate_set_with_prefetches(speakers=False, divisions=False).all()
+    use_codes = use_team_code_names(tournament, False)
 
     subject = Template(tournament.pref('adj_email_subject_line'))
     body = Template(tournament.pref('adj_email_message'))
@@ -58,11 +60,12 @@ def _assemble_panel(adjs):
         return ", ".join(adj_string)
 
     for debate in draw:
+        matchup = debate.matchup_codes if use_codes else debate.matchup
         context = {
             'ROUND': round.name,
             'VENUE': debate.venue.name,
             'PANEL': _assemble_panel(debate.adjudicators.with_positions()),
-            'DRAW': debate.matchup
+            'DRAW': matchup
         }
 
         for adj, pos in debate.adjudicators.with_positions():
diff --git a/tabbycat/results/utils.py b/tabbycat/results/utils.py
--- a/tabbycat/results/utils.py
+++ b/tabbycat/results/utils.py
@@ -8,9 +8,11 @@
 from django.utils.translation import gettext as _
 from django.utils.translation import gettext_lazy
 
+from adjallocation.models import DebateAdjudicator
 from draw.models import Debate
 from notifications.models import SentMessageRecord
 from notifications.utils import TournamentEmailMessage
+from options.utils import use_team_code_names
 from tournaments.utils import get_side_name
 
 logger = logging.getLogger(__name__)
@@ -221,19 +223,22 @@ def send_ballot_receipt_emails_to_adjudicators(ballots, debate):
     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="C").adjudicator
+            judge = debate.debateadjudicator_set.get(type=DebateAdjudicator.TYPE_CHAIR).adjudicator
 
         if judge.email is None:
             continue
 
-        scores = ''
+        scores = ""
         for team in ballot['teams']:
-            scores += _("(%(side)s) %(team)s\n") % {'side': team['side'], 'team': team['team'].short_name}
+
+            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']}


Test patch: