Skip to content

Commit

Permalink
get default mimetype correctly for RichText widgets with no context
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Mar 30, 2012
1 parent e0198b6 commit 4937f74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Expand Up @@ -5,6 +5,11 @@ HISTORY
1.2.11 (unreleased)
-------------------

- Determine mimetype correctly for plone.app.textfield RichText widgets with
ignoreContext = True (such as on add forms).
(Requires plone.app.textfield >= 1.1.1)
[davisagli]

- Removed comma at the end of a dict in tiny_mce_init.js. This fixes javascript
error on IE7. This was broken in 1.2.10.
[vincentfretin]
Expand Down
3 changes: 2 additions & 1 deletion Products/TinyMCE/skins/tinymce/tinymce_wysiwyg_support.pt
Expand Up @@ -6,7 +6,8 @@
<tal:block define="field field|nothing;
fname fieldName|inputname|nothing;
force_wysiwyg force_wysiwyg|nothing;
text_format python:here.portal_tinymce.getContentType(object=here, fieldname=fname);
object object|here;
text_format python:here.portal_tinymce.getContentType(object=object, field=field, fieldname=fname);
suppressed python:request.form.get('tinymce.suppress') == fname;
wysiwyg python:(not suppressed and 'html' in text_format.lower()) or force_wysiwyg;
configuration_method nocall:here/@@tinymce-jsonconfiguration;
Expand Down
36 changes: 30 additions & 6 deletions Products/TinyMCE/utility.py
Expand Up @@ -9,6 +9,7 @@
from zope.i18nmessageid import MessageFactory
from zope.interface import classProvides
from zope.interface import implements
from zope.interface import Interface
from zope.schema.fieldproperty import FieldProperty
from zope.globalrequest import getRequest
from AccessControl import ClassSecurityInfo
Expand Down Expand Up @@ -38,6 +39,13 @@
from Products.TinyMCE import TMCEMessageFactory as _


try:
from plone.app.textfield.interfaces import IRichText
except ImportError:
class IRichText(Interface):
pass


def form_adapter(context):
"""Form Adapter"""
return getUtility(ITinyMCE)
Expand Down Expand Up @@ -577,18 +585,34 @@ def getValidElements(self):
return valid_elements

security.declareProtected('View', 'getContentType')
def getContentType(self, object=None, fieldname=None):
def getContentType(self, object=None, field=None, fieldname=None):
context = aq_base(object)
if IBaseObject.providedBy(context):
if context is not None and IBaseObject.providedBy(context):
# support Archetypes fields
if fieldname is None:
field = context.getPrimaryField()
else:
if field is not None:
pass
elif fieldname is not None:
field = context.getField(fieldname) or getattr(context, fieldname, None)
else:
field = context.getPrimaryField()
if field and hasattr(aq_base(field), 'getContentType'):
return field.getContentType(context)
elif '.widgets.' in fieldname:
elif IRichText.providedBy(field):
# support plone.app.textfield RichTextValues

# First try to get a stored value and check its mimetype.
mimetype = None
if context is not None:
value = getattr(context, fieldname, None)
mimetype = getattr(value, 'mimeType', None)

# Fall back to the field's default mimetype
if mimetype is None:
mimetype = field.default_mime_type
return mimetype
elif context is not None and fieldname is not None and '.widgets.' in fieldname:
# We don't have the field object but we can at least try
# to get the mimetype from an attribute on the object
fieldname = fieldname.split('.widgets.')[-1]
field = getattr(context, fieldname, None)
mimetype = getattr(field, 'mimeType', None)
Expand Down

0 comments on commit 4937f74

Please sign in to comment.