@frontend.route('/objects/<int:object_id>/permissions', methods=['POST'])
@object_permissions_required(Permissions.GRANT)
def update_object_permissions(object_id):
    edit_user_permissions_form = ObjectPermissionsForm()
    add_user_permissions_form = ObjectUserPermissionsForm()
    add_group_permissions_form = ObjectGroupPermissionsForm()
    add_project_permissions_form = ObjectProjectPermissionsForm()
    copy_permissions_form = CopyPermissionsForm()
    if ('copy_permissions' in flask.request.form):
        if (not flask.current_app.config['LOAD_OBJECTS_IN_BACKGROUND']):
            existing_objects = get_objects_with_permissions(user_id=flask_login.current_user.id, permissions=Permissions.GRANT)
            copy_permissions_form.object_id.choices = [(str(existing_object.id), existing_object.data['name']['text']) for existing_object in existing_objects if (existing_object.id != object_id)]
        else:
            copy_permissions_form.object_id.choices = []
        if copy_permissions_form.validate_on_submit():
            logic.object_permissions.copy_permissions(object_id, int(copy_permissions_form.object_id.data))
            logic.object_permissions.set_user_object_permissions(object_id, flask_login.current_user.id, Permissions.GRANT)
            flask.flash(_('Successfully copied object permissions.'), 'success')
    elif (('edit_user_permissions' in flask.request.form) and edit_user_permissions_form.validate_on_submit()):
        set_object_public(object_id, (edit_user_permissions_form.public_permissions.data == 'read'))
        for user_permissions_data in edit_user_permissions_form.user_permissions.data:
            user_id = user_permissions_data['user_id']
            try:
                get_user(user_id)
            except UserDoesNotExistError:
                continue
            permissions = Permissions.from_name(user_permissions_data['permissions'])
            set_user_object_permissions(object_id=object_id, user_id=user_id, permissions=permissions)
        for group_permissions_data in edit_user_permissions_form.group_permissions.data:
            group_id = group_permissions_data['group_id']
            try:
                get_group(group_id)
            except GroupDoesNotExistError:
                continue
            permissions = Permissions.from_name(group_permissions_data['permissions'])
            set_group_object_permissions(object_id=object_id, group_id=group_id, permissions=permissions)
        for project_permissions_data in edit_user_permissions_form.project_permissions.data:
            project_id = project_permissions_data['project_id']
            try:
                get_project(project_id)
            except ProjectDoesNotExistError:
                continue
            permissions = Permissions.from_name(project_permissions_data['permissions'])
            set_project_object_permissions(object_id=object_id, project_id=project_id, permissions=permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    elif (('add_user_permissions' in flask.request.form) and add_user_permissions_form.validate_on_submit()):
        user_id = add_user_permissions_form.user_id.data
        permissions = Permissions.from_name(add_user_permissions_form.permissions.data)
        object_permissions = get_object_permissions_for_users(object_id=object_id, include_instrument_responsible_users=False, include_groups=False, include_projects=False, include_admin_permissions=False)
        assert (permissions in [Permissions.READ, Permissions.WRITE, Permissions.GRANT])
        assert (user_id not in object_permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        set_user_object_permissions(object_id=object_id, user_id=user_id, permissions=permissions)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    elif (('add_group_permissions' in flask.request.form) and add_group_permissions_form.validate_on_submit()):
        group_id = add_group_permissions_form.group_id.data
        permissions = Permissions.from_name(add_group_permissions_form.permissions.data)
        object_permissions = get_object_permissions_for_groups(object_id=object_id)
        assert (permissions in [Permissions.READ, Permissions.WRITE, Permissions.GRANT])
        assert (group_id not in object_permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        set_group_object_permissions(object_id=object_id, group_id=group_id, permissions=permissions)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    elif (('add_project_permissions' in flask.request.form) and add_project_permissions_form.validate_on_submit()):
        project_id = add_project_permissions_form.project_id.data
        permissions = Permissions.from_name(add_project_permissions_form.permissions.data)
        object_permissions = get_object_permissions_for_projects(object_id=object_id)
        assert (permissions in [Permissions.READ, Permissions.WRITE, Permissions.GRANT])
        assert (project_id not in object_permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        set_project_object_permissions(object_id=object_id, project_id=project_id, permissions=permissions)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    else:
        flask.flash(_('A problem occurred while changing the object permissions. Please try again.'), 'error')
    return flask.redirect(flask.url_for('.object_permissions', object_id=object_id))
@frontend.route('/objects/<int:object_id>/permissions', methods=['POST'])
@object_permissions_required(Permissions.GRANT)
def update_object_permissions(object_id):
    edit_user_permissions_form = ObjectPermissionsForm()
    add_user_permissions_form = ObjectUserPermissionsForm()
    add_group_permissions_form = ObjectGroupPermissionsForm()
    add_project_permissions_form = ObjectProjectPermissionsForm()
    copy_permissions_form = CopyPermissionsForm()
    if ('copy_permissions' in flask.request.form):
        if (not flask.current_app.config['LOAD_OBJECTS_IN_BACKGROUND']):
            existing_objects = get_objects_with_permissions(user_id=flask_login.current_user.id, permissions=Permissions.GRANT)
            copy_permissions_form.object_id.choices = [(str(existing_object.id), existing_object.data['name']['text']) for existing_object in existing_objects if (existing_object.id != object_id)]
        else:
            copy_permissions_form.object_id.choices = []
        if copy_permissions_form.validate_on_submit():
            logic.object_permissions.copy_permissions(object_id, int(copy_permissions_form.object_id.data))
            logic.object_permissions.set_user_object_permissions(object_id, flask_login.current_user.id, Permissions.GRANT)
            flask.flash(_('Successfully copied object permissions.'), 'success')
    elif (('edit_user_permissions' in flask.request.form) and edit_user_permissions_form.validate_on_submit()):
        set_object_public(object_id, (edit_user_permissions_form.public_permissions.data == 'read'))
        for user_permissions_data in edit_user_permissions_form.user_permissions.data:
            user_id = user_permissions_data['user_id']
            try:
                get_user(user_id)
            except UserDoesNotExistError:
                continue
            permissions = Permissions.from_name(user_permissions_data['permissions'])
            set_user_object_permissions(object_id=object_id, user_id=user_id, permissions=permissions)
        for group_permissions_data in edit_user_permissions_form.group_permissions.data:
            group_id = group_permissions_data['group_id']
            try:
                get_group(group_id)
            except GroupDoesNotExistError:
                continue
            permissions = Permissions.from_name(group_permissions_data['permissions'])
            set_group_object_permissions(object_id=object_id, group_id=group_id, permissions=permissions)
        for project_permissions_data in edit_user_permissions_form.project_permissions.data:
            project_id = project_permissions_data['project_id']
            try:
                get_project(project_id)
            except ProjectDoesNotExistError:
                continue
            permissions = Permissions.from_name(project_permissions_data['permissions'])
            set_project_object_permissions(object_id=object_id, project_id=project_id, permissions=permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    elif (('add_user_permissions' in flask.request.form) and add_user_permissions_form.validate_on_submit()):
        user_id = add_user_permissions_form.user_id.data
        permissions = Permissions.from_name(add_user_permissions_form.permissions.data)
        object_permissions = get_object_permissions_for_users(object_id=object_id, include_instrument_responsible_users=False, include_groups=False, include_projects=False, include_admin_permissions=False)
        assert (permissions in [Permissions.READ, Permissions.WRITE, Permissions.GRANT])
        assert (user_id not in object_permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        set_user_object_permissions(object_id=object_id, user_id=user_id, permissions=permissions)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    elif (('add_group_permissions' in flask.request.form) and add_group_permissions_form.validate_on_submit()):
        group_id = add_group_permissions_form.group_id.data
        permissions = Permissions.from_name(add_group_permissions_form.permissions.data)
        object_permissions = get_object_permissions_for_groups(object_id=object_id)
        assert (permissions in [Permissions.READ, Permissions.WRITE, Permissions.GRANT])
        assert (group_id not in object_permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        set_group_object_permissions(object_id=object_id, group_id=group_id, permissions=permissions)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    elif (('add_project_permissions' in flask.request.form) and add_project_permissions_form.validate_on_submit()):
        project_id = add_project_permissions_form.project_id.data
        permissions = Permissions.from_name(add_project_permissions_form.permissions.data)
        object_permissions = get_object_permissions_for_projects(object_id=object_id)
        assert (permissions in [Permissions.READ, Permissions.WRITE, Permissions.GRANT])
        assert (project_id not in object_permissions)
        user_log.edit_object_permissions(user_id=flask_login.current_user.id, object_id=object_id)
        set_project_object_permissions(object_id=object_id, project_id=project_id, permissions=permissions)
        flask.flash(_('Successfully updated object permissions.'), 'success')
    else:
        flask.flash(_('A problem occurred while changing the object permissions. Please try again.'), 'error')
    return flask.redirect(flask.url_for('.object_permissions', object_id=object_id))