Back to index

plone3  3.1.7
favorite.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.ATFavorite'
00026 
00027 import urlparse
00028 import logging
00029 
00030 from AccessControl import ClassSecurityInfo
00031 from AccessControl import Unauthorized
00032 from ComputedAttribute import ComputedAttribute
00033 from ZODB.POSException import ConflictError
00034 
00035 from Products.CMFCore.utils import getToolByName
00036 from Products.CMFCore.permissions import View
00037 from Products.CMFCore.permissions import ModifyPortalContent
00038 
00039 from Products.Archetypes.atapi import Schema
00040 from Products.Archetypes.atapi import StringField
00041 from Products.Archetypes.atapi import StringWidget
00042 
00043 from Products.ATContentTypes.config import PROJECTNAME
00044 from Products.ATContentTypes.content.base import registerATCT
00045 from Products.ATContentTypes.content.base import ATCTContent
00046 from Products.ATContentTypes.interfaces import IATFavorite
00047 from Products.ATContentTypes.content.schemata import ATContentTypeSchema
00048 from Products.ATContentTypes.content.schemata import finalizeATCTSchema
00049 
00050 from Products.ATContentTypes import ATCTMessageFactory as _
00051 
00052 LOG = logging.getLogger('ATCT')
00053 
00054 ATFavoriteSchema = ATContentTypeSchema.copy() + Schema((
00055     StringField('remoteUrl',
00056                 required=True,
00057                 searchable=True,
00058                 accessor='_getRemoteUrl',
00059                 primary=True,
00060                 validators = (),
00061                 widget = StringWidget(
00062                         description=_(u'help_url', default=u'Add http:// to link outside the site.'),
00063                         label = _(u'label_url', default=u'URL')
00064                         )),
00065     ))
00066 finalizeATCTSchema(ATFavoriteSchema, moveDiscussion=False)
00067 
00068 class ATFavorite(ATCTContent):
00069     """A placeholder item linking to a favorite object in the site."""
00070 
00071     schema         =  ATFavoriteSchema
00072 
00073     portal_type    = 'Favorite'
00074     archetype_name = 'Favorite'
00075     _atct_newTypeFor = {'portal_type' : 'CMF Favorite', 'meta_type' : 'Favorite'}
00076     assocMimetypes = ()
00077     assocFileExt   = ('fav', )
00078     cmf_edit_kws   = ('remote_url',)
00079 
00080     __implements__ = ATCTContent.__implements__, IATFavorite
00081 
00082     security       = ClassSecurityInfo()
00083 
00084     # Support for preexisting api
00085     security.declareProtected(View, 'getRemoteUrl')
00086     def getRemoteUrl(self):
00087         """returns the remote URL of the Link
00088         """
00089         # need to check why this is different than PortalLink
00090         utool  = getToolByName(self, 'portal_url')
00091         portal_url = utool()
00092         remote = self._getRemoteUrl()
00093         if remote:
00094             if remote.startswith('/'):
00095                 remote = remote[1:]
00096             return '%s/%s' % (portal_url, remote)
00097         else:
00098             return portal_url
00099 
00100     def _getRemoteUrl(self):
00101         """Accessor
00102         """
00103         return self.getField('remoteUrl').get(self)
00104 
00105     remote_url = ComputedAttribute(_getRemoteUrl, 1)
00106 
00107     security.declareProtected(ModifyPortalContent, 'setRemoteUrl')
00108     def setRemoteUrl(self, remote_url):
00109         """Set url relative to portal root
00110         """
00111         utool  = getToolByName(self, 'portal_url')
00112         # strip off scheme and machine from URL if present
00113         tokens = urlparse.urlparse(remote_url, 'http')
00114         if tokens[1]:
00115             # There is a nethost, remove it
00116             t=('', '') + tokens[2:]
00117             remote_url = urlparse.urlunparse(t)
00118         # if URL begins with site URL, remove site URL
00119         portal_url = utool.getPortalPath()
00120         i = remote_url.find(portal_url)
00121         if i==0:
00122             remote_url=remote_url[len(portal_url):]
00123         # if site is still absolute, make it relative
00124         if remote_url[:1]=='/':
00125             remote_url=remote_url[1:]
00126 
00127         self.getField('remoteUrl').set(self, remote_url)
00128 
00129     security.declareProtected(View, 'getIcon')
00130     def getIcon(self, relative_to_portal=0):
00131         """Instead of a static icon, like for Link objects, we want
00132         to display an icon based on what the Favorite links to.
00133         """
00134         obj =  self.getObject()
00135         if obj is not None:
00136             return obj.getIcon(relative_to_portal)
00137         else:
00138             return 'favorite_broken_icon.gif'
00139 
00140     security.declareProtected(View, 'getObject')
00141     def getObject(self):
00142         """Return the actual object that the Favorite is
00143         linking to
00144         """
00145         utool  = getToolByName(self, 'portal_url')
00146         portal = utool.getPortalObject()
00147         relative_url = self._getRemoteUrl()
00148         try:
00149             obj = portal.restrictedTraverse(relative_url)
00150         except ConflictError:
00151             raise
00152         except (KeyError, AttributeError, Unauthorized, 'Unauthorized', ):
00153             ##LOG.error('Failed to get object for %s with url of %s' % (repr(self),
00154             ##          relative_url), exc_info=True)
00155             obj = None
00156         return obj
00157 
00158     security.declarePrivate('cmf_edit')
00159     def cmf_edit(self, remote_url=None, **kwargs):
00160         self.update(remoteUrl = remote_url, **kwargs)
00161 
00162 registerATCT(ATFavorite, PROJECTNAME)