Ejemplo n.º 1
0
    async def import_folder(self, api, tm, folder, page_data, depth=1):
        self._count += 1
        if 'pageid' not in page_data:
            print(f"XXX could not import {page_data['title']}")
            return
        _id = str(page_data['pageid'])
        print(f"{self._count} importing {page_data['title']}")
        try:
            obj = await create_content_in_container(
                folder, 'Folder', _id, id=_id,
                creators=('root',), contributors=('root',),
                title=page_data['title'])
        except ConflictIdOnContainer:
            obj = await folder.async_get(_id)
        behavior = IDublinCore(obj)
        await behavior.load(create=True)
        behavior.description = page_data.get('extract', '')

        if self._count % self._batch_size == 0:
            await tm.commit()
            await tm.begin(self.request)

        if self.arguments.depth > depth:
            folder_count = 0
            async for page_data in api.iter_pages():
                await self.import_folder(api, tm, obj, page_data, depth + 1)
                folder_count += 1
                if folder_count >= self.arguments.per_node:
                    break
Ejemplo n.º 2
0
async def test_use_behavior_annotation(dummy_txn_root):
    async with dummy_txn_root as root:
        ob1 = Item()
        await root.async_set('ob1', ob1)
        dublin = IDublinCore(ob1)
        await dublin.load()
        dublin.publisher = 'foobar'
        assert dublin.publisher == 'foobar'
Ejemplo n.º 3
0
async def test_create_contenttype_with_date(container_requester):
    async with container_requester as requester:
        _, status = await requester(
            'POST',
            '/db/guillotina/',
            data=json.dumps({
                "@type": "Item",
                "title": "Item1",
                "id": "item1",
            })
        )
        assert status == 201
        date_to_test = "2016-11-30T14:39:07.394273+01:00"
        _, status = await requester(
            'PATCH',
            '/db/guillotina/item1',
            data=json.dumps({
                "guillotina.behaviors.dublincore.IDublinCore": {
                    "creation_date": date_to_test,
                    "expiration_date": date_to_test
                }
            })
        )

        request = utils.get_mocked_request(db=requester.db)
        root = await utils.get_root(request)
        async with transaction(db=requester.db, abort_when_done=True):
            container = await root.async_get('guillotina')
            obj = await container.async_get('item1')
            from guillotina.behaviors.dublincore import IDublinCore
            behavior = IDublinCore(obj)
            await behavior.load()
            assert behavior.creation_date.isoformat() == date_to_test  # pylint: disable=E1101
            assert behavior.expiration_date.isoformat() == date_to_test  # pylint: disable=E1101
Ejemplo n.º 4
0
    async def test_creator_used_from_content_creation(self, dummy_request):
        self.request = dummy_request
        utils.login(self.request)

        container = await create_content(
            'Container',
            id='guillotina',
            title='Guillotina')
        container.__name__ = 'guillotina'
        utils._p_register(container)

        import guillotina.tests
        configure.register_configuration(Folder, dict(
            type_name="TestType2",
            behaviors=[],
            module=guillotina.tests  # for registration initialization
        ), 'contenttype')
        root = get_utility(IApplication, name='root')

        configure.load_configuration(
            root.app.config, 'guillotina.tests', 'contenttype')
        root.app.config.execute_actions()
        load_cached_schema()

        obj = await create_content_in_container(
            container, 'TestType2', 'foobar',
            creators=('root',), contributors=('root',))

        assert obj.creators == ('root',)
        assert obj.contributors == ('root',)

        behavior = IDublinCore(obj)
        assert behavior.creators == ('root',)
        assert behavior.contributors == ('root',)
Ejemplo n.º 5
0
async def test_create_contenttype_with_date(site_requester):
    async with await site_requester as requester:
        response, status = await requester('POST',
                                           '/db/guillotina/',
                                           data=json.dumps({
                                               "@type": "Item",
                                               "title": "Item1",
                                               "id": "item1",
                                           }))
        assert status == 201
        date_to_test = "2016-11-30T14:39:07.394273+01:00"
        response, status = await requester(
            'PATCH',
            '/db/guillotina/item1',
            data=json.dumps({
                "guillotina.behaviors.dublincore.IDublinCore": {
                    "created": date_to_test,
                    "expires": date_to_test
                }
            }))

        request = utils.get_mocked_request(requester.db)
        root = await utils.get_root(request)
        site = await root.async_get('guillotina')
        obj = await site.async_get('item1')
        from guillotina.behaviors.dublincore import IDublinCore
        behavior = IDublinCore(obj)
        await behavior.load()
        assert behavior.created.isoformat() == date_to_test
        assert behavior.expires.isoformat() == date_to_test
Ejemplo n.º 6
0
async def test_creator_used_from_content_creation(dummy_guillotina):
    utils.login()

    async with transaction(db=await get_database("db")):
        container = await create_content("Container", id="guillotina", title="Guillotina")
        container.__name__ = "guillotina"
        utils.register(container)

        import guillotina.tests

        configure.register_configuration(
            Folder,
            dict(
                type_name="TestType2", behaviors=[], module=guillotina.tests
            ),  # for registration initialization
            "contenttype",
        )
        root = get_utility(IApplication, name="root")

        configure.load_configuration(root.app.config, "guillotina.tests", "contenttype")
        root.app.config.execute_actions()
        load_cached_schema()

        obj = await create_content_in_container(
            container, "TestType2", "foobar", creators=("root",), contributors=("root",)
        )

        assert obj.creators == ("root",)
        assert obj.contributors == ("root",)

        behavior = IDublinCore(obj)
        assert behavior.creators == ("root",)
        assert behavior.contributors == ("root",)
