Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow values to be specified in a <records /> block

svn path=/plone.app.registry/trunk/; revision=34859
  • Loading branch information...
commit eaa3897748aff04c473a318c426fd1b9a3e97b19 1 parent a01c9ee
@MatthewWilkes MatthewWilkes authored
View
11 README.txt
@@ -226,9 +226,14 @@ If you just want to use the interface as a template you can supply a
which will generate fields named ``my.zoo.entryPrice`` and
``my.zoo.messageOfTheDay``.
-(Note that there is no way to create multiple records from an interface and
-set their values at the same time, but you can use the ``default`` attribute
-of each field to achieve a similar effect.)
+In order to set the values of the fields created by a <records /> directive
+you must provide ``value`` entries with keys corresponding to the fields on
+the interface, as follows:
+
+ <records interface="my.package.interfaces.IZooSettings" prefix="my.zoo">
+ <value key="entryPrice">40</value>
+ <value key="messageOfTheDay">We've got lions and tigers!</value>
+ </records>
Values can be set as above using the full record name. However, we can also
explicitly state that we are setting a record bound to an interface, like so::
View
8 docs/HISTORY.txt
@@ -4,13 +4,17 @@ Changelog
1.0b2 - unreleased
------------------
-* Improved the look and feel of the registry records control panel.
- [optilude]
+* Allow <value> elements inside <records> if they contain a key attribute.
+ This uses the record importer to set the values after creation.
+ [MatthewWilkes]
* Add a prefix attribute to the <records /> importer to take advantage of the
interfaces-as-templates pattern from plone.registry
[MatthewWilkes]
+* Improved the look and feel of the registry records control panel.
+ [optilude]
+
1.0b1 - 2009-08-02
------------------
View
25 plone/app/registry/exportimport/handler.py
@@ -74,7 +74,11 @@ def importRecord(self, node):
fieldName = str(node.get('field', ''))
if not name and (interfaceName and fieldName):
- name = "%s.%s" % (interfaceName, fieldName,)
+ prefix = node.get('prefix', None)
+ if prefix is None:
+ prefix = interfaceName
+
+ name = "%s.%s" % (prefix, fieldName,)
if not name:
raise NameError("No name given for <record /> node!")
@@ -163,7 +167,6 @@ def importRecord(self, node):
if existing_record is not None:
if change_field:
existing_record.field = field
-
existing_value = existing_record.value
if change_field or value != existing_value:
@@ -199,13 +202,31 @@ def importRecords(self, node):
interface = resolve(interfaceName)
omit = []
+ values = [] # Fields that should have their value set as they don't exist yet
+
for child in node:
if child.tag.lower() == 'omit':
if child.text:
omit.append(unicode(child.text))
+ elif child.tag.lower() == 'value':
+ values.append(child)
# May raise TypeError
self.context.registerInterface(interface, omit=tuple(omit), prefix=prefix)
+
+ if not values:
+ # Skip out if there are no value records to handle
+ return
+
+ # The prefix we ended up needs to be found
+ if prefix is None:
+ prefix = interface.__identifier__
+
+ for value in values:
+ field = ElementTree.Element("record", interface=interface, field=value.attrib["key"], prefix=prefix)
+ field.append(value)
+ self.importRecord(field)
+
class RegistryExporter(object):
View
21 plone/app/registry/tests/test_exportimport.py
@@ -169,6 +169,27 @@ def test_import_records_with_prefix(self):
self.failUnless('plone.app.registry.tests.data.SomethingElse.name' in self.registry)
self.failUnless('plone.app.registry.tests.data.SomethingElse.age' in self.registry)
+ def test_import_records_with_values(self):
+ xml = """\
+<registry>
+ <records interface="plone.app.registry.tests.data.ITestSettings" prefix="plone.app.registry.tests.data.SomethingElse">
+ <value key="name">Magic</value>
+ <value key="age">42</value>
+ </records>
+</registry>
+"""
+ context = DummyImportContext(self.site, purge=False)
+ context._files = {'registry.xml': xml}
+
+ importRegistry(context)
+
+ self.assertEquals(2, len(self.registry.records))
+
+ self.failUnless('plone.app.registry.tests.data.SomethingElse.name' in self.registry)
+ self.failUnless('plone.app.registry.tests.data.SomethingElse.age' in self.registry)
+
+ self.assertEqual(self.registry['plone.app.registry.tests.data.SomethingElse.name'], 'Magic')
+ self.assertEqual(self.registry['plone.app.registry.tests.data.SomethingElse.age'], 42)
def test_import_value_only(self):
xml = """\
Please sign in to comment.
Something went wrong with that request. Please try again.