source: trunk/Piece.py @ 580

Revision 580, 4.6 KB checked in by szabolcs, 13 years ago (diff)

closes #610, spent 2h

  • Property svn:eol-style set to native
Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19from SharedMetadata import *
20from Products.Archetypes.public import *
21from Globals import InitializeClass
22from Products.CMFCore.utils import getToolByName
23from AccessControl import ClassSecurityInfo, Unauthorized
24from config import PROJECTNAME, VIEW
25from FieldsWidgets import WYSIWYMField, WYSIWYMWidget
26from Resources import Resource
27
28schema = BaseSchema + description + tags + author_schema + original_author_schema + license_schema + coverImage + parentVersion + contentFile + sourceFile + lemill_metadata_mods 
29
30schema = schema + Schema((
31    ImageField('image',
32               required=False,
33               accessor='getImage',
34               mutator='setImage',
35               sizes={'small':(160,120),'medium':(320,240),'large':(500,500)},
36               widget = ImageWidget(visible={'view':'invisible','edit':'invisible'}),
37               ),
38    StringField(
39        'language',
40        schemata="metadata",
41        accessor="Language",
42        default_method ='defaultLanguage',
43        vocabulary='getLanguagelist',
44        default = '',
45        widget=SelectionWidget(
46            label='Language',
47            label_msgid="label_language",
48            description="Select the language of this resource",
49            description_msgid="help_language",
50            i18n_domain="lemill",
51            visible = {'edit': 'invisible', 'view': 'invisible'},
52            ),
53        ),
54    ))
55
56schema.moveField('rights', pos='bottom')
57
58class Piece(Resource):
59    """Piece"""
60    schema = schema
61    meta_type = "Piece"
62    archetype_name = "Piece"
63    security = ClassSecurityInfo()
64    security.declareObjectPublic()
65
66    # Use the file as cover image, if it's an image
67    def setFile(self,value,**kwargs):
68        file=self.getField('file')
69        # Call normal mutator for this field
70        file.set(self,value,**kwargs)
71        if file.getContentType(self).startswith('image/'):
72            # Call mutator for the coverImage field as well
73            cover=self.getField('coverImage')
74            cover.set(self,value,**kwargs)
75            # set 'hasCoverImage'-flag
76            has_cover=self.getField('hasCoverImage')
77            has_cover.set(self,True)
78            # Populate the image field
79            image=self.getField('image')
80            image.set(self,value,**kwargs)
81
82    def pretty_title_or_id(self):
83        """Return modified title."""
84        return '[P] ' + self.Title()
85
86    def isImage(self):
87        """Returns whether the piece is an image."""
88        file=self.getField('file')
89        return file.getContentType(self).startswith('image/')
90
91    def download(self, REQUEST, RESPONSE):
92        """ download a file """
93        from Products.Archetypes.utils import contentDispositionHeader
94        field = self.getField('file')
95        org_filename = field.getFilename(self)
96        filename = self.Title()
97        pu = getToolByName(self, 'plone_utils')
98        filename = pu.normalizeString(filename)
99        extension = ''
100        if org_filename:        # extract .doc .pdf or something
101            extension = org_filename[org_filename.rfind('.'):]
102            if extension == -1: extension = ''
103        else:                   # try to guess extension
104            ct = field.getContentType(self)
105            mr = getToolByName(self, 'mimetypes_registry')
106            mt = mr.lookup(ct)
107            if mt:              # mt is something like (<mimetype text/plain>,) so we'll take first one
108                extension = mt[0].extensions[0]       # and take first one from here too
109                extension = '.'+extension
110        if extension:
111            filename += extension
112        header_value = contentDispositionHeader('attachment', self.getCharset(), filename=filename)
113        RESPONSE.setHeader("Content-disposition", header_value)
114        file = field.get(self)
115        return file.index_html(REQUEST, RESPONSE)
116
117registerType(Piece, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.