def item_search(name): """ Return JSON result for a specific search name is the request name. """ if request.is_xhr: cache_key = 'item:search:%s' % (name.lower().replace(" ", ""),) data = cache.get(cache_key) if data is None: items = Item.query.filter( Item.name.ilike('%' + name.lower() + '%'), ).order_by( Item.name.asc() ).all() data = [] for item in items: data.append({ 'id': item.id, 'name': item.name, 'icon': item.icon_32(), }) # cache for 7 day as it does not change that often cache.set(cache_key, json.dumps(data), 24 * 3600 * 7) else: data = json.loads(data) return jsonify(result=data) else: return 'Cannot call this page directly', 403
def item_search(name): """ Return JSON result for a specific search name is the request name. """ if request.is_xhr: cache_key = 'item:search:%s' % (name.lower().replace(" ", ""), ) data = cache.get(cache_key) if data is None: items = Item.query.filter( Item.name.ilike('%' + name.lower() + '%'), ).order_by( Item.name.asc()).all() data = [] for item in items: data.append({ 'id': item.id, 'name': item.name, 'icon': item.icon_32(), }) # cache for 7 day as it does not change that often cache.set(cache_key, json.dumps(data), 24 * 3600 * 7) else: data = json.loads(data) return jsonify(result=data) else: return 'Cannot call this page directly', 403
def get(self, key, default=None): cached = cache.get(_hash(key)) return cached if cached is not None else default
def blueprint_search(name): """ Return JSON result for a specific search name is the request name. """ if request.is_xhr: name_lower = name.lower() # prevent only % string, to avoid query the full database at once... if name_lower == '%' * len(name_lower): return jsonify(result=[]) cache_key = 'blueprint:search:%s' % (name_lower.replace(" ", ""),) data = cache.get(cache_key) if data is None: blueprints = Item.query.filter( Item.name.ilike('%' + name_lower + '%'), Item.max_production_limit.isnot(None) ).outerjoin( ActivityProduct, ( (Item.id == ActivityProduct.item_id) & ( (ActivityProduct.activity == Activity.INVENTION) | (ActivityProduct.activity == Activity.REACTIONS) ) ) ).options( db.contains_eager(Item.activity_products__eager) ).order_by( Item.name.asc() ).all() data = [] for bp in blueprints: invention = False reaction = False # we can't have invention AND reaction # at the same time as product. if bp.activity_products__eager: invention = ( bp.activity_products__eager[0].activity == Activity.INVENTION ) reaction = ( bp.activity_products__eager[0].activity == Activity.REACTIONS ) data.append({ 'id': bp.id, 'name': bp.name, 'invention': invention, 'reaction': reaction }) # cache for 7 day as it does not change that often cache.set(cache_key, json.dumps(data), 24 * 3600 * 7) else: data = json.loads(data) return jsonify(result=data) else: return 'Cannot call this page directly', 403