# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- """ Container for Images """ __version__ = "$Revision$"[11:-2] import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2 from zope.interface import implements from Image import Image from common import commons from interfaces import IImages try: import PIL from PIL import Image as PILimage except ImportError: pass from cStringIO import StringIO class Images(BTreeFolder2, Persistent, Explicit, commons): """ Images """ meta_type = 'Images' security = ClassSecurityInfo() security.declareObjectPublic() implements(IImages) manage_options = BTreeFolder2.manage_options id = 'images' def __init__(self): BTreeFolder2.__init__(self, self.id) def add_new(self, image, title='', precondition='', content_type='', REQUEST=None, user='', id=''): """ Add a new Image object. Creates a new Image object 'id' with the contents of 'file'. """ if REQUEST is not None: id = self.random_generator('image_') title=str(title) content_type=str(content_type) precondition=str(precondition) self=self.this() # First, we create the image without data: self._setObject(id, Image(id,title,'',content_type, precondition)) # Now we "upload" the data. By doing this in two steps, we # can use a database trick to make the upload more efficient. if image: # resize image if necessary img = PILimage.open(image) format = img.format (width, height) = img.size if width > 600 or height > 600: if width >= height: hpercent = 600/float(width) hsize = int((float(height)*float(hpercent))) img = img.resize((600, hsize), PILimage.ANTIALIAS) elif width < height: wpercent = 600/float(height) wsize = int((float(width)*float(wpercent))) img = img.resize((wsize, 600), PILimage.ANTIALIAS) newimg = StringIO() img.save(newimg, format, quality=100) newimg.seek(0) self._getOb(id).manage_upload(newimg) else: self._getOb(id).manage_upload(image) im = self._getOb(id) if REQUEST is not None: im._setFilename(image.filename) user = str(REQUEST.get('AUTHENTICATED_USER')) im._setUser(user) im.reindex_object() if content_type: self._getOb(id).content_type=content_type if REQUEST is not None: return REQUEST.RESPONSE.redirect(self.absolute_url()) def getImages(self, user): images = self.cat_images(getUser=str(user), getErased=0, sort_order='getLastChange') return images def delete_images(self, REQUEST, remove=''): """ """ if remove: if isinstance(remove, type('')): remove = [remove,] for image in remove: im = self._getOb(image) im._setErased() im.reindex_object() return REQUEST.RESPONSE.redirect(self.absolute_url()) def getImagesXML(self): """ return a xml list of images for kupu """ msg = '' for i in self.getImages(str(self.REQUEST.AUTHENTICATED_USER)): x = self.getImageById(i.getId) msg += '\n' % x.getId() msg += '%s\n' % x.absolute_url() msg += '%s\n' % x.title msg += '\n' msg += '%s\n' % x.get_size() msg += '%s\n' % x.height msg += '%s\n' % x.width msg += '\n' return msg Globals.InitializeClass(Images) #EOF