def update(self): try: from plone.protect.interfaces import IDisableCSRFProtection alsoProvides(self.request, IDisableCSRFProtection) except: pass if self.request.environ['REQUEST_METHOD'] == 'POST': pc = api.portal.get_tool('portal_catalog') portal = getSite() absolute_path = '/'.join(portal.getPhysicalPath()) if self.request.form['id'] != '': id_community = absolute_path + '/' + self.request.form['id'] self.context.plone_log( 'Actualitzant Elasticsearch dades comunitat {}'.format( id_community)) community = pc.unrestrictedSearchResults(path=id_community) if community: obj = community[0]._unrestrictedGetObject() try: self.elastic = getUtility(IElasticSearch) self.elastic().search( index=ElasticSharing().get_index_name()) except: self.elastic().indices.create( index=ElasticSharing().get_index_name(), body={ 'mappings': { 'sharing': { 'properties': { 'path': { 'type': 'string' }, 'principal': { 'type': 'string', 'index': 'not_analyzed' }, 'roles': { 'type': 'string' }, 'uuid': { 'type': 'string' } } } } }) for brain in community: obj = brain._unrestrictedGetObject() if not ICommunity.providedBy(obj): elastic_sharing = queryUtility(IElasticSharing) elastic_sharing.modified(obj) self.context.plone_log( 'Actualitzat objecte: {}, de la comunitat: {}'. format(obj, id_community))
def test_get(self): folder = api.content.create(container=self.portal, type='Folder', title='Test folder') api.user.grant_roles(username='******', obj=folder, roles=['Editor']) ElasticSharing().add(folder, 'janet.dura') time.sleep(1) result = ElasticSharing().get(folder, 'janet.dura') self.assertTrue('Editor' in result[0]['roles']) self.assertTrue(result[0]['path'] == '/test-folder') self.assertTrue(result[0]['principal'] == 'janet.dura')
def setUp(self): self.app = self.layer['app'] self.portal = self.layer['portal'] self.request = self.layer['request'] api.portal.set_registry_record( 'genweb.controlpanel.core.IGenwebCoreControlPanelSettings.elasticsearch', u'http://pc60012.estacions.upcnet.es:9200') self.elastic = getUtility(IElasticSearch) self.elastic.create_new_connection() self.elastic().indices.create(index=ElasticSharing().get_index_name())
def test_add_modify_delete_record(self): folder = api.content.create(container=self.portal, type='Folder', title='Test folder') api.user.grant_roles(username='******', obj=folder, roles=['Editor']) ElasticSharing().add(folder, 'janet.dura') time.sleep(1) result = self.elastic().search( index=ElasticSharing().get_index_name(), doc_type='sharing', body={'query': { 'match': { 'principal': 'janet.dura' } }}) self.assertTrue(result['hits']['total'] == 1) api.user.grant_roles(username='******', obj=folder, roles=['Manager']) ElasticSharing().modify( folder, 'janet.dura', dict(roles=api.user.get_roles(username='******', obj=folder))) ElasticSharing().remove(folder, 'janet.dura') time.sleep(1) result = self.elastic().search( index=ElasticSharing().get_index_name(), doc_type='sharing', body={'query': { 'match': { 'principal': 'janet.dura' } }}) self.assertTrue(result['hits']['total'] == 0)
def test_make_record(self): folder = api.content.create(container=self.portal, type='Folder', title='Test folder') api.user.grant_roles(username='******', obj=folder, roles=['Editor']) record = ElasticSharing().make_record(folder, 'janet.dura') self.assertTrue(record['index'].startswith('security.plone')) self.assertTrue('Editor' in record['body']['roles']) self.assertTrue(record['body']['path'] == '/test-folder') self.assertTrue(record['body']['principal'] == 'janet.dura')
def render(self): try: from plone.protect.interfaces import IDisableCSRFProtection alsoProvides(self.request, IDisableCSRFProtection) except: pass try: self.elastic = getUtility(IElasticSearch) self.elastic().search(index=ElasticSharing().get_index_name()) except: self.elastic().indices.create( index=ElasticSharing().get_index_name(), body={ 'mappings': { 'sharing': { 'properties': { 'path': { 'type': 'string' }, 'principal': { 'type': 'string', 'index': 'not_analyzed' }, 'roles': { 'type': 'string' }, 'uuid': { 'type': 'string' } } } } }) self.response.setBody('OK')
def render(self): try: from plone.protect.interfaces import IDisableCSRFProtection alsoProvides(self.request, IDisableCSRFProtection) except: pass pc = api.portal.get_tool('portal_catalog') portal = getSite() absolute_path = '/'.join(portal.getPhysicalPath()) communities = pc.unrestrictedSearchResults( portal_type="ulearn.community") for num, community in enumerate(communities): obj = community._unrestrictedGetObject() id_community = absolute_path + '/' + obj.id self.context.plone_log('Processant {} de {}. Comunitat {}'.format( num, len(communities), obj)) community = pc.unrestrictedSearchResults(path=id_community) try: self.elastic = getUtility(IElasticSearch) self.elastic().search(index=ElasticSharing().get_index_name()) except: self.elastic().indices.create( index=ElasticSharing().get_index_name(), body={ 'mappings': { 'sharing': { 'properties': { 'path': { 'type': 'string' }, 'principal': { 'type': 'string', 'index': 'not_analyzed' }, 'roles': { 'type': 'string' }, 'uuid': { 'type': 'string' } } } } }) for brain in community: obj = brain._unrestrictedGetObject() if not ICommunity.providedBy(obj): elastic_sharing = queryUtility(IElasticSharing) elastic_sharing.modified(obj) self.context.plone_log( 'Actualitzat objecte: {}, de la comunitat: {}'.format( obj, id_community)) logger.info('Finished update sharing in communities: {}'.format( portal.absolute_url())) self.response.setBody('OK')
def tearDown(self): self.elastic().indices.delete(index=ElasticSharing().get_index_name())