[Zope3-checkins] CVS: Zope3/src/zope/app/browser/container - adding.py:1.12
Jim Fulton
jim@zope.com
Thu, 12 Jun 2003 05:29:50 -0400
Update of /cvs-repository/Zope3/src/zope/app/browser/container
In directory cvs.zope.org:/tmp/cvs-serv26226/src/zope/app/browser/container
Modified Files:
adding.py
Log Message:
Some changes to support in-place add:
Abstracted a base class that isn't aware of add menus. This allows
use of the adding code (specifically, the action method) for
containers without add menus.
When we need to compute a url for a custom add view, use the
absolute url of the adding view, rather than a relative url. This is
necessary when the adding view is called from in-place add code in a
contents view, rather than from a url.
Changed to use zope.app.zapi.
=== Zope3/src/zope/app/browser/container/adding.py 1.11 => 1.12 ===
--- Zope3/src/zope/app/browser/container/adding.py:1.11 Thu Jun 5 08:41:53 2003
+++ Zope3/src/zope/app/browser/container/adding.py Thu Jun 12 05:29:50 2003
@@ -17,6 +17,8 @@
"""
__metaclass__ = type
+from zope.app import zapi
+
from zope.app.interfaces.exceptions import UserError
from zope.app.interfaces.container import IAdding
@@ -25,35 +27,30 @@
from zope.app.event.objectevent import ObjectCreatedEvent
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
-from zope.component import \
- getView, getService, createObject, queryFactory, queryView, getAdapter
from zope.app.event import publish
-from zope.context import ContextSuper, ContextMethod
from zope.publisher.browser import BrowserView
from zope.publisher.interfaces import IPublishTraverse
from zope.app.i18n import ZopeMessageIDFactory as _
from zope.interface import implements
-class Adding(BrowserView):
+class BasicAdding(BrowserView):
implements(IAdding, IPublishTraverse)
- menu_id = "add_content"
-
############################################################
# Implementation methods for interface
# IAdding.py
def add(self, content):
- container = getAdapter(self.context, IZopeContainer)
+ container = zapi.getAdapter(self.context, IZopeContainer)
name = container.setObject(self.contentName, content)
return container[name]
contentName = None # usually set by Adding traverser
def nextURL(self):
- return (str(getView(self.context, "absolute_url", self.request))
+ return (str(zapi.getView(self.context, "absolute_url", self.request))
+ '/@@contents.html')
request = None # set in BrowserView.__init__
@@ -67,43 +64,29 @@
if view_name.startswith('@@'):
view_name = view_name[2:]
- return getView(self, view_name, request)
+ return zapi.getView(self, view_name, request)
if name.startswith('@@'):
view_name = name[2:]
else:
view_name = name
- view = queryView(self, view_name, request)
+ view = zapi.queryView(self, view_name, request)
if view is not None:
return view
- factory = queryFactory(self.context, name)
+ factory = zapi.queryFactory(self.context, name)
if factory is None:
- return ContextSuper(Adding, self).publishTraverse(request, name)
+ return zapi.ContextSuper(Adding, self).publishTraverse(
+ request, name)
return factory
- publishTraverse = ContextMethod(publishTraverse)
+ publishTraverse = zapi.ContextMethod(publishTraverse)
#
############################################################
- index = ViewPageTemplateFile("add.pt")
-
- def addingInfo(wrapped_self):
- """Return menu data.
-
- This is sorted by title.
- """
- menu_service = getService(wrapped_self.context, "BrowserMenu")
- result = menu_service.getMenu(wrapped_self.menu_id,
- wrapped_self,
- wrapped_self.request)
- result.sort(lambda a, b: cmp(a['title'], b['title']))
- return result
- addingInfo = ContextMethod(addingInfo)
-
def action(self, type_name='', id=''):
if not type_name:
raise UserError(_(u"You must select the type of object to add."))
@@ -111,21 +94,45 @@
if type_name.startswith('@@'):
type_name = type_name[2:]
- if queryView(self, type_name, self.request) is not None:
- url = "%s=%s" % (type_name, id)
+ if zapi.queryView(self, type_name, self.request) is not None:
+ url = "%s/%s=%s" % (
+ zapi.getView(self, "absolute_url", self.request),
+ type_name, id)
self.request.response.redirect(url)
return
if not id:
- raise ValueError(_(u"You must specify an id"))
+ raise UserError(_(u"You must specify an id"))
self.contentName = id
- content = createObject(self, type_name)
+ content = zapi.createObject(self, type_name)
publish(self.context, ObjectCreatedEvent(content))
self.add(content)
self.request.response.redirect(self.nextURL())
+ action = zapi.ContextMethod(action)
+
+
def namesAccepted(self):
return not IContainerNamesContainer.isImplementedBy(self.context)
+
+class Adding(BasicAdding):
+
+ menu_id = "add_content"
+
+ index = ViewPageTemplateFile("add.pt")
+
+ def addingInfo(wrapped_self):
+ """Return menu data.
+
+ This is sorted by title.
+ """
+ menu_service = zapi.getService(wrapped_self.context, "BrowserMenu")
+ result = menu_service.getMenu(wrapped_self.menu_id,
+ wrapped_self,
+ wrapped_self.request)
+ result.sort(lambda a, b: cmp(a['title'], b['title']))
+ return result
+ addingInfo = zapi.ContextMethod(addingInfo)