Пример #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
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
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
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
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
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
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
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
def get(id):
    require.logged_in()
    entity = obj_or_404(Entity.by_user_and_id(request.user, id))
    return jsonify(entity)
Пример #17
0
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
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
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)