Ejemplo n.º 1
0
def create_worksheet_items():
    """
    Bulk add worksheet items.

    |replace| - Replace existing items in host worksheets. Default is False.
    """
    replace = query_get_bool('replace', False)

    new_items = WorksheetItemSchema(
        strict=True,
        many=True,
    ).load(request.json).data

    worksheet_to_items = {}
    for item in new_items:
        worksheet_to_items.setdefault(item['worksheet_uuid'], []).append(item)

    for worksheet_uuid, items in worksheet_to_items.iteritems():
        worksheet_info = get_worksheet_info(worksheet_uuid, fetch_items=True)
        if replace:
            # Replace items in the worksheet
            update_worksheet_items(worksheet_info,
                                   [Worksheet.Item.as_tuple(i) for i in items],
                                   convert_items=False)
        else:
            # Append items to the worksheet
            for item in items:
                add_worksheet_item(worksheet_uuid,
                                   Worksheet.Item.as_tuple(item))

    return WorksheetItemSchema(many=True).dump(new_items).data
Ejemplo n.º 2
0
def create_worksheet_items():
    """
    Bulk add worksheet items.

    |replace| - Replace existing items in host worksheets. Default is False.
    """
    replace = query_get_bool('replace', False)
    # Get the uuid of the current worksheet
    worksheet_uuid = query_get_type(str, 'uuid')
    new_items = WorksheetItemSchema(strict=True,
                                    many=True).load(request.json).data

    # Map of worksheet_uuid to list of items that exist in this worksheet
    worksheet_to_items = {}
    worksheet_to_items.setdefault(worksheet_uuid, [])

    for item in new_items:
        worksheet_to_items[worksheet_uuid].append(item)

    for worksheet_uuid, items in worksheet_to_items.items():
        worksheet_info = get_worksheet_info(worksheet_uuid, fetch_items=True)
        if replace:
            # Replace items in the worksheet
            update_worksheet_items(worksheet_info,
                                   [Worksheet.Item.as_tuple(i) for i in items],
                                   convert_items=False)
        else:
            # Append items to the worksheet
            for item in items:
                add_worksheet_item(worksheet_uuid,
                                   Worksheet.Item.as_tuple(item))

    return WorksheetItemSchema(many=True).dump(new_items).data
Ejemplo n.º 3
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
Ejemplo n.º 4
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