Ejemplo n.º 7
0
 async def __call__(self):
     behavior = IDublinCore(self.context)
     if behavior is None:
         return []
     await behavior.load()
     if behavior.tags is not None:
         return behavior.tags
     else:
         return []
Ejemplo n.º 8
0
    async def import_folder(self, api, tm, txn, folder, page_data, depth=1):
        self._count += 1
        if "pageid" not in page_data:
            print(f"XXX could not import {page_data['title']}")
            return
        _id = str(page_data["pageid"])
        print(f"{self._count} importing {page_data['title']}")
        obj = await folder.async_get(_id)
        if obj is None:
            obj = await create_content_in_container(
                folder,
                "Folder",
                _id,
                id=_id,
                creators=("root", ),
                contributors=("root", ),
                title=page_data["title"],
                check_constraints=False,
            )
        behavior = IDublinCore(obj)
        await behavior.load(create=True)
        behavior.description = page_data.get("extract", "")

        if self._count % self._batch_size == 0:
            try:
                await tm.commit(txn=txn)
            except ConflictIdOnContainer:
                # ignore id conflicts
                await tm.abort(txn=txn)
            await tm.begin()

        if self.arguments.depth > depth:
            folder_count = 0
            async for page_data in api.iter_pages():
                await self.import_folder(api, tm, txn, obj, page_data,
                                         depth + 1)
                folder_count += 1
                if folder_count >= self.arguments.per_node:
                    break
Ejemplo n.º 9
0
async def get_versions(context, request):
    cursor = request.GET.get('cursor')

    try:
        size = int(request.GET['size'])
    except KeyError:
        size = 20

    if cursor is None:
        dublincore = IDublinCore(context)
        await dublincore.load()
        items = [{
            'timestamp': dublincore.creation_date,
            'diff': None,
            'id': 'current',
            'stub': True,
            'filename': None,
            'size': None,
            'content_type': None
        }]
    else:
        items = []

    versioning = IVersioning(context, None)
    if versioning is None:
        return {'items': items, 'total': len(items), 'cursor': None}
    await versioning.load()
    if versioning.diffs is None:
        return {'items': items, 'total': len(items), 'cursor': None}
    diffs = versioning.diffs
    current_annotation_index = diffs.current_annotation_index
    metadata = diffs.annotations_metadata.get(current_annotation_index, {})
    start = metadata.get('len', 0)
    if cursor and cursor != 'current':
        current_annotation_index, start = cursor.split('-')
        current_annotation_index = int(current_annotation_index)
        start = int(start)
    batch_size = size
    while current_annotation_index >= 0 and len(items) < size:
        annotation = await diffs.get_annotation(context,
                                                current_annotation_index,
                                                create=False)
        if annotation is not None:
            end = max(start - batch_size, 0)
            for idx, item in enumerate(reversed(
                    annotation['items'][end:start])):
                item_idx = annotation['items'].index(item)
                items.append({
                    'timestamp': item.get('timestamp'),
                    'diff': item.get('diff'),
                    'filename': item.get('filename'),
                    'size': item.get('size'),
                    'content_type': item.get('content_type'),
                    'id': f'{current_annotation_index}-{item_idx}',
                    'stub': item.get('stub', False)
                })
                batch_size -= 1
            cursor = f'{current_annotation_index}-{end}'
        if current_annotation_index == 0:
            break
        current_annotation_index -= 1
        metadata = diffs.annotations_metadata.get(current_annotation_index, {})
        start = metadata.get('len', 0)
    return {'items': items, 'total': len(diffs) + 1, 'cursor': cursor}
Ejemplo n.º 10
0
async def get_versions(context, request):
    cursor = request.GET.get("cursor")

    try:
        size = int(request.GET["size"])
    except KeyError:
        size = 20

    if cursor is None:
        dublincore = IDublinCore(context)
        await dublincore.load()
        items = [{
            "timestamp": dublincore.creation_date,
            "diff": None,
            "id": "current",
            "stub": True,
            "filename": None,
            "size": None,
            "content_type": None,
        }]
    else:
        items = []

    versioning = IVersioning(context, None)
    if versioning is None:
        return {"items": items, "total": len(items), "cursor": None}
    await versioning.load()
    if versioning.diffs is None:
        return {"items": items, "total": len(items), "cursor": None}
    diffs = versioning.diffs
    current_annotation_index = diffs.current_annotation_index
    metadata = diffs.annotations_metadata.get(current_annotation_index, {})
    start = metadata.get("len", 0)
    if cursor and cursor != "current":
        current_annotation_index, start = cursor.split("-")
        current_annotation_index = int(current_annotation_index)
        start = int(start)
    batch_size = size
    while current_annotation_index >= 0 and len(items) < size:
        annotation = await diffs.get_annotation(context,
                                                current_annotation_index,
                                                create=False)
        if annotation is not None:
            end = max(start - batch_size, 0)
            for idx, item in enumerate(reversed(
                    annotation["items"][end:start])):
                item_idx = annotation["items"].index(item)
                items.append({
                    "timestamp": item.get("timestamp"),
                    "diff": item.get("diff"),
                    "filename": item.get("filename"),
                    "size": item.get("size"),
                    "content_type": item.get("content_type"),
                    "id": f"{current_annotation_index}-{item_idx}",
                    "stub": item.get("stub", False),
                })
                batch_size -= 1
            cursor = f"{current_annotation_index}-{end}"
        if current_annotation_index == 0:
            break
        current_annotation_index -= 1
        metadata = diffs.annotations_metadata.get(current_annotation_index, {})
        start = metadata.get("len", 0)
    return {"items": items, "total": len(diffs) + 1, "cursor": cursor}