def test_handler(self): """ """ with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f: fhir_str = f.read() fhir_field = getFields(ITestToken)['resource'] # Test: available as adapter resource_handler = queryMultiAdapter((fhir_field, ), IToUnicode) self.assertIsNotNone(resource_handler) self.assertIsInstance(resource_handler, handler.JSONToUnicode) self.assertIsInstance(resource_handler.context, JSON) fhir_value = fhir_field.fromUnicode(fhir_str) self.assertIsInstance(resource_handler.toUnicode(fhir_value), unicode) # Test: available as Uitility fhir_hanlder_util = queryUtility(IFieldExportImportHandler, name='plone.app.jsonfield.field.JSON') self.assertIsNotNone(fhir_hanlder_util) self.assertEqual(fhir_hanlder_util, handler.JSONHandler) class ITestPatient(model.Schema): model.load(os.path.join(BASE_TEST_PATH, 'schema', 'patient.xml')) fhir_field2 = getFields(ITestToken)['resource'] self.assertEqual(fhir_field2.__class__, fhir_field.__class__) xml_schema = serializeSchema(ITestToken) self.assertIn( '<field name="resource" type="plone.app.jsonfield.field.JSON">', xml_schema)
def updateSchema(self): sch = getUtility(ISchemaType, self.__context__.oid) sch.spec = self.schema sch.Type.__schema__ = self.schema interface.classImplements(sch.Type, self.schema) self.model = unicode(supermodel.serializeSchema(self.schema))
def definition_schema_handler(context, event): """ Event handler for object modification parses XML schema definition with plone.supermodel (via local ISchemaSaver utility), then saves: * context.signature (persistent attribute) * context._v_schema (convenient caching for current thread)** ** other threads can load this on-demand, that is left to the FormDefinition.schema property implementation. This handler also normalizes the XML schema string in context.entry_schema with context.entry_schema.strip() (just in case it is needed). Does nothing if entry_schema is absent/empty. """ if context.entry_schema: ## serialization fixups and normalization: schema = context.entry_schema = context.entry_schema.strip() schema = schema.replace('\r', '') # remove all CRs if (schema == DEFAULT_MODEL_XML and context.signature == DEFAULT_SIGNATURE): return # initial schema, not modification of schema; done. # normalize xml: reserialize as a check on manual edits: schema = serializeSchema(parse_schema(schema)) if context.entry_schema != schema: context.entry_schema = schema # save normalized xml saver = queryUtility(ISchemaSaver) # get local utility sig = context.signature = saver.add(schema) # persist xml/sig in saver context._v_schema = (sig, saver.load(saver.get(sig))) # cache schema update_form_entries(context)
def test_write_selectable_schema(self): model = loadString(self.selectable_schema) set_selectable_fields(model.schema, ['spare']) xml = self.deprettify(serializeSchema(model.schema)) self.assertIn( '<people:column selectable="true"><people:item>spare', xml )
def copy_schema(schema): """ Make a transient copy of a schema object; useful for applications modifying the schema (e.g. plone.schemaeditor) to avoid modifying a schema in-place. Usage pattern should be to get a copy, make changes to the schema on the copy, and then replace the original. """ return parse_schema(serializeSchema(schema))
def set_actions(context, schema): # fix setting widgets schema.setTaggedValue('plone.autoform.widgets', {}) # serialize the current schema snew_schema = serializeSchema(schema) # store the current schema context.actions_model = snew_schema context.notifyModified()
def set_actions(context, schema): # fix setting widgets schema.setTaggedValue('plone.autoform.widgets', {}) # serialize the current schema snew_schema = serializeSchema(schema) # store the current schema context.actions_model = snew_schema context.notifyModified()
def copy_schema(schema): """ Make a transient copy of a schema object; useful for applications modifying the schema (e.g. plone.schemaeditor) to avoid modifying a schema in-place. Usage pattern should be to get a copy, make changes to the schema on the copy, and then replace the original. """ return parse_schema(serializeSchema(schema))
def set_actions(context, schema): # fix setting widgets schema.setTaggedValue("plone.autoform.widgets", {}) # serialize the current schema snew_schema = serializeSchema(schema) # store the current schema context.actions_model = snew_schema context.notifyModified() context.reindexObject(idxs=["modified"])
def set_fields(context, schema): # serialize the current schema snew_schema = serializeSchema(schema) # store the current schema context.fields_model = snew_schema # Plain reindexObject() will call notifyModified and then reindex all. # We know nothing has changed that needs indexing, # except that we must update the modified date, and reindex its index. context.notifyModified() context.reindexObject(idxs=["modified"])
def test_write_title_schema(self): model = loadString(self.title_xml) set_title_fields(model.schema, ['stop', 'hammertime']) xml = serializeSchema(model.schema) self.assertIn('<people:item>stop</people:item>', xml) self.assertIn('<people:item>hammertime</people:item>', xml) self.assertTrue( xml.find('<people:item>stop</people:item>') < xml.find('<people:item>hammertime</people:item>') )
def test_dynamic_schema_identification(self): from hashlib import md5 from plone.supermodel import serializeSchema iface = ITestSchema2 oldname = iface.__name__ iface.__name__ = '' assert iface.__module__ == 'uu.retrieval.tests.test_schema' expected_signature = md5(serializeSchema(iface).strip()).hexdigest() expected_name = 'I%s' % expected_signature expected_identifier = '.'.join((iface.__module__, expected_name)) assert identify_interface(ITestSchema2) == expected_identifier iface.__name__ = oldname # clean up
def test_dynamic_schema_identification(self): from hashlib import md5 from plone.supermodel import serializeSchema iface = ITestSchema2 oldname = iface.__name__ iface.__name__ = "" assert iface.__module__ == "uu.retrieval.tests.test_schema" expected_signature = md5(serializeSchema(iface).strip()).hexdigest() expected_name = "I%s" % expected_signature expected_identifier = ".".join((iface.__module__, expected_name)) assert identify_interface(ITestSchema2) == expected_identifier iface.__name__ = oldname # clean up
def test_write_order_schema(self): model = loadString(self.order_xml) set_order(model.schema, ['third', 'second', 'first']) xml = serializeSchema(model.schema) self.assertIn('<people:item>third</people:item>', xml) self.assertIn('<people:item>second</people:item>', xml) self.assertIn('<people:item>first</people:item>', xml) self.assertTrue( xml.find('<people:item>third</people:item>') < xml.find('<people:item>second</people:item>') < xml.find('<people:item>first</people:item>') )
def serializeSchemaContext(schema_context, event=None): """ Serializes the schema associated with a schema context. The serialized schema is saved to the model_source property of the FTI associated with the schema context. """ # find the FTI and model toolkit = schema_context.toolkit #schemaName = schema_context.schemaName schema = schema_context.schema #model = fti.lookupModel() # synchronize changes to the model # FIXME: activate this to improve schema caching # syncSchema(schema, model.schemata[schemaName], overwrite=True) toolkit.schema = serializeSchema(schema)
def add(self, schema): """ given schema as xml or interface, save to mapping, return md5 signature for the saved xml serialization. """ if IInterface.providedBy(schema): xml = serializeSchema(schema).strip() signature = self.signature(xml) if signature != DEFAULT_SIGNATURE: self.invalidate(schema) # if schema modified, del stale sig loaded[signature] = schema else: xml = schema.strip() signature = self.signature(xml) if signature not in self: self[signature] = xml return signature
def add(self, schema): """ given schema as xml or interface, save to mapping, return md5 signature for the saved xml serialization. """ if IInterface.providedBy(schema): xml = serializeSchema(schema).strip() signature = self.signature(xml) if signature != DEFAULT_SIGNATURE: self.invalidate(schema) # if schema modified, del stale sig loaded[signature] = schema else: xml = schema.strip() signature = self.signature(xml) if signature not in self: self[signature] = xml return signature
def test_write_details_schema(self): model = loadString(self.details_schema) set_detail_fields(model.schema, { 'top': ['image', 'lastname', 'firstname'] }) xml = self.deprettify(serializeSchema(model.schema)) self.assertIn('<people:details position="top">', xml) self.assertIn('<people:item>lastname</people:item>', xml) self.assertIn('<people:item>firstname</people:item>', xml) self.assertTrue( xml.find('<people:item>lastname</people:item>') < xml.find('<people:item>firstname</people:item>') ) self.assertEqual(xml.count('<people:details'), 1)
def test_write_column_schema(self): model = loadString(self.column_xml) set_columns( model.schema, [['first'], ['third', 'fourth']] ) set_custom_column_titles( model.schema, ['one', 'two'] ) xml = self.deprettify(serializeSchema(model.schema)) self.assertIn('<people:column title="one"><people:item>first<', xml) self.assertIn('<people:column title="two"><people:item>third<', xml) self.assertIn('</people:item><people:item>fourth</people:item>', xml) self.assertTrue( xml.find('<people:item>first</people:item>') < xml.find('<people:item>third</people:item>') < xml.find('<people:item>fourth</people:item>') )
def test_write_render_options(self): model = loadString(self.render_options_xml) set_list_render_options(model.schema, { 'one': { 'image_size': 'small', } }) set_detail_render_options(model.schema, { 'one': { 'image_size': 'tiny', } }) xml = serializeSchema(model.schema) self.assertIn( '<people:item render-options="image_size=small">one', xml ) self.assertIn( '<people:item render-options="image_size=tiny">one', xml )
import uuid from hashlib import md5 from plone.supermodel import serializeSchema from zope.interface.interfaces import IInterface # misc type check stuff: not_string = lambda v: not isinstance(v, basestring) iterable = lambda v: hasattr(v, '__iter__') is_multiple = lambda v: not_string(v) and iterable(v) _itemmerge = lambda a, b: dict(a.items() + b.items()) mergedict = lambda s: reduce(_itemmerge, s) signature = lambda iface: md5(serializeSchema(iface).strip()).hexdigest() def identify_dynamic_interface(iface): name = 'I%s' % signature(iface) return '.'.join((iface.__module__, name)) def identify_interface(iface): if not IInterface.providedBy(iface): raise ValueError('Interface class not provided: %s' % repr(iface)) if iface.__name__: return iface.__identifier__ # will have fully qualified dottedname return identify_dynamic_interface(iface) # fallback to identify dynamic
def signature(self, schema): if IInterface.providedBy(schema): schema = serializeSchema(schema) return md5(schema.strip()).hexdigest()
def set_fields(context, schema): # serialize the current schema snew_schema = serializeSchema(schema) # store the current schema context.fields_model = snew_schema context.notifyModified()
import uuid from hashlib import md5 from plone.supermodel import serializeSchema from zope.interface.interfaces import IInterface # misc type check stuff: not_string = lambda v: not isinstance(v, basestring) iterable = lambda v: hasattr(v, '__iter__') is_multiple = lambda v: not_string(v) and iterable(v) _itemmerge = lambda a, b: dict(a.items() + b.items()) mergedict = lambda s: reduce(_itemmerge, s) signature = lambda iface: md5(serializeSchema(iface).strip()).hexdigest() def identify_dynamic_interface(iface): name = 'I%s' % signature(iface) return '.'.join((iface.__module__, name)) def identify_interface(iface): if not IInterface.providedBy(iface): raise ValueError('Interface class not provided: %s' % repr(iface)) if iface.__name__: return iface.__identifier__ # will have fully qualified dottedname return identify_dynamic_interface(iface) # fallback to identify dynamic def normalize_uuid(v):
def set_fields(context, schema): # serialize the current schema snew_schema = serializeSchema(schema) # store the current schema context.fields_model = snew_schema context.notifyModified()
def signature(self, schema): if IInterface.providedBy(schema): schema = serializeSchema(schema) return md5(schema.strip()).hexdigest()