Commit 4872db70 authored by Ayush Tiwari's avatar Ayush Tiwari

ERP5CatalogTool: ERP5-ify CatalogTool

ERP5CatalogTool class inherits from BaseTool.

Significant addition/changes in:
-------------------------------
  ERP5CatalogTool:
    Add functions _isBootstrapRequired and _bootstrap
    Explicilty add manage option tabs in Catalog Tool

  BusinessTemplate:
    Update BusinessTemplate installation according to changes made in ERP5Catalog and Tool

  testCopySupport:
    Its better to change the tests where they need to call getpath function of
    portal_catalog to use portal_catalog.getpath instead of portal_catalog.getPath,
    as we have overridden the 'getPath' function for CatalogTool class due to change
    in inheritence.

  ERP5Site:
    Create portal_catalog while setting up erp5site
parent 557c20bd
......@@ -1312,11 +1312,27 @@ class ObjectTemplateItem(BaseTemplateItem):
# The id match, but better double check with the meta type
# while avoiding the impact of systematic check
container_container = portal.unrestrictedTraverse(container_path[:-1])
if container_container.meta_type == 'ERP5 Catalog':
# Check for meta_type of container before creating Catalog
if container_container.meta_type == 'Catalog Tool':
container_container.newContent(portal_type='Catalog', id=container_path[-1], title='')
elif container_container.meta_type == 'ERP5 Catalog':
container_container.manage_addProduct['ZSQLCatalog'].manage_addSQLCatalog(id=container_path[-1], title='')
if len(container_container.objectIds()) == 1:
container_container.default_sql_catalog_id = container_path[-1]
container = portal.unrestrictedTraverse(container_path)
else:
# Raise in case meta_type don't match
raise ValueError(
'No meta_type exists for %r during Catalog installation' % (
container_container.title,
),
)
# Update default catalog ID
if len(container_container.objectIds()) == 1:
# Set the default catalog. Here, thanks to consistency between
# ERP5CatalogTool and ZSQLCatalog, we can use the explicit accessor
# `_setDefaultSqlCatalogId` to update both `default_sql_catalog_id`
# and `default_erp5_catalog_id`
container_container._setDefaultSqlCatalogId(container_path[-1])
container = portal.unrestrictedTraverse(container_path)
else:
raise
saved_uid_dict = {}
......
......@@ -2035,10 +2035,8 @@ class ERP5Generator(PortalGenerator):
addERP5Tool(p, 'portal_caches', 'Cache Tool')
addERP5Tool(p, 'portal_memcached', 'Memcached Tool')
# Add ERP5 SQL Catalog Tool
addTool = p.manage_addProduct['ERP5Catalog'].manage_addTool
if not p.hasObject('portal_catalog'):
addTool('ERP5 Catalog', None)
# Add erp5 catalog tool
addERP5Tool(p, 'portal_catalog', 'Catalog Tool')
sql_reset = kw.get('sql_reset', 0)
def addSQLConnection(id, title, **kw):
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2016 Nexedi SARL and Contributors. All Rights Reserved.
# Ayush Tiwari <ayush.tiwari@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import sys
from Products.ZSQLCatalog.ZSQLCatalog import ZCatalog
from Products.ERP5Type import Permissions
from Products.CMFCore.CatalogTool import CatalogTool as CMFCoreCatalogTool
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
from Products.CMFActivity.ActivityTool import GroupedMessage
from Products.ERP5Type.Core.Folder import Folder
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import PropertySheet
from AccessControl import ClassSecurityInfo
from zLOG import LOG, PROBLEM, WARNING, INFO
from Products.ERP5Catalog import CatalogTool as CMFCore_CatalogToolModule
from Products.ERP5Catalog.CatalogTool import IndexableObjectWrapper
from Products.ERP5Catalog.CatalogTool import RelatedBaseCategory
CMFCore_CatalogTool = CMFCore_CatalogToolModule.CatalogTool
class ERP5CatalogTool(BaseTool, CMFCore_CatalogTool):
id = 'portal_catalog'
title = 'Catalog Tool'
meta_type = 'Catalog Tool'
portal_type = 'Catalog Tool'
allowed_types = ('Catalog',)
# Declarative security
security = ClassSecurityInfo()
# Explicitly add tabs for manage_options
manage_options = ({'label': 'View', 'action': 'view'},
{'label': 'Contents', 'action': 'manage_main'},
{'label': 'Security', 'action': 'manage_access'},
{'label': 'Undo', 'action': 'manage_UndoForm'},
{'label': 'Ownership', 'action': 'manage_owner'},
{'label': 'Interfaces', 'action': 'manage_interfaces'},
{'label': 'Find', 'action': 'manage_findForm'},
{'label': 'History', 'action': 'manage_change_history_page'},
{'label': 'Workflows', 'action': 'manage_workflowsTab'},
)
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem
, PropertySheet.Folder
, PropertySheet.CategoryCore
, PropertySheet.CatalogTool
)
# Use reindexObject method from BaseTool class and declare it public
reindexObject = BaseTool.reindexObject
security.declarePublic('reindexObject')
# Explicit Inheritance
  • Even though I find it very convenient for code navigation ... it is quite strange to have this in object-oriented code...

  • The reason for having explicit inheritance here is to solve conflict between the same function available in both parent classes.

