From 600432e8693486eb49ac8f583e9500c084bdfaf8 Mon Sep 17 00:00:00 2001 From: Clayton Parker Date: Fri, 18 Jun 2010 02:52:52 +0000 Subject: [PATCH] Added the ability to set the mimetype of a field with a `[key].mimetype` key --- docs/HISTORY.txt | 4 ++++ src/plone/app/transmogrifier/atschemaupdater.py | 3 +++ src/plone/app/transmogrifier/atschemaupdater.txt | 15 ++++++++++++++- src/plone/app/transmogrifier/tests.py | 10 +++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index e365e74..9d8ff83 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -6,6 +6,10 @@ Change History 1.2 (unreleased) ================ +- Added the ability to set the mimetype of a field with a + ``[key].mimetype`` key. + [claytron] + 1.1 (2010-03-30) ================ diff --git a/src/plone/app/transmogrifier/atschemaupdater.py b/src/plone/app/transmogrifier/atschemaupdater.py index a691fc4..38849f3 100644 --- a/src/plone/app/transmogrifier/atschemaupdater.py +++ b/src/plone/app/transmogrifier/atschemaupdater.py @@ -60,6 +60,9 @@ def __iter__(self): continue if not _compare(field.get(obj), v): field.set(obj, v) + mimetype_key = "%s.mimetype" % k + if mimetype_key in item: + field.setContentType(obj, item[mimetype_key]) changed = True obj.unmarkCreationFlag() diff --git a/src/plone/app/transmogrifier/atschemaupdater.txt b/src/plone/app/transmogrifier/atschemaupdater.txt index 540cc5f..cfa7778 100644 --- a/src/plone/app/transmogrifier/atschemaupdater.txt +++ b/src/plone/app/transmogrifier/atschemaupdater.txt @@ -29,6 +29,11 @@ Paths to objects are always interpreted as relative to the context. Any writable field who's id matches a key in the current item will be updated with the corresponding value, using the field's mutator. +The mime type of a field can be set by adding a special key. The format of the +key is ``[key].mimetype``. A good example would be the ``text`` field of a +Document, the special key would be ``text.mimetype``. The value can be any of +the available markup formats (e.g. ``text/html``, ``text/restructured``, etc.) + >>> import pprint >>> atschema = """ ... [transmogrifier] @@ -61,5 +66,13 @@ the corresponding value, using the field's mutator. [('_path', '/spam/eggs/notatcontent'), ('fieldtwo', 2), ('title', 'Should not be updated, not an AT base object')] + [('_path', '/spam/eggs/bar'), + ('fieldtext', '

some html code

'), + ('fieldtext.mimetype', 'text/html'), + ('title', 'Mimetype should be set')] >>> pprint.pprint(plone.updated) - (('spam/eggs/foo', 'fieldone', 'one value'), ('spam/eggs/foo', 'fieldtwo', 2)) + (('spam/eggs/foo', 'fieldone', 'one value'), + ('spam/eggs/foo', 'fieldtwo', 2), + ('spam/eggs/bar', 'fieldtext', '

some html code

')) + >>> pprint.pprint(plone.mimes) + (('spam/eggs/bar', 'fieldtext', 'text/html'),) diff --git a/src/plone/app/transmogrifier/tests.py b/src/plone/app/transmogrifier/tests.py index e308a4f..acbc5f2 100644 --- a/src/plone/app/transmogrifier/tests.py +++ b/src/plone/app/transmogrifier/tests.py @@ -52,7 +52,7 @@ def unrestrictedTraverse(self, path, default): _last_field = None def getField(self, name): - if name.startswith('field'): + if not name.endswith('mimetype') and name.startswith('field'): self._last_field = name return self @@ -66,6 +66,10 @@ def get(self, ob): def set(self, ob, val): self.updated += ((self._last_path, self._last_field, val),) + mimes = () + def setContentType(self, ob, val): + self.mimes += ((self._last_path, self._last_field, val),) + def checkCreationFlag(self): return len(self.updated) % 2 @@ -97,6 +101,10 @@ def __init__(self, *args, **kw): title='Should not be updated, no path'), dict(_path='/spam/eggs/notatcontent', fieldtwo=2, title='Should not be updated, not an AT base object'), + {'_path': '/spam/eggs/bar', + 'fieldtext': '

some html code

', + 'fieldtext.mimetype': 'text/html', + 'title': 'Mimetype should be set'} ) provideUtility(SchemaSource, name=u'plone.app.transmogrifier.tests.schemasource')