def set_worksheet_permissions():
    """
    Bulk set worksheet permissions.
    """
    new_permissions = WorksheetPermissionSchema(strict=True, many=True).load(request.json).data

    for p in new_permissions:
        worksheet = local.model.get_worksheet(p['object_uuid'], fetch_items=False)
        set_worksheet_permission(worksheet, p['group_uuid'], p['permission'])
    return WorksheetPermissionSchema(many=True).dump(new_permissions).data
Пример #2
0
def fetch_worksheet(uuid):
    worksheet = get_worksheet_info(
        uuid,
        fetch_items=True,
        fetch_permission=True,
    )

    # Build response document
    document = WorksheetSchema().dump(worksheet).data

    # Include items
    json_api_include(document, WorksheetItemSchema(), worksheet['items'])

    # Include bundles
    bundle_uuids = {
        item['bundle_uuid']
        for item in worksheet['items']
        if item['type'] == worksheet_util.TYPE_BUNDLE
        and item['bundle_uuid'] is not None
    }
    bundle_infos = get_bundle_infos(bundle_uuids).values()
    json_api_include(document, BundleSchema(), bundle_infos)

    # Include users
    user_ids = {b['owner_id'] for b in bundle_infos}
    user_ids.add(worksheet['owner_id'])
    if user_ids:
        json_api_include(document, UserSchema(),
                         local.model.get_users(user_ids))

    # Include subworksheets
    subworksheet_uuids = {
        item['subworksheet_uuid']
        for item in worksheet['items']
        if item['type'] == worksheet_util.TYPE_WORKSHEET
        and item['subworksheet_uuid'] is not None
    }
    json_api_include(
        document, WorksheetSchema(),
        local.model.batch_get_worksheets(fetch_items=False,
                                         uuid=subworksheet_uuids))

    # FIXME: tokenizing directive args
    # value_obj = formatting.string_to_tokens(value) if type == worksheet_util.TYPE_DIRECTIVE else value

    # Include permissions
    json_api_include(document, WorksheetPermissionSchema(),
                     worksheet['group_permissions'])

    return document
Пример #3
0
def fetch_worksheets():
    """
    Fetch worksheets by worksheet specs (names) OR search keywords.

    Query parameters:

     - `include`: comma-separated list of related resources to include, such as "owner"
    """
    keywords = query_get_list('keywords')
    specs = query_get_list('specs')
    base_worksheet_uuid = request.query.get('base')
    include_set = query_get_json_api_include_set(
        supported={'owner', 'group_permissions'})

    if specs:
        uuids = [
            get_worksheet_uuid_or_create(base_worksheet_uuid, spec)
            for spec in specs
        ]
        worksheets = [
            w.to_dict()
            for w in local.model.batch_get_worksheets(fetch_items=False,
                                                      uuid=uuids)
        ]
    else:
        keywords = resolve_owner_in_keywords(keywords)
        worksheets = local.model.search_worksheets(request.user.user_id,
                                                   keywords)

    # Build response document
    document = WorksheetSchema(many=True).dump(worksheets).data

    # Include users
    if 'owner' in include_set:
        owner_ids = {w['owner_id'] for w in worksheets}
        if owner_ids:
            json_api_include(
                document, UserSchema(),
                local.model.get_users(user_ids=owner_ids)['results'])

    # Include permissions
    if 'group_permissions' in include_set:
        for w in worksheets:
            if 'group_permissions' in w:
                json_api_include(document, WorksheetPermissionSchema(),
                                 w['group_permissions'])

    return document
Пример #4
0
def fetch_worksheets():
    """
    Fetch bundles by bundle specs OR search keywords.
    """
    keywords = query_get_list('keywords')
    specs = query_get_list('specs')
    base_worksheet_uuid = request.query.get('base')

    if specs:
        uuids = [
            get_worksheet_uuid_or_create(base_worksheet_uuid, spec)
            for spec in specs
        ]
        worksheets = [
            w.to_dict()
            for w in local.model.batch_get_worksheets(fetch_items=False,
                                                      uuid=uuids)
        ]
    else:
        keywords = resolve_owner_in_keywords(keywords)
        worksheets = local.model.search_worksheets(request.user.user_id,
                                                   keywords)

    # Build response document
    document = WorksheetSchema(many=True).dump(worksheets).data

    # Include users
    owner_ids = {w['owner_id'] for w in worksheets}
    if owner_ids:
        json_api_include(document, UserSchema(),
                         local.model.get_users(owner_ids))

    # Include permissions
    for w in worksheets:
        if 'group_permissions' in w:
            json_api_include(document, WorksheetPermissionSchema(),
                             w['group_permissions'])

    return document
Пример #5
0
def fetch_worksheet(uuid):
    """
    Fetch a single worksheet by UUID.

    Query parameters:

     - `include`: comma-separated list of related resources to include, such as "owner"
    """
    include_set = query_get_json_api_include_set(
        supported={
            'owner',
            'group_permissions',
            'items',
            'items.bundle',
            'items.bundle.owner',
            'items.subworksheet',
        })
    worksheet = get_worksheet_info(
        uuid,
        fetch_items='items' in include_set,
        fetch_permissions='group_permissions' in include_set,
    )

    # Build response document
    document = WorksheetSchema().dump(worksheet).data

    # Include items
    if 'items' in include_set:
        json_api_include(document, WorksheetItemSchema(), worksheet['items'])

    user_ids = set()

    # Include bundles
    if 'items.bundle' in include_set:
        bundle_uuids = {
            item['bundle_uuid']
            for item in worksheet['items']
            if item['type'] == worksheet_util.TYPE_BUNDLE
            and item['bundle_uuid'] is not None
        }
        bundle_infos = list(get_bundle_infos(bundle_uuids).values())
        json_api_include(document, BundleSchema(), bundle_infos)
        if 'items.bundle.owner' in include_set:
            user_ids.update({b['owner_id'] for b in bundle_infos})

    # Include users
    if 'owner' in include_set:
        user_ids.add(worksheet['owner_id'])
    if user_ids:
        json_api_include(document, UserSchema(),
                         local.model.get_users(user_ids=user_ids)['results'])

    # Include subworksheets
    if 'items.subworksheets' in include_set:
        subworksheet_uuids = {
            item['subworksheet_uuid']
            for item in worksheet['items']
            if item['type'] == worksheet_util.TYPE_WORKSHEET
            and item['subworksheet_uuid'] is not None
        }
        json_api_include(
            document,
            WorksheetSchema(),
            local.model.batch_get_worksheets(fetch_items=False,
                                             uuid=subworksheet_uuids),
        )

    # Include permissions
    if 'group_permissions' in include_set:
        json_api_include(document, WorksheetPermissionSchema(),
                         worksheet['group_permissions'])

    return document