Back to index

plone3  3.1.7
ContainerTab.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 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 """ A convenient base class for representing a container as a management tab.
00014 
00015 $Id: ContainerTab.py 71021 2006-11-01 16:23:56Z yuppie $
00016 """
00017 
00018 from Acquisition import aq_base, aq_inner, aq_parent
00019 from OFS.Folder import Folder
00020 
00021 _marker = []  # Create a new marker object.
00022 
00023 
00024 class ContainerTab(Folder):
00025 
00026     def __init__(self, id):
00027         self.id = id
00028         self._mapping = {}
00029 
00030     def getId(self):
00031         return self.id
00032 
00033     def manage_options(self):
00034         parent = aq_parent(aq_inner(self))
00035         res = []
00036         options = parent.manage_options
00037         if callable(options):
00038             options = options()
00039         for item in options:
00040             item = item.copy()
00041             item['action'] = '../' + item['action']
00042             res.append(item)
00043         return res
00044 
00045     def manage_workspace(self, RESPONSE):
00046         '''
00047         Redirects to the primary option.
00048         '''
00049         RESPONSE.redirect(self.absolute_url() + '/manage_main')
00050 
00051     def _checkId(self, id, allow_dup=0):
00052         if not allow_dup:
00053             if self._mapping.has_key(id):
00054                 raise 'Bad Request', 'The id "%s" is already in use.' % id
00055         return Folder._checkId(self, id, allow_dup)
00056 
00057     def _getOb(self, name, default=_marker):
00058         mapping = self._mapping
00059         if mapping.has_key(name):
00060             res = mapping[name]
00061             if hasattr(res, '__of__'):
00062                 res = res.__of__(self)
00063             return res
00064         else:
00065             if default is _marker:
00066                 raise KeyError, name
00067             return default
00068 
00069     def __getattr__(self, name):
00070         ob = self._mapping.get(name, None)
00071         if ob is not None:
00072             return ob
00073         raise AttributeError, name
00074 
00075     def _setOb(self, name, value):
00076         mapping = self._mapping
00077         mapping[name] = aq_base(value)
00078         self._mapping = mapping  # Trigger persistence.
00079 
00080     def _delOb(self, name):
00081         mapping = self._mapping
00082         del mapping[name]
00083         self._mapping = mapping  # Trigger persistence.
00084 
00085     def get(self, name, default=None):
00086         if self._mapping.has_key(name):
00087             return self[name]
00088         else:
00089             return default
00090 
00091     def has_key(self, key):
00092         return self._mapping.has_key(key)
00093 
00094     def objectIds(self, spec=None):
00095         # spec is not important for now...
00096         return self._mapping.keys()
00097 
00098     def keys(self):
00099         return self._mapping.keys()
00100 
00101     def items(self):
00102         return map(lambda id, self=self: (id, self._getOb(id)),
00103                    self._mapping.keys())
00104 
00105     def values(self):
00106         return map(lambda id, self=self: self._getOb(id),
00107                    self._mapping.keys())
00108 
00109     def manage_renameObjects(self, ids=[], new_ids=[], REQUEST=None):
00110         """Rename several sub-objects"""
00111         if len(ids) != len(new_ids):
00112             raise 'Bad Request', 'Please rename each listed object.'
00113         for i in range(len(ids)):
00114             if ids[i] != new_ids[i]:
00115                 self.manage_renameObject(ids[i], new_ids[i])
00116         if REQUEST is not None:
00117             return self.manage_main(REQUEST)
00118         return None