def evolve(context): root = getRootFolder(context) old_site = getSite() assert ISchoolToolApplication.providedBy(root) setSite(root) requireURICache(root) connection = context.connection link_oids, linkset_oids = collectOIDs(connection) evolveLinks(connection, link_oids) transaction.savepoint(optimistic=True) evolveLinkSets(connection, linkset_oids) transaction.savepoint(optimistic=True) addLinkCatalog(root) transaction.savepoint(optimistic=True) evolveGroupRelationships(root) evolveCourseRelationships(root) evolveResourceRelationships(root) evolveAdvisorRelationships(root) evolveSectionsRelationships(root) transaction.savepoint(optimistic=True) evolveContactRelationships(root) setSite(old_site)
def setUp(self): portal = self.layer['portal'] self.portal = portal self.request = self.layer['request'] setRoles(portal, TEST_USER_ID, ['Manager']) setHooks() setSite(portal)
def testProxyHostComps(self): pers_comps = BaseComponents(BASE, 'persistent', (BASE,)) host_comps = BaseComponents(BASE, 'example.com', (BASE,)) host_sm = HSM('example.com', 'siteman', host_comps, pers_comps) host_site = MockSite(host_sm) host_site.__name__ = host_sm.__name__ setSite(host_site) new_comps = BaseComponents(BASE, 'sub_site', (pers_comps,)) new_site = MockSite(new_comps) new_site.__name__ = new_comps.__name__ interface.alsoProvides(new_site, IFoo) threadSiteSubscriber(new_site, None) cur_site = getSite() # It should implement the static and dynamic # ifaces assert_that(cur_site, validly_provides(IFoo)) assert_that(cur_site, validly_provides(IMock)) # It should have the marker property assert_that(cur_site.getSiteManager(), has_property('host_components', host_comps)) assert_that(ro.ro(cur_site.getSiteManager()), contains( # The first entry is synthesized has_property('__name__', new_comps.__name__), pers_comps, # The host comps appear after all the bases # in the ro of the new site host_comps, BASE))
def afterSetUp(self): """ Set up """ setSite(self.portal) self.setRoles(['Manager']) self.portal.invokeFactory('Folder', id='folder') self.portal.invokeFactory('Folder', id='themes') self.createObject = createObject createObject(self.portal, 'Document', 'doc1') createObject(self.portal, 'File', 'video1') createObject(self.portal, 'Document', 'backref1') path = os.path.join(os.path.dirname(__file__), 'barsandtones.flv') zfile = open(path, 'r') self.portal.video1.setFile(zfile) zfile.close() # mimetype isn't set automatically f = self.portal.video1.getPrimaryField().getAccessor( self.portal.video1)() f.setContentType('video/x-flv') alsoProvides(self.portal.video1, IVideo) self.portal.video1.reindexObject() user = self.portal.portal_membership.getAuthenticatedMember() user.setProperties(email='*****@*****.**') # make all themes public (non deprecated) for theme in self.portal.portal_vocabularies.themes.objectValues(): self.portal.portal_workflow.doActionFor(theme, 'publish')
def wrapped_func(*args, **kw): sm, site = getSecurityManager(), getSite() try: return func(*args, **kw) finally: setSecurityManager(sm) setSite(site)
def setUp(test): from zope.component.testing import setUp setUp() from zope.container.testing import setUp setUp() from zope.component import hooks hooks.setHooks() from zope.traversing.testing import setUp setUp() from zope.publisher.browser import BrowserLanguages from zope.publisher.interfaces.http import IHTTPRequest from zope.i18n.interfaces import IUserPreferredLanguages zope.component.provideAdapter(BrowserLanguages, [IHTTPRequest], IUserPreferredLanguages) from zope.site.folder import rootFolder site = rootFolder() from zope.site.site import LocalSiteManager from zope.component.interfaces import ISite if not ISite.providedBy(site): site.setSiteManager(LocalSiteManager(site)) hooks.setSite(site) test.globs = { 'print_function': print_function, 'root': site, 'pprint': pprint.pprint}
def get(self, path): if path == '/logout': if users.get_current_user(): self.redirect(users.create_logout_url('/')) else: self.redirect(self.request.relative_url('/')) return setRequest(self.request) app = createObject('Application', self.request, self.response) setSite(app) traverser = ITraverser(app) stack = path.split('/') if stack[-1] == '': stack = stack[:-1] if stack[0] == '': stack = stack[1:] obj = traverser.traverse(self.request, self.response, stack) requires = getattr(obj, 'permission_required', None) if requires: user = users.get_current_user() if not checkPermission(obj, user, requires): self.redirect(users.create_login_url(self.request.uri)) return if getattr(obj, 'render', None): self.response.out.write(obj.render()) else: self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(obj) getUtility(ISession).commit() clearRequest()
def ploneSite(db=None, connection=None, environ=None): """Context manager for working with the Plone portal during layer setup:: with ploneSite() as portal: ... This is based on the ``z2.zopeApp()`` context manager. See the module ``plone.testing.z2`` for details. Do not use this in a test. Use the 'portal' resource from the PloneFixture layer instead! Pass a ZODB handle as ``db`` to use a specificdatabase. Alternatively, pass an open connection as ``connection`` (the connection will not be closed). """ setHooks() site = getSite() with z2.zopeApp(db, connection, environ) as app: portal = app[PLONE_SITE_ID] setSite(portal) login(portal, TEST_USER_NAME) try: yield portal finally: logout() if site is not portal: setSite(site)
def tearDown(self): with z2.zopeApp() as app: portal = app[PLONE_SITE_ID] setHooks() setSite(portal) # Allow subclass to tear down persistent fixture self.tearDownPloneSite(portal) setSite(None) # Make sure zope.security checkers can be set up and torn down # reliably security.popCheckers() # Pop the component registry, thus removing component # architecture registrations popGlobalRegistry(portal) # Remove PAS plugins self.tearDownMultiPlugins() # Allow subclass to tear down products self.tearDownZope(app) # Zap the configuration context del self['configurationContext'] # Pop the demo storage, thus restoring the database to the # previous state self['zodbDB'].close() del self['zodbDB']
def dbconfig(event): if conf is None: log.error('No product config found! Configuration will not be set') return db = Zope2.DB connection = db.open() root_folder = connection.root().get(ZopePublication.root_name, None) for portal_id in conf.get('portals', '').split(','): portal = root_folder.get(portal_id) if not portal: log.error('No such portal: ' + portal_id) continue url = conf.get('.'.join((portal_id, 'external_url'))) if not url: log.error('No external_url provided for ' + portal_id) continue setSite(portal) registry = getUtility(IRegistry) try: recensio_settings = registry.forInterface(IRecensioSettings) except Exception as e: log.exception(e) log.error('Could not get recensio settings for ' + portal_id) continue if recensio_settings.external_portal_url != safe_unicode(url): recensio_settings.external_portal_url = safe_unicode(url) transaction.commit()
def test_components_registered_on_add(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = self.mocker.proxy(PersistentComponents(bases=(getGlobalSiteManager(),))) getSiteManager_mock = self.mocker.replace('zope.component.hooks.getSiteManager') self.expect(getSiteManager_mock(dummy_site)).result(site_manager_mock) # We expect that no components are registered , so look for all registrations self.expect(site_manager_mock.registerUtility(fti, IDexterityFTI, portal_type, info='plone.dexterity.dynamic')).passthrough() self.expect(site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x.portal_type == portal_type), IFactory, portal_type, info='plone.dexterity.dynamic')).passthrough() self.replay() ftiAdded(fti, ObjectAddedEvent(fti, container_dummy, fti.getId())) site_dummy = self.create_dummy(getSiteManager = lambda: site_manager_mock) setSite(site_dummy) setHooks() self.assertNotEquals(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertNotEquals(None, queryUtility(IFactory, name=portal_type))
def test_components_reregistered_on_rename(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = self.mocker.proxy(PersistentComponents(bases=(getGlobalSiteManager(),))) getSiteManager_mock = self.mocker.replace('zope.component.hooks.getSiteManager') self.expect(getSiteManager_mock(dummy_site)).result(site_manager_mock).count(1,None) # First look for unregistration of all local components self.expect(site_manager_mock.unregisterUtility(provided=IDexterityFTI, name=portal_type)).passthrough() # Then look for re-registration of global components self.expect(site_manager_mock.registerUtility(fti, IDexterityFTI, portal_type, info='plone.dexterity.dynamic')).passthrough() self.expect(site_manager_mock.registerUtility( mocker.MATCH(lambda x: isinstance(x, DexterityFactory) and x.portal_type == portal_type), IFactory, portal_type, info='plone.dexterity.dynamic')).passthrough() self.assertEquals('string:${folder_url}/++add++testtype', fti.add_view_expr) self.replay() ftiRenamed(fti, ObjectMovedEvent(fti, container_dummy, fti.getId(), container_dummy, u"newtype")) site_dummy = self.create_dummy(getSiteManager = lambda: site_manager_mock) setSite(site_dummy) setHooks() self.assertNotEquals(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertNotEquals(None, queryUtility(IFactory, name=portal_type))
def main(app): for site in app.objectValues('Plone Site'): if qiext_installed(site): print '== MIGRATING SITE: %s ==' % site.getId() setSite(site) migrate(site) app._p_jar.cacheMinimize()
def test_registry_assignment(self): setRoles(self.portal, TEST_USER_ID, ['Manager']) self.oid = self.portal.invokeFactory('Folder', 'folder') setRoles(self.portal, TEST_USER_ID, ['Member']) folder = self.portal[self.oid] registry = getUtility(IRegistry) if not ISite.providedBy(folder): make_objectmanager_site(folder) setSite(folder) pc = getToolByName(folder, 'portal_catalog') pc.reindexObject(folder, idxs=['object_provides']) enableRegistry(folder) self.assertIn(REGISTRY_NAME, folder.objectIds()) registry = getUtility(IRegistry) self.assertTrue(isinstance(registry, LineageRegistry)) disableRegistry(folder) registry = getUtility(IRegistry) self.assertTrue(isinstance(registry, Registry))
def setUp(self): """Test setUp based on z3c.form.testing.setUp minus their globals. """ from zope.component.testing import setUp setUp() from zope.container.testing import setUp setUp() from zope.component import hooks hooks.setHooks() from zope.traversing.testing import setUp setUp() from zope.publisher.browser import BrowserLanguages from zope.publisher.interfaces.http import IHTTPRequest from zope.i18n.interfaces import IUserPreferredLanguages import zope.component zope.component.provideAdapter( BrowserLanguages, [IHTTPRequest], IUserPreferredLanguages) from zope.site.folder import rootFolder site = rootFolder() from zope.site.site import LocalSiteManager from zope.component.interfaces import ISite if not ISite.providedBy(site): site.setSiteManager(LocalSiteManager(site)) hooks.setSite(site)
def site(site): old_site = getSite() setSite(site) try: yield finally: setSite(old_site)
def evolve(context): root = getRootFolder(context) old_site = getSite() app = root setSite(app) setUpLeaveSchoolDemographics(app) setSite(old_site)
def getNodeText(self, jid, node): app = Zope2.app() text = '' try: portal = app.unrestrictedTraverse(self.portal_id, None) if portal is None: raise DSCException( 'Portal with id %s not found' % self.portal_id) setSite(portal) acl_users = getToolByName(portal, 'acl_users') user_id = unescapeNode(JID(jid).user) user = acl_users.getUserById(user_id) if user is None: raise DSCException( 'Invalid user %s' % user_id) newSecurityManager(None, user) ct = getToolByName(portal, 'portal_catalog') uid, html_id = node.split('#') item = ct.unrestrictedSearchResults(UID=uid) if not item: raise DSCException( 'Content with UID %s not found' % uid) item = ICollaborativelyEditable(item[0].getObject()) text = item.getNodeTextFromHtmlID(html_id) finally: noSecurityManager() setSite(None) return text
def createSiteManager(folder, setsite=False): "Make the given folder a site, and optionally make it the current site." if not ISite.providedBy(folder): folder.setSiteManager(LocalSiteManager(folder)) if setsite: setSite(folder) return traverse(folder, "++etc++site")
def check_root_site_manager(tool): """2.0.x to 2.1.0 upgrade step checker """ portal = aq_parent(aq_inner(tool)) try: # We have to call setSite to make sure we have a site with a proper # acquisition context. setSite(portal) sm = portal.getSiteManager() if sm.utilities.LookupClass != FiveVerifyingAdapterLookup: return True except ComponentLookupError: return True for tool_interface in _BAD_UTILITIES: try: iface = resolve(tool_interface) except ImportError: continue if sm.queryUtility(iface) is not None: return True for tool_id, tool_interface in _TOOL_UTILITIES: tool_obj = getToolByName(portal, tool_id, default=None) try: iface = resolve(tool_interface) except ImportError: continue if tool_obj is not None and sm.queryUtility(iface) is None: return True return False
def pushGlobalRegistry(portal, new=None, name=None): """Set a new global component registry that uses the current registry as a base. If you use this, you *must* call ``popGlobalRegistry()`` to restore the original state. If ``new`` is not given, a new registry is created. If given, you must provide a ``zope.component.globalregistry.BaseGlobalComponents`` object. Returns the new registry. Also ensure that the persistent component registry at ``portal`` has the new global registry as its base. """ site = getSite() localSiteManager = portal.getSiteManager() current = zca.pushGlobalRegistry(new=new) if current not in localSiteManager.__bases__: localSiteManager.__bases__ = (current, ) if site is not None: setHooks() setSite(site) return current
def runTransaction(self, attr, set_committing, *args, **kw): self.beginTransaction() max_db_retries = self.max_db_retries result = None conflict_exception = None for n_try in range(min(1, max_db_retries+1)): try: old_site = getSite() setSite(self.schooltool_app) callable = getattr(self.remote_task, attr) result = callable(*args, **kw) setSite(old_site) if set_committing: try: status = TaskWriteState(self.request.id) status.set_committing() except Exception: pass # don't care really except ConflictError, conflict: # Transaction conflict, let's repeat conflict_exception = conflict except Exception: failure = FormattedTraceback() try: self.abortTransaction() except Exception: failure.append(FormattedTraceback()) raise failure
def setUp(self): portal = self.layer["portal"] self.portal = portal self.request = self.layer["request"] setRoles(portal, TEST_USER_ID, ["Manager"]) setHooks() setSite(portal)
def afterSetUp(self): """ After setup """ setSite(self.portal) provideAdapter(ThemeTaggableMerged) provideAdapter(AttributeAnnotations, provides=IAnnotations, adapts=[IThemeTaggable]) classImplements(ATNewsItem, IThemeTaggable) self.setRoles('Manager') wftool = self.portal.portal_workflow # create a swedish themecentre, an english feed folder and # a swedish feed self.portal.invokeFactory('Folder', id='svthemecentre') obj = self.portal.svthemecentre # turn the folder into a themecentre by calling the promote view # and setting a theme tag obj.unrestrictedTraverse('@@promote2ThemeCentre')() IThemeCentreSchema(obj).tags = 'climate' obj.setLanguage('sv') obj.intro.setLanguage('sv') wftool.doActionFor(obj, 'publish') obj.reindexObject() self.portal.invokeFactory('Folder', id='feedfolder') self.portal.feedfolder.setLanguage('en') self.portal.feedfolder.reindexObject() self.portal.portal_languages.addSupportedLanguage('sv') self.basic_auth = '%s:%s' % (default_user, default_password)
def test_global_components_not_unregistered_on_delete(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = self.mocker.proxy(PersistentComponents(bases=(getGlobalSiteManager(),))) getSiteManager_mock = self.mocker.replace("zope.component.hooks.getSiteManager") self.expect(getSiteManager_mock(dummy_site)).result(site_manager_mock) # Register FTI utility and factory utility self.mock_utility(fti, IDexterityFTI, name=portal_type) self.mock_utility(DexterityFactory(portal_type), IFactory, name=portal_type) # We expect to always be able to unregister without error, even if the # component exists. The factory is only unregistered if it was registered # with info='plone.dexterity.dynamic'. self.expect(site_manager_mock.unregisterUtility(provided=IDexterityFTI, name=portal_type)).passthrough() self.replay() ftiRemoved(fti, ObjectRemovedEvent(fti, container_dummy, fti.getId())) site_dummy = self.create_dummy(getSiteManager=lambda: site_manager_mock) setSite(site_dummy) setHooks() self.assertNotEquals(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertNotEquals(None, queryUtility(IFactory, name=portal_type))
def upgrade( self, upgrade_portal=True, upgrade_all_profiles=True, upgrade_profiles=(), **kw): hooks.setSite(self.context) setRequest(self.context.REQUEST) # initialize portal_skins self.context.setupCurrentSkin(self.context.REQUEST) # setup language for plone 4: see https://docs.plone.org/manage/upgrading/version_specific_migration/p4x_to_p5x_upgrade.html#portal-languages-is-now-a-utility # noqa if getattr(self.context, 'portal_languages', None): self.context.portal_languages(self.context, self.context.REQUEST) self.setup = getToolByName(self.context, 'portal_setup') self.log('Upgrading {0}'.format(self.context)) # setup BrowserLayer, see: https://dev.plone.org/ticket/11673 notify(BeforeTraverseEvent(self.context, self.context.REQUEST)) baseline = self.setup.getBaselineContextID() prof_type, profile_id = baseline.split('-', 1) self.base_profile = profile_id if upgrade_portal: # Do the baseline profile upgrade first self.upgradeProfile(profile_id, **kw) # Upgrade extension profiles if upgrade_all_profiles: if upgrade_profiles: raise ValueError( 'upgrade_profiles conflicts with upgrade_all_profiles') upgrade_profiles = self.setup.listProfilesWithPendingUpgrades() if upgrade_profiles: self.upgradeExtensions(upgrade_profiles, **kw) self.log('Upgraded {0}'.format(self.context))
def test_components_unregistered_on_delete(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = self.mocker.proxy(PersistentComponents(bases=(getGlobalSiteManager(),))) getSiteManager_mock = self.mocker.replace("zope.component.hooks.getSiteManager") self.expect(getSiteManager_mock(dummy_site)).result(site_manager_mock).count(1, None) # We expect to always be able to unregister without error, even if the # components do not exists (as here) self.expect(site_manager_mock.unregisterUtility(provided=IDexterityFTI, name=portal_type)).passthrough() self.expect(site_manager_mock.unregisterUtility(provided=IFactory, name=portal_type)).passthrough() self.replay() # First add the components ftiAdded(fti, ObjectAddedEvent(fti, container_dummy, fti.getId())) # Then remove them again ftiRemoved(fti, ObjectRemovedEvent(fti, container_dummy, fti.getId())) site_dummy = self.create_dummy(getSiteManager=lambda: site_manager_mock) setSite(site_dummy) setHooks() self.assertEquals(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertEquals(None, queryUtility(IFactory, name=portal_type))
def dispatch_request(self, request): request.response = Response() appname = request.path_info_peek() if not appname: appname = DEFAULT_EXTJS_APPLICATION context = queryUtility(IApplicationContext, name=appname) if context is None: raise NotImplementedError('No utility found for IApplicationContext. Application can not be started.') # hooks context to local site manager setSite(context) request.path_info_pop() dispatchname = request.path_info_peek() if dispatchname is None: dispatchname = 'index' dispatcher = queryMultiAdapter((context, request), interfaces.IRootDispatcher, dispatchname) try: if dispatcher is None: raise HTTPNotFound('%s was not found' % dispatchname) notify(events.PreRequestProcessingEvent(context, request)) transaction.begin() dispatcher() transaction.commit() notify(events.PostRequestProcessingEvent(context, request)) return request.response except Exception as e: handler = interfaces.IExceptionHandler(e) return handler()
def global_site_manager(): site = getSite() if site is not None: setSite() yield if site is not None: setSite(site)
def test_components_unregistered_on_delete(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) container_dummy = self.create_dummy() # Mock the lookup of the site and the site manager at the site root dummy_site = self.create_dummy() self.mock_utility(dummy_site, ISiteRoot) site_manager_mock = Mock( wraps=PersistentComponents(bases=(getGlobalSiteManager(),))) from zope.component.hooks import getSiteManager self.patch_global(getSiteManager, return_value=site_manager_mock) # First add the components ftiAdded(fti, ObjectAddedEvent(fti, container_dummy, fti.getId())) # Then remove them again ftiRemoved(fti, ObjectRemovedEvent(fti, container_dummy, fti.getId())) site_dummy = self.create_dummy( getSiteManager=lambda: site_manager_mock ) setSite(site_dummy) setHooks() self.assertEqual(None, queryUtility(IDexterityFTI, name=portal_type)) self.assertEqual(None, queryUtility(IFactory, name=portal_type))
app = makerequest.makerequest(app) admin = app.acl_users.getUser( 'admin') # RR: should we have a dedicated sync admin? admin = admin.__of__(app.acl_users) newSecurityManager(None, admin) site = app.get(site_id, None) if site is None: print "'%s' not found (maybe a typo?)." % site_id print "To create a site call 'import_structure' first." raise ValueError setSite(site) # enable lookup of local components targetfolder = site.communities source = open(FILE_NAME, 'r') raw = json.load(source) rct_data = defaultdict(dict) # cast the raw data into some nested structure for easy access later for item in raw: rct_data[item['model']][item['pk']] = item.copy() rct_communities = rct_data['rct.community'].copy() def preparedata(values):
def tearDown(test): setSite() catalogTearDown(test)
def index_batch(remove, index, positions, es=None): if es is None: from collective.elasticsearch.es import ElasticSearchCatalog es = ElasticSearchCatalog(api.portal.get_tool('portal_catalog')) setSite(api.portal.get()) conn = es.connection bulk_size = es.get_setting('bulk_size', 50) if len(remove) > 0: bulk_data = [] for uid in remove: bulk_data.append({ 'delete': { '_index': es.index_name, '_type': es.doc_type, '_id': uid } }) es.connection.bulk( index=es.index_name, doc_type=es.doc_type, body=bulk_data) if len(index) > 0: if type(index) in (list, tuple, set): # does not contain objects, must be async, convert to dict index = dict([(k, None) for k in index]) bulk_data = [] for uid, obj in index.items(): if obj is None: obj = uuidToObject(uid) if obj is None: continue bulk_data.extend([{ 'index': { '_index': es.index_name, '_type': es.doc_type, '_id': uid } }, get_index_data(obj, es)]) if len(bulk_data) % bulk_size == 0: conn.bulk( index=es.index_name, doc_type=es.doc_type, body=bulk_data) bulk_data = [] if len(bulk_data) > 0: conn.bulk( index=es.index_name, doc_type=es.doc_type, body=bulk_data) if len(positions) > 0: bulk_data = [] index = getIndex(es.catalogtool._catalog, 'getObjPositionInParent') for uid, ids in positions.items(): if uid == '/': parent = getSite() else: parent = uuidToObject(uid) if parent is None: logger.warn('could not find object to index positions') continue for _id in ids: ob = parent[_id] wrapped_object = get_wrapped_object(ob, es) try: value = index.get_value(wrapped_object) except Exception: continue bulk_data.extend([{ 'update': { '_index': es.index_name, '_type': es.doc_type, '_id': IUUID(ob) } }, { 'doc': { 'getObjPositionInParent': value } }]) if len(bulk_data) % bulk_size == 0: conn.bulk( index=es.index_name, doc_type=es.doc_type, body=bulk_data) bulk_data = [] if len(bulk_data) > 0: conn.bulk( index=es.index_name, doc_type=es.doc_type, body=bulk_data)
# Set the zope component site # # Assumptions: # You have an "app" object. site = app['mysiteid'] # noqa from zope.component.hooks import setSite setSite(site)
def afterSetUp(self): setSite(self.app.site) self.app.site.setupCurrentSkin(self.app.REQUEST) newSecurityManager(None, UnrestrictedUser('god', '', ['Manager'], ''))
def index_batch(remove, index, positions, es=None): # noqa: C901 if es is None: from collective.elasticsearch.es import ElasticSearchCatalog es = ElasticSearchCatalog(api.portal.get_tool('portal_catalog')) setSite(api.portal.get()) conn = es.connection bulk_size = es.get_setting('bulk_size', 50) if len(remove) > 0: bulk_data = [] for uid in remove: bulk_data.append({ 'delete': { '_index': es.index_name, '_type': es.doc_type, '_id': uid } }) result = es.connection.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data) if "errors" in result and result["errors"] is True: logger.error("Error in bulk indexing removal: %s" % result) if len(index) > 0: if type(index) in (list, tuple, set): # does not contain objects, must be async, convert to dict index = dict([(k, None) for k in index]) bulk_data = [] for uid, obj in index.items(): # If content has been moved (ie by a contentrule) then the object # passed here is the original object, not the moved one. # So if there is a uuid, we use this to get the correct object. # See https://github.com/collective/collective.elasticsearch/issues/65 # noqa if uid is not None: obj = uuidToObject(uid) if obj is None: obj = uuidToObject(uid) if obj is None: continue bulk_data.extend([{ 'index': { '_index': es.index_name, '_type': es.doc_type, '_id': uid } }, get_index_data(obj, es)]) if len(bulk_data) % bulk_size == 0: result = conn.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data) if "errors" in result and result["errors"] is True: logger.error("Error in bulk indexing: %s" % result) bulk_data = [] if len(bulk_data) > 0: result = conn.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data) if "errors" in result and result["errors"] is True: logger.error("Error in bulk indexing: %s" % result) if len(positions) > 0: bulk_data = [] index = getIndex(es.catalogtool._catalog, 'getObjPositionInParent') for uid, ids in positions.items(): if uid == '/': parent = getSite() else: parent = uuidToObject(uid) if parent is None: logger.warn('could not find object to index positions') continue for _id in ids: ob = parent[_id] wrapped_object = get_wrapped_object(ob, es) try: value = index.get_value(wrapped_object) except Exception: continue bulk_data.extend([{ 'update': { '_index': es.index_name, '_type': es.doc_type, '_id': IUUID(ob) } }, { 'doc': { 'getObjPositionInParent': value } }]) if len(bulk_data) % bulk_size == 0: conn.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data) bulk_data = [] if len(bulk_data) > 0: conn.bulk(index=es.index_name, doc_type=es.doc_type, body=bulk_data)
alsoProvides(request, layer) # some initial script setup site_id = os.environ.get("SITE_ID", "Plone") print("Using site id: {0}".format(site_id)) compile_path = os.environ.get("COMPILE_DIR", "") print("Target compile path: {0}".format(compile_path or "fetch from bundles")) # app was injected by the script calling instance current = app # noqa for part in site_id.split("/"): current = current[part] portal = current setSite(portal) applyBrowserLayers(portal) # start the juicy stuff temp_resource_folder = "temp_resources" registry = getUtility(IRegistry) bundles = registry.collectionOfInterface(IBundleRegistry, prefix="plone.bundles", check=False) resources = registry.collectionOfInterface(IResourceRegistry, prefix="plone.resources", check=False) lessvariables = registry.records["plone.lessvariables"].value GRUNTFILE_TEMPLATE = """// This file is generated by "plone-compile-resources"
default='applications', help= 'the ID of the folder within the site in which to create the applications; defaults to "applications"', # noqa ) parser.add_argument( '--skip-existing', dest='skip_existing', default=False, help= 'set to True to skip creating the application if an application with the same ID already exists in the site; defaults to False', # noqa ) args, _ = parser.parse_known_args() # Sets the current site as the active site site_id = args.site_id setSite(app[site_id]) # noqa site = getSite() # noqa # ensure folder exists toplevel_items = site.items() if 'applications' not in [id for id, obj in toplevel_items]: folder = api.content.create( container=site, type='Folder', id=folder_id, title='Applications', ) month_values = { 'January': 1, 'February': 2,
def reinstall_product(self, site, product): setSite(self.app[site]) qi = self.app[site].portal_quickinstaller self.log(site, "Reinstalling: " + str(product)) qi.reinstallProducts([product]) transaction.commit()
def _callFUT(self, site): from zope.component.hooks import setSite return setSite(site)
def setUp(self): super(TestAcceptTaskStorageManager, self).setUp() site = self.create_dummy(getSiteManager=getSiteManager) alsoProvides(site, IAttributeAnnotatable) setSite(site)
def upgrade_profile(self, site, product): setSite(self.app[site]) qi = self.app[site].portal_quickinstaller self.log(site, "Upgrading: " + str(product)) qi.upgradeProduct(product) transaction.commit()