Esempio n. 1
0
def index_level(client,
                path,
                folders,
                files,
                systemId,
                username,
                reindex=False,
                update_pems=True):
    """
    Index a set of folders and files corresponding to the output from one 
    iteration of walk_levels
    """
    from designsafe.libs.elasticsearch.docs.files import BaseESFile
    for obj in folders + files:
        obj_dict = obj.to_dict()
        obj_dict.pop('permissions')
        obj_dict.pop('trail')
        obj_dict.pop('_links')
        doc = BaseESFile(username, reindex=reindex, **obj_dict)

        saved = doc.save()

        if update_pems:
            permissions = client.files.listPermissions(systemId=systemId,
                                                       filePath=obj.path)
            for pem in permissions:
                pem.pop('_links')
            doc.update(**{'permissions': permissions})

    children_paths = [_file.path for _file in folders + files]
    es_root = BaseESFile(username, systemId, path, reindex=reindex)
    for doc in es_root.children():
        if doc is not None and doc.path not in children_paths:
            doc.delete()
Esempio n. 2
0
    def test_children_function(self, mock_index):
        child_doc1 = IndexedFile(**{
            'name': 'child1',
            'system': 'test.system',
            'path': '/path/to/child1'
        })
        child_doc2 = IndexedFile(**{
            'name': 'child2',
            'system': 'test.system',
            'path': '/path/to/child2'
        })

        mock_index.return_value.children.side_effect = [([child_doc1], 'KEY1'),
                                                        ([child_doc2], 'KEY2'),
                                                        ([], None)]

        wrapped_doc = IndexedFile(**{
            'name': 'file1',
            'system': 'test.system',
            'path': '/path/to/file'
        })
        base = BaseESFile('test_user',
                          system='test.system',
                          wrapped_doc=wrapped_doc)

        # Need to set attrs manually because the custom setter/getter in BaseESResource are mocked

        object.__setattr__(base, 'username', 'test_user')
        object.__setattr__(base, '_reindex', False)
        object.__setattr__(base, 'system', 'test.system')
        object.__setattr__(base, 'path', '/path/to/file')

        child_generator = base.children(limit=1)
        for child in child_generator:
            continue

        mock_index().children.assert_has_calls([
            call('test_user', 'test.system', '/path/to/file', limit=1),
            call('test_user',
                 'test.system',
                 '/path/to/file',
                 limit=1,
                 search_after='KEY1'),
            call('test_user',
                 'test.system',
                 '/path/to/file',
                 limit=1,
                 search_after='KEY2'),
        ])

        # Check that iteration ends after all children have been listed.
        self.assertRaises(StopIteration, child_generator.__next__)