Commit 3df9d7ca authored by Alexandre Boeglin's avatar Alexandre Boeglin

Add support for individual variations in Resource and Variated API.

Original work by Daniel Feliubadalo and Romain Courteaud.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20291 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 1041f3f4
......@@ -82,7 +82,8 @@ class Resource(XMLMatrix, Variated):
def getVariationRangeCategoryItemList(self, base_category_list=(), base=1,
root=1, display_id='title',
display_base_category=1,
current_category=None, **kw):
current_category=None,
omit_individual_variation=0, **kw):
"""
Returns possible variations
......@@ -113,41 +114,40 @@ class Resource(XMLMatrix, Variated):
"""
result = []
if base_category_list is ():
base_category_list = self.getVariationBaseCategoryList()
elif type(base_category_list) is type('a'):
base_category_list = self.getVariationBaseCategoryList(
omit_individual_variation=omit_individual_variation)
elif isinstance(base_category_list, str):
base_category_list = (base_category_list,)
individual_variation_list = self.searchFolder(
portal_type=self.getPortalVariationTypeList(),
sort_on=[('title','ascending')])
individual_variation_list = [x.getObject() for x in
individual_variation_list]
other_base_category_dict = dict([(i,1) for i in base_category_list])
other_variations = self.searchFolder( \
portal_type=self.getPortalVariationTypeList(),
sort_on=[('title','ascending')])
other_variations = [x.getObject() for x in other_variations]
other_variations = [x for x in other_variations if x is not None]
for object in other_variations:
for base_category in object.getVariationBaseCategoryList():
if (base_category_list is ()) or \
(base_category in base_category_list):
other_base_category_dict[base_category] = 0
# XXX now, call Renderer a lot of time.
# Better implementation needed
result.extend(Renderer(
base_category=base_category,
display_base_category=display_base_category,
display_none_category=0, base=base,
current_category=current_category,
display_id=display_id).\
render([object]))
other_base_category_item_list = filter(lambda x: x[1]==1,
other_base_category_dict.items())
other_base_category_list = map(lambda x: x[0],
other_base_category_item_list)
if not omit_individual_variation:
for variation in individual_variation_list:
for base_category in variation.getVariationBaseCategoryList():
if base_category_list is ()\
or base_category in base_category_list:
other_base_category_dict[base_category] = 0
# XXX now, call Renderer a lot of time.
# Better implementation needed
result.extend(Renderer(
base_category=base_category,
display_base_category=display_base_category,
display_none_category=0, base=base,
current_category=current_category,
display_id=display_id).render([variation]))
other_base_category_list = [x for x, y in
other_base_category_dict.iteritems() if y == 1]
# Get category variation
if len(other_base_category_list) != 0:
result += Variated.getVariationRangeCategoryItemList(
if other_base_category_list:
result.extend(Variated.getVariationRangeCategoryItemList(
self, base_category_list=other_base_category_list,
base=base, display_base_category=display_base_category, **kw)
base=base, display_base_category=display_base_category, **kw))
# Return result
return result
......@@ -169,31 +169,37 @@ class Resource(XMLMatrix, Variated):
*old parameters: base=1, current_category=None,
display_id='getTitle' (default value getTitleOrId)
"""
base_category_list = base_category_list or \
self.getVariationBaseCategoryList()
individual_bc_list = self.getIndividualVariationBaseCategoryList()
other_bc_list = [x for x in base_category_list if x not
in individual_bc_list]
result = Variated.getVariationCategoryItemList(self,
base_category_list=base_category_list,
base_category_list=other_bc_list,
display_base_category=display_base_category,
display_id=display_id, base=base, **kw)
if not omit_individual_variation:
other_variations = self.searchFolder(
portal_type=self.getPortalVariationTypeList())
other_variations = map(lambda x: x.getObject(), other_variations)
other_variations = filter(lambda x: x is not None, other_variations)
for object in other_variations:
for base_category in object.getVariationBaseCategoryList():
if (base_category_list is ()) or \
(base_category in base_category_list):
individual_variation_list = self.searchFolder(
portal_type=self.getPortalVariationTypeList())
individual_variation_list = [x.getObject() for x in
individual_variation_list]
for variation in individual_variation_list:
for base_category in variation.getVariationBaseCategoryList():
if (base_category_list is () or base_category in
base_category_list) and base_category in individual_bc_list:
# XXX append object, relative_url ?
# XXX now, call Renderer a lot of time.
# Better implementation needed
result.extend(Renderer(
base_category=base_category,
display_base_category=display_base_category,
display_none_category=0, base=base,
current_category=current_category,
display_id=display_id, **kw).\
render([object]))
base_category=base_category,
display_base_category=display_base_category,
display_none_category=0, base=base,
current_category=current_category, display_id=display_id,
**kw).render([variation]))
return result
security.declareProtected(Permissions.AccessContentsInformation,
......@@ -231,7 +237,7 @@ class Resource(XMLMatrix, Variated):
vcil = self.getVariationCategoryItemList(
base_category_list=base_category_list,
omit_individual_variation=omit_individual_variation,**kw)
return map(lambda x: x[1], vcil)
return [x[1] for x in vcil]
# Unit conversion
security.declareProtected(Permissions.AccessContentsInformation, 'convertQuantity')
......
......@@ -62,7 +62,7 @@ class Variated(Base):
security.declareProtected(Permissions.AccessContentsInformation,
'getVariationBaseCategoryList')
def getVariationBaseCategoryList(self, omit_optional_variation=0,
omit_option_base_category=None):
omit_option_base_category=None, omit_individual_variation=0):
"""
Return the list of variation base category.
If omit_optional_variation==1, do not include base category
......@@ -81,6 +81,12 @@ class Variated(Base):
# of a good API.
option_base_category_list = self.getPortalOptionBaseCategoryList()
vbcl = [x for x in vbcl if x not in option_base_category_list]
else:
vbcl.extend(self.getOptionalVariationBaseCategoryList())
if omit_individual_variation == 0:
vbcl.extend(self.getIndividualVariationBaseCategoryList())
return vbcl
security.declareProtected(Permissions.AccessContentsInformation,
......@@ -222,7 +228,8 @@ class Variated(Base):
security.declareProtected(Permissions.AccessContentsInformation,
'getVariationBaseCategoryItemList')
def getVariationBaseCategoryItemList(self, display_id='title_or_id',
omit_optional_variation=0, omit_option_base_category=None):
omit_optional_variation=0, omit_option_base_category=None,
omit_individual_variation=0):
"""
Returns base category of the resource
as a list of tuples (title, id). This is mostly
......@@ -236,7 +243,8 @@ class Variated(Base):
omit_optional_variation = omit_option_base_category
variation_base_category_list = self.getVariationBaseCategoryList(
omit_optional_variation=omit_optional_variation)
omit_optional_variation=omit_optional_variation,
omit_individual_variation=omit_individual_variation)
result = []
for base_category in variation_base_category_list:
bc = self.portal_categories.resolveCategory(base_category)
......@@ -348,16 +356,18 @@ class Variated(Base):
security.declareProtected(Permissions.AccessContentsInformation,
'getVariationRangeCategoryList')
def getVariationRangeCategoryList(self, base_category_list=(), base=1,
root=1, current_category=None):
root=1, current_category=None,
omit_individual_variation=0):
"""
Returns the range of acceptable categories
"""
vrcil = self.getVariationRangeCategoryItemList(
base_category_list=base_category_list,
base=base, root=root,
current_category=current_category)
base_category_list=base_category_list,
base=base, root=root,
current_category=current_category,
omit_individual_variation=omit_individual_variation)
# display is on left
return map(lambda x: x[1], vrcil)
return [x[1] for x in vrcil]
# Context related methods
security.declarePublic('newVariationValue')
......
This diff is collapsed.
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