Please register or sign in to reply
__url = CMFCoreCatalogTool._CatalogTool__url
unindexObject = CMFCore_CatalogTool.unindexObject
__call__ = CMFCore_CatalogTool.__call__
_aq_dynamic = CMFCore_CatalogTool._aq_dynamic
ZopeFindAndApply = CMFCore_CatalogTool.ZopeFindAndApply
#_checkId = CMFCore_CatalogTool._checkId
listDAVObjects = CMFCore_CatalogTool.listDAVObjects
__class_init__ = CMFCore_CatalogTool.__class_init__
security.declareProtected(Permissions.ManagePortal
, 'manage_overview')
manage_overview = DTMLFile('dtml/explainCatalogTool', globals())
# IMPORTANT:Solve inheritance conflict, this is necessary as getObject from
# Base gives the current object, which migth be harmful for CatalogTool as
# we use this function here to sometimes get objects to delete which if
# not solved of inheritance conflict might lead to catalog deletion.
getObject = ZCatalog.getObject
default_erp5_catalog_id = None
def __init__(self, id=''):
ZCatalog.__init__(self, self.getId())
BaseTool.__init__(self, self.getId())
def _isBootstrapRequired(self):
return False
def _bootstrap(self):
pass
def getDefaultSqlCatalogId(self):
return self.default_erp5_catalog_id
def _setDefaultSqlCatalogId(self, value):
"""
Function to maintain compatibility between ZSQLCatalog and ERP5CatalogTool
Notice that we update the attribute `default_erp5_catalog_id` here and not
the property. This is because there maybe cases(migration) whern we don't
have accessors defined and there we'll need the attribute.
"""
self.default_erp5_catalog_id = value
# Filter content (ZMI))
def filtered_meta_types(self, user=None):
# Filters the list of available meta types for CatalogTool
meta_types = []
for meta_type in ERP5CatalogTool.inheritedAttribute('filtered_meta_types')(self):
if meta_type['name'] in self.allowed_types:
meta_types.append(meta_type)
return meta_types
allowedContentTypes = BaseTool.allowedContentTypes
getVisibleAllowedContentTypeList = BaseTool.getVisibleAllowedContentTypeList
# The functions 'getERP5CatalogIdList' and 'getERP5Catalog' are meant to
Please register or sign in to reply
# be used in restricted environment, cause the reason they were created is
# the transition of Catalog from SQLCatalog to ERP5Catalog, which basically
# means Catalog is going to be an ERP5 object, which is why we need these
# functions to be declared public.
security.declarePublic('getERP5CatalogIdList')
def getERP5CatalogIdList(self):
"""
Get ERP5 Catalog Ids
"""
return list(self.objectIds(spec=('ERP5 Catalog',)))
security.declarePublic('getERP5Catalog')
def getERP5Catalog(self, id=None, default_value=None):
"""
Get current ERP5 Catalog
  • We should introduce punishments for such comments (you just copied the name of the method in the comment).

    Comment should tell you

    1. When you want to use the method
    2. How to use it
Please register or sign in to reply
"""
if id is None:
if not self.default_erp5_catalog_id:
id_list = self.getERP5CatalogIdList()
if len(id_list) > 0:
self.default_erp5_catalog_id = id_list[0]
else:
return default_value
id = self.default_erp5_catalog_id
return self._getOb(id, default_value)
security.declarePublic('getSQLCatalog')
getSQLCatalog = getERP5Catalog # For compatibilty
security.declarePrivate('reindexCatalogObject')
def reindexCatalogObject(self, object, idxs=None, sql_catalog_id=None, **kw):
'''Update catalog after object data has changed.
The optional idxs argument is a list of specific indexes
to update (all of them by default).
'''
if idxs is None: idxs = []
url = self.__url(object)
self.catalog_object(
object,
url,
idxs=idxs,
sql_catalog_id=sql_catalog_id,
**kw
)
security.declarePrivate('getCatalogUrl')
def getCatalogUrl(self, object):
return self.__url(object)
def _redirectHotReindexAll(self, REQUEST, RESPONSE):
'''
Override this function from ZSQLCatalog as here we want to redirect to
the view for ERP5CatalogTool.
'''
if not RESPONSE:
try:
RESPONSE = REQUEST.RESPONSE
except AttributeError:
return
if RESPONSE is not None:
url = self.absolute_url() + '/view' \
+ '?portal_status_message=HotReindexing%20Started'
return RESPONSE.redirect(url)
InitializeClass(ERP5CatalogTool)
......@@ -37,11 +37,12 @@ this_module = sys.modules[ __name__ ]
document_classes = updateGlobals( this_module, globals(), permissions_module = Permissions)
# Define object classes and tools
from Tool import ArchiveTool
from Tool import ArchiveTool, ERP5CatalogTool
from Document import ERP5Catalog
import CatalogTool
object_classes = (ERP5Catalog.ERP5Catalog,)
portal_tools = (CatalogTool.CatalogTool,
ERP5CatalogTool.ERP5CatalogTool,
ArchiveTool.ArchiveTool)
content_classes = ()
content_constructors = ()
......
......@@ -89,7 +89,7 @@ class TestCopySupport(ERP5TypeTestCase):
person = self.portal.person_module.newContent(portal_type='Person',
address_city='Lille')
self.tic()
getPath = self.portal.portal_catalog.getPath
getPath = self.portal.portal_catalog.getpath
address = person.default_address
self.assertEqual(address.getPath(), getPath(address.getUid()))
person.recursiveReindexObject()
......
......@@ -193,7 +193,7 @@ class ZCatalog(Folder, Persistent, Implicit):
self.id=id
self.title=title
security.declarePrivate('getSQLCatalogIdList')
security.declarePublic('getSQLCatalogIdList')
def getSQLCatalogIdList(self):
return self.objectIds(spec=('SQLCatalog',))
......@@ -530,7 +530,14 @@ class ZCatalog(Folder, Persistent, Implicit):
destination_sql_catalog_id=destination_sql_catalog_id,
skin_selection_dict=skin_selection_dict,
sql_connection_id_dict=sql_connection_id_dict)
if RESPONSE is not None:
self._redirectHotReindexAll(REQUEST, RESPONSE)
def _redirectHotReindexAll(self, REQUEST, RESPONSE):
'''
We need to separate the final redirection from manage_reindexAll to
remove the need of copy and patch for the ERP5CatalogTool.
'''
if RESPONE is not None:
URL1 = REQUEST.get('URL1')
RESPONSE.redirect(URL1 + '/manage_catalogHotReindexing?manage_tabs_message=HotReindexing%20Started')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment