Copie de MosaicDocument à partir de :
[MosaicDocument.git] / ImageSlot.py
1 # (c) 2003 Centre de Recherche en Informatique ENSMP Fontainebleau <http://cri.ensmp.fr>
2 # (c) 2003 Benoît PIN <mailto:pin@cri.ensmp.fr>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 # 02111-1307, USA.
17 #
18
19 from OFS.Image import cookId
20 from Globals import InitializeClass
21
22 from Products.CMFCore.permissions import View, ModifyPortalContent
23 from AccessControl import ClassSecurityInfo
24 from Products.Photo import Photo
25 from BaseSlot import BaseSlot
26
27 factory_type_information = ( {'id' : 'Image Slot',
28 'meta_type' : 'Image Slot',
29 'description' : "Image Slot for Mosaic Document",
30 'icon' : 'mosaic_tool/photo_icon.gif',
31 'product' : 'MosaicDocument',
32 'factory' : 'addImageSlot',
33 'immediate_view' : 'view',
34 'actions' :
35 ({'id' : 'view',
36 'name' : 'View',
37 'action' : 'slot_image_view',
38 'permissions' : (View, )
39 },
40
41 {'id' : 'edit',
42 'name' : 'Edit',
43 'action' : 'slot_image_form',
44 'permissions' : (ModifyPortalContent, )
45 },
46 )
47 },
48 )
49
50 class ImageSlot(BaseSlot, Photo) :
51 """Slot"""
52 meta_type = 'Image Slot'
53 manage_options = Photo.manage_options
54
55 _indexableFields = ('title',)
56
57 security = ClassSecurityInfo()
58
59
60 def __init__(self, parentContainer, id, title='', file='',
61 content_type='', precondition='', blankThumbnail = '', **kw) :
62 blankThumbnailOb = None
63 if blankThumbnail :
64 blankThumbnailOb = parentContainer.restrictedTraverse(blankThumbnail)
65 Photo.__init__(self, id, title=title, file=file,
66 defaultBlankThumbnail = blankThumbnailOb, **kw)
67
68
69 # This method is overloaded to manage file upload.
70 # It's necessary to use manage_upload method from image
71 # because manage_upload invoke update_data and the overload of
72 # update_data in Photo product update the internal thumbnail image.
73 security.declareProtected(ModifyPortalContent, 'edit')
74 def edit(self, title='', thumb_width='440', thumb_height='440', file='', REQUEST=None):
75 """ Edit image slot"""
76 if file :
77 self.manage_upload(file=file)
78
79 if self.thumb_height != thumb_height or thumb_width != thumb_width :
80 if thumb_width <= thumb_height :
81 self.manage_editProperties(thumb_width=thumb_width, thumb_height=thumb_height)
82 else :
83 self.manage_editProperties(thumb_width=thumb_height, thumb_height=thumb_width)
84 if title :
85 self.manage_editProperties(title = title, no_refresh = 1)
86
87 view = index_html = __call__ = Photo.index_html
88
89 InitializeClass(ImageSlot)
90
91 def addImageSlot(dispatcher, id, file='', title='',
92 precondition='', content_type='', REQUEST=None, **kw) :
93 """
94 Add a new Photo object.
95 Creates a new Photo object 'id' with the contents of 'file'.
96 """
97 id=str(id)
98 title=str(title)
99 content_type=str(content_type)
100 precondition=str(precondition)
101
102 id, title = cookId(id, title, file)
103 parentContainer = dispatcher.Destination()
104
105 # First, we create the image without data:
106 parentContainer._setObject(id, ImageSlot(parentContainer, id,title,'',content_type, precondition, **kw))
107
108 # Now we "upload" the data. By doing this in two steps, we
109 # can use a database trick to make the upload more efficient.
110 if file:
111 parentContainer._getOb(id).manage_upload(file)
112 if content_type:
113 parentContainer._getOb(id).content_type=content_type
114
115 if REQUEST is not None:
116 try: url=dispatcher.DestinationURL()
117 except: url=REQUEST['URL1']
118 REQUEST.RESPONSE.redirect('%s/manage_main' % url)
119 return id