Commit 43684d40 authored by Lucas Carvalho's avatar Lucas Carvalho

- increased the performance once this is a front page script

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@28809 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent e5b4eb9f
...@@ -58,26 +58,28 @@ ...@@ -58,26 +58,28 @@
#TODO : USE CACHE\n #TODO : USE CACHE\n
# The goal of this script is to get all the products from all the visible Web Sections\n # The goal of this script is to get all the products from all the visible Web Sections\n
# and it must select randomly which product must be displayed for a given context.\n # and it must select randomly which product must be displayed for a given context.\n
from random import randrange\n from random import choice\n
\n \n
web_site = context.getWebSiteValue() or context.REQUEST.get(\'current_web_site\')\n web_site = context.getWebSiteValue() or context.REQUEST.get(\'current_web_site\')\n
product_list = []\n
kw[\'portal_type\'] = \'Product\'\n kw[\'portal_type\'] = \'Product\'\n
kw[\'limit\'] = limit\n
\n \n
# Getting all the products from all the visible Web Section.\n # Getting all the products from all the visible Web Section.\n
product_dict = {}\n
for web_section in web_site.WebSite_getMainSectionList():\n for web_section in web_site.WebSite_getMainSectionList():\n
product_list.extend([x.uid for x in web_section.getDocumentValueList(all_versions=1, all_languages=1, **kw) if x.uid not in product_list])\n for product in web_section.getDocumentValueList(all_versions=1, all_languages=1, **kw):\n
product_dict[product.uid] = product\n
\n \n
if len(product_list) > limit:\n if len(product_dict) > limit:\n
random_index_list = []\n random_uid_list = []\n
while len(random_index_list) < limit:\n key_list = product_dict.keys()\n
random_number = randrange(0, len(product_list))\n while len(random_uid_list) < limit:\n
if random_number not in random_index_list:\n random_uid = choice(key_list)\n
random_index_list.append(random_number)\n key_list.remove(random_uid)\n
product_list = [product_list[x] for x in random_index_list]\n random_uid_list.append(random_uid)\n
product_list = [product_dict.get(uid) for uid in random_uid_list]\n
else:\n
product_list = product_dict.values()\n
\n \n
product_list = context.portal_catalog(portal_type=\'Product\', uid=product_list)\n
return product_list\n return product_list\n
...@@ -137,22 +139,24 @@ return product_list\n ...@@ -137,22 +139,24 @@ return product_list\n
<string>limit</string> <string>limit</string>
<string>kw</string> <string>kw</string>
<string>random</string> <string>random</string>
<string>randrange</string> <string>choice</string>
<string>_getattr_</string> <string>_getattr_</string>
<string>context</string> <string>context</string>
<string>web_site</string> <string>web_site</string>
<string>product_list</string>
<string>_write_</string> <string>_write_</string>
<string>product_dict</string>
<string>_getiter_</string> <string>_getiter_</string>
<string>web_section</string> <string>web_section</string>
<string>append</string>
<string>$append0</string>
<string>_apply_</string> <string>_apply_</string>
<string>x</string> <string>product</string>
<string>len</string> <string>len</string>
<string>random_index_list</string> <string>random_uid_list</string>
<string>random_number</string> <string>key_list</string>
<string>_getitem_</string> <string>random_uid</string>
<string>append</string>
<string>$append0</string>
<string>uid</string>
<string>product_list</string>
</tuple> </tuple>
</value> </value>
</item> </item>
......
198 200
\ No newline at end of file \ No newline at end of file
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