Back to index

plone3  3.1.7
link.py
Go to the documentation of this file.
00001 ##########################################################
00002 #
00003 # Licensed under the terms of the GNU Public License
00004 # (see docs/LICENSE.GPL)
00005 #
00006 # Copyright (c) 2005:
00007 #   - The Open Planning Project (http://www.openplans.org/)
00008 #   - Whit Morriss <whit at www.openplans.org>
00009 #   - and contributors
00010 #
00011 ##########################################################
00012 from urllib import quote
00013 from zope.interface import Interface
00014 from zope.component import adapter, handle
00015 from Products.Five import BrowserView
00016 from wicked import utils
00017 from wicked.normalize import titleToNormalizedId as normalize
00018 from wicked.config import BACKLINK_RELATIONSHIP
00019 from wicked.utils import getWicked
00020 from wicked.link import WickedAdd, BasicLink
00021 from wicked.interfaces import IWickedContentAddedEvent
00022 from Products.Archetypes.interfaces import IBaseObject
00023 from Products.CMFPlone.utils import transaction_note
00024 from Products.CMFCore.utils import getToolByName
00025 
00026 _marker=object()
00027 
00028 class ATWickedAdd(WickedAdd, BrowserView):
00029 
00030     def add_content(self, title=None, section=None, type_name=_marker):
00031         # make it possible to pass in container
00032         # pre-populate with backlinks?
00033         type_name = self.request.get('type_name', type_name)
00034         if type_name is _marker:
00035             type_name = self.context.portal_type
00036         title = self.request.get('Title', title)
00037         section = self.request.get('section', section)
00038         newcontent = self._create_content(title, type_name)
00039         self.notify_content_added(newcontent, title, section)
00040 
00041     def _create_content(self, title, type_name):
00042         assert title, 'Must have a title to create content' 
00043         newcontentid = normalize(title)
00044         container = self.aq_parent.aq_parent
00045         newcontentid = container.invokeFactory(type_name, id=newcontentid,
00046                                                title=title)
00047         return getattr(self.context, newcontentid)
00048 
00049 
00050 class ATPortalFactoryAdd(ATWickedAdd):
00051 
00052     def _create_content(self, title, type_name):
00053         assert title, 'Must have a title to create content' 
00054         id_=self.context.generateUniqueId(type_name)
00055         
00056         new_content = self.context.restrictedTraverse('portal_factory/%s/%s' % (type_name, id_))
00057 
00058         transaction_note('Initiated creation of %s with id %s in %s' % \
00059                          (new_content.getTypeInfo().getId(),
00060                           id_,
00061                           self.context.absolute_url()))
00062         new_content.setTitle(title)
00063         newcontentid = normalize(title)
00064         new_content.setId(newcontentid)
00065         return new_content
00066 
00067 # channel on AT
00068 @adapter(IBaseObject, IWickedContentAddedEvent)
00069 def handle_at_newcontent(context, event):
00070     field = context.Schema()[event.section]
00071     wicked = getWicked(field, context, event)
00072     wicked.manageLink(context, event.title)
00073     portal_status_message=quote('"%s" has been created' % event.title)
00074     url = event.newcontent.absolute_url()
00075     restr = "%s/edit?title=%s&portal_status_message=%s" %(url,
00076                                                           quote(event.title),
00077                                                           portal_status_message)
00078     event.request.RESPONSE.redirect(restr)
00079 
00080     
00081 class BasicFiveLink(BasicLink, BrowserView):
00082     """
00083     Five prepared link implementation
00084     """
00085     __init__=BasicLink.__init__
00086 
00087 
00088 
00089 def test_suite():
00090     import unittest
00091     from zope.testing import doctest
00092     optionflags = doctest.ELLIPSIS
00093     from Products.PloneTestCase.layer import PloneSite, ZCMLLayer
00094     from wicked.plone.tests import ZCMLLayer as PloneWickedZCMLLayer
00095     from Testing.ZopeTestCase import ZopeDocFileSuite, FunctionalDocFileSuite
00096     from Products.PloneTestCase import ptc
00097     ptc.setupPloneSite()
00098     renderer = ZopeDocFileSuite('renderer.txt',
00099                                 package='wicked.at',
00100                                 optionflags=optionflags)
00101     renderer.layer = ZCMLLayer
00102     add = FunctionalDocFileSuite('add.txt',
00103                            package='wicked.at',
00104                            test_class=ptc.FunctionalTestCase,
00105                            optionflags=optionflags)
00106     add.layer = PloneWickedZCMLLayer
00107     return unittest.TestSuite((add, renderer))
00108 
00109