Commit ea376035 authored by Romain Courteaud's avatar Romain Courteaud

Modify code layout.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@3959 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent f53292c8
############################################################################## ##############################################################################
# #
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved. # Copyright (c) 2002, 2005 Nexedi SARL and Contributors. All Rights Reserved.
# Jean-Paul Smets-Solanes <jp@nexedi.com> # Jean-Paul Smets-Solanes <jp@nexedi.com>
# Sebastien Robin <seb@nexedi.com> # Sebastien Robin <seb@nexedi.com>
# #
...@@ -27,56 +27,49 @@ ...@@ -27,56 +27,49 @@
# #
############################################################################## ##############################################################################
from Products.CMFCore.utils import UniqueObject
from Acquisition import Implicit
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, DTMLFile from Globals import InitializeClass, DTMLFile
from Products.ERP5Type import Permissions from Products.ERP5Type import Permissions
from Products.ERP5 import _dtmldir from Products.ERP5 import _dtmldir
from Products.ERP5Type.Tool.BaseTool import BaseTool from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type.Document.Folder import Folder
from zLOG import LOG from zLOG import LOG
from DateTime import DateTime from DateTime import DateTime
class DomainTool(BaseTool): class DomainTool(BaseTool):
""" """
A tool to define ranges and subranges (predicates) A tool to define ranges and subranges (predicates)
""" """
id = 'portal_domains' id = 'portal_domains'
meta_type = 'ERP5 Domain Tool' meta_type = 'ERP5 Domain Tool'
portal_type = 'Domain Tool' portal_type = 'Domain Tool'
allowed_types = ( 'ERP5 Domain',) allowed_types = ('ERP5 Domain', )
# Declarative Security # Declarative Security
security = ClassSecurityInfo() security = ClassSecurityInfo()
# security.declareProtected(Permissions.ManagePortal, 'manage_overview')
# ZMI methods manage_overview = DTMLFile('explainDomainTool', _dtmldir)
#
#manage_options = Folder.manage_options
security.declareProtected( Permissions.ManagePortal, 'manage_overview' )
manage_overview = DTMLFile( 'explainDomainTool', _dtmldir )
# XXX FIXME method should not be public
# (some users are not able to see resource's price)
security.declarePublic('searchPredicateList') security.declarePublic('searchPredicateList')
def searchPredicateList(self,context,test=1,sort_method=None, def searchPredicateList(self, context, test=1, sort_method=None,
ignored_category_list=None,filter_method=None, ignored_category_list=None, filter_method=None,
acquired=1,**kw): acquired=1, **kw):
""" """
Search all predicates wich corresponds to this particular context. Search all predicates which corresponds to this particular
context.
- The sort_method parameter allows to give a method wich will be - The sort_method parameter allows to give a method which will be
used in order to sort the list of predicates founds. The most used in order to sort the list of predicates founds. The most
important predicate is the first one in the list. important predicate is the first one in the list.
- ignored_category_list : this is the list of category that we do - ignored_category_list: this is the list of category that we do
not want to test. For example, we might want to not test the not want to test. For example, we might want to not test the
destination or the source of a predicate. destination or the source of a predicate.
- the acquired parameter allow to define if we want to use - the acquired parameter allows to define if we want to use
acquisition for categories. By default we want. acquisition for categories. By default we want.
""" """
portal_catalog = context.portal_catalog portal_catalog = context.portal_catalog
portal_categories = context.portal_categories portal_categories = context.portal_categories
...@@ -84,47 +77,53 @@ class DomainTool(BaseTool): ...@@ -84,47 +77,53 @@ class DomainTool(BaseTool):
expression_list = [] expression_list = []
checked_column_list = [] checked_column_list = []
sql_kw = {} sql_kw = {}
# Search the columns of the predicate table
for column in portal_catalog.getColumnIds(): for column in portal_catalog.getColumnIds():
if column.startswith('predicate.'): if column.startswith('predicate.'):
column_list.append(column.split('.')[1]) column_list.append(column.split('.')[1])
for column in column_list: for column in column_list:
if column in checked_column_list: if column not in checked_column_list:
continue range_property = 0
range_property = 0 if (column.endswith('_range_min')) or \
if column.endswith('_range_min') or column.endswith('_range_max'): (column.endswith('_range_max')):
range_property = 1 range_property = 1
property = column[-len('_range_min')] # XXX FIXME: what means property here ?
if '%s_range_min' % column in column_list: property = column[-len('_range_min')]
range_property = 1 if ('%s_range_min' % column) in column_list:
property = column range_property = 1
if range_property: property = column
# We have to check a range property if range_property:
base_name = 'predicate.%s' % property # We have to check a range property
# LOG('searchPredicateList, getPath',0,context.getPath()) base_name = 'predicate.%s' % property
# LOG('searchPredicateList, base_name',0,base_name) # LOG('searchPredicateList, getPath', 0, context.getPath())
# LOG('searchPredicateList, property',0,property) # LOG('searchPredicateList, base_name', 0, base_name)
# LOG('searchPredicateList, getProperty',0,context.getProperty(property)) # LOG('searchPredicateList, property', 0, property)
value = context.getProperty(property) # LOG('searchPredicateList, getProperty', 0,
expression = '' # context.getProperty(property))
if value is None: value = context.getProperty(property)
expression += "%s is NULL AND %s_range_min is NULL AND %s_range_max is NULL" \ format_dict = {'base_name': base_name}
% ((base_name,)*3) expression = "(%(base_name)s is NULL) AND " \
else: "(%(base_name)s_range_min is NULL) AND " \
if isinstance(value, DateTime): "(%(base_name)s_range_max is NULL)" % format_dict
value = value.ISO() if value is not None:
expression = "%s is NULL AND %s_range_min is NULL AND %s_range_max is NULL " % ((base_name,)*3) # Handle Mysql datetime correctly
expression += "OR %s = '%s' " % (base_name,value) if isinstance(value, DateTime):
expression += "OR %s_range_min <= '%s' AND %s_range_max is NULL " \ value = value.ISO()
% (base_name,value,base_name) format_dict['value'] = value
expression += "OR %s_range_min is NULL AND %s_range_max > '%s' " \ # Generate expression
% (base_name,base_name,value) expression += "OR (%(base_name)s = '%(value)s') " \
expression += "OR %s_range_min <= '%s' AND %s_range_max > '%s' " \ "OR (%(base_name)s_range_min <= '%(value)s') AND " \
% (base_name,value,base_name,value) "(%(base_name)s_range_max is NULL) " \
expression = '( ' + expression + ' )' "OR (%(base_name)s_range_min is NULL) AND " \
expression_list.append(expression) "%(base_name)s_range_max > '%(value)s' " \
checked_column_list.append('%s' % property) "OR (%(base_name)s_range_min <= '%(value)s') AND " \
checked_column_list.append('%s_range_min' % property) "%(base_name)s_range_max > '%(value)s' " \
checked_column_list.append('%s_range_max' % property) % format_dict
expression = '( %s )' % expression
expression_list.append(expression)
checked_column_list.append('%s' % property)
checked_column_list.append('%s_range_min' % property)
checked_column_list.append('%s_range_max' % property)
# Add predicate.uid for automatic join # Add predicate.uid for automatic join
sql_kw['predicate.uid'] = '!=0' sql_kw['predicate.uid'] = '!=0'
where_expression = ' AND '.join(expression_list) where_expression = ' AND '.join(expression_list)
...@@ -136,9 +135,12 @@ class DomainTool(BaseTool): ...@@ -136,9 +135,12 @@ class DomainTool(BaseTool):
category_list = context.getCategoryList() category_list = context.getCategoryList()
if len(category_list)==0: if len(category_list)==0:
category_list = ['NULL'] category_list = ['NULL']
category_expression = portal_categories.buildSQLSelector(category_list,query_table='predicate_category') category_expression = portal_categories.buildSQLSelector(
if len(where_expression)>0: category_list,
where_expression = '(%s) AND (%s)' % (where_expression,category_expression) query_table='predicate_category')
if len(where_expression) > 0:
where_expression = '(%s) AND (%s)' % \
(where_expression,category_expression)
else: else:
where_expression = category_expression where_expression = category_expression
sql_kw['where_expression'] = where_expression sql_kw['where_expression'] = where_expression
...@@ -148,55 +150,60 @@ class DomainTool(BaseTool): ...@@ -148,55 +150,60 @@ class DomainTool(BaseTool):
# LOG('searchPredicateList, kw',0,kw) # LOG('searchPredicateList, kw',0,kw)
sql_result_list = portal_catalog.searchResults(**kw) sql_result_list = portal_catalog.searchResults(**kw)
if kw.has_key('src__') and kw['src__']: if kw.get('src__'):
return sql_result_list return sql_result_list
result_list = [] result_list = []
#LOG('searchPredicateList, result_list before test',0,[x.getObject() for x in sql_result_list]) # LOG('searchPredicateList, result_list before test', 0,
# [x.getObject() for x in sql_result_list])
for predicate in [x.getObject() for x in sql_result_list]: for predicate in [x.getObject() for x in sql_result_list]:
if test==0 or predicate.test(context): if test==0 or predicate.test(context):
result_list.append(predicate) result_list.append(predicate)
#LOG('searchPredicateList, result_list before sort',0,result_list) # LOG('searchPredicateList, result_list before sort', 0, result_list)
if filter_method is not None: if filter_method is not None:
result_list = filter_method(result_list) result_list = filter_method(result_list)
if sort_method is not None: if sort_method is not None:
result_list.sort(sort_method) result_list.sort(sort_method)
#LOG('searchPredicateList, result_list after sort',0,result_list) # LOG('searchPredicateList, result_list after sort', 0, result_list)
return result_list return result_list
# XXX FIXME method should not be public
# (some users are not able to see resource's price)
security.declarePublic('generateMappedValue') security.declarePublic('generateMappedValue')
def generateMappedValue(self,context,test=1,predicate_list=None,**kw): def generateMappedValue(self, context, test=1, predicate_list=None, **kw):
""" """
We will generate a mapped value with the list of all predicates founds. Let's say We will generate a mapped value with the list of all predicates
we have 3 predicates (in the order we want) like this: founds.
Let's say we have 3 predicates (in the order we want) like this:
Predicate 1 [ base_price1, , , , , , ] Predicate 1 [ base_price1, , , , , , ]
Predicate 2 [ base_price2, quantity2 , , , , , ] Predicate 2 [ base_price2, quantity2 , , , , , ]
Predicate 3 [ base_price3, quantity3 , , , , , ] Predicate 3 [ base_price3, quantity3 , , , , , ]
Our MappedValue generated will have the base_price of the
Our MappedValue generated will have the base_price of the predicate1, and the quantity of predicate1, and the quantity of the Predicate2, because Predicate
the Predicate2, because Predicate 1 is the first one wich defines a base_price and the 1 is the first one which defines a base_price and the Predicate2
Predicate2 is the first one wich defines a quantity is the first one wich defines a quantity.
""" """
# First get the list of predicates # First get the list of predicates
if predicate_list is None: if predicate_list is None:
predicate_list = self.searchPredicateList(context,test=test,**kw) predicate_list = self.searchPredicateList(context, test=test, **kw)
if len(predicate_list)==0: if len(predicate_list)==0:
return None # No predicate, return None
mapped_value = None
#mapped_value = newTempBase(self.getPortalObject(),'new_mapped_value') else:
from Products.ERP5Type.Document import newTempDeliveryCell # Generate tempDeliveryCell
mapped_value = newTempDeliveryCell(self.getPortalObject(),'new_mapped_value') from Products.ERP5Type.Document import newTempDeliveryCell
mapped_value_property_dict = {} mapped_value = newTempDeliveryCell(self.getPortalObject(),
'new_mapped_value')
# Look for each property the first predicate wich defines the property mapped_value_property_dict = {}
for predicate in predicate_list: # Look for each property the first predicate which defines the
for mapped_value_property in predicate.getMappedValuePropertyList(): # property
if not mapped_value_property_dict.has_key(mapped_value_property): for predicate in predicate_list:
value = predicate.getProperty(mapped_value_property) for mapped_value_property in predicate.getMappedValuePropertyList():
if value is not None: if not mapped_value_property_dict.has_key(mapped_value_property):
mapped_value_property_dict[mapped_value_property] = value value = predicate.getProperty(mapped_value_property)
mapped_value = mapped_value.asContext(**mapped_value_property_dict) if value is not None:
mapped_value_property_dict[mapped_value_property] = value
# Update mapped value
mapped_value = mapped_value.asContext(**mapped_value_property_dict)
return mapped_value return mapped_value
InitializeClass(DomainTool) InitializeClass(DomainTool)
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