Back to index

plone3  3.1.7
newsitem.py
Go to the documentation of this file.
00001 #  ATContentTypes http://plone.org/products/atcontenttypes/
00002 #  Archetypes reimplementation of the CMF core types
00003 #  Copyright (c) 2003-2006 AT Content Types development team
00004 #
00005 #  This program is free software; you can redistribute it and/or modify
00006 #  it under the terms of the GNU General Public License as published by
00007 #  the Free Software Foundation; either version 2 of the License, or
00008 #  (at your option) any later version.
00009 #
00010 #  This program is distributed in the hope that it will be useful,
00011 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #  GNU General Public License for more details.
00014 #
00015 #  You should have received a copy of the GNU General Public License
00016 #  along with this program; if not, write to the Free Software
00017 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 """
00020 
00021 
00022 """
00023 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00024 __docformat__ = 'restructuredtext'
00025 __old_name__ = 'Products.ATContentTypes.types.ATNewsItem'
00026 
00027 from AccessControl import ClassSecurityInfo
00028 
00029 from Products.Archetypes.atapi import Schema
00030 from Products.Archetypes.atapi import ImageField
00031 from Products.Archetypes.atapi import StringField
00032 from Products.Archetypes.atapi import TextField
00033 from Products.Archetypes.atapi import ImageWidget
00034 from Products.Archetypes.atapi import RichWidget
00035 from Products.Archetypes.atapi import StringWidget
00036 from Products.Archetypes.atapi import RFC822Marshaller
00037 from Products.Archetypes.atapi import AnnotationStorage
00038 
00039 from Products.ATContentTypes.config import PROJECTNAME
00040 from Products.ATContentTypes.configuration import zconf
00041 from Products.ATContentTypes.content.base import registerATCT
00042 from Products.ATContentTypes.content.base import translateMimetypeAlias
00043 from Products.ATContentTypes.content.document import ATDocumentBase
00044 from Products.ATContentTypes.content.image import ATCTImageTransform
00045 from Products.ATContentTypes.interfaces import IATNewsItem
00046 from Products.ATContentTypes.content.schemata import ATContentTypeSchema
00047 from Products.ATContentTypes.content.schemata import finalizeATCTSchema
00048 
00049 from Products.ATContentTypes import ATCTMessageFactory as _
00050 
00051 from Products.CMFCore.permissions import View
00052 
00053 from Products.validation.config import validation
00054 from Products.validation.validators.SupplValidators import MaxSizeValidator
00055 from Products.validation import V_REQUIRED
00056 
00057 validation.register(MaxSizeValidator('checkNewsImageMaxSize',
00058                                      maxsize=zconf.ATNewsItem.max_file_size))
00059 
00060 
00061 ATNewsItemSchema = ATContentTypeSchema.copy() + Schema((
00062     TextField('text',
00063         required = False,
00064         searchable = True,
00065         primary = True,
00066         storage = AnnotationStorage(migrate=True),
00067         validators = ('isTidyHtmlWithCleanup',),
00068         #validators = ('isTidyHtml',),
00069         default_output_type = 'text/x-html-safe',
00070         widget = RichWidget(
00071             description = '',
00072             label = _(u'label_body_text', u'Body Text'),
00073             rows = 25,
00074             allow_file_upload = zconf.ATDocument.allow_document_upload)
00075         ),
00076     ImageField('image',
00077         required = False,
00078         storage = AnnotationStorage(migrate=True),
00079         languageIndependent = True,
00080         max_size = zconf.ATNewsItem.max_image_dimension,
00081         sizes= {'large'   : (768, 768),
00082                 'preview' : (400, 400),
00083                 'mini'    : (200, 200),
00084                 'thumb'   : (128, 128),
00085                 'tile'    :  (64, 64),
00086                 'icon'    :  (32, 32),
00087                 'listing' :  (16, 16),
00088                },
00089         validators = (('isNonEmptyFile', V_REQUIRED),
00090                              ('checkNewsImageMaxSize', V_REQUIRED)),
00091         widget = ImageWidget(
00092             description = _(u'help_news_image', default=u'Will be shown in the news listing, and in the news item itself. Image will be scaled to a sensible size.'),
00093             label= _(u'label_news_image', default=u'Image'),
00094             show_content_type = False)
00095         ),
00096     StringField('imageCaption',
00097         required = False,
00098         searchable = True,
00099         widget = StringWidget(
00100             description = '',
00101             label = _(u'label_image_caption', default=u'Image Caption'),
00102             size = 40)
00103         ),
00104     ), marshall=RFC822Marshaller()
00105     )
00106 finalizeATCTSchema(ATNewsItemSchema)
00107 
00108 class ATNewsItem(ATDocumentBase, ATCTImageTransform):
00109     """An announcement that will show up on the news portlet and in the news listing."""
00110 
00111     schema         =  ATNewsItemSchema
00112 
00113     portal_type    = 'News Item'
00114     archetype_name = 'News Item'
00115     _atct_newTypeFor = {'portal_type' : 'CMF News Item', 'meta_type' : 'News Item'}
00116     assocMimetypes = ()
00117     assocFileExt   = ('news', )
00118     cmf_edit_kws   = ATDocumentBase.cmf_edit_kws
00119 
00120     __implements__ = ATDocumentBase.__implements__, IATNewsItem
00121 
00122     security = ClassSecurityInfo()
00123 
00124     security.declareProtected(View, 'tag')
00125     def tag(self, **kwargs):
00126         """Generate image tag using the api of the ImageField
00127         """
00128         if 'title' not in kwargs:
00129             kwargs['title'] = self.getImageCaption()
00130         return self.getField('image').tag(self, **kwargs)
00131 
00132     security.declarePrivate('cmf_edit')
00133     def cmf_edit(self, text, description=None, text_format=None, **kwargs):
00134         if description is not None:
00135             self.setDescription(description)
00136         self.setText(text, mimetype=translateMimetypeAlias(text_format))
00137         self.update(**kwargs)
00138 
00139     def __bobo_traverse__(self, REQUEST, name):
00140         """Transparent access to image scales
00141         """
00142         if name.startswith('image'):
00143             field = self.getField('image')
00144             image = None
00145             if name == 'image':
00146                 image = field.getScale(self)
00147             else:
00148                 scalename = name[len('image_'):]
00149                 if scalename in field.getAvailableSizes(self):
00150                     image = field.getScale(self, scale=scalename)
00151             if image is not None and not isinstance(image, basestring):
00152                 # image might be None or '' for empty images
00153                 return image
00154 
00155         return ATDocumentBase.__bobo_traverse__(self, REQUEST, name)
00156 
00157 registerATCT(ATNewsItem, PROJECTNAME)