|
| 1 | +from cerberus import validator |
| 2 | + |
| 3 | +_VALID_POKEMON_LEVELS = [10 * x for x in range(10)] |
| 4 | + |
| 5 | + |
| 6 | +def _valid_level(field, value, error): |
| 7 | + if value not in _VALID_POKEMON_LEVELS: |
| 8 | + error(field, "Invalid Pokemon Level") |
| 9 | + |
| 10 | + |
| 11 | +_VALID_POKEMON_TYPES = 'ELECTRIC GROUND FIRE WATER WIND PSYCHIC GRASS'.split() |
| 12 | + |
| 13 | + |
| 14 | +def _valid_type(field, value, error): |
| 15 | + if value not in _VALID_POKEMON_TYPES: |
| 16 | + error(field, "Invalid Pokemon Type") |
| 17 | + |
| 18 | + |
| 19 | +_NEW_POKEMON_SCHEMA = {'pokadex_id': {'required': True, 'type': 'integer'}, |
| 20 | + 'name': {'required': True, 'type': 'string'}, |
| 21 | + 'nickname': {'required': True, 'type': 'string'}, |
| 22 | + 'level': {'required': True, 'check_with': _valid_level}, |
| 23 | + 'type': {'required': True, 'check_with': _valid_type}, |
| 24 | + 'skills': {'required': True, 'type': 'list', 'schema': {'type': 'string'}} |
| 25 | + } |
| 26 | + |
| 27 | +_POKEMON_INDEX_SCHEMA = { |
| 28 | + 'settings': { |
| 29 | + "analysis": { |
| 30 | + "filter": { |
| 31 | + "autocomplete_filter": { |
| 32 | + "type": "edge_ngram", |
| 33 | + "min_gram": 1, |
| 34 | + "max_gram": 20 |
| 35 | + } |
| 36 | + }, |
| 37 | + "analyzer": { |
| 38 | + "autocomplete": { |
| 39 | + "type": "custom", |
| 40 | + "tokenizer": "standard", |
| 41 | + "filter": [ |
| 42 | + "lowercase", |
| 43 | + "autocomplete_filter" |
| 44 | + ] |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + 'mappings': { |
| 50 | + 'properties': { |
| 51 | + 'pokadex_id': {'type': 'integer'}, |
| 52 | + 'name': {'type': 'completion', 'analyzer': 'autocomplete'}, |
| 53 | + 'nickname': {'type': 'completion', 'analyzer': 'autocomplete'}, |
| 54 | + 'level': {'type': 'integer'}, |
| 55 | + 'type': {'type': 'text'}, |
| 56 | + 'skills': {'type': 'text'} |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +def valid_new_pokemon_schema(dictionary): |
| 63 | + val = validator.Validator(_NEW_POKEMON_SCHEMA) |
| 64 | + return val.validate(dictionary) |
| 65 | + |
| 66 | + |
| 67 | +def valid_pokemon_dict_to_id_body(dictionary): |
| 68 | + pokemon_id = dictionary.get('pokadex_id') |
| 69 | + return int(pokemon_id), dictionary |
| 70 | + |
| 71 | + |
| 72 | +def generate_index(elastic_obj): |
| 73 | + if elastic_obj.indices.exists(index='pokemon'): |
| 74 | + elastic_obj.indices.delete(index='pokemon') |
| 75 | + elastic_obj.indices.create(index='pokemon', body=_POKEMON_INDEX_SCHEMA) |
0 commit comments