def view(dataset, value): dataset = Dataset.find(dataset) value = Value.find(dataset, value) format = response_format() if format == 'json': return jsonify(value) query = request.args.get('query', '').strip().lower() choices = match_op(value.value, dataset) choices = filter(lambda (c,v,s): v != value.id, choices) if len(query): choices = filter(lambda (c,v,s): query in Value.find(dataset,v).value.lower(), choices) # THIS is very inefficient - rather do this # differently pager = Pager(choices, '.view', dataset=dataset.name, value=value.id, limit=10) # HACK: Fetch only the values on the selected page. value_objs = Value.id_map(dataset, map(lambda (c,v,s): v, pager.query[pager.offset:pager.offset+pager.limit])) for i, (c,v,s) in enumerate(pager.query): if v in value_objs: pager.query[i] = (c, value_objs.get(v), s) return render_template('value/view.html', dataset=dataset, value=value, values=pager, query=query)
def view(dataset, entity): dataset = Dataset.find(dataset) entity = Entity.find(dataset, entity) print entity.data format = response_format() if format == 'json': return jsonify(entity) query = request.args.get('query', '').strip().lower() choices = match_op(entity.name, dataset) choices = filter(lambda (c,e,s): e != entity.id, choices) if len(query): choices = filter(lambda (c,e,s): query in Entity.find(dataset,e).name.lower(), choices) # THIS is very inefficient - rather do this # differently pager = Pager(choices, '.view', dataset=dataset.name, entity=entity.id, limit=10) # HACK: Fetch only the entities on the selected page. entities = Entity.id_map(dataset, map(lambda (c,v,s): v, pager.query[pager.offset:pager.offset+pager.limit])) for i, (c,e,s) in enumerate(pager.query): if e in entities: pager.query[i] = (c, entities.get(e), s) return render_template('entity/view.html', dataset=dataset, entity=entity, entities=pager, query=query)
def lookup(dataset): dataset = Dataset.find(dataset) readonly = validators.StringBool(if_empty=False, if_missing=False)\ .to_python(request.args.get('readonly')) readonly = readonly if authz.logged_in() else True data = request_content() if response_format() != 'json': return Response("Not implemented!", status=400) try: alias = Alias.lookup(dataset, data, request.account, readonly=readonly) if alias is None: return jsonify({ 'is_matched': False, 'entity': None, 'name': data.get('name'), 'dataset': dataset.name }, status=404) if isinstance(alias, Entity): return jsonify({ 'is_matched': True, 'entity': alias, 'name': data.get('name'), 'dataset': dataset.name }, status=200) db.session.commit() status = 200 if alias.is_matched else 404 status = 418 if alias.is_invalid else status return jsonify(alias, status=status) except Invalid, inv: return handle_invalid(inv, index, data=data, args=[dataset.name])
def view(dataset, link): dataset = Dataset.find(dataset) link = Link.find(dataset, link) format = response_format() if format == 'json': return jsonify(link) return "Not implemented!"
def handle_exceptions(exc): """ Re-format exceptions to JSON if accept requires that. """ format = response_format() if format == "json": body = {"status": exc.code, "name": exc.name, "message": exc.get_description(request.environ)} return jsonify(body, status=exc.code, headers=exc.get_headers(request.environ)) return exc
def handle_exceptions(exc): """ Re-format exceptions to JSON if accept requires that. """ format = response_format() if format == 'json': body = { 'status': exc.code, 'name': exc.name, 'message': exc.get_description(request.environ) } return jsonify(body, status=exc.code, headers=exc.get_headers(request.environ)) return exc
def view(dataset): dataset = Dataset.find(dataset) format = response_format() if format == 'json': return jsonify(dataset) unmatched = Alias.all_unmatched(dataset).count() entities = Entity.all(dataset, query=request.args.get('query')) pager = Pager(entities, '.view', dataset=dataset.name, limit=10) return render_template('dataset/view.html', entities=pager, num_entities=len(pager), num_aliases=Alias.all(dataset).count(), invalid=Alias.all_invalid(dataset).count(), query=request.args.get('query', ''), dataset=dataset, unmatched=unmatched)
def handle_invalid(exc, html_func, data=None, args=()): format = response_format() if format == 'json': body = {'status': 400, 'name': 'Invalid Data', 'message': unicode(exc), 'errors': exc.unpack_errors()} return jsonify(body, status=400) elif format == 'html': data = data if data is not None else request_content() content = htmlfill.render(html_func(*args), defaults=data, errors=exc.unpack_errors()) return Response(content, status=400, mimetype='text/html') return Response(repr(exc.unpack_errors()), status=400, mimetype='text/plain')
def view(dataset, value): dataset = Dataset.find(dataset) value = Value.find(dataset, value) format = response_format() if format == 'json': return jsonify(value) query = request.args.get('query', '').strip().lower() choices = match_op(value.value, dataset) choices = filter(lambda (c,v,s): v != value, choices) if len(query): choices = filter(lambda (c,v,s): query in v.value.lower(), choices) pager = Pager(choices, '.view', dataset=dataset.name, value=value.id, limit=10) return render_template('value/view.html', dataset=dataset, value=value, values=pager, query=query)
def view(dataset): dataset = Dataset.find(dataset) format = response_format() if format == "json": return jsonify(dataset) unmatched = Link.all_unmatched(dataset).count() values = Value.all(dataset, query=request.args.get("query")) pager = Pager(values, ".view", dataset=dataset.name, limit=10) return render_template( "dataset/view.html", values=pager, num_values=len(pager), num_links=Link.all(dataset).count(), invalid=Link.all_invalid(dataset).count(), query=request.args.get("query", ""), dataset=dataset, unmatched=unmatched, )
def view(dataset): dataset = Dataset.find(dataset) format = response_format() headers = { 'X-Dataset': dataset.name, 'Last-Modified': http_date(dataset.last_modified) } if format == 'json': return jsonify(dataset, headers=headers) unmatched = Alias.all_unmatched(dataset).count() entities = Entity.all(dataset, query=request.args.get('query')) pager = Pager(entities, '.view', dataset=dataset.name, limit=10) html = render_template('dataset/view.html', entities=pager, num_entities=len(pager), num_aliases=Alias.all(dataset).count(), invalid=Alias.all_invalid(dataset).count(), query=request.args.get('query', ''), dataset=dataset, unmatched=unmatched) return Response(html, headers=headers)
def lookup(dataset): dataset = Dataset.find(dataset) readonly = validators.StringBool(if_empty=False, if_missing=False)\ .to_python(request.args.get('readonly')) readonly = readonly if authz.logged_in() else True data = request_content() if response_format() != 'json': return Response("Not implemented!", status=400) try: link = Link.lookup(dataset, data, request.account, readonly=readonly) if link is None: return jsonify({ 'is_matched': False, 'value': None, 'key': data.get('key'), 'dataset': dataset.name }, status=404) if isinstance(link, Value): add_candidate_to_cache(dataset, data.get('key'), link.id) return jsonify({ 'is_matched': True, 'value': link, 'key': data.get('key'), 'dataset': dataset.name }, status=200) if link.value: add_candidate_to_cache(dataset, link.key, link.value.id) db.session.commit() status = 200 if link.is_matched else 404 status = 418 if link.is_invalid else status return jsonify(link, status=status) except Invalid, inv: return handle_invalid(inv, index, data=data, args=[dataset.name])
def lookup(dataset): dataset = Dataset.find(dataset) readonly = validators.StringBool(if_empty=False, if_missing=False)\ .to_python(request.args.get('readonly')) readonly = readonly if authz.logged_in() else True data = request_content() if response_format() != 'json': return Response("Not implemented!", status=400) try: alias = Alias.lookup(dataset, data, request.account, readonly=readonly) if alias is None: return jsonify( { 'is_matched': False, 'entity': None, 'name': data.get('name'), 'dataset': dataset.name }, status=404) if isinstance(alias, Entity): return jsonify( { 'is_matched': True, 'entity': alias, 'name': data.get('name'), 'dataset': dataset.name }, status=200) db.session.commit() status = 200 if alias.is_matched else 404 status = 418 if alias.is_invalid else status return jsonify(alias, status=status) except Invalid, inv: return handle_invalid(inv, index, data=data, args=[dataset.name])
def view(dataset, entity): dataset = Dataset.find(dataset) entity = Entity.find(dataset, entity) print entity.data format = response_format() if format == 'json': return jsonify(entity) query = request.args.get('query', '').strip().lower() choices = match_op(entity.name, dataset) choices = filter(lambda (c, e, s): e != entity.id, choices) if len(query): choices = filter( lambda (c, e, s): query in Entity.find(dataset, e).name.lower(), choices) # THIS is very inefficient - rather do this # differently pager = Pager(choices, '.view', dataset=dataset.name, entity=entity.id, limit=10) # HACK: Fetch only the entities on the selected page. entities = Entity.id_map( dataset, map(lambda (c, v, s): v, pager.query[pager.offset:pager.offset + pager.limit])) for i, (c, e, s) in enumerate(pager.query): if e in entities: pager.query[i] = (c, entities.get(e), s) return render_template('entity/view.html', dataset=dataset, entity=entity, entities=pager, query=query)
def index(dataset): dataset = Dataset.find(dataset) format = response_format() if format == 'json': return jsonify(Value.all(dataset)) return "Not implemented!"
choice = 'INVALID' if s <= 50 else e.id return htmlfill.render(html, force_defaults=False, defaults={'choice': choice, 'name': alias.name, 'query': request.args.get('query', ''), 'random': random}) @section.route('/<dataset>/aliases/<alias>/match', methods=['POST']) def match_save(dataset, alias): dataset = Dataset.find(dataset) authz.require(authz.dataset_edit(dataset)) alias = Alias.find(dataset, alias) random = request.form.get('random')=='True' data = request_content() try: alias.match(dataset, data, request.account) db.session.commit() except Invalid, inv: return handle_invalid(inv, match, data=data, args=[dataset.name, alias.id, random]) flash("Matched: %s" % alias.name, "success") format = response_format() if format == 'json': return jsonify(alias) if random: return match_random(dataset.name) else: return match(dataset.name, alias.id)
def index(): format = response_format() if format == 'json': return jsonify(Dataset.all()) return "Not implemented!"
'name': alias.name, 'query': request.args.get('query', ''), 'random': random }) @section.route('/<dataset>/aliases/<alias>/match', methods=['POST']) def match_save(dataset, alias): dataset = Dataset.find(dataset) authz.require(authz.dataset_edit(dataset)) alias = Alias.find(dataset, alias) random = request.form.get('random') == 'True' data = request_content() try: alias.match(dataset, data, request.account) db.session.commit() except Invalid, inv: return handle_invalid(inv, match, data=data, args=[dataset.name, alias.id, random]) flash("Matched: %s" % alias.name, "success") format = response_format() if format == 'json': return jsonify(alias) if random: return match_random(dataset.name) else: return match(dataset.name, alias.id)
def index(dataset): dataset = Dataset.find(dataset) format = response_format() if format == 'json': return jsonify(Link.all(dataset, eager=True)) return "Not implemented!"