Back to index

plone3  3.1.7
link.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 links.
00014 
00015 $Id: link.py 71045 2006-11-03 17:22:43Z yuppie $
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.CMFDefault.formlib.form import ContentEditFormBase
00029 from Products.CMFDefault.formlib.schema import ProxyFieldProperty
00030 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00031 from Products.CMFDefault.interfaces import IMutableLink
00032 from Products.CMFDefault.utils import Message as _
00033 
00034 from utils import decode
00035 from utils import memoize
00036 from utils import ViewBase
00037 
00038 
00039 class ILinkSchema(Interface):
00040 
00041     title = TextLine(
00042         title=_(u'Title'),
00043         description=_(u'Title'),
00044         readonly=True)
00045 
00046     remote_url = BytesLine(
00047         title=_(u'URL'),
00048         required=False,
00049         missing_value=u'')
00050 
00051 
00052 class LinkSchemaAdapter(SchemaAdapterBase):
00053 
00054     adapts(IMutableLink)
00055     implements(ILinkSchema)
00056 
00057     title = ProxyFieldProperty(ILinkSchema['title'], 'Title')
00058     remote_url = ProxyFieldProperty(ILinkSchema['remote_url'])
00059 
00060 
00061 class LinkView(ViewBase):
00062 
00063     """View for ILink.
00064     """
00065 
00066     # interface
00067 
00068     @memoize
00069     @decode
00070     def url(self):
00071         return self.context.getRemoteUrl()
00072 
00073 
00074 class LinkURIWidget(BytesWidget):
00075 
00076     """Custom widget for remote_url.
00077     """
00078 
00079     def _toFieldValue(self, input):
00080         value = super(LinkURIWidget, self)._toFieldValue(input)
00081         if not value:
00082             return value
00083         tokens = urlparse.urlparse(value, 'http')
00084         if tokens[0] == 'http':
00085             if tokens[1]:
00086                 # We have a nethost. All is well.
00087                 return urlparse.urlunparse(tokens)
00088             elif tokens[2:] == ('', '', '', ''):
00089                 # Empty URL
00090                 return u''
00091             else:
00092                 # Relative URL, keep it that way, without http:
00093                 tokens = ('', '') + tokens[2:]
00094                 return urlparse.urlunparse(tokens)
00095         else:
00096             # Other scheme, keep original
00097             return urlparse.urlunparse(tokens)
00098 
00099 
00100 class LinkEditView(ContentEditFormBase):
00101 
00102     """Edit view for IMutableLink.
00103     """
00104 
00105     form_fields = form.FormFields(ILinkSchema)
00106     form_fields['remote_url'].custom_widget = LinkURIWidget