nodes: 0, module; 1, function_definition; 2, function_name:check_for_territories; 3, parameters; 4, block; 5, identifier:query; 6, expression_statement; 7, if_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, for_statement; 13, if_statement; 14, comment:# Sort matching results by population and area.; 15, return_statement; 16, comment:"""
      Return a geozone queryset of territories given the `query`.
  
      Results are sorted by population and area (biggest first).
      """; 17, boolean_operator; 18, block; 19, assignment; 20, assignment; 21, assignment; 22, assignment; 23, identifier:level; 24, call; 25, block; 26, attribute; 27, block; 28, call; 29, not_operator; 30, not_operator; 31, return_statement; 32, identifier:dbqs; 33, call; 34, identifier:query; 35, call; 36, identifier:is_digit; 37, call; 38, identifier:query_length; 39, call; 40, attribute; 41, argument_list; 42, if_statement; 43, expression_statement; 44, if_statement; 45, comment:# Meta Q object, ready to be passed to a queryset.; 46, expression_statement; 47, identifier:dbqs; 48, identifier:empty; 49, return_statement; 50, attribute; 51, argument_list; 52, identifier:query; 53, call; 54, list; 55, attribute; 56, argument_list; 57, attribute; 58, argument_list; 59, attribute; 60, argument_list; 61, identifier:len; 62, argument_list; 63, attribute; 64, identifier:get; 65, string; 66, comparison_operator:level == 'country'; 67, block; 68, assignment; 69, parenthesized_expression; 70, comment:# Counties + Corsica.; 71, block; 72, elif_clause; 73, elif_clause; 74, elif_clause; 75, else_clause; 76, augmented_assignment; 77, list; 78, call; 79, identifier:order_by; 80, string; 81, string; 82, attribute; 83, argument_list; 84, identifier:db; 85, identifier:Q; 86, identifier:query; 87, identifier:lower; 88, identifier:query; 89, identifier:isdigit; 90, identifier:query; 91, identifier:current_app; 92, identifier:config; 93, string_content:HANDLED_LEVELS; 94, identifier:level; 95, string; 96, continue_statement; 97, comment:# Level not fully handled yet.; 98, identifier:q; 99, call; 100, boolean_operator; 101, expression_statement; 102, boolean_operator; 103, comment:# French DROM-COM.; 104, block; 105, boolean_operator; 106, comment:# INSEE code then postal codes with Corsica exceptions.; 107, block; 108, comparison_operator:query_length >= 4; 109, comment:# Check names starting with query or exact match.; 110, block; 111, block; 112, identifier:dbqs; 113, identifier:q; 114, attribute; 115, argument_list; 116, string_content:-population; 117, string_content:-area; 118, attribute; 119, identifier:get; 120, string; 121, string_content:country; 122, attribute; 123, argument_list; 124, boolean_operator; 125, parenthesized_expression; 126, augmented_assignment; 127, boolean_operator; 128, identifier:is_digit; 129, expression_statement; 130, boolean_operator; 131, parenthesized_expression; 132, expression_statement; 133, identifier:query_length; 134, integer:4; 135, expression_statement; 136, continue_statement; 137, identifier:GeoZone; 138, identifier:objects; 139, identifier:dbqs; 140, identifier:current_app; 141, identifier:config; 142, string_content:ACTIVATE_TERRITORIES; 143, identifier:db; 144, identifier:Q; 145, keyword_argument; 146, comparison_operator:query_length == 2; 147, comparison_operator:level == 'fr:departement'; 148, boolean_operator; 149, identifier:q; 150, call; 151, comparison_operator:query_length == 3; 152, comparison_operator:level == 'fr:departement'; 153, augmented_assignment; 154, comparison_operator:query_length == 5; 155, comparison_operator:level == 'fr:commune'; 156, boolean_operator; 157, augmented_assignment; 158, augmented_assignment; 159, identifier:level; 160, identifier:level; 161, identifier:query_length; 162, integer:2; 163, identifier:level; 164, string; 165, identifier:is_digit; 166, comparison_operator:query in ('2a', '2b'); 167, attribute; 168, argument_list; 169, identifier:query_length; 170, integer:3; 171, identifier:level; 172, string; 173, identifier:q; 174, call; 175, identifier:query_length; 176, integer:5; 177, identifier:level; 178, string; 179, boolean_operator; 180, call; 181, identifier:q; 182, binary_operator:db.Q(code=query) | db.Q(keys__postal__contains=query); 183, identifier:q; 184, binary_operator:db.Q(name__istartswith=query) | db.Q(name__iexact=query); 185, string_content:fr:departement; 186, identifier:query; 187, tuple; 188, identifier:db; 189, identifier:Q; 190, keyword_argument; 191, string_content:fr:departement; 192, attribute; 193, argument_list; 194, string_content:fr:commune; 195, identifier:is_digit; 196, call; 197, attribute; 198, argument_list; 199, call; 200, call; 201, call; 202, call; 203, string; 204, string; 205, identifier:code; 206, identifier:query; 207, identifier:db; 208, identifier:Q; 209, keyword_argument; 210, attribute; 211, argument_list; 212, identifier:query; 213, identifier:startswith; 214, string; 215, attribute; 216, argument_list; 217, attribute; 218, argument_list; 219, attribute; 220, argument_list; 221, attribute; 222, argument_list; 223, string_content:2a; 224, string_content:2b; 225, identifier:code; 226, identifier:query; 227, identifier:query; 228, identifier:startswith; 229, string; 230, string_content:2b; 231, identifier:db; 232, identifier:Q; 233, keyword_argument; 234, identifier:db; 235, identifier:Q; 236, keyword_argument; 237, identifier:db; 238, identifier:Q; 239, keyword_argument; 240, identifier:db; 241, identifier:Q; 242, keyword_argument; 243, string_content:2a; 244, identifier:code; 245, identifier:query; 246, identifier:keys__postal__contains; 247, identifier:query; 248, identifier:name__istartswith; 249, identifier:query; 250, identifier:name__iexact; 251, identifier:query
code: def check_for_territories(query):
      """
      Return a geozone queryset of territories given the `query`.
  
      Results are sorted by population and area (biggest first).
      """
      if not query or not current_app.config.get('ACTIVATE_TERRITORIES'):
          return []
  
      dbqs = db.Q()
      query = query.lower()
      is_digit = query.isdigit()
      query_length = len(query)
      for level in current_app.config.get('HANDLED_LEVELS'):
          if level == 'country':
              continue  # Level not fully handled yet.
          q = db.Q(level=level)
          if (query_length == 2 and level == 'fr:departement' and
                  (is_digit or query in ('2a', '2b'))):
              # Counties + Corsica.
              q &= db.Q(code=query)
          elif query_length == 3 and level == 'fr:departement' and is_digit:
              # French DROM-COM.
              q &= db.Q(code=query)
          elif query_length == 5 and level == 'fr:commune' and (
                  is_digit or query.startswith('2a') or query.startswith('2b')):
              # INSEE code then postal codes with Corsica exceptions.
              q &= db.Q(code=query) | db.Q(keys__postal__contains=query)
          elif query_length >= 4:
              # Check names starting with query or exact match.
              q &= db.Q(name__istartswith=query) | db.Q(name__iexact=query)
          else:
              continue
  
          # Meta Q object, ready to be passed to a queryset.
          dbqs |= q
  
      if dbqs.empty:
          return []
  
      # Sort matching results by population and area.
      return GeoZone.objects(dbqs).order_by('-population', '-area')