Back to index

plone3  3.1.7
favorite.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 favorites.
00014 
00015 $Id: favorite.py 76996 2007-06-24 00:18:49Z hannosch $
00016 """
00017 
00018 import urlparse
00019 
00020 from zope.app.form.browser import BytesWidget
00021 from zope.component import adapts
00022 from zope.formlib import form
00023 from zope.interface import implements
00024 from zope.interface import Interface
00025 from zope.schema import BytesLine
00026 from zope.schema import TextLine
00027 
00028 from Products.CMFCore.utils import getToolByName
00029 from Products.CMFDefault.formlib.form import ContentEditFormBase
00030 from Products.CMFDefault.formlib.schema import ProxyFieldProperty
00031 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00032 from Products.CMFDefault.interfaces import IMutableFavorite
00033 from Products.CMFDefault.utils import Message as _
00034 
00035 
00036 class IFavoriteSchema(Interface):
00037 
00038     title = TextLine(
00039         title=_(u'Title'),
00040         description=_(u'Title'),
00041         readonly=True)
00042 
00043     remote_url = BytesLine(
00044         title=_(u'URL'),
00045         description=_(u'URL relative to the site root.'),
00046         required=False,
00047         missing_value=u'')
00048 
00049 
00050 class FavoriteSchemaAdapter(SchemaAdapterBase):
00051 
00052     adapts(IMutableFavorite)
00053     implements(IFavoriteSchema)
00054 
00055     _remote_url = ProxyFieldProperty(IFavoriteSchema['remote_url'])
00056 
00057     def _getRemoteURL(self):
00058         return self._remote_url
00059 
00060     def _setRemoteURL(self, value):
00061         self._remote_url = value
00062         self.context.remote_uid = self.context._getUidByUrl()
00063 
00064     title = ProxyFieldProperty(IFavoriteSchema['title'], 'Title')
00065     remote_url = property(_getRemoteURL, _setRemoteURL)
00066 
00067 
00068 class FavoriteURIWidget(BytesWidget):
00069 
00070     """Custom widget for remote_url.
00071     """
00072 
00073     def _toFieldValue(self, input):
00074         value = super(FavoriteURIWidget, self)._toFieldValue(input)
00075         if not value:
00076             return value
00077         # strip off scheme and machine from URL if present
00078         tokens = urlparse.urlparse(value, 'http')
00079         if tokens[1]:
00080             # There is a nethost, remove it
00081             tokens = ('', '') + tokens[2:]
00082             value = urlparse.urlunparse(tokens)
00083         # if URL begins with site URL, remove site URL
00084         obj = self.context.context.context
00085         portal_url = getToolByName(obj, 'portal_url').getPortalPath()
00086         if value.startswith(portal_url):
00087             value = value[len(portal_url):]
00088         # if site is still absolute, make it relative
00089         if value[:1]=='/':
00090             value = value[1:]
00091         return value
00092 
00093 
00094 class FavoriteEditView(ContentEditFormBase):
00095 
00096     """Edit view for IMutableFavorite.
00097     """
00098 
00099     form_fields = form.FormFields(IFavoriteSchema)
00100     form_fields['remote_url'].custom_widget = FavoriteURIWidget