Back to index

plone3  3.1.7
metadata.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Browser views for metadata.
00014 
00015 $Id: metadata.py 77356 2007-07-03 14:55:11Z yuppie $
00016 """
00017 
00018 from Acquisition import aq_self
00019 from zope.app.form.browser import DatetimeI18nWidget
00020 from zope.component import adapts
00021 from zope.component import getUtility
00022 from zope.formlib import form
00023 from zope.interface import implements
00024 from zope.interface import Interface
00025 from zope.schema import Choice
00026 from zope.schema import Datetime
00027 from zope.schema import Set
00028 from zope.schema import Text
00029 from zope.schema import TextLine
00030 from zope.schema import Tuple
00031 from zope.schema import URI
00032 
00033 from Products.CMFCore.interfaces import IDiscussionTool
00034 from Products.CMFCore.interfaces import IMutableDublinCore
00035 from Products.CMFCore.interfaces import IMutableMinimalDublinCore
00036 from Products.CMFDefault.formlib.form import ContentEditFormBase
00037 from Products.CMFDefault.formlib.schema import ProxyFieldProperty
00038 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00039 from Products.CMFDefault.formlib.vocabulary import SimpleVocabulary
00040 from Products.CMFDefault.formlib.widgets import SubjectInputWidget
00041 from Products.CMFDefault.formlib.widgets import TupleInputWidget
00042 from Products.CMFDefault.utils import Message as _
00043 
00044 available_settings = [
00045         ('off', False, _(u'Off')),
00046         ('on', True, _(u'On')) ]
00047 
00048 
00049 class IMinimalMetadataSchema(Interface):
00050 
00051     """Schema for minimal metadata views.
00052     """
00053 
00054     title = TextLine(
00055         title=_(u'Title'),
00056         required=False,
00057         missing_value=u'')
00058 
00059     description = Text(
00060         title=_(u'Description'),
00061         required=False,
00062         missing_value=u'')
00063 
00064 
00065 class IMetadataSchema(Interface):
00066 
00067     """Schema for metadata views.
00068     """
00069 
00070     allow_discussion = Choice(
00071         title=_(u'Enable Discussion?'),
00072         required=False,
00073         vocabulary=SimpleVocabulary.fromTitleItems(available_settings))
00074 
00075     identifier = URI(
00076         title=_(u'Identifier'),
00077         readonly=True)
00078 
00079     title = TextLine(
00080         title=_(u'Title'),
00081         required=False,
00082         missing_value=u'')
00083 
00084     description = Text(
00085         title=_(u'Description'),
00086         required=False,
00087         missing_value=u'')
00088 
00089     subject = Set(
00090         title=_(u'Subject'),
00091         required=False,
00092         missing_value=set(),
00093         value_type=TextLine())
00094 
00095     contributors = Tuple(
00096         title=_(u'Contributors'),
00097         required=False,
00098         missing_value=(),
00099         value_type=TextLine())
00100 
00101     created = Datetime(
00102         title=_(u'Creation Date'),
00103         readonly=True)
00104 
00105     modified = Datetime(
00106         title=_(u'Last Modified Date'),
00107         readonly=True)
00108 
00109     effective = Datetime(
00110         title=_(u'Effective Date'),
00111         required=False
00112         )
00113 
00114     expires = Datetime(
00115         title=_(u'Expiration Date'),
00116         required=False
00117         )
00118 
00119     format = TextLine(
00120         title=_(u'Format'),
00121         required=False,
00122         missing_value=u'')
00123 
00124     language = TextLine(
00125         title=_(u'Language'),
00126         required=False,
00127         missing_value=u'')
00128 
00129     rights = TextLine(
00130         title=_(u'Rights'),
00131         required=False,
00132         missing_value=u'')
00133 
00134 
00135 class MinimalMetadataSchemaAdapter(SchemaAdapterBase):
00136 
00137     """Adapter for IMutableMinimalDublinCore.
00138     """
00139 
00140     adapts(IMutableMinimalDublinCore)
00141     implements(IMinimalMetadataSchema)
00142 
00143     title = ProxyFieldProperty(IMetadataSchema['title'], 'Title', 'setTitle')
00144     description = ProxyFieldProperty(IMetadataSchema['description'],
00145                                      'Description', 'setDescription')
00146 
00147 
00148 class MetadataSchemaAdapter(SchemaAdapterBase):
00149 
00150     """Adapter for IMutableDublinCore.
00151     """
00152 
00153     adapts(IMutableDublinCore)
00154     implements(IMetadataSchema)
00155 
00156     _effective = ProxyFieldProperty(IMetadataSchema['effective'],
00157                                     'effective', 'setEffectiveDate')
00158     _expires = ProxyFieldProperty(IMetadataSchema['expires'],
00159                                   'expires', 'setExpirationDate')
00160 
00161     def _getEffective(self):
00162         if self.context.EffectiveDate() == 'None':
00163             return None
00164         return self._effective
00165 
00166     def _getExpires(self):
00167         if self.context.ExpirationDate() == 'None':
00168             return None
00169         return self._expires
00170 
00171     def _getAllowDiscussion(self):
00172         context = aq_self(self.context)
00173         return getattr(context, 'allow_discussion', None)
00174 
00175     def _setAllowDiscussion(self, value):
00176         dtool = getUtility(IDiscussionTool)
00177         dtool.overrideDiscussionFor(self.context, value)
00178 
00179     allow_discussion = property(_getAllowDiscussion, _setAllowDiscussion)
00180     identifier = ProxyFieldProperty(IMetadataSchema['identifier'],
00181                                     'Identifier')
00182     title = ProxyFieldProperty(IMetadataSchema['title'], 'Title', 'setTitle')
00183     description = ProxyFieldProperty(IMetadataSchema['description'],
00184                                      'Description', 'setDescription')
00185     subject = ProxyFieldProperty(IMetadataSchema['subject'],
00186                                  'Subject', 'setSubject')
00187     contributors = ProxyFieldProperty(IMetadataSchema['contributors'],
00188                                       'listContributors', 'setContributors')
00189     created = ProxyFieldProperty(IMetadataSchema['created'])
00190     modified = ProxyFieldProperty(IMetadataSchema['modified'])
00191     effective = property(_getEffective, _effective.__set__)
00192     expires = property(_getExpires, _expires.__set__)
00193     format = ProxyFieldProperty(IMetadataSchema['format'],
00194                                      'Format', 'setFormat')
00195     language = ProxyFieldProperty(IMetadataSchema['language'],
00196                                      'Language', 'setLanguage')
00197     rights = ProxyFieldProperty(IMetadataSchema['rights'],
00198                                      'Rights', 'setRights')
00199 
00200 
00201 class MinimalMetadataEditView(ContentEditFormBase):
00202 
00203     """Edit view for IMutableMinimalDublinCore.
00204     """
00205 
00206     form_fields = form.FormFields(IMinimalMetadataSchema)
00207 
00208     label = _(u'Properties')
00209 
00210     def setUpWidgets(self, ignore_request=False):
00211         super(MinimalMetadataEditView,
00212               self).setUpWidgets(ignore_request=ignore_request)
00213         self.widgets['description'].height = 4
00214 
00215 
00216 class MetadataEditView(ContentEditFormBase):
00217 
00218     """Edit view for IMutableDublinCore.
00219     """
00220 
00221     actions = form.Actions(
00222         form.Action(
00223             name='change',
00224             label=_(u'Change'),
00225             validator='handle_validate',
00226             success='handle_change_success',
00227             failure='handle_failure'),
00228         form.Action(
00229             name='change_and_edit',
00230             label=_(u'Change and Edit'),
00231             validator='handle_validate',
00232             success='handle_change_and_edit_success',
00233             failure='handle_failure'),
00234         form.Action(
00235             name='change_and_view',
00236             label=_(u'Change and View'),
00237             validator='handle_validate',
00238             success='handle_change_and_view_success',
00239             failure='handle_failure'))
00240 
00241     form_fields = form.FormFields(IMetadataSchema)
00242     form_fields['subject'].custom_widget = SubjectInputWidget
00243     form_fields['contributors'].custom_widget = TupleInputWidget
00244     form_fields['effective'].custom_widget = DatetimeI18nWidget
00245     form_fields['expires'].custom_widget = DatetimeI18nWidget
00246 
00247     label = _(u'Properties')
00248 
00249     def setUpWidgets(self, ignore_request=False):
00250         super(MetadataEditView,
00251               self).setUpWidgets(ignore_request=ignore_request)
00252         self.widgets['allow_discussion']._messageNoValue = _(u'Default')
00253         self.widgets['description'].height = 4
00254         self.widgets['subject'].split = True
00255         self.widgets['contributors'].height = 6
00256         self.widgets['contributors'].split = True
00257         self.widgets['created'].split = True
00258         self.widgets['modified'].split = True
00259         self.widgets['effective'].split = True
00260         self.widgets['expires'].split = True
00261 
00262     def handle_change_success(self, action, data):
00263         self._handle_success(action, data)
00264         return self._setRedirect('portal_types', 'object/metadata')
00265 
00266     def handle_change_and_edit_success(self, action, data):
00267         self._handle_success(action, data)
00268         return self._setRedirect('portal_types', 'object/edit')