예제 #1
0
def user_index(id):
    require.user_id(id)

    q = Entity.all().filter(Entity.user_id == id)
    if 'category' in request.args:
        q = q.filter(Entity.category == request.args.get('category'))
    all_entities = [{"term": {"entities": e.id}} for e in q]

    esq = {
        "query": {
            "filtered": {
                "query": {"match_all": {}}, "filter": {}
            }
        },
        "size": 0,
        "facets": {
            "entities": {
                "terms": {"field": "entities"}
            },
            "global": {
                "terms": {"field": "entities"},
                "global": True,
                "facet_filter": {"or": all_entities}
            }
        }
    }

    filters = request.args.getlist('entity')
    if len(filters):
        esq['query']['filtered']['filter']['and'] = []
        for entity_id in filters:
            fq = {"term": {"entities": entity_id}}
            esq['query']['filtered']['filter']['and'].append(fq)
    else:
        esq['query']['filtered']['filter']['or'] = all_entities

    res = elastic.search_raw(esq, elastic_index, 'frame')
    filtered_counts = res['facets']['entities']['terms']
    filtered_counts = dict([(int(c['term']), c['count']) for c in filtered_counts])
    total_counts = res['facets']['global']['terms']
    total_counts = dict([(int(c['term']), c['count']) for c in total_counts])

    q = Entity.all().filter(Entity.user_id == id)
    if 'category' in request.args:
        q = q.filter(Entity.category == request.args.get('category'))

    def transform_result(entity):
        data = entity.to_ref()
        data['filtered_count'] = filtered_counts.get(entity.id, 0)
        data['total_count'] = total_counts.get(entity.id, 0)
        return data

    return query_pager(q, 'entities.user_index', transform=transform_result, id=id)
예제 #2
0
파일: entities.py 프로젝트: arc64/datawi.re
def create():
    data = EntityForm().deserialize(request_data())
    authz.require(data['collection'])
    authz.require(authz.collection_write(data['collection'].id))
    entity = Entity.create(data, current_user)
    db.session.commit()
    return view(entity.id)
예제 #3
0
def create():
    data = EntityForm().deserialize(request_data())
    authz.require(data['collection'])
    authz.require(authz.collection_write(data['collection'].id))
    entity = Entity.create(data, current_user)
    db.session.commit()
    return view(entity.id)
