nodes: 0, module; 1, function_definition; 2, function_name:get_ordered_entries; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, if_statement; 10, return_statement; 11, identifier:queryset; 12, False; 13, comment:"""
          Custom ordering. First we get the average views and rating for
          the categories's entries. Second we created a rank by multiplying
          both. Last, we sort categories by this rank from top to bottom.
  
          Example:
          - Cat_1
              - Entry_1 (500 Views, Rating 2)
              - Entry_2 (200 Views, Rating -4)
              - Entry_3 (100 Views, Rating 3)
          - Cat_2
              - Entry_1 (200 Views, Rating 7)
              - Entry_2 (50 Views, Rating 2)
  
          Result:
          Cat_1 has a rank by: 88.88 (avg. views: 266.66, avg. rating: 0.33)
          Cat_2 has a rank by: 562.5 (avg. views: 125, avg. rating: 4.5)
  
          Cat_2 will be displayed at the top. The algorithm is quality-oriented,
          as you can see.
  
          """; 14, identifier:queryset; 15, block; 16, else_clause; 17, attribute; 18, block; 19, attribute; 20, expression_statement; 21, block; 22, identifier:self; 23, identifier:queryset; 24, for_statement; 25, expression_statement; 26, identifier:self; 27, identifier:queryset; 28, assignment; 29, expression_statement; 30, identifier:category; 31, attribute; 32, block; 33, assignment; 34, attribute; 35, identifier:queryset; 36, assignment; 37, identifier:self; 38, identifier:queryset; 39, expression_statement; 40, if_statement; 41, attribute; 42, call; 43, identifier:self; 44, identifier:queryset; 45, attribute; 46, call; 47, assignment; 48, identifier:entries; 49, block; 50, else_clause; 51, identifier:self; 52, identifier:queryset; 53, identifier:sorted; 54, argument_list; 55, identifier:self; 56, identifier:queryset; 57, attribute; 58, argument_list; 59, identifier:entries; 60, call; 61, expression_statement; 62, expression_statement; 63, expression_statement; 64, expression_statement; 65, expression_statement; 66, expression_statement; 67, block; 68, attribute; 69, keyword_argument; 70, keyword_argument; 71, attribute; 72, identifier:all; 73, attribute; 74, argument_list; 75, assignment; 76, assignment; 77, assignment; 78, assignment; 79, assignment; 80, call; 81, expression_statement; 82, identifier:self; 83, identifier:queryset; 84, identifier:key; 85, lambda; 86, identifier:reverse; 87, True; 88, identifier:EntryCategory; 89, identifier:objects; 90, identifier:category; 91, identifier:get_entries; 92, identifier:amount_list; 93, list_comprehension; 94, identifier:rating_list; 95, list_comprehension; 96, identifier:views_per_entry; 97, binary_operator:fsum(amount_list) / len(amount_list); 98, identifier:rating_per_entry; 99, binary_operator:fsum(rating_list) / len(rating_list); 100, attribute; 101, binary_operator:views_per_entry * rating_per_entry; 102, attribute; 103, argument_list; 104, assignment; 105, lambda_parameters; 106, attribute; 107, attribute; 108, for_in_clause; 109, call; 110, for_in_clause; 111, call; 112, call; 113, call; 114, call; 115, identifier:category; 116, identifier:last_rank; 117, identifier:views_per_entry; 118, identifier:rating_per_entry; 119, identifier:category; 120, identifier:save; 121, attribute; 122, call; 123, identifier:c; 124, identifier:c; 125, identifier:last_rank; 126, identifier:e; 127, identifier:amount_of_views; 128, identifier:e; 129, identifier:entries; 130, attribute; 131, argument_list; 132, identifier:e; 133, identifier:entries; 134, identifier:fsum; 135, argument_list; 136, identifier:len; 137, argument_list; 138, identifier:fsum; 139, argument_list; 140, identifier:len; 141, argument_list; 142, identifier:self; 143, identifier:queryset; 144, attribute; 145, argument_list; 146, identifier:e; 147, identifier:rating; 148, identifier:amount_list; 149, identifier:amount_list; 150, identifier:rating_list; 151, identifier:rating_list; 152, attribute; 153, identifier:exclude; 154, keyword_argument; 155, identifier:self; 156, identifier:queryset; 157, identifier:pk; 158, attribute; 159, identifier:category; 160, identifier:pk
code: def get_ordered_entries(self, queryset=False):
          """
          Custom ordering. First we get the average views and rating for
          the categories's entries. Second we created a rank by multiplying
          both. Last, we sort categories by this rank from top to bottom.
  
          Example:
          - Cat_1
              - Entry_1 (500 Views, Rating 2)
              - Entry_2 (200 Views, Rating -4)
              - Entry_3 (100 Views, Rating 3)
          - Cat_2
              - Entry_1 (200 Views, Rating 7)
              - Entry_2 (50 Views, Rating 2)
  
          Result:
          Cat_1 has a rank by: 88.88 (avg. views: 266.66, avg. rating: 0.33)
          Cat_2 has a rank by: 562.5 (avg. views: 125, avg. rating: 4.5)
  
          Cat_2 will be displayed at the top. The algorithm is quality-oriented,
          as you can see.
  
          """
          if queryset:
              self.queryset = queryset
          else:
              self.queryset = EntryCategory.objects.all()
          if self.queryset:
              for category in self.queryset:
                  entries = category.get_entries()
                  if entries:
                      amount_list = [e.amount_of_views for e in entries]
                      rating_list = [e.rating() for e in entries]
                      views_per_entry = fsum(amount_list) / len(amount_list)
                      rating_per_entry = fsum(rating_list) / len(rating_list)
                      category.last_rank = views_per_entry * rating_per_entry
                      category.save()
                  else:
                      self.queryset = self.queryset.exclude(pk=category.pk)
              self.queryset = sorted(self.queryset, key=lambda c: c.last_rank,
                                     reverse=True)
          return self.queryset