def test_auto_migration(self): from BTrees.OOBTree import OOBTree from plone.registry.registry import Registry, Records, _Records from plone.registry.record import Record from plone.registry import field # Create an "old-looking registry" registry = Registry() registry._records = Records(registry) registry._records.data = OOBTree() f = field.TextLine(title=u"Foo") record = Record(f, u"Bar") record.__dict__['field'] = f record.__dict__['value'] = u"Bar" registry._records.data['foo.bar'] = record # Attempt to access it value = registry['foo.bar'] # Migration should have happened self.assertEqual(value, u"Bar") self.assertEqual(registry.records['foo.bar'].field.title, u"Foo") self.assertEqual(registry.records['foo.bar'].value, u"Bar") self.assertFalse(isinstance(registry._records, Records)) self.assertTrue(isinstance(registry._records, _Records))
def create_registry_keys(site, logger): registry = getUtility(IRegistry) keys = [ ('agsci.common.ai_api_key', Record(field.TextLine(title=u'Activity Insight API Key')), u''), ('agsci.common.department_id', Record(field.TextLine(title=u'Department Id')), u''), ('agsci.common.global_public_tags', Record( field.Tuple(title=u'Global Public Tags', value_type=field.TextLine())), ()), ('agsci.common.enhanced_public_tags', Record(field.Bool(title=u'Enhanced Public Tags')), False), ('agsci.common.person_restricted_profile', Record( field.Bool( title=u'Editing of profiles is restricted to administrators?') ), False), ] for (key, record, value) in keys: if key not in registry: record.value = value registry.records[key] = record logger.info("Added key %s" % key) else: logger.info("Key %s exists. Did not add." % key)
def __getitem__(self, name): field = self._getField(name) value = self._values[name] record = Record(field, value, _validate=False) record.__name__ = name record.__parent__ = self.__parent__ return record
def test_character_counter_registry_records_removed(self): title = u'Miscellaneous' step = self.get_upgrade_step(title) self.assertIsNotNone(step) registry = getUtility(IRegistry) BASE_REGISTRY = 'collective.nitf.controlpanel.INITFCharCountSettings.' records = [ BASE_REGISTRY + 'description_max_chars', BASE_REGISTRY + 'description_optimal_chars', BASE_REGISTRY + 'show_description_counter', BASE_REGISTRY + 'show_title_counter', BASE_REGISTRY + 'title_max_chars', BASE_REGISTRY + 'title_optimal_chars', ] # simulate state on previous version for r in records: registry.records[r] = Record(field.TextLine(title=u'Test')) self.assertIn(r, registry) # execute upgrade step and verify changes were applied self.execute_upgrade_step(step) for r in records: self.assertNotIn(r, registry)
def updateRecordFromDict(record, data): for name in record.__schema__.names(): if name in ['last_compilation']: continue if name in data: # almost all string data needs to be str, not unicode val = data[name] if isinstance(val, unicode): val = val.encode('utf-8') if isinstance(val, list): newval = [] for item in val: if isinstance(item, unicode): item = item.encode('utf-8') newval.append(item) val = newval full_name = record.__prefix__ + name try: record.__registry__[full_name] = val except (AttributeError, KeyError) as ex: # noqa # upgrade record on the fly, try to at least if not val: continue if type(val) == bool: record.__registry__.records[full_name] = Record( field.Bool(title=u""), val) else: raise
def test_migrate_settings(self): registry = getUtility(IRegistry) del registry.records['castle.file_upload_fields'] record = Record(List(), [u'title']) registry.records['castle.required_file_upload_fields'] = record self.assertIn('castle.required_file_upload_fields', registry.records._fields) self.assertIn('castle.required_file_upload_fields', registry.records._values) upgrade_2_2_0.upgrade(self.portal) self.assertEquals(len(registry['castle.file_upload_fields']), 4) self.assertTrue(registry['castle.file_upload_fields'] is not None) self.assertTrue(registry.get('castle.required_file_upload_fields') is None) # check registry cleaned up as well self.assertIn('castle.file_upload_fields', registry.records._fields) self.assertIn('castle.file_upload_fields', registry.records._values) self.assertNotIn('castle.required_file_upload_fields', registry.records._fields) self.assertNotIn('castle.required_file_upload_fields', registry.records._values) # also, test we can safely run it again without overwritting existing values fields = deepcopy(registry['castle.file_upload_fields']) fields.append({ u'name': u'foobar', u'label': u'Foobar', u'widget': u'text', u'required': u'false', u'for-file-types': u'*' }) registry['castle.file_upload_fields'] = fields upgrade_2_2_0.upgrade(self.portal) self.assertEquals(len(registry['castle.file_upload_fields']), 5)
def test_set_valid_registry_record(self): """Test that setting a valid registry record succeeds.""" registry = getUtility(IRegistry) registry.records['plone.api.plone_power'] = Record( field.TextLine(title=u"Plone's Power")) portal.set_registry_record('plone.api.plone_power', u'awesome') self.assertEqual(registry['plone.api.plone_power'], u'awesome')
def registerInterface(self, interface, omit=(), prefix=None): if prefix is None: prefix = interface.__identifier__ if not prefix.endswith("."): prefix += '.' for name, field in getFieldsInOrder(interface): if name in omit or field.readonly: continue record_name = prefix + name persistent_field = queryAdapter(field, IPersistentField) if persistent_field is None: raise TypeError("There is no persistent field equivalent for " "the field `%s` of type `%s`." % (name, field.__class__.__name__)) persistent_field.interfaceName = interface.__identifier__ persistent_field.fieldName = name value = persistent_field.default # Attempt to retain the exisiting value if record_name in self.records: existing_record = self.records[record_name] value = existing_record.value bound_field = persistent_field.bind(existing_record) try: bound_field.validate(value) except: value = persistent_field.default self.records[record_name] = Record(persistent_field, value, _validate=False)
def fix_double_smaxage(context): """Fix caching definition. plone.resource.maxage has title and description from shared maxage. See https://github.com/plone/Products.CMFPlone/issues/1989 """ from plone.registry.interfaces import IPersistentField from plone.registry.record import Record from plone.registry import field from plone.registry import FieldRef from zope.component import queryAdapter registry = getUtility(IRegistry) # If these three registry records are not defined, # we do no fix. maxage = 'plone.app.caching.strongCaching.plone.resource.maxage' def_maxage = 'plone.app.caching.strongCaching.maxage' def_smaxage = 'plone.app.caching.strongCaching.smaxage' for name in (maxage, def_maxage, def_smaxage): if name not in registry: return if registry.records[maxage].field.recordName != def_smaxage: # no fix needed return field_ref = FieldRef(def_maxage, registry.records[def_maxage].field) old_value = registry[maxage] registry.records[maxage] = Record(field_ref, old_value) logger.info('Fixed {} to refer to {}.'.format(maxage, def_maxage))
def add_password_reset_registry_entry(context): portal_registry = api.portal.get_tool('portal_registry') portal_registry.records[ 'ploneintranet.userprofile.enable_password_reset'] = Record( Bool(), True) assert (portal_registry['ploneintranet.userprofile.enable_password_reset'] is True)
def add_solr_disabled_field(context): ''' Add a registry record if it is not there ''' pr = api.portal.get_tool('portal_registry') key = 'ploneintranet.search.solr.disabled' if key in pr.records: return pr.records[key] = Record(Bool(), False)
def setup_default_grouping(context): ''' Add a registry record if it is not there ''' pr = api.portal.get_tool('portal_registry') key = 'ploneintranet.workspace.default_grouping' if key in pr.records: return pr.records[key] = Record(TextLine(), u'folder')
def test_fallback_to_default_settings_until_upgrade_done(self): registry = getUtility(IRegistry) del registry.records['castle.file_upload_fields'] record = Record(List(), [u'title', u'description']) registry.records['castle.required_file_upload_fields'] = record fields = get_upload_fields(registry) self.assertTrue(fields[0]['required']) self.assertTrue(fields[1]['required']) self.assertEquals(len(fields), 4)
def test_get_existing_registry_record(self): """Test that existing registry records are returned correctly.""" registry = getUtility(IRegistry) registry.records['plone.api.norris_power'] = Record( field.TextLine(title=u"Chuck Norris' Power")) registry.records['plone.api.unset'] = Record( field.TextLine(title=u"An unset field")) registry['plone.api.norris_power'] = u'infinite' self.assertEqual( portal.get_registry_record('plone.api.norris_power'), u'infinite', ) self.assertEqual( portal.get_registry_record('plone.api.unset'), None, )
def test_new_record_modified(self): registry = getUtility(IRegistry) registry.records['collective.fingerpointing.new_field'] = Record( field.TextLine(title=u'New field')) with LogCapture('collective.fingerpointing', level=INFO) as log: api.portal.set_registry_record('collective.fingerpointing.new_field', u'Descrição') # noqa: E501 log.check( ('collective.fingerpointing', 'INFO', 'user=test_user_1_ ip=None action=modify object=<Record collective.fingerpointing.new_field> value=Descrição'), # noqa: E501 )
def setUp(self): self.app = self.layer['app'] self.portal = self.layer['portal'] self.portal_url = self.portal.absolute_url() setRoles(self.portal, TEST_USER_ID, ['Manager']) self.api_session = RelativeSession(self.portal_url) self.api_session.headers.update({'Accept': 'application/json'}) self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) registry = getUtility(IRegistry) record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar'] = record for counter in range(1, 100): record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar' + str(counter)] = record transaction.commit()
def setUp(self): self.app = self.layer["app"] self.portal = self.layer["portal"] self.portal_url = self.portal.absolute_url() setRoles(self.portal, TEST_USER_ID, ["Manager"]) self.api_session = RelativeSession(self.portal_url, test=self) self.api_session.headers.update({"Accept": "application/json"}) self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) registry = getUtility(IRegistry) record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar"] = record for counter in range(1, 100): record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar" + str(counter)] = record transaction.commit()
def test_batched(self): registry = Registry() for counter in range(1, 100): record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar' + str(counter)] = record obj = self.serialize(registry) expected = ['@id', 'items_total', 'items', 'batching'] self.assertEqual(set(expected), set(obj)) self.assertEqual(obj['items_total'], len(list(range(1, 100))))
def test_batched(self): registry = Registry() for counter in range(1, 100): record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar" + str(counter)] = record obj = self.serialize(registry) expected = ["@id", "items_total", "items", "batching"] self.assertEqual(set(expected), set(obj)) self.assertEqual(obj["items_total"], len(list(range(1, 100))))
def test_structure(self): registry = Registry() record = Record(field.TextLine(title=u"Foo Bar"), u"Lorem Ipsum") registry.records['foo.bar'] = record obj = self.serialize(registry) item = obj['items'][0] self.assertEqual(set(item.keys()), set(['name', 'value'])) self.assertEqual(item['name'], 'foo.bar') self.assertEqual(item['value'], u"Lorem Ipsum")
def test_structure(self): registry = Registry() record = Record(field.TextLine(title="Foo Bar"), "Lorem Ipsum") registry.records["foo.bar"] = record obj = self.serialize(registry) item = obj["items"][0] self.assertEqual(set(item), {"name", "value", "schema"}) self.assertEqual(set(item["schema"]), {"properties"}) self.assertEqual(item["name"], "foo.bar") self.assertEqual(item["value"], "Lorem Ipsum")
def test_set_no_value_param_for_existing_record(self): """Test that calling portal.set_registry_record with a name parameter for an existing record, but without a value, raises an Exception. """ registry = getUtility(IRegistry) registry.records['plone.api.plone_power'] = Record( field.TextLine(title=u"Plone's Power")) from plone.api.exc import MissingParameterError with self.assertRaises(MissingParameterError): portal.set_registry_record(name='plone.api.plone_power')
def test_update_several_registry_records(self): registry = getUtility(IRegistry) record = Record(field.TextLine(title="Foo Bar Baz"), "Lorem Ipsum Dolor") registry.records["foo.bar.baz"] = record transaction.commit() payload = {"foo.bar": "lorem ipsum", "foo.bar.baz": "lorem ipsum dolor"} response = self.api_session.patch("/@registry", json=payload) transaction.commit() self.assertEqual(response.status_code, 204) self.assertEqual(registry["foo.bar"], "lorem ipsum") self.assertEqual(registry["foo.bar.baz"], "lorem ipsum dolor")
def test_provider_without_url(self): """Deal with a provider set without an url template""" from plone.registry import field from plone.registry.record import Record reg = self.reg value = {u"url": u"", u"logo": u"", u"id": u"foo"} record = Record(field.Dict(title=u"Foo"), value) reg.records["sc.social.bookmarks.providers.foo"] = record self.set_up_social_bookmarks(providers=("foo", )) viewlet = SocialBookmarksViewlet(self.page, self.request, None) viewlet.update() html = viewlet.render() self.assertFalse("foo" in html)
def test_update_several_registry_records(self): registry = getUtility(IRegistry) record = Record(field.TextLine(title=u"Foo Bar Baz"), u"Lorem Ipsum Dolor") registry.records['foo.bar.baz'] = record transaction.commit() payload = {'foo.bar': 'lorem ipsum', 'foo.bar.baz': 'lorem ipsum dolor'} response = self.api_session.patch('/@registry', json=payload) transaction.commit() self.assertEqual(response.status_code, 204) self.assertEqual(registry['foo.bar'], 'lorem ipsum') self.assertEqual(registry['foo.bar.baz'], 'lorem ipsum dolor')
def test_fix_double_smaxage(self): from plone.registry.interfaces import IRegistry from plone.registry.record import Record from plone.registry import FieldRef from plone.app.upgrade.v50.final import fix_double_smaxage # Run the upgrade before plone.app.caching is installed, # to check that it does not harm. portal_setup = self.layer['portal'].portal_setup fix_double_smaxage(portal_setup) registry = getUtility(IRegistry) maxage = 'plone.app.caching.strongCaching.plone.resource.maxage' def_maxage = 'plone.app.caching.strongCaching.maxage' def_smaxage = 'plone.app.caching.strongCaching.smaxage' # Install default caching profile. portal_setup.runAllImportStepsFromProfile( 'plone.app.caching:default', ) self.assertTrue(def_maxage in registry) self.assertTrue(def_smaxage in registry) # Run upgrade. fix_double_smaxage(portal_setup) # Install the with-caching-proxy settings. portal_setup.runAllImportStepsFromProfile( 'plone.app.caching:with-caching-proxy', ) # Run upgrade. fix_double_smaxage(portal_setup) # Old situation had maxage referencing the s-maxage definition: field_ref = FieldRef(def_smaxage, registry.records[def_smaxage].field) registry.records[maxage] = Record(field_ref, 999) self.assertEqual(registry.records[maxage].field.recordName, def_smaxage) self.assertEqual(registry[maxage], 999) self.assertIn('shared', registry.records[maxage].field.title.lower()) # Run upgrade. fix_double_smaxage(portal_setup) # Test that this fixes the reference and keeps the value. self.assertEqual(registry.records[maxage].field.recordName, def_maxage) self.assertEqual(registry[maxage], 999) self.assertNotIn('shared', registry.records[maxage].field.title.lower()) # Run upgrade. fix_double_smaxage(portal_setup) self.assertEqual(registry.records[maxage].field.recordName, def_maxage) self.assertEqual(registry[maxage], 999)
def test_event_with_package_uninstalled(self): self.registry.records['foo'] = Record(field.ASCIILine(), 'foo') qi = get_installer(self.portal, self.request) qi.uninstall_product(PROJECTNAME) # should not raise exceptions on changes self.registry['foo'] = 'bar'
def test_event_with_package_uninstalled(self): self.registry.records['foo'] = Record(field.ASCIILine(), 'foo') qi = self.portal['portal_quickinstaller'] qi.uninstallProducts(products=[PROJECTNAME]) # should not raise exceptions on changes self.registry['foo'] = 'bar'