[CMF-checkins] SVN: CMF/trunk/CMFTopic/ Add import support for
topic criteria.
Tres Seaver
tseaver at palladion.com
Sun Nov 20 21:56:43 EST 2005
Log message for revision 40286:
Add import support for topic criteria.
Changed:
U CMF/trunk/CMFTopic/exportimport.py
U CMF/trunk/CMFTopic/tests/test_exportimport.py
-=-
Modified: CMF/trunk/CMFTopic/exportimport.py
===================================================================
--- CMF/trunk/CMFTopic/exportimport.py 2005-11-21 02:46:17 UTC (rev 40285)
+++ CMF/trunk/CMFTopic/exportimport.py 2005-11-21 02:56:43 UTC (rev 40286)
@@ -64,7 +64,6 @@
def import_(self, import_context, subdir, root=False):
""" See IFilesystemImporter
"""
- return
FolderishExporterImporter.import_(self, import_context, subdir, root)
self.encoding = import_context.getEncoding()
@@ -77,12 +76,9 @@
subdir)
if data is not None:
-
dom = parseString(data)
root = dom.firstChild
assert root.tagName == self._ROOT_TAGNAME
-
- self.context.title = self._getNodeAttr(root, 'title', None)
self._updateFromDOM(root)
def _getNodeAttr(self, node, attrname, default=None):
@@ -95,27 +91,27 @@
return value
def _purgeContext(self):
- return
context = self.context
criterion_ids = context.objectIds(context._criteria_metatype_ids())
for criterion_id in criterion_ids:
self.context._delObject(criterion_id)
def _updateFromDOM(self, root):
- return
- for group in root.getElementsByTagName('group'):
- group_id = self._getNodeAttr(group, 'group_id', None)
- predicate = self._getNodeAttr(group, 'predicate', None)
- title = self._getNodeAttr(group, 'title', None)
- description = self._getNodeAttr(group, 'description', None)
- active = self._getNodeAttr(group, 'active', None)
+ for criterion in root.getElementsByTagName('criterion'):
+ c_type = self._getNodeAttr(criterion, 'type', None)
+ field = self._getNodeAttr(criterion, 'field', None)
+ attributes = {}
+ for attribute in criterion.getElementsByTagName('attribute'):
+ name = self._getNodeAttr(attribute, 'name', None)
+ value = self._getNodeAttr(attribute, 'value', None)
+ if name == 'reversed':
+ value = value in ('True', 'true', '1')
+ attributes[name] = value
- self.context.addGroup(group_id,
- predicate,
- title,
- description,
- active == 'True',
- )
+ self.context.addCriterion(field, c_type)
+ added = self.context.getCriterion(field)
+ added.edit(**attributes)
+
def _getExportInfo(self):
context = self.context
criterion_info = []
@@ -144,3 +140,9 @@
return {'criteria': criterion_info,
}
+ def _mustPreserve(self):
+ context = self.context
+ keepers = FolderishExporterImporter._mustPreserve(self)
+ keepers.extend(context.objectItems(context._criteria_metatype_ids()))
+ return keepers
+
Modified: CMF/trunk/CMFTopic/tests/test_exportimport.py
===================================================================
--- CMF/trunk/CMFTopic/tests/test_exportimport.py 2005-11-21 02:46:17 UTC (rev 40285)
+++ CMF/trunk/CMFTopic/tests/test_exportimport.py 2005-11-21 02:56:43 UTC (rev 40286)
@@ -242,6 +242,176 @@
self._compareDOM( text, _MIXED_TOPIC_CRITERIA )
self.assertEqual( content_type, 'text/xml' )
+ def test_import_empty_with_string_criterion(self):
+ topic = self._makeTopic('empty', False).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, encoding='ascii')
+ context._files['test/empty/criteria.xml'] = _STRING_TOPIC_CRITERIA
+
+ adapter.import_(context, 'test', False)
+
+ expected = _CRITERIA_DATA[0]
+ found = topic.listCriteria()
+ self.assertEqual(len(found), 1)
+
+ criterion = found[0]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.Field(), expected[0])
+ self.assertEqual(criterion.value, expected[2]['value'])
+
+ def test_import_empty_with_integer_criterion(self):
+ topic = self._makeTopic('empty', False).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, encoding='ascii')
+ context._files['test/empty/criteria.xml'] = _INTEGER_TOPIC_CRITERIA
+
+ adapter.import_(context, 'test', False)
+
+ expected = _CRITERIA_DATA[1]
+ found = topic.listCriteria()
+ self.assertEqual(len(found), 1)
+
+ criterion = found[0]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.Field(), expected[0])
+ self.assertEqual(criterion.value, expected[2]['value'])
+ self.assertEqual(criterion.direction, expected[2]['direction'])
+
+ def test_import_empty_with_date_criterion(self):
+ topic = self._makeTopic('empty', False).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, encoding='ascii')
+ context._files['test/empty/criteria.xml'] = _DATE_TOPIC_CRITERIA
+
+ adapter.import_(context, 'test', False)
+
+ expected = _CRITERIA_DATA[2]
+ found = topic.listCriteria()
+ self.assertEqual(len(found), 1)
+
+ criterion = found[0]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.Field(), expected[0])
+ self.assertEqual(criterion.value, expected[2]['value'])
+ self.assertEqual(criterion.operation, expected[2]['operation'])
+ self.assertEqual(criterion.daterange, expected[2]['daterange'])
+
+ def test_import_empty_with_list_criterion(self):
+ topic = self._makeTopic('empty', False).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, encoding='ascii')
+ context._files['test/empty/criteria.xml'] = _LIST_TOPIC_CRITERIA
+
+ adapter.import_(context, 'test', False)
+
+ expected = _CRITERIA_DATA[3]
+ found = topic.listCriteria()
+ self.assertEqual(len(found), 1)
+
+ criterion = found[0]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.Field(), expected[0])
+ self.assertEqual(','.join(criterion.value), expected[2]['value'])
+ self.assertEqual(criterion.operator, expected[2]['operator'])
+
+ def test_import_empty_with_sort_criterion(self):
+ topic = self._makeTopic('empty', False).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, encoding='ascii')
+ context._files['test/empty/criteria.xml'] = _SORT_TOPIC_CRITERIA
+
+ adapter.import_(context, 'test', False)
+
+ expected = _CRITERIA_DATA[4]
+ found = topic.listCriteria()
+ self.assertEqual(len(found), 1)
+
+ criterion = found[0]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.field, None)
+ self.assertEqual(criterion.index, expected[0])
+ self.assertEqual(criterion.reversed, bool(expected[2]['reversed']))
+
+ def test_import_empty_with_mixed_criterion(self):
+ topic = self._makeTopic('empty', False).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, encoding='ascii')
+ context._files['test/empty/criteria.xml'] = _MIXED_TOPIC_CRITERIA
+
+ adapter.import_(context, 'test', False)
+
+ found = topic.listCriteria()
+ self.assertEqual(len(found), 3)
+
+ criterion = found[0]
+ expected = _CRITERIA_DATA[0]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.Field(), expected[0])
+ self.assertEqual(criterion.value, expected[2]['value'])
+
+ criterion = found[1]
+ expected = _CRITERIA_DATA[2]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.Field(), expected[0])
+ self.assertEqual(criterion.value, expected[2]['value'])
+ self.assertEqual(criterion.operation, expected[2]['operation'])
+ self.assertEqual(criterion.daterange, expected[2]['daterange'])
+
+ criterion = found[2]
+ expected = _CRITERIA_DATA[4]
+
+ self.assertEqual(criterion.getId(), 'crit__%s' % expected[0])
+ self.assertEqual(criterion.Type(), expected[1])
+ self.assertEqual(criterion.field, None)
+ self.assertEqual(criterion.index, expected[0])
+ self.assertEqual(criterion.reversed, bool(expected[2]['reversed']))
+
+ def test_import_without_purge_leaves_existing_criteria(self):
+
+ topic = self._makeTopic('with_criteria', True).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, purge=False)
+ context._files['test/with_criteria/criteria.xml'
+ ] = _EMPTY_TOPIC_CRITERIA
+
+ self.assertEqual(len(topic.listCriteria()), len(_CRITERIA_DATA))
+ adapter.import_(context, 'test', False)
+ self.assertEqual(len(topic.listCriteria()), len(_CRITERIA_DATA))
+
+ def test_import_with_purge_removes_existing_criteria(self):
+
+ topic = self._makeTopic('with_criteria', True).__of__(self.root)
+ adapter = self._makeOne(topic)
+
+ context = DummyImportContext(topic, purge=True)
+ context._files['test/with_criteria/criteria.xml'
+ ] = _EMPTY_TOPIC_CRITERIA
+
+ self.assertEqual(len(topic.listCriteria()), len(_CRITERIA_DATA))
+ adapter.import_(context, 'test', False)
+ self.assertEqual(len(topic.listCriteria()), 0)
+
_EMPTY_TOPIC_CRITERIA = """\
<?xml version="1.0" ?>
<criteria>
More information about the CMF-checkins
mailing list