nodes: 0, module; 1, function_definition; 2, function_name:get_teachers_sorted; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, for_statement; 11, expression_statement; 12, comment:# Hack to return QuerySet in given order; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, return_statement; 18, comment:"""Get teachers sorted by last name.
  
          This is used for the announcement request page.
  
          """; 19, assignment; 20, assignment; 21, identifier:t; 22, identifier:teachers; 23, block; 24, identifier:t; 25, identifier:teachers; 26, block; 27, call; 28, assignment; 29, assignment; 30, assignment; 31, assignment; 32, identifier:queryset; 33, identifier:teachers; 34, call; 35, identifier:teachers; 36, list_comprehension; 37, if_statement; 38, if_statement; 39, attribute; 40, argument_list; 41, identifier:id_list; 42, list_comprehension; 43, identifier:clauses; 44, call; 45, identifier:ordering; 46, binary_operator:'CASE %s END' % clauses; 47, identifier:queryset; 48, call; 49, attribute; 50, argument_list; 51, tuple; 52, for_in_clause; 53, boolean_operator; 54, block; 55, boolean_operator; 56, block; 57, identifier:teachers; 58, identifier:sort; 59, keyword_argument; 60, subscript; 61, for_in_clause; 62, attribute; 63, argument_list; 64, string; 65, identifier:clauses; 66, attribute; 67, argument_list; 68, identifier:self; 69, identifier:get_teachers; 70, attribute; 71, attribute; 72, attribute; 73, identifier:u; 74, identifier:teachers; 75, boolean_operator; 76, comparison_operator:t[2] is None; 77, expression_statement; 78, comparison_operator:t[0] is None; 79, comparison_operator:len(t[0]) <= 1; 80, expression_statement; 81, identifier:key; 82, lambda; 83, identifier:t; 84, integer:2; 85, identifier:t; 86, identifier:teachers; 87, string; 88, identifier:join; 89, list_comprehension; 90, string_content:CASE %s END; 91, call; 92, identifier:extra; 93, keyword_argument; 94, keyword_argument; 95, identifier:u; 96, identifier:last_name; 97, identifier:u; 98, identifier:first_name; 99, identifier:u; 100, identifier:id; 101, boolean_operator; 102, comparison_operator:t[1] is None; 103, subscript; 104, None; 105, call; 106, subscript; 107, None; 108, call; 109, integer:1; 110, call; 111, lambda_parameters; 112, tuple; 113, string_content:; 114, binary_operator:'WHEN id=%s THEN %s' % (pk, i); 115, for_in_clause; 116, attribute; 117, argument_list; 118, identifier:select; 119, dictionary; 120, identifier:order_by; 121, tuple; 122, comparison_operator:t is None; 123, comparison_operator:t[0] is None; 124, subscript; 125, None; 126, identifier:t; 127, integer:2; 128, attribute; 129, argument_list; 130, identifier:t; 131, integer:0; 132, identifier:len; 133, argument_list; 134, attribute; 135, argument_list; 136, identifier:u; 137, subscript; 138, subscript; 139, string; 140, tuple; 141, pattern_list; 142, call; 143, attribute; 144, identifier:filter; 145, keyword_argument; 146, pair; 147, string; 148, identifier:t; 149, None; 150, subscript; 151, None; 152, identifier:t; 153, integer:1; 154, identifier:teachers; 155, identifier:remove; 156, identifier:t; 157, subscript; 158, identifier:teachers; 159, identifier:remove; 160, identifier:t; 161, identifier:u; 162, integer:0; 163, identifier:u; 164, integer:1; 165, string_content:WHEN id=%s THEN %s; 166, identifier:pk; 167, identifier:i; 168, identifier:i; 169, identifier:pk; 170, identifier:enumerate; 171, argument_list; 172, identifier:User; 173, identifier:objects; 174, identifier:id__in; 175, identifier:id_list; 176, string; 177, identifier:ordering; 178, string_content:ordering; 179, identifier:t; 180, integer:0; 181, identifier:t; 182, integer:0; 183, identifier:id_list; 184, string_content:ordering
code: def get_teachers_sorted(self):
          """Get teachers sorted by last name.
  
          This is used for the announcement request page.
  
          """
          teachers = self.get_teachers()
          teachers = [(u.last_name, u.first_name, u.id) for u in teachers]
          for t in teachers:
              if t is None or t[0] is None or t[1] is None or t[2] is None:
                  teachers.remove(t)
          for t in teachers:
              if t[0] is None or len(t[0]) <= 1:
                  teachers.remove(t)
          teachers.sort(key=lambda u: (u[0], u[1]))
          # Hack to return QuerySet in given order
          id_list = [t[2] for t in teachers]
          clauses = ' '.join(['WHEN id=%s THEN %s' % (pk, i) for i, pk in enumerate(id_list)])
          ordering = 'CASE %s END' % clauses
          queryset = User.objects.filter(id__in=id_list).extra(select={'ordering': ordering}, order_by=('ordering',))
          return queryset