nodes: 0, module; 1, function_definition; 2, function_name:ordering_url; 3, parameters; 4, block; 5, identifier:self; 6, identifier:field_name; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, expression_statement; 15, comment:# copy the list; 16, for_statement; 17, expression_statement; 18, for_statement; 19, expression_statement; 20, return_statement; 21, comment:"""
          Creates a url link for sorting the given field.
  
          The direction of sorting will be either ascending, if the field is not
          yet sorted, or the opposite of the current sorting if sorted.
          """; 22, assignment; 23, assignment; 24, assignment; 25, assignment; 26, assignment; 27, not_operator; 28, block; 29, assignment; 30, identifier:ordering_field; 31, call; 32, block; 33, assignment; 34, identifier:item; 35, identifier:merged_ordering; 36, block; 37, assignment; 38, tuple; 39, identifier:path; 40, attribute; 41, identifier:direction; 42, string:""; 43, identifier:query_params; 44, call; 45, identifier:ordering; 46, call; 47, identifier:field; 48, call; 49, identifier:ordering; 50, expression_statement; 51, identifier:merged_ordering; 52, call; 53, attribute; 54, argument_list; 55, if_statement; 56, identifier:new_ordering; 57, list; 58, if_statement; 59, subscript; 60, call; 61, binary_operator:path + "?" + query_params.urlencode(safe=","); 62, identifier:direction; 63, attribute; 64, identifier:path; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, assignment; 72, identifier:list; 73, argument_list; 74, identifier:self; 75, identifier:get_ordering_fields_lookups; 76, boolean_operator; 77, block; 78, comparison_operator:item.lstrip("-") == field.lstrip("-"); 79, block; 80, identifier:query_params; 81, string:"order"; 82, attribute; 83, argument_list; 84, binary_operator:path + "?"; 85, call; 86, identifier:self; 87, identifier:request; 88, attribute; 89, identifier:copy; 90, call; 91, identifier:split; 92, string:","; 93, identifier:self; 94, identifier:_get_ordering_field_lookup; 95, identifier:field_name; 96, identifier:ordering; 97, call; 98, identifier:ordering; 99, parenthesized_expression; 100, parenthesized_expression; 101, expression_statement; 102, call; 103, call; 104, if_statement; 105, string:","; 106, identifier:join; 107, identifier:new_ordering; 108, identifier:path; 109, string:"?"; 110, attribute; 111, argument_list; 112, attribute; 113, identifier:GET; 114, attribute; 115, argument_list; 116, attribute; 117, argument_list; 118, comparison_operator:ordering_field.lstrip("-") not in ordering; 119, comparison_operator:("-" + ordering_field.lstrip("-")) not in ordering; 120, call; 121, attribute; 122, argument_list; 123, attribute; 124, argument_list; 125, boolean_operator; 126, block; 127, else_clause; 128, identifier:query_params; 129, identifier:urlencode; 130, keyword_argument; 131, identifier:self; 132, identifier:request; 133, attribute; 134, identifier:get; 135, string:"order"; 136, string:""; 137, identifier:self; 138, identifier:get_default_ordering; 139, call; 140, identifier:ordering; 141, parenthesized_expression; 142, identifier:ordering; 143, attribute; 144, argument_list; 145, identifier:item; 146, identifier:lstrip; 147, string:"-"; 148, identifier:field; 149, identifier:lstrip; 150, string:"-"; 151, parenthesized_expression; 152, not_operator; 153, if_statement; 154, expression_statement; 155, block; 156, identifier:safe; 157, string:","; 158, attribute; 159, identifier:GET; 160, attribute; 161, argument_list; 162, binary_operator:"-" + ordering_field.lstrip("-"); 163, identifier:merged_ordering; 164, identifier:append; 165, identifier:ordering_field; 166, comparison_operator:item[0] == "-"; 167, parenthesized_expression; 168, comparison_operator:item in ordering; 169, block; 170, call; 171, expression_statement; 172, expression_statement; 173, identifier:self; 174, identifier:request; 175, identifier:ordering_field; 176, identifier:lstrip; 177, string:"-"; 178, string:"-"; 179, call; 180, subscript; 181, string:"-"; 182, comparison_operator:item in ordering; 183, identifier:item; 184, identifier:ordering; 185, expression_statement; 186, attribute; 187, argument_list; 188, assignment; 189, call; 190, attribute; 191, argument_list; 192, identifier:item; 193, integer:0; 194, identifier:item; 195, identifier:ordering; 196, assignment; 197, identifier:new_ordering; 198, identifier:insert; 199, integer:0; 200, call; 201, identifier:direction; 202, string:"asc"; 203, attribute; 204, argument_list; 205, identifier:ordering_field; 206, identifier:lstrip; 207, string:"-"; 208, identifier:direction; 209, string:"desc"; 210, attribute; 211, argument_list; 212, identifier:new_ordering; 213, identifier:insert; 214, integer:0; 215, binary_operator:"-" + item; 216, identifier:item; 217, identifier:lstrip; 218, string:"-"; 219, string:"-"; 220, identifier:item
code: def ordering_url(self, field_name):
          """
          Creates a url link for sorting the given field.
  
          The direction of sorting will be either ascending, if the field is not
          yet sorted, or the opposite of the current sorting if sorted.
          """
          path = self.request.path
          direction = ""
          query_params = self.request.GET.copy()
          ordering = self.request.GET.get("order", "").split(",")
          field = self._get_ordering_field_lookup(field_name)
          if not ordering:
              ordering = self.get_default_ordering()
          merged_ordering = list(ordering)  # copy the list
  
          for ordering_field in self.get_ordering_fields_lookups():
              if (ordering_field.lstrip("-") not in ordering) and (
                  ("-" + ordering_field.lstrip("-")) not in ordering
              ):
                  merged_ordering.append(ordering_field)
  
          new_ordering = []
          for item in merged_ordering:
              if item.lstrip("-") == field.lstrip("-"):
                  if (item[0] == "-") or not (item in ordering):
                      if item in ordering:
                          direction = "desc"
                      new_ordering.insert(0, item.lstrip("-"))
                  else:
                      direction = "asc"
                      new_ordering.insert(0, "-" + item)
  
          query_params["order"] = ",".join(new_ordering)
  
          return (path + "?" + query_params.urlencode(safe=","), direction)