mirror of
https://github.com/veekun/pokedex.git
synced 2024-08-20 18:16:34 +00:00
Make user and code valid_types not interfere when one is language and the other is table.
This commit is contained in:
parent
6c004737ac
commit
3ea1174a31
2 changed files with 41 additions and 33 deletions
|
@ -294,38 +294,40 @@ class PokedexLookup(object):
|
||||||
# Merge the valid types together. Only types that appear in BOTH lists
|
# Merge the valid types together. Only types that appear in BOTH lists
|
||||||
# may be used.
|
# may be used.
|
||||||
# As a special case, if the user asked for types that are explicitly
|
# As a special case, if the user asked for types that are explicitly
|
||||||
# forbidden, completely ignore what the user requested
|
# forbidden, completely ignore what the user requested.
|
||||||
combined_valid_types = []
|
# And, just to complicate matters: "type" and language need to be
|
||||||
if user_valid_types and valid_types:
|
# considered separately.
|
||||||
combined_valid_types = list(
|
def merge_requirements(func):
|
||||||
set(user_valid_types) & set(combined_valid_types)
|
user = filter(func, user_valid_types)
|
||||||
)
|
system = filter(func, valid_types)
|
||||||
|
|
||||||
if not combined_valid_types:
|
if user and system:
|
||||||
# No overlap! Just use the enforced ones
|
merged = list(set(user) & set(system))
|
||||||
combined_valid_types = valid_types
|
if merged:
|
||||||
|
return merged
|
||||||
else:
|
else:
|
||||||
# One list or the other was blank, so just use the one that isn't
|
# No overlap; use the system restrictions
|
||||||
combined_valid_types = valid_types + user_valid_types
|
return system
|
||||||
|
else:
|
||||||
|
# One or the other is blank; use the one that's not
|
||||||
|
return user or system
|
||||||
|
|
||||||
if not combined_valid_types:
|
# @foo means language must be foo; otherwise it's a table name
|
||||||
# No restrictions
|
lang_requirements = merge_requirements(lambda req: req[0] == u'@')
|
||||||
return name, [], None
|
type_requirements = merge_requirements(lambda req: req[0] != u'@')
|
||||||
|
all_requirements = lang_requirements + type_requirements
|
||||||
|
|
||||||
# Construct the term
|
# Construct the term
|
||||||
type_terms = []
|
|
||||||
lang_terms = []
|
lang_terms = []
|
||||||
final_valid_types = []
|
for lang in lang_requirements:
|
||||||
for valid_type in combined_valid_types:
|
|
||||||
if valid_type.startswith(u'@'):
|
|
||||||
# @foo means: language must be foo.
|
|
||||||
# Allow for either country or language codes
|
# Allow for either country or language codes
|
||||||
lang_code = valid_type[1:]
|
lang_code = lang[1:]
|
||||||
lang_terms.append(whoosh.query.Term(u'iso639', lang_code))
|
lang_terms.append(whoosh.query.Term(u'iso639', lang_code))
|
||||||
lang_terms.append(whoosh.query.Term(u'iso3166', lang_code))
|
lang_terms.append(whoosh.query.Term(u'iso3166', lang_code))
|
||||||
else:
|
|
||||||
# otherwise, this is a type/table name
|
type_terms = []
|
||||||
table_name = self._parse_table_name(valid_type)
|
for type in type_requirements:
|
||||||
|
table_name = self._parse_table_name(type)
|
||||||
|
|
||||||
# Quietly ignore bogus valid_types; more likely to DTRT
|
# Quietly ignore bogus valid_types; more likely to DTRT
|
||||||
if table_name:
|
if table_name:
|
||||||
|
@ -338,7 +340,7 @@ class PokedexLookup(object):
|
||||||
if lang_terms:
|
if lang_terms:
|
||||||
all_terms.append(whoosh.query.Or(lang_terms))
|
all_terms.append(whoosh.query.Or(lang_terms))
|
||||||
|
|
||||||
return name, combined_valid_types, whoosh.query.And(all_terms)
|
return name, all_requirements, whoosh.query.And(all_terms)
|
||||||
|
|
||||||
|
|
||||||
def _parse_table_name(self, name):
|
def _parse_table_name(self, name):
|
||||||
|
|
|
@ -66,10 +66,12 @@ def test_type_lookup():
|
||||||
assert_equal(results[0].object.__tablename__, 'pokemon',
|
assert_equal(results[0].object.__tablename__, 'pokemon',
|
||||||
u'Type restriction works correctly')
|
u'Type restriction works correctly')
|
||||||
assert_equal(len(results), 1, u'Only one id result when type is specified')
|
assert_equal(len(results), 1, u'Only one id result when type is specified')
|
||||||
assert_equal(results[0].name, u'Bulbasaur', u'Type + id returns the right result')
|
assert_equal(results[0].object.name, u'Bulbasaur',
|
||||||
|
u'Type + id returns the right result')
|
||||||
|
|
||||||
results = lookup.lookup(u'1', valid_types=['pokemon'])
|
results = lookup.lookup(u'1', valid_types=['pokemon'])
|
||||||
assert_equal(results[0].name, u'Bulbasaur', u'valid_types works as well as type: prefix')
|
assert_equal(results[0].object.name, u'Bulbasaur',
|
||||||
|
u'valid_types works as well as type: prefix')
|
||||||
|
|
||||||
def test_language_lookup():
|
def test_language_lookup():
|
||||||
# There are two objects named "charge": the move Charge, and the move
|
# There are two objects named "charge": the move Charge, and the move
|
||||||
|
@ -91,6 +93,10 @@ def test_language_lookup():
|
||||||
assert_equal(results[0].object.name, u'Tackle',
|
assert_equal(results[0].object.name, u'Tackle',
|
||||||
u'Languages and types both work together')
|
u'Languages and types both work together')
|
||||||
|
|
||||||
|
results = lookup.lookup(u'@fr:charge', valid_types=['move'])
|
||||||
|
assert_equal(results[0].object.name, u'Tackle',
|
||||||
|
u'valid_types and language prefixes get along')
|
||||||
|
|
||||||
def test_fuzzy_lookup():
|
def test_fuzzy_lookup():
|
||||||
tests = [
|
tests = [
|
||||||
# Regular English names
|
# Regular English names
|
||||||
|
|
Loading…
Reference in a new issue