Commit c2deeacc authored by Ivan Tyagov's avatar Ivan Tyagov

Refactor script:

- no need to set / get selections, this is not a listbox but a search dialog form
- adjust to current ZSQLCatalog API
- raise if search criteria not specified rather than return empty list
- use proper variable naming

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33150 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 6a04b330
...@@ -56,100 +56,74 @@ ...@@ -56,100 +56,74 @@
<value> <string encoding="cdata"><![CDATA[ <value> <string encoding="cdata"><![CDATA[
"""\n """\n
The main search script. Receives one big string - a searchabletext, in\n The main search script. Receives one big string - a searchabletext, in\n
the search syntax, parses the string using external method Base_parseSearchString,\n the search syntax, parses the string using external method Base_parseSearchString,\n
then does the following:\n then does the following:\n
- processes arguments for searching by any category\n - processes arguments for searching by any category\n
- selects search mode\n - selects search mode\n
- adds creation and modification date clauses\n - adds creation and modification date clauses\n
- searches\n - searches\n
- if requested, filters result so that only the user\'s docs are returned\n - if requested, filters result so that only the user\'s docs are returned\n
- if requested, filters result to return only the newest versions\n - if requested, filters result to return only the newest versions\n
"""\n """\n
portal = context.getPortalObject()\n
\n \n
# if no args we return empty list\n query_kw = {}\n
# we use only searchabletext - all params are passed through this\n date_format = \'%Y-%m-%d\'\n
\n \n
# searchabletext can be supplied in request or stored in selection\n if searchabletext is None:\n
sstr = context.REQUEST.get(\'searchabletext\')\n # searchabletext can be supplied in request (fallback)\n
searchabletext = context.REQUEST.get(\'searchabletext\')\n
\n \n
if sstr:\n if searchabletext is None:\n
context.portal_selections.setSelectionParamsFor(\'search_params_selection\', dict(searchabletext=sstr))\n raise ValueError, "No search string specified."\n
else:\n
params = context.portal_selections.getSelectionParamsFor(\'search_params_selection\')\n
if params:\n
sstr = params.get(\'searchabletext\')\n
\n
if not sstr: return []\n
\n \n
args = context.Base_parseSearchString(sstr)\n parsed_search_string = context.Base_parseSearchString(searchabletext)\n
\n \n
# if no portal type specified, take all\n # if no portal type specified, take all\n
if not args.has_key(\'portal_type\') or args[\'portal_type\'] == ():\n portal_type = parsed_search_string.get(\'portal_type\', None)\n
args[\'portal_type\'] = context.getPortalDocumentTypeList()\n if portal_type is None or not len(portal_type):\n
\n query_kw[\'portal_type\'] = portal.getPortalDocumentTypeList()\n
### All document types in DMS do not use any xxx_relative property.\n \n
### So, I comment out here. (yusei)\n # ZSQLCatalog wants table.key to avoid ambiguity\n
## process searching by category\n parsed_searchabletext = parsed_search_string.get(\'searchabletext\', None)\n
#cats = []\n if parsed_searchabletext is not None: \n
#bases = context.portal_categories.getBaseCategoryList()\n query_kw[\'full_text.SearchableText\'] = parsed_searchabletext\n
#for k,v in args.items():\n \n
# if k in bases and k!=\'language\':\n where_expression_list = []\n
# args[k+\'_relative_url\'] = k + \'/\' + v\n creation_from = parsed_search_string.get(\'creation_from\', None)\n
\n creation_to = parsed_search_string.get(\'creation_to\', None)\n
# expand simplified notation of search mode\n modification_from = parsed_search_string.get(\'modification_from\', None)\n
#mode = args.get(\'mode\')\n modification_to = parsed_search_string.get(\'modification_to\', None)\n
#modemap = {\'natural\':0, \'boolean\':\'in_boolean_mode\', \'expanded\':\'with_query_expansion\'}\n if creation_from:\n
#if mode and modemap.has_key(mode):\n where_expression_list.append(\'catalog.creation_date >= "%s"\' \\\n
# args[\'SearchableText\'] = dict(query=args[\'SearchableText\'], search_mode=modemap[mode])\n %creation_from.strftime(date_format))\n
\n if creation_to:\n
# we always do boolean because we want these options in search dialog\n where_expression_list.append(\'catalog.creation_date <= "%s"\' \\\n
args[\'searchabletext\'] = dict(query=args[\'searchabletext\'], search_mode=\'in_boolean_mode\')\n %creation_to.strftime(date_format))\n
\n if modification_from:\n
# a hack because SQLCatalog wants table.key now \n where_expression_list.append(\'catalog.modification_date >= "%s"\' \\\n
# dunno if it is a bug or a feature\n %modification_from.strftime(date_format))\n
if args.has_key(\'searchabletext\'):\n if modification_to:\n
args[\'full_text.SearchableText\'] = args[\'searchabletext\']\n where_expression_list.append(\'catalog.modification_date <= "%s"\' \\\n
args.pop(\'searchabletext\')\n %modification_to.strftime(date_format))\n
\n if len(where_expression_list):\n
cf = args.get(\'creation_from\')\n query_kw[\'where_expression\'] = \' AND \'.join(where_expression_list)\n
ct = args.get(\'creation_to\')\n \n
mf = args.get(\'modification_from\')\n if parsed_search_string.get(\'mine\'):\n
mt = args.get(\'modification_to\')\n # user wants only his documents\n
\n query_kw[\'owner\'] = str(portal.portal_membership.getAuthenticatedMember())\n
wheres = []\n \n
if cf:\n if parsed_search_string.get(\'newest\'):\n
wheres.append(\'catalog.creation_date>"\' + cf.strftime(\'%Y-%m-%d\') + \'"\')\n #...and now we check for only the newest versions\n
if ct:\n # but we need to preserve order\n
wheres.append(\'catalog.creation_date<"\' + cf.strftime(\'%Y-%m-%d\') + \'"\')\n query_kw[\'group_by\'] = (\'reference\',)\n
if mf:\n result = [doc.getLatestVersionValue() \\\n
wheres.append(\'catalog.modification_date>"\' + cf.strftime(\'%Y-%m-%d\') + \'"\')\n for doc in context.portal_catalog(**query_kw)]\n
if mt:\n
wheres.append(\'catalog.modification_date<"\' + cf.strftime(\'%Y-%m-%d\') + \'"\')\n
if wheres != []:\n
args[\'where_expression\'] = \' AND \'.join(wheres)\n
\n
# now we search\n
# any language?\n
if args.get(\'language\') == \'0\': args.pop(\'language\')\n
\n
# user wants only his documents\n
if args.get(\'mine\'): \n
from AccessControl import getSecurityManager\n
sm = getSecurityManager()\n
user = sm.getUser()\n
args[\'owner\'] = str(user)\n
\n
#...and now we check for only the newest versions\n
# but we need to preserve order\n
if args.get(\'newest\'):\n
args[\'group_by\'] = (\'reference\',)\n
res = context.portal_catalog(**args)\n
res = [doc.getLatestVersionValue() for doc in res]\n
else:\n else:\n
res = context.portal_catalog(**args)\n result = portal.portal_catalog(**query_kw)\n
\n \n
return res\n return result\n
]]></string> </value> ]]></string> </value>
...@@ -162,7 +136,7 @@ return res\n ...@@ -162,7 +136,7 @@ return res\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>**kw</string> </value> <value> <string>searchabletext=None, **kw</string> </value>
</item> </item>
<item> <item>
<key> <string>errors</string> </key> <key> <string>errors</string> </key>
...@@ -182,37 +156,38 @@ return res\n ...@@ -182,37 +156,38 @@ return res\n
<dictionary> <dictionary>
<item> <item>
<key> <string>co_argcount</string> </key> <key> <string>co_argcount</string> </key>
<value> <int>0</int> </value> <value> <int>1</int> </value>
</item> </item>
<item> <item>
<key> <string>co_varnames</string> </key> <key> <string>co_varnames</string> </key>
<value> <value>
<tuple> <tuple>
<string>searchabletext</string>
<string>kw</string> <string>kw</string>
<string>_getattr_</string> <string>_getattr_</string>
<string>context</string> <string>context</string>
<string>sstr</string> <string>portal</string>
<string>dict</string> <string>query_kw</string>
<string>params</string> <string>date_format</string>
<string>args</string> <string>None</string>
<string>_getitem_</string> <string>ValueError</string>
<string>parsed_search_string</string>
<string>portal_type</string>
<string>len</string>
<string>_write_</string> <string>_write_</string>
<string>cf</string> <string>parsed_searchabletext</string>
<string>ct</string> <string>where_expression_list</string>
<string>mf</string> <string>creation_from</string>
<string>mt</string> <string>creation_to</string>
<string>wheres</string> <string>modification_from</string>
<string>AccessControl</string> <string>modification_to</string>
<string>getSecurityManager</string>
<string>sm</string>
<string>user</string>
<string>str</string> <string>str</string>
<string>_apply_</string>
<string>res</string>
<string>append</string> <string>append</string>
<string>$append0</string> <string>$append0</string>
<string>_getiter_</string> <string>_getiter_</string>
<string>_apply_</string>
<string>doc</string> <string>doc</string>
<string>result</string>
</tuple> </tuple>
</value> </value>
</item> </item>
...@@ -224,7 +199,9 @@ return res\n ...@@ -224,7 +199,9 @@ return res\n
<item> <item>
<key> <string>func_defaults</string> </key> <key> <string>func_defaults</string> </key>
<value> <value>
<none/> <tuple>
<none/>
</tuple>
</value> </value>
</item> </item>
<item> <item>
......
1070 1073
\ 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