Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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])
Пример #4
0
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!"
Пример #5
0
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
Пример #6
0
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
Пример #7
0
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)
Пример #8
0
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')
Пример #9
0
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)
Пример #10
0
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)
Пример #11
0
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,
    )
Пример #12
0
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)
Пример #13
0
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])
Пример #14
0
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])
Пример #15
0
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)
Пример #16
0
def index(dataset):
    dataset = Dataset.find(dataset)
    format = response_format()
    if format == 'json':
        return jsonify(Value.all(dataset))
    return "Not implemented!"
Пример #17
0
        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)
Пример #18
0
def index():
    format = response_format()
    if format == 'json':
        return jsonify(Dataset.all())
    return "Not implemented!"
Пример #19
0
                               '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)
Пример #20
0
def index():
    format = response_format()
    if format == 'json':
        return jsonify(Dataset.all())
    return "Not implemented!"
Пример #21
0
def index(dataset):
    dataset = Dataset.find(dataset)
    format = response_format()
    if format == 'json':
        return jsonify(Link.all(dataset, eager=True))
    return "Not implemented!"