X-Git-Url: http://git.veekun.com/zzz-pokedex.git/blobdiff_plain/706d0f3ea899d26403b412545d60877996e81994..71a6e529a593aed38d5a7898b50df5cdbaefc004:/pokedex/lookup.py diff --git a/pokedex/lookup.py b/pokedex/lookup.py index 115e9f0..f029b77 100644 --- a/pokedex/lookup.py +++ b/pokedex/lookup.py @@ -290,15 +290,17 @@ class PokedexLookup(object): Returns None for a bogus name. """ + # Table object if hasattr(name, '__tablename__'): return getattr(name, '__tablename__') - elif name in self.indexed_tables: - return name - elif name + 's' in self.indexed_tables: - return name + 's' - else: - # Bogus. Be nice and return dummy - return None + + # Table name + for table in self.indexed_tables.values(): + if name in (table.__tablename__, table.__singlename__): + return table.__tablename__ + + # Bogus. Be nice and return dummy + return None def _whoosh_records_to_results(self, records, exact=True): """Converts a list of whoosh's indexed records to LookupResult tuples @@ -387,13 +389,20 @@ class PokedexLookup(object): # Do different things depending what the query looks like # Note: Term objects do an exact match, so we don't have to worry about # a query parser tripping on weird characters in the input + try: + # Let Python try to convert to a number, so 0xff works + name_as_number = int(name, base=0) + except ValueError: + # Oh well + name_as_number = None + if '*' in name or '?' in name: exact_only = True query = whoosh.query.Wildcard(u'name', name) - elif rx_is_number.match(name): + elif name_as_number is not None: # Don't spell-check numbers! exact_only = True - query = whoosh.query.Term(u'row_id', name) + query = whoosh.query.Term(u'row_id', unicode(name_as_number)) else: # Not an integer query = whoosh.query.Term(u'name', name)