예제 #4
0
파일: entities.py 프로젝트: e6/datawi.re
def delete(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    publish(entity_queue, "entity.delete", entity)
    entity.delete()
    db.session.commit()
    return jsonify({"status": "gone"}, status=410)
예제 #5
0
def delete(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    publish(entity_queue, 'entity.delete', entity)
    entity.delete()
    db.session.commit()
    return jsonify({'status': 'gone'}, status=410)
예제 #6
0
def handle_entity(body, message):
    from datawire.processing.matching import backsearch
    queue, operation = message.delivery_info.get('routing_key').split('.')
    log.info('%s - %s', queue, operation)
    if operation == 'create':
        entity = Entity.by_id(body.get('id'))
        backsearch(entity)
예제 #7
0
def update(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    data = {'old': entity.to_dict()}
    entity.update(request.form)
    db.session.commit()
    data['new'] = entity
    publish(entity_queue, 'entity.update', data)
    return jsonify(entity)
예제 #8
0
파일: entities.py 프로젝트: arc64/datawi.re
def update(id):
    entity = obj_or_404(Entity.by_id(id))
    authz.require(authz.collection_write(entity.collection_id))
    data = EntityForm().deserialize(request_data())
    authz.require(data['list'])
    authz.require(authz.collection_write(data['list'].id))
    entity.update(data)
    db.session.commit()
    return view(entity.id)
예제 #9
0
파일: entities.py 프로젝트: e6/datawi.re
def update(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    data = {"old": entity.to_dict()}
    entity.update(request.form)
    db.session.commit()
    data["new"] = entity
    publish(entity_queue, "entity.update", data)
    return jsonify(entity)
예제 #10
0
def update(id):
    entity = obj_or_404(Entity.by_id(id))
    authz.require(authz.collection_write(entity.collection_id))
    data = EntityForm().deserialize(request_data())
    authz.require(data['list'])
    authz.require(authz.collection_write(data['list'].id))
    entity.update(data)
    db.session.commit()
    return view(entity.id)
예제 #11
0
파일: entities.py 프로젝트: e6/datawi.re
def user_index(id):
    require.user_id(id)

    q = Entity.all().filter(Entity.user_id == id)
    if "category" in request.args:
        q = q.filter(Entity.category == request.args.get("category"))
    all_entities = [{"term": {"entities": e.id}} for e in q]

    esq = {
        "query": {"filtered": {"query": {"match_all": {}}, "filter": {}}},
        "size": 0,
        "facets": {
            "entities": {"terms": {"field": "entities"}},
            "global": {"terms": {"field": "entities"}, "global": True, "facet_filter": {"or": all_entities}},
        },
    }

    filters = request.args.getlist("entity")
    if len(filters):
        esq["query"]["filtered"]["filter"]["and"] = []
        for entity_id in filters:
            fq = {"term": {"entities": entity_id}}
            esq["query"]["filtered"]["filter"]["and"].append(fq)
    else:
        esq["query"]["filtered"]["filter"]["or"] = all_entities

    res = elastic.search_raw(esq, elastic_index, "frame")
    filtered_counts = res["facets"]["entities"]["terms"]
    filtered_counts = dict([(int(c["term"]), c["count"]) for c in filtered_counts])
    total_counts = res["facets"]["global"]["terms"]
    total_counts = dict([(int(c["term"]), c["count"]) for c in total_counts])

    q = Entity.all().filter(Entity.user_id == id)
    if "category" in request.args:
        q = q.filter(Entity.category == request.args.get("category"))

    def transform_result(entity):
        data = entity.to_ref()
        data["filtered_count"] = filtered_counts.get(entity.id, 0)
        data["total_count"] = total_counts.get(entity.id, 0)
        return data

    return query_pager(q, "entities.user_index", transform=transform_result, id=id)
예제 #12
0
파일: entities.py 프로젝트: arc64/datawi.re
def index():
    collection_ids = Collection.user_ids(current_user)
    filter_collections = request.args.getlist('collection')
    if len(filter_collections):
        try:
            collection_ids = [l for l in collection_ids if l
                              in filter_collections]
        except ValueError:
            raise BadRequest()

    prefix = request.args.get('prefix')
    q = Entity.by_collection(collection_ids, prefix=prefix)
    return jsonify(Pager(q))
예제 #13
0
def index():
    collection_ids = Collection.user_ids(current_user)
    filter_collections = request.args.getlist('collection')
    if len(filter_collections):
        try:
            collection_ids = [
                l for l in collection_ids if l in filter_collections
            ]
        except ValueError:
            raise BadRequest()

    prefix = request.args.get('prefix')
    q = Entity.by_collection(collection_ids, prefix=prefix)
    return jsonify(Pager(q))
예제 #14
0
def user_index(id):
    require.user_id(id)

    esq = {
        "query": {
            "filtered": {
                "query": {"match_all": {}}, "filter": {}
            }
        },
        "sort": [{"action_at": {"order": "desc"}}],
        "size": get_limit(),
        "from": get_offset(),
        "facets": {"entities": {
            "terms": {"field": "entities"}}
        }
    }

    filters = request.args.getlist('entity')
    if len(filters):
        esq['query']['filtered']['filter']['and'] = []
        for entity_id in filters:
            fq = {"term": {"entities": entity_id}}
            esq['query']['filtered']['filter']['and'].append(fq)
    else:
        esq['query']['filtered']['filter']['or'] = []
        for entity in Entity.all().filter(Entity.user_id == id):
            fq = {"term": {"entities": entity.id}}
            esq['query']['filtered']['filter']['or'].append(fq)

    res = elastic.search_raw(esq, elastic_index, 'frame')
    frame_urns = [r['_id'] for r in res['hits']['hits']]
    q = Frame.all().filter(Frame.urn.in_(frame_urns))
    frames = dict([(f.urn, f) for f in q])
    frames = [frames.get(urn) for urn in frame_urns]
    return query_pager(frames, 'frames.user_index',
                       count=res['hits']['total'],
                       paginate=False,
                       id=id)
예제 #15
0
파일: entities.py 프로젝트: e6/datawi.re
def create():
    require.logged_in()
    entity = Entity.create(request.form, request.user)
    db.session.commit()
    publish(entity_queue, "entity.create", entity)
    return jsonify(entity)
예제 #16
0
파일: entities.py 프로젝트: e6/datawi.re
def get(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    return jsonify(entity)
예제 #17
0
파일: entities.py 프로젝트: arc64/datawi.re
def suggest():
    prefix = request.args.get('prefix')
    results = Entity.suggest_prefix(prefix, authz.authz_collection('read'))
    return jsonify({'results': results})
예제 #18
0
def delete(id):
    entity = obj_or_404(Entity.by_id(id))
    authz.require(authz.collection_write(entity.collection_id))
    entity.delete()
    db.session.commit()
    return jsonify({'status': 'ok'})
예제 #19
0
def view(id):
    entity = obj_or_404(Entity.by_id(id))
    authz.require(authz.collection_read(entity.collection_id))
    return jsonify(entity)
예제 #20
0
def suggest():
    prefix = request.args.get('prefix')
    results = Entity.suggest_prefix(prefix, authz.authz_collection('read'))
    return jsonify({'results': results})
예제 #21
0
파일: entities.py 프로젝트: arc64/datawi.re
def view(id):
    entity = obj_or_404(Entity.by_id(id))
    authz.require(authz.collection_read(entity.collection_id))
    return jsonify(entity)
예제 #22
0
def get_filters():
    # TODO: one day, this will live in memory.
    filters = defaultdict(list)
    for entity in Entity.all():
        filters[entity.pattern].append(entity.id)
    return filters.items()
예제 #23
0
파일: entities.py 프로젝트: arc64/datawi.re
def delete(id):
    entity = obj_or_404(Entity.by_id(id))
    authz.require(authz.collection_write(entity.collection_id))
    entity.delete()
    db.session.commit()
    return jsonify({'status': 'ok'})
예제 #24
0
def create():
    require.logged_in()
    entity = Entity.create(request.form, request.user)
    db.session.commit()
    publish(entity_queue, 'entity.create', entity)
    return jsonify(entity)
예제 #25
0
def get(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    return jsonify(entity)