Introduction d'un callback « beforeUpload ».
[Plinn.git] / AttachmentTool.py
1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Plinn - http://plinn.org #
4 # Copyright (C) 2007 Benoît PIN <benoit.pin@ensmp.fr> #
5 # #
6 # This program is free software; you can redistribute it and/or #
7 # modify it under the terms of the GNU General Public License #
8 # as published by the Free Software Foundation; either version 2 #
9 # of the License, or (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program; if not, write to the Free Software #
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
19 #######################################################################################
20 """ Basic portal attachment management tool.
21
22
23
24 """
25
26 from AccessControl import ClassSecurityInfo
27 from Acquisition import aq_base
28 from Globals import InitializeClass
29 from OFS.SimpleItem import SimpleItem
30 from OFS.Folder import Folder
31 from OFS.Image import File, cookId
32 from Products.Photo import Photo
33 from Products.CMFCore.utils import UniqueObject, getToolByName
34 from Products.CMFCore.permissions import ModifyPortalContent
35 from Products.CMFCore.exceptions import AccessControl_Unauthorized
36 from Products.Plinn.utils import makeValidId
37
38
39 class AttachmentTool( UniqueObject, SimpleItem):
40 """ Links attachment objects to contents.
41 """
42
43 id = 'portal_attachment'
44 meta_type = 'Attachment Tool'
45 manage_options = SimpleItem.manage_options
46
47 security = ClassSecurityInfo()
48
49 security.declarePublic('getAttachmentsFor')
50 def getAttachmentsFor(self, content):
51 """getAttachmentsFor returns attachments container of content
52 """
53 if getattr( aq_base(content), 'attachments', None ) is None :
54 self._createAttachmentContainerFor(content)
55
56 return content.attachments
57
58 security.declarePrivate('_createAttachmentContainerFor')
59 def _createAttachmentContainerFor(self, content):
60 """_createAttachmentContainerFor documentation
61 """
62
63 content.attachments = AttachmentContainer()
64
65 security.declarePublic('uploadAttachmentFor')
66 def uploadAttachmentFor(self, content, file, title='', typeName='File') :
67 "upload attachment inside content's attachment folder."
68
69 mtool = getToolByName(self, 'portal_membership')
70 if not mtool.checkPermission(ModifyPortalContent, content) :
71 raise AccessControl_Unauthorized
72
73 utool = getToolByName(self, 'portal_url')
74 portal = utool.getPortalObject()
75
76 attachments = self.getAttachmentsFor(content)
77 dummy, title = cookId('', title, file)
78 id = makeValidId(attachments, title)
79
80 if typeName == 'Photo':
81 thumbSize = {'thumb_height' : portal.getProperty('thumb_height', 128),
82 'thumb_width' : portal.getProperty('thumb_width', 128)}
83 fileOb = Photo(id, title, file, **thumbSize)
84 elif typeName == 'File' :
85 fileOb = File(id, title, '')
86 fileOb.manage_upload(file)
87 else :
88 raise AccessControl_Unauthorized
89
90 content.attachments._setObject(id, fileOb)
91 fileOb = getattr(content.attachments, id)
92 return fileOb
93
94
95
96 InitializeClass( AttachmentTool )
97
98
99 class AttachmentContainer (Folder):
100
101 meta_type = 'Attachment container'
102 security = ClassSecurityInfo()
103
104 def __init__(self):
105 self.id = 'attachments'
106
107 InitializeClass(AttachmentContainer)