1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Plinn - http://plinn.org #
4 # Copyright (C) 2007 BenoƮt PIN <benoit.pin@ensmp.fr> #
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. #
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. #
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.
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
39 class AttachmentTool( UniqueObject
, SimpleItem
):
40 """ Links attachment objects to contents.
43 id = 'portal_attachment'
44 meta_type
= 'Attachment Tool'
45 manage_options
= SimpleItem
.manage_options
47 security
= ClassSecurityInfo()
49 security
.declarePublic('getAttachmentsFor')
50 def getAttachmentsFor(self
, content
):
51 """getAttachmentsFor returns attachments container of content
53 if getattr( aq_base(content
), 'attachments', None ) is None :
54 self
._createAttachmentContainerFor
(content
)
56 return content
.attachments
58 security
.declarePrivate('_createAttachmentContainerFor')
59 def _createAttachmentContainerFor(self
, content
):
60 """_createAttachmentContainerFor documentation
63 content
.attachments
= AttachmentContainer()
65 security
.declarePublic('uploadAttachmentFor')
66 def uploadAttachmentFor(self
, content
, file, title
='', typeName
='File') :
67 "upload attachment inside content's attachment folder."
69 mtool
= getToolByName(self
, 'portal_membership')
70 if not mtool
.checkPermission(ModifyPortalContent
, content
) :
71 raise AccessControl_Unauthorized
73 utool
= getToolByName(self
, 'portal_url')
74 portal
= utool
.getPortalObject()
76 attachments
= self
.getAttachmentsFor(content
)
77 dummy
, title
= cookId('', title
, file)
78 id = makeValidId(attachments
, title
)
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)
88 raise AccessControl_Unauthorized
90 content
.attachments
._setObject
(id, fileOb
)
91 fileOb
= getattr(content
.attachments
, id)
96 InitializeClass( AttachmentTool
)
99 class AttachmentContainer (Folder
):
101 meta_type
= 'Attachment container'
102 security
= ClassSecurityInfo()
105 self
.id = 'attachments'
107 InitializeClass(AttachmentContainer
)