web/views/magicsearch.py
changeset 3451 6b46d73823f5
parent 3408 c92170fca813
child 3462 3a79fecdd2b4
equal deleted inserted replaced
3448:495862266785 3451:6b46d73823f5
   169     and attributes
   169     and attributes
   170     """
   170     """
   171     priority = 2
   171     priority = 2
   172     def preprocess_query(self, uquery, req):
   172     def preprocess_query(self, uquery, req):
   173         rqlst = parse(uquery, print_errors=False)
   173         rqlst = parse(uquery, print_errors=False)
   174         schema = self.vreg.schema
   174         schema = self._cw.vreg.schema
   175         # rql syntax tree will be modified in place if necessary
   175         # rql syntax tree will be modified in place if necessary
   176         translate_rql_tree(rqlst, trmap(self.config, schema, req.lang), schema)
   176         translate_rql_tree(rqlst, trmap(self._cw.config, schema, req.lang), schema)
   177         return rqlst.as_string(),
   177         return rqlst.as_string(),
   178 
   178 
   179 
   179 
   180 class QSPreProcessor(BaseQueryProcessor):
   180 class QSPreProcessor(BaseQueryProcessor):
   181     """Quick search preprocessor
   181     """Quick search preprocessor
   185     priority = 4
   185     priority = 4
   186 
   186 
   187     def preprocess_query(self, uquery, req):
   187     def preprocess_query(self, uquery, req):
   188         """try to get rql from an unicode query string"""
   188         """try to get rql from an unicode query string"""
   189         args = None
   189         args = None
   190         self.req = req
   190         self._cw = req
   191         try:
   191         try:
   192             # Process as if there was a quoted part
   192             # Process as if there was a quoted part
   193             args = self._quoted_words_query(uquery)
   193             args = self._quoted_words_query(uquery)
   194         ## No quoted part
   194         ## No quoted part
   195         except BadRQLQuery:
   195         except BadRQLQuery:
   208         """check if the given word is matching an entity type, return it if
   208         """check if the given word is matching an entity type, return it if
   209         it's the case or raise BadRQLQuery if not
   209         it's the case or raise BadRQLQuery if not
   210         """
   210         """
   211         etype = word.capitalize()
   211         etype = word.capitalize()
   212         try:
   212         try:
   213             return trmap(self.config, self.vreg.schema, self.req.lang)[etype]
   213             return trmap(self._cw.config, self._cw.vreg.schema, self._cw.lang)[etype]
   214         except KeyError:
   214         except KeyError:
   215             raise BadRQLQuery('%s is not a valid entity name' % etype)
   215             raise BadRQLQuery('%s is not a valid entity name' % etype)
   216 
   216 
   217     def _get_attribute_name(self, word, eschema):
   217     def _get_attribute_name(self, word, eschema):
   218         """check if the given word is matching an attribute of the given entity type,
   218         """check if the given word is matching an attribute of the given entity type,
   220         """
   220         """
   221         """Returns the attributes's name as stored in the DB"""
   221         """Returns the attributes's name as stored in the DB"""
   222         # Need to convert from unicode to string (could be whatever)
   222         # Need to convert from unicode to string (could be whatever)
   223         rtype = word.lower()
   223         rtype = word.lower()
   224         # Find the entity name as stored in the DB
   224         # Find the entity name as stored in the DB
   225         translations = trmap(self.config, self.vreg.schema, self.req.lang)
   225         translations = trmap(self._cw.config, self._cw.vreg.schema, self._cw.lang)
   226         try:
   226         try:
   227             translations = translations[rtype]
   227             translations = translations[rtype]
   228         except KeyError:
   228         except KeyError:
   229             raise BadRQLQuery('%s is not a valid attribute for %s entity type'
   229             raise BadRQLQuery('%s is not a valid attribute for %s entity type'
   230                               % (word, eschema))
   230                               % (word, eschema))
   247 
   247 
   248     def _complete_rql(self, searchstr, etype, rtype=None, var=None, searchattr=None):
   248     def _complete_rql(self, searchstr, etype, rtype=None, var=None, searchattr=None):
   249         searchop = ''
   249         searchop = ''
   250         if '%' in searchstr:
   250         if '%' in searchstr:
   251             if rtype:
   251             if rtype:
   252                 possible_etypes = self.schema.rschema(rtype).objects(etype)
   252                 possible_etypes = self._cw.schema.rschema(rtype).objects(etype)
   253             else:
   253             else:
   254                 possible_etypes = [self.schema.eschema(etype)]
   254                 possible_etypes = [self._cw.schema.eschema(etype)]
   255             if searchattr or len(possible_etypes) == 1:
   255             if searchattr or len(possible_etypes) == 1:
   256                 searchattr = searchattr or possible_etypes[0].main_attribute()
   256                 searchattr = searchattr or possible_etypes[0].main_attribute()
   257                 searchop = 'LIKE '
   257                 searchop = 'LIKE '
   258         searchattr = searchattr or 'has_text'
   258         searchattr = searchattr or 'has_text'
   259         if var is None:
   259         if var is None:
   273 
   273 
   274     def _three_words_query(self, word1, word2, word3):
   274     def _three_words_query(self, word1, word2, word3):
   275         """Specific process for three words query (case (3) of preprocess_rql)
   275         """Specific process for three words query (case (3) of preprocess_rql)
   276         """
   276         """
   277         etype = self._get_entity_type(word1)
   277         etype = self._get_entity_type(word1)
   278         eschema = self.schema.eschema(etype)
   278         eschema = self._cw.schema.eschema(etype)
   279         rtype = self._get_attribute_name(word2, eschema)
   279         rtype = self._get_attribute_name(word2, eschema)
   280         # expand shortcut if rtype is a non final relation
   280         # expand shortcut if rtype is a non final relation
   281         if not self.schema.rschema(rtype).is_final():
   281         if not self._cw.schema.rschema(rtype).is_final():
   282             return self._expand_shortcut(etype, rtype, word3)
   282             return self._expand_shortcut(etype, rtype, word3)
   283         if '%' in word3:
   283         if '%' in word3:
   284             searchop = 'LIKE '
   284             searchop = 'LIKE '
   285         else:
   285         else:
   286             searchop = ''
   286             searchop = ''
   346     __regid__  = 'magicsearch'
   346     __regid__  = 'magicsearch'
   347     def __init__(self, req, rset=None):
   347     def __init__(self, req, rset=None):
   348         super(MagicSearchComponent, self).__init__(req, rset=rset)
   348         super(MagicSearchComponent, self).__init__(req, rset=rset)
   349         processors = []
   349         processors = []
   350         self.by_name = {}
   350         self.by_name = {}
   351         for processorcls in self.vreg['components']['magicsearch_processor']:
   351         for processorcls in self._cw.vreg['components']['magicsearch_processor']:
   352             # instantiation needed
   352             # instantiation needed
   353             processor = processorcls()
   353             processor = processorcls()
   354             processors.append(processor)
   354             processors.append(processor)
   355             if processor.name is not None:
   355             if processor.name is not None:
   356                 assert not processor.name in self.by_name
   356                 assert not processor.name in self.by_name