Skip to content

Commit

Permalink
Added the ability to set the mimetype of a field with a `[key].mimety…
Browse files Browse the repository at this point in the history
…pe` key
  • Loading branch information
claytron committed Jun 18, 2010
1 parent 3911cb8 commit 600432e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/HISTORY.txt
Expand Up @@ -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)
================

Expand Down
3 changes: 3 additions & 0 deletions src/plone/app/transmogrifier/atschemaupdater.py
Expand Up @@ -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()

Expand Down
15 changes: 14 additions & 1 deletion src/plone/app/transmogrifier/atschemaupdater.txt
Expand Up @@ -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]
Expand Down Expand Up @@ -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', '<p>some html code</p>'),
('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', '<p>some html code</p>'))
>>> pprint.pprint(plone.mimes)
(('spam/eggs/bar', 'fieldtext', 'text/html'),)
10 changes: 9 additions & 1 deletion src/plone/app/transmogrifier/tests.py
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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': '<p>some html code</p>',
'fieldtext.mimetype': 'text/html',
'title': 'Mimetype should be set'}
)
provideUtility(SchemaSource,
name=u'plone.app.transmogrifier.tests.schemasource')
Expand Down

0 comments on commit 600432e

Please sign in to comment.