Skip to content

Commit

Permalink
Handle dexterity-image in own method.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbauer committed Apr 7, 2012
1 parent fc0a239 commit 3c33aea
Showing 1 changed file with 40 additions and 41 deletions.
81 changes: 40 additions & 41 deletions Products/TinyMCE/adapters/Upload.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
except pkg_resources.DistributionNotFound: except pkg_resources.DistributionNotFound:
pass pass
else: else:
from plone.dexterity.interfaces import IDexterityContent
from plone.namedfile import NamedBlobImage, NamedImage from plone.namedfile import NamedBlobImage, NamedImage
from plone.namedfile.interfaces import INamedImageField, INamedBlobImageField
from plone.rfc822.interfaces import IPrimaryFieldInfo from plone.rfc822.interfaces import IPrimaryFieldInfo


from Products.TinyMCE.interfaces.utility import ITinyMCE from Products.TinyMCE.interfaces.utility import ITinyMCE
Expand Down Expand Up @@ -129,7 +131,12 @@ def upload(self):
newid = id newid = id


obj = getattr(context, newid, None) obj = getattr(context, newid, None)
self.setImage(obj, id, metatype) if IDexterityContent.providedBy(obj):
if not self.setDexterityImage(obj, id, metatype):
return self.errorMessage(_("The content-type '%s' has no image-field!" % metatype))
else:
pf = obj.getPrimaryField()
pf.set(obj, request['uploadfile'])
break break


except ValueError: except ValueError:
Expand Down Expand Up @@ -164,48 +171,40 @@ def upload(self):


return self.okMessage("%s" % (obj.absolute_url())) return self.okMessage("%s" % (obj.absolute_url()))


def setImage(self, obj, id, metatype): def setDexterityImage(self, obj, id, metatype):
""" Deal with dexterity-types. This works with the "Image"-Type of
plone.app.contenttypes and has fallbacks for other implementations
of image-types.
"""
request = self.context.REQUEST request = self.context.REQUEST
iinfo = getMultiAdapter((obj, request), name=u'plone_interface_info') field_name = ''
if iinfo.provides('plone.dexterity.interfaces.IDexterityContent'): info = ''
# Deal with dexterity-types. This works with the "Image"-Type of try:
# plone.app.contenttypes and has fallbacks for other implementations # use the primary field if it's an image-field
use_blob = True info = IPrimaryFieldInfo(obj, None)
try: except TypeError:
# use the primary field if if it's an image-field # ttw-types without a primary field throw a TypeError on
info = IPrimaryFieldInfo(obj, None) # IPrimaryFieldInfo(obj, None)
if info: pass
field_schema = getattr(info.field, 'schema', None) if info:
if field_schema: field = info.field
if field_schema.getName() in ['INamedBlobImage', 'INamedImage']: if INamedImageField.providedBy(field):
pf = getattr(info, 'fieldname', None) field_name = info.fieldname
use_blob = field_schema.getName() == 'INamedBlobImage' if not field_name:
except TypeError: # use the first image-field in the schema
# the object has no primary field obj_schema = queryContentType(obj)
pass obj_fields = getFieldsInOrder(obj_schema)
if not pf: for field_info in obj_fields:
# let's use the first image-field field = field_info[1]
obj_schema = queryContentType(obj) field_schema = getattr(field, 'schema', None)
obj_fields = getFieldsInOrder(obj_schema) if field_schema and field_schema.getName() in ['INamedBlobImage', 'INamedImage']:
for field in obj_fields: field_name = field_info[0]
field_schema = getattr(field[1], 'schema', None) break
if field_schema and field_schema.getName() in ['INamedBlobImage', 'INamedImage']: if not field_name:
pf = field[0] return False
use_blob = field_schema.getName() == 'INamedBlobImage'
break
if not pf:
return self.errorMessage(_("The content-type '%s' has no image-field!" % metatype))
if use_blob:
setattr(obj, pf, NamedBlobImage(request['uploadfile'].read(), filename=unicode(id)))
else:
setattr(obj, pf, NamedImage(request['uploadfile'].read(), filename=unicode(id)))

else: else:
# handle Archetypes setattr(obj, field_name, field._type(request['uploadfile'].read(), filename=unicode(id)))
pf = obj.getPrimaryField() return True
pf.set(obj, request['uploadfile'])

return


def setDescription(self, description): def setDescription(self, description):
self.context.setDescription(description) self.context.setDescription(description)

0 comments on commit 3c33aea

Please sign in to comment.