def update_datadoc_owner(doc_id, next_owner_id, originator=None):
    with DBSession() as session:
        # Add previous owner as an editor to the doc
        assert_is_owner(doc_id, session=session)
        current_owner_editor = logic.create_data_doc_editor(
            data_doc_id=doc_id,
            uid=current_user.id,
            read=True,
            write=True,
            commit=False,
            session=session,
        )
        current_owner_editor_dict = current_owner_editor.to_dict()
        # Remove next owner as a doc editor
        next_owner_editor = logic.get_data_doc_editor_by_id(
            next_owner_id, session=session
        )
        next_owner_editor_dict = next_owner_editor.to_dict()
        logic.delete_data_doc_editor(
            id=next_owner_id, doc_id=doc_id, session=session, commit=False
        )
        next_owner_uid = next_owner_editor_dict["uid"]
        # Update doc owner to next owner
        doc = logic.update_data_doc(
            id=doc_id, commit=False, session=session, owner_uid=next_owner_uid
        )
        doc_dict = doc.to_dict()
        session.commit()
        socketio.emit(
            "data_doc_editor",
            (originator, doc_id, current_user.id, current_owner_editor_dict),
            namespace="/datadoc",
            room=doc_id,
            broadcast=True,
        )
        socketio.emit(
            "data_doc_editor",
            (
                originator,
                next_owner_editor_dict["data_doc_id"],
                next_owner_editor_dict["uid"],
                None,
            ),
            namespace="/datadoc",
            room=next_owner_editor_dict["data_doc_id"],
            broadcast=True,
        )
        socketio.emit(
            "data_doc_updated",
            (originator, doc_dict,),
            namespace="/datadoc",
            room=next_owner_editor_dict["data_doc_id"],
            broadcast=True,
        )
        logic.update_es_data_doc_by_id(doc_id)
        send_datadoc_transfer_notification(doc_id, next_owner_uid, session)
        return current_owner_editor_dict
Ejemplo n.º 2
0
def add_datadoc_editor(
        doc_id,
        uid,
        read=None,
        write=None,
        originator=None,  # Used for websocket to identify sender, optional
):
    with DBSession() as session:
        assert_can_write(doc_id, session=session)
        editor = logic.create_data_doc_editor(data_doc_id=doc_id,
                                              uid=uid,
                                              read=read,
                                              write=write,
                                              commit=False)
        editor_dict = editor.to_dict()

        access_request = logic.get_data_doc_access_request_by_doc_id(
            doc_id=doc_id, uid=uid)
        if access_request:
            logic.remove_datadoc_access_request(doc_id=doc_id,
                                                uid=uid,
                                                commit=False)

        session.commit()

        # Update queries in elasticsearch to reflect new permissions
        logic.update_es_queries_by_datadoc_id(doc_id, session=session)

        if access_request:
            socketio.emit(
                "data_doc_access_request",
                (originator, doc_id, uid, None),
                namespace="/datadoc",
                room=doc_id,
                broadcast=True,
            )

        socketio.emit(
            "data_doc_editor",
            (originator, doc_id, uid, editor_dict),
            namespace="/datadoc",
            room=doc_id,
            broadcast=True,
        )
        logic.update_es_data_doc_by_id(doc_id)
        send_add_datadoc_editor_email(doc_id, uid, read, write)
        return editor_dict