Esempio n. 1
0
def delete_collection(collection_id=None):
    # Deleting a collection affects many associated objects and requires
    # checks, so this is done manually and in detail here.
    q = db.session.query(Collection).filter(Collection.id == collection_id)
    collection = q.first()
    if collection is None:
        log.error("No collection with ID: %r", collection_id)

    log.info("Deleting collection [%r]: %r", collection.id, collection.label)
    deleted_at = datetime.utcnow()
    for entity in collection.entities:
        entity.collections = [c for c in entity.collections
                              if c.id != collection.id]
        db.session.add(entity)
        if not len(entity.collections):
            delete_entity(entity)
        else:
            update_entity(entity)

    for document in collection.documents:
        document.collections = [c for c in document.collections
                                if c.id != collection.id]
        if not len(document.collections):
            delete_document(document, deleted_at=deleted_at)
        else:
            if collection_id == document.source_collection_id:
                document.source_collection_id = None
            db.session.add(document)
            update_document(document)

    collection.delete(deleted_at=deleted_at)
    db.session.commit()
    index_delete(collection_id)
    with graph.transaction() as tx:
        graph.remove_collection(tx, collection_id)
Esempio n. 2
0
def create():
    data = parse_request(schema=EntityCreateSchema)
    collection = get_db_collection(data['collection_id'], request.authz.WRITE)
    entity = Entity.create(data, collection)
    db.session.commit()
    update_entity(entity)
    update_collection(collection)
    return view(entity.id)
Esempio n. 3
0
def update(id):
    _, entity = get_entity(id, request.authz.WRITE)
    data = parse_request(schema=EntitySchema)
    if arg_bool('merge'):
        data['data'] = merge_data(data.get('data') or {}, entity.data or {})
    entity.update(data)
    db.session.commit()
    update_entity(entity)
    update_collection(entity.collection)
    return view(entity.id)
Esempio n. 4
0
def update(id):
    entity = get_db_entity(id, request.authz.WRITE)
    data = parse_request(schema=EntityUpdateSchema)
    if as_bool(request.args.get('merge')):
        props = merge_data(data.get('properties'), entity.data)
        data['properties'] = props
    entity.update(data)
    db.session.commit()
    update_entity(entity)
    update_collection(entity.collection)
    return view(entity.id)
Esempio n. 5
0
def update(entity_id):
    entity = get_db_entity(entity_id, request.authz.WRITE)
    tag_request(collection_id=entity.collection_id)
    data = parse_request(EntityUpdateSchema)
    if get_flag('merge'):
        props = merge_data(data.get('properties'), entity.data)
        data['properties'] = props
    entity.update(data)
    db.session.commit()
    update_entity(entity, sync=get_flag('sync', True))
    entity = get_index_entity(entity_id, request.authz.READ)
    return EntitySerializer.jsonify(entity)
Esempio n. 6
0
def merge(id, other_id):
    entity = get_db_entity(id, request.authz.WRITE)
    other = get_db_entity(other_id, request.authz.WRITE)

    try:
        entity.merge(other)
    except ValueError as ve:
        raise BadRequest(ve.message)

    db.session.commit()
    data = update_entity(entity)
    update_entity(other)
    update_collection(entity.collection)
    return serialize_data(data, CombinedSchema)
Esempio n. 7
0
def merge(id, other_id):
    entity = get_db_entity(id, request.authz.WRITE)
    other = get_db_entity(other_id, request.authz.WRITE)

    try:
        entity.merge(other)
    except ValueError as ve:
        raise BadRequest(ve.message)

    db.session.commit()
    sync = get_flag('sync', True)
    data = update_entity(entity, sync=sync)
    update_entity(other, sync=sync)
    return EntitySerializer.jsonify(data)
Esempio n. 8
0
def merge(id, other_id):
    entity = get_db_entity(id, request.authz.WRITE)
    other = get_db_entity(other_id, request.authz.WRITE)

    try:
        entity.merge(other)
    except ValueError as ve:
        raise BadRequest(ve.message)

    db.session.commit()
    update_entity(entity)
    update_entity(other)
    update_collection(entity.collection)
    return view(entity.id)
Esempio n. 9
0
def merge(entity_id, other_id):
    entity = get_db_entity(entity_id, request.authz.WRITE)
    other = get_db_entity(other_id, request.authz.WRITE)
    tag_request(collection_id=entity.collection_id)

    try:
        entity.merge(other)
    except ValueError as ve:
        raise BadRequest(ve.message)

    db.session.commit()
    sync = get_flag('sync', True)
    data = update_entity(entity, sync=sync)
    update_entity(other, sync=sync)
    return EntitySerializer.jsonify(data)
Esempio n. 10
0
def create():
    data = parse_request(EntityCreateSchema)
    collection = get_db_collection(data['collection_id'], request.authz.WRITE)
    entity = Entity.create(data, collection)
    db.session.commit()
    data = update_entity(entity)
    update_collection(collection)
    return jsonify(data, schema=CombinedSchema)
Esempio n. 11
0
def create():
    data = parse_request(EntityCreateSchema)
    collection = get_db_collection(data['collection_id'], request.authz.WRITE)
    entity = Entity.create(data, collection)
    db.session.commit()
    data = update_entity(entity)
    update_collection(collection)
    refresh_index(entities_index())
    return serialize_data(data, CombinedSchema)
Esempio n. 12
0
def update(id):
    entity = get_db_entity(id, request.authz.WRITE)
    data = parse_request(EntityUpdateSchema)
    if get_flag('merge'):
        props = merge_data(data.get('properties'), entity.data)
        data['properties'] = props
    entity.update(data)
    db.session.commit()
    data = update_entity(entity, sync=get_flag('sync', True))
    return serialize_data(data, CombinedSchema)
Esempio n. 13
0
def update(entity_id):
    entity = get_db_entity(entity_id, request.authz.WRITE)
    tag_request(collection_id=entity.collection_id)
    data = parse_request(EntityUpdateSchema)
    if get_flag('merge'):
        props = merge_data(data.get('properties'), entity.data)
        data['properties'] = props
    entity.update(data)
    db.session.commit()
    data = update_entity(entity, sync=get_flag('sync', True))
    return EntitySerializer.jsonify(data)
Esempio n. 14
0
def update(entity_id):
    """
    ---
    post:
      summary: Update an entity
      description: >
        Update the entity with id `entity_id`. This only applies to
        entities which are backed by a database row, i.e. not any
        entities resulting from a mapping or bulk load.
      parameters:
      - in: path
        name: entity_id
        required: true
        schema:
          type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EntityUpdate'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Entity'
      tags:
      - Entity
    """
    entity = get_db_entity(entity_id, request.authz.WRITE)
    tag_request(collection_id=entity.collection_id)
    data = parse_request('EntityUpdate')
    if get_flag('merge'):
        props = merge_data(data.get('properties'), entity.data)
        data['properties'] = props
    entity.update(data)
    db.session.commit()
    update_entity(entity, sync=get_flag('sync', True))
    entity = get_index_entity(entity_id, request.authz.READ)
    return EntitySerializer.jsonify(entity)
Esempio n. 15
0
def delete_collection(collection_id=None):
    # Deleting a collection affects many associated objects and requires
    # checks, so this is done manually and in detail here.
    q = db.session.query(Collection).filter(Collection.id == collection_id)
    collection = q.first()
    if collection is None:
        log.error("No collection with ID: %r", collection_id)

    log.info("Deleting collection [%r]: %r", collection.id, collection.label)
    deleted_at = datetime.utcnow()
    for entity in collection.entities:
        entity.collections = [
            c for c in entity.collections if c.id != collection.id
        ]
        db.session.add(entity)
        if not len(entity.collections):
            delete_entity(entity)
        else:
            update_entity(entity)

    for document in collection.documents:
        document.collections = [
            c for c in document.collections if c.id != collection.id
        ]
        if not len(document.collections):
            delete_document(document, deleted_at=deleted_at)
        else:
            if collection_id == document.source_collection_id:
                document.source_collection_id = None
            db.session.add(document)
            update_document(document)

    collection.delete(deleted_at=deleted_at)
    db.session.commit()
    index_delete(collection_id)
    with graph.transaction() as tx:
        graph.remove_collection(tx, collection_id)
Esempio n. 16
0
def op_update_entity_handler(collection, task):
    update_entity(collection, job_id=task.stage.job.id, **task.payload)
Esempio n. 17
0
def op_update_entity_handler(collection, task):
    update_entity(collection, **task.payload)