Exemplo n.º 1
0
def match(dataset, alias, random=False):
    dataset = Dataset.find(dataset)
    authz.require(authz.dataset_edit(dataset))
    alias = Alias.find(dataset, alias)
    random = random or request.args.get('random')=='True'
    choices = match_op(alias.name, dataset,
            query=request.args.get('query'))
    pager = Pager(choices, '.match',
        dataset=dataset.name, alias=alias.id,
        limit=10)

    # HACK: Fetch only the entities on the selected page.
    entities = Entity.id_map(dataset, map(lambda (c,e,s): e,
        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)

    html = render_template('alias/match.html',
            dataset=dataset, alias=alias, choices=pager,
            random=random)
    choice = 'INVALID' if alias.is_invalid else alias.entity_id
    if len(choices) and choice is None:
        c, e, s = choices[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})
Exemplo n.º 2
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])
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def import_upload(dataset_name, id, account_id,
                  entity_col, alias_col):
    dataset = Dataset.find(dataset_name)
    account = Account.by_id(account_id)
    metadata, row_set = parse_upload(dataset, id)
    headers = detect_headers(row_set)
    for row in row_set:
        data = dict([(c.column, c.value) for c in row])
        entity = data.pop(entity_col) if entity_col else None
        alias = data.pop(alias_col) if alias_col else None
        if alias_col and alias is not None and len(alias) and alias != entity:
            d = {'name': alias, 'data': data}
            alias_obj = Alias.lookup(dataset, d, account,
                                     match_entity=False)
            data = {}
        if entity_col and entity is not None and len(entity):
            d = {'name': entity, 'data': data}
            entity_obj = Entity.by_name(dataset, entity)
            if entity_obj is None:
                entity_obj = Entity.create(dataset, d, account)
            entity_obj.data = data
        if alias_col and entity_col:
            alias_obj.match(dataset, {'choice': entity_obj.id}, account)
    db.session.commit()
    flush_cache(dataset)
Exemplo n.º 6
0
def index(dataset, format='json'):
    dataset = Dataset.find(dataset)
    q = Alias.all(dataset, eager=True)
    if format == 'csv':
        fn = csv_filename(dataset, 'aliases')
        headers = {'Content-Disposition': 'attachment; filename=' + fn}
        return csvify(q, headers=headers)
    return jsonify(q)
Exemplo n.º 7
0
def index(dataset, format='json'):
    dataset = Dataset.find(dataset)
    q = Alias.all(dataset, eager=True)
    if format == 'csv':
        fn = csv_filename(dataset, 'aliases')
        headers = {
            'Content-Disposition': 'attachment; filename=' + fn
        }
        return csvify(q, headers=headers)
    return jsonify(q)
Exemplo n.º 8
0
def match_random(dataset):
    dataset = Dataset.find(dataset)
    authz.require(authz.dataset_edit(dataset))
    aliases = Alias.all_unmatched(dataset)
    count = aliases.count()
    if count == 0:
        return redirect(url_for('dataset.view', dataset=dataset.name))
    alias = aliases.offset(randint(0, count - 1)).first()
    return redirect(
        url_for('.match', dataset=dataset.name, alias=alias.id, random=True))
Exemplo n.º 9
0
def match_random(dataset):
    dataset = Dataset.find(dataset)
    authz.require(authz.dataset_edit(dataset))
    aliases = Alias.all_unmatched(dataset)
    count = aliases.count()
    if count == 0:
        return redirect(url_for('dataset.view',
            dataset=dataset.name))
    alias = aliases.offset(randint(0, count-1)).first()
    return redirect(url_for('.match', dataset=dataset.name, alias=alias.id,
                            random=True))
Exemplo n.º 10
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)
Exemplo n.º 11
0
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])
Exemplo n.º 12
0
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])
Exemplo n.º 13
0
def match(dataset, alias, random=False):
    dataset = Dataset.find(dataset)
    authz.require(authz.dataset_edit(dataset))
    alias = Alias.find(dataset, alias)
    random = random or request.args.get('random') == 'True'
    choices = match_op(alias.name, dataset, query=request.args.get('query'))
    pager = Pager(choices,
                  '.match',
                  dataset=dataset.name,
                  alias=alias.id,
                  limit=10)

    # HACK: Fetch only the entities on the selected page.
    entities = Entity.id_map(
        dataset,
        map(lambda (c, e, s): e,
            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)

    html = render_template('alias/match.html',
                           dataset=dataset,
                           alias=alias,
                           choices=pager,
                           random=random)
    choice = 'INVALID' if alias.is_invalid else alias.entity_id
    if len(choices) and choice is None:
        c, e, s = choices[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
                           })
Exemplo n.º 14
0
def import_upload(dataset_name, sig, account_id, entity_col, alias_col):
    dataset = Dataset.find(dataset_name)
    account = Account.by_id(account_id)
    metadata, row_set = parse_upload(dataset, sig)
    headers = detect_headers(row_set)
    for row in row_set:
        data = dict([(c.column, c.value) for c in row])
        entity = data.pop(entity_col) if entity_col else None
        alias = data.pop(alias_col) if alias_col else None
        if alias_col and alias is not None and len(alias) and alias != entity:
            d = {'name': alias, 'data': data}
            alias_obj = Alias.lookup(dataset, d, account, match_entity=False)
            data = {}
        if entity_col and entity is not None and len(entity):
            d = {'name': entity, 'data': data}
            entity_obj = Entity.by_name(dataset, entity)
            if entity_obj is None:
                entity_obj = Entity.create(dataset, d, account)
            entity_obj.data = data
        if alias_col and entity_col:
            alias_obj.match(dataset, {'choice': entity_obj.id}, account)
    db.session.commit()
    flush_cache()
Exemplo n.º 15
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])
Exemplo n.º 16
0
def view_by_name(dataset):
    dataset = Dataset.find(dataset)
    alias = Alias.by_name(dataset, request.args.get('name'))
    if alias is None:
        raise NotFound("No such alias: %s" % request.args.get('name'))
    return view(dataset.name, alias.id)
Exemplo n.º 17
0
def view_by_name(dataset):
    dataset = Dataset.find(dataset)
    alias = Alias.by_name(dataset, request.args.get('name'))
    if alias is None:
        raise NotFound("No such alias: %s" % request.args.get('name'))
    return view(dataset.name, alias.id)
Exemplo n.º 18
0
def view(dataset, alias):
    dataset = Dataset.find(dataset)
    alias = Alias.find(dataset, alias)
    #format = response_format()
    #if format == 'json':
    return jsonify(alias)
Exemplo n.º 19
0
def view(dataset, alias):
    dataset = Dataset.find(dataset)
    alias = Alias.find(dataset, alias)
    #format = response_format()
    #if format == 'json':
    return jsonify(alias)