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