def item(index, item): "Get information for an item" try: document = get_item(index, item) if request.args.get('update', False): update_item(index, item, document) link_types = get_index(index).link_types() for ( link_type, links ) in document['_source']['_geordi']['links']['links'].iteritems(): if link_type != 'version': for link in links: subitem = "{}-{}".format( link_type, link[link_types[link_type]['key']]) get_subitem(index, subitem, create=True, seed=copy.deepcopy(link)) response = Response(json.dumps({ 'code': 200, 'document': document }), 200, mimetype="application/json") except ElasticHttpNotFoundError: response = Response(json.dumps({ 'code': 404, 'error': 'The provided item could not be found.' }), 404, mimetype="application/json") response.headers.add('Access-Control-Allow-Origin', '*') return response
def document(index_name, item): if request.args.get('import', False): template = 'import.html' else: template = 'document.html' try: data = get_item(index_name, item) index = get_index(index_name) mapoptions = get_mapoptions(data['_source']['_geordi']['mapping']) subitems = {} link_types = index.link_types() code_url = index.code_url() matching_enabled = index.matching_enabled() for (link_type, links ) in data['_source']['_geordi']['links']['links'].iteritems(): if link_type != 'version': for link in links: subitem = "{}-{}".format( link_type, link[link_types[link_type]['key']]) subitems[subitem] = get_subitem(index_name, subitem, create=True, seed=copy.deepcopy(link)) return render_template(template, item=item, index=index_name, data=data, mapping=data['_source']['_geordi']['mapping'], mapoptions=mapoptions, subitems=subitems, code_url=code_url, matching_enabled=matching_enabled) except ElasticHttpNotFoundError: return render_template('notfound.html')
def matchsubitem(index, subitem): "Submit a match for this subitem" if get_index(index).matching_enabled(): if current_user.is_authenticated(): auto = False user = None if request.form.get('unmatch', False): return register_match(index, subitem, 'subitem', 'unmatch', [], auto, user) else: auto = True user = request.form.get('user') matchtype = request.form.get('type', 'artist') mbids = request.form.getlist('mbid') if not mbids: mbids = re.split(',\s*', request.form.get('mbids')) return register_match(index, subitem, 'subitem', matchtype, mbids, auto, user) else: return Response(json.dumps({ 'code': 400, 'error': 'Matching is not enabled for this index' }), 400, mimetype="application/json")
def do_subitem_search(query_string, index, subtype, start_from=None, filters=None, size=None): link_types = get_index(index).link_types() key = link_types[subtype]['key'] query_field = '_geordi.links.links.{subtype}.{key}'.format(subtype=subtype, key=key) query = {'query': {'match': {query_field: query_string}}} return do_search_raw(query, [index], start_from, filters, 'item', size=size)
def before_request(): g.all_indices = app.config['AVAILABLE_INDICES'] g.link_types = dict([(index, get_index(index).link_types()) for index in app.config['AVAILABLE_INDICES']]) g.json = json g.re = re g.quote = quote g.comma_list = comma_list g.comma_only_list = comma_only_list g.dictarray = dictarray
def get_search_params(): "Shared search functionality" search_type = request.args.get('type', 'query') start_from = request.args.get('from', "0") query = request.args.get('query') indices = request.args.getlist('index') itemtypes = request.args.getlist('itemtype') size = request.args.get('size', None) if size is not None: size = int(size) if size is not None and size == 10: size = None if len(itemtypes) == 0: itemtypes = ['item'] filters = make_filters(human=request.args.get('human', False), auto=request.args.get('auto', False), un=request.args.get('un', False)) if search_type == 'raw': json_query = json.loads(query) if size is None and 'size' in json_query: try: size = int(json_query['size']) except: pass try: data = do_search_raw(json_query, indices, start_from=request.args.get('from', None), filters=filters, doc_type=itemtypes, size=size) except ValueError: return {'error': "Malformed or missing JSON."} elif search_type == 'query': if query: data = do_search(query, indices, start_from=request.args.get('from', None), filters=filters, doc_type=itemtypes, size=size) else: return {'error': 'You must provide a query.'} elif search_type == 'sub': index = request.args.get('subitem_index') subtype = request.args.get('subitem_type') if subtype not in get_index(index).link_types().keys(): return {'error': 'Invalid subitem type for index {}'.format(index)} data = do_subitem_search(query, index, subtype, start_from=request.args.get('from', None), filters=filters, size=size) else: return {'error': 'Search type {} unimplemented.'.format(search_type)} mapping = map_search_data(data) return {"start_from": start_from, "query": query, "mapping": mapping, "data": data, "page_size": size}
def item(index, item): "Get information for an item" try: document = get_item(index, item) if request.args.get('update', False): update_item(index, item, document) link_types = get_index(index).link_types() for (link_type, links) in document['_source']['_geordi']['links']['links'].iteritems(): if link_type != 'version': for link in links: subitem = "{}-{}".format(link_type, link[link_types[link_type]['key']]) get_subitem(index, subitem, create=True, seed=copy.deepcopy(link)) response = Response(json.dumps({'code': 200, 'document': document}), 200, mimetype="application/json") except ElasticHttpNotFoundError: response = Response(json.dumps({'code': 404, 'error': 'The provided item could not be found.'}), 404, mimetype="application/json") response.headers.add('Access-Control-Allow-Origin', '*') return response
def matchsubitem(index, subitem): "Submit a match for this subitem" if get_index(index).matching_enabled(): if current_user.is_authenticated(): auto = False user = None if request.form.get('unmatch', False): return register_match(index, subitem, 'subitem', 'unmatch', [], auto, user) else: auto = True user = request.form.get('user') matchtype = request.form.get('type', 'artist') mbids = request.form.getlist('mbid') if not mbids: mbids = re.split(',\s*', request.form.get('mbids')) return register_match(index, subitem, 'subitem', matchtype, mbids, auto, user) else: return Response(json.dumps({'code': 400, 'error': 'Matching is not enabled for this index'}), 400, mimetype="application/json")
def document(index_name, item): if request.args.get('import', False): template = 'import.html' else: template = 'document.html' try: data = get_item(index_name, item) index = get_index(index_name) mapoptions = get_mapoptions(data['_source']['_geordi']['mapping']) subitems = {} link_types = index.link_types() code_url = index.code_url() matching_enabled = index.matching_enabled() for (link_type, links) in data['_source']['_geordi']['links']['links'].iteritems(): if link_type != 'version': for link in links: subitem = "{}-{}".format(link_type, link[link_types[link_type]['key']]) subitems[subitem] = get_subitem(index_name, subitem, create=True, seed=copy.deepcopy(link)) return render_template(template, item=item, index=index_name, data=data, mapping=data['_source']['_geordi']['mapping'], mapoptions=mapoptions, subitems=subitems, code_url=code_url, matching_enabled=matching_enabled) except ElasticHttpNotFoundError: return render_template('notfound.html')
def get_search_params(): "Shared search functionality" search_type = request.args.get('type', 'query') start_from = request.args.get('from', "0") query = request.args.get('query') indices = request.args.getlist('index') itemtypes = request.args.getlist('itemtype') size = request.args.get('size', None) if size is not None: size = int(size) if size is not None and size == 10: size = None if len(itemtypes) == 0: itemtypes = ['item'] filters = make_filters(human=request.args.get('human', False), auto=request.args.get('auto', False), un=request.args.get('un', False)) if search_type == 'raw': json_query = json.loads(query) if size is None and 'size' in json_query: try: size = int(json_query['size']) except: pass try: data = do_search_raw(json_query, indices, start_from=request.args.get('from', None), filters=filters, doc_type=itemtypes, size=size) except ValueError: return {'error': "Malformed or missing JSON."} elif search_type == 'query': if query: data = do_search(query, indices, start_from=request.args.get('from', None), filters=filters, doc_type=itemtypes, size=size) else: return {'error': 'You must provide a query.'} elif search_type == 'sub': index = request.args.get('subitem_index') subtype = request.args.get('subitem_type') if subtype not in get_index(index).link_types().keys(): return {'error': 'Invalid subitem type for index {}'.format(index)} data = do_subitem_search(query, index, subtype, start_from=request.args.get('from', None), filters=filters, size=size) else: return {'error': 'Search type {} unimplemented.'.format(search_type)} mapping = map_search_data(data) return { "start_from": start_from, "query": query, "mapping": mapping, "data": data, "page_size": size }