def evolve(root): from repoze.catalog.query import Any from repoze.catalog.query import Contains from repoze.catalog.query import Eq from pyramid.traversal import resource_path from voteit.core.models.catalog import reindex_object from voteit.core.models.catalog import resolve_catalog_docid print "Removing absolute urls in profile links" catalog = root.catalog host = None while not host: host = raw_input( "Enter a host to replace (ex http://127.0.0.1:6543): ") count, result = catalog.query(Eq('path', resource_path(root)) & \ Contains('searchable_text', 'class="inlineinfo"') & \ Any('content_type', ('DiscussionPost', 'Proposal', ))) catalog = root.catalog print "Processing %s objects" % count for docid in result: # get object obj = resolve_catalog_docid(catalog, root, docid) obj.title = obj.title.replace(host, '') reindex_object(catalog, obj)
def query2(self, soup): # fetch user vendor uids vendor_uids = get_vendor_uids_for() # filter by given vendor uid or user vendor uids vendor_uid = self.request.form.get('vendor') if vendor_uid: vendor_uid = uuid.UUID(vendor_uid) # raise if given vendor uid not in user vendor uids if vendor_uid not in vendor_uids: raise Unauthorized query = Any('vendor_uids', [vendor_uid]) else: query = Any('vendor_uids', vendor_uids) # filter by customer if given customer = self.request.form.get('customer') if customer: query = query & Eq('creator', customer) # filter by search term if given term = self.request.form['sSearch'].decode('utf-8') if term: query = query & Contains(self.search_text_index, term) # query orders and return result sort = self.sort() res = soup.lazy(query, sort_index=sort['index'], reverse=sort['reverse'], with_size=True) length = res.next() return length, res
def __call__(self): settings = Settings(self.context) catalog = settings.catalog if catalog: query = self.request.form.get('q') results = catalog.query(Contains('text', query)) return json.dumps({"results": list(results[1]), "query": query}) return json.dumps({"results": [], "query": query})
def change_mentions(root, userid, userid_lower): catalog = root.catalog result = catalog.query(Eq('path', resource_path(root)) & \ Contains('searchable_text', userid) & \ Any('content_type', ('DiscussionPost', 'Proposal', )))[1] for docid in result: # get object obj = resolve_catalog_docid(catalog, root, docid) title = obj.title for match in re.finditer('<a class="inlineinfo" href="http:\/\/[\da-z.-:]*/[\w-]*/_userinfo\?userid=('+userid+')" title="[\w\s-]*">@('+userid+')</a>', title, re.UNICODE): title = title[0:match.start(1)] + userid_lower + title[match.end(1):len(title)] # replace in url title = title[0:match.start(2)] + userid_lower + title[match.end(2):len(title)] # replace in text obj.title = title
def __call__(self): settings = Settings(self.context) catalog = settings.catalog query = self.request.form.get('q') results = None if query: try: results = catalog.query(Contains('text', query)) except (TypeError, ParseError): pass if catalog and results: return json.dumps({"results": list(results[1]), "query": query}) return json.dumps({"results": [], "query": query})
def query(self, soup): soup.reindex() # fetch user vendor uids vendor_uids = get_vendor_uids_for() # filter by given vendor uid or user vendor uids vendor_uid = self.request.form.get('vendor') if vendor_uid: vendor_uid = uuid.UUID(vendor_uid) # raise if given vendor uid not in user vendor uids if vendor_uid not in vendor_uids: raise Unauthorized query = Any('vendor_uid', [vendor_uid]) else: query = Any('vendor_uid', vendor_uids) # filter by customer if given customer = self.request.form.get('customer') if customer: query = query & Eq('creator', customer) # filter by search term if given term = self.request.form['sSearch'].decode('utf-8') if term: query = query & Contains(self.search_text_index, term) # Show only tickets that are paid query = query & Eq('salaried', 'yes') if not IPloneSiteRoot.providedBy(self.context): buyable_uids = self._get_buyables_in_context() query = query & Any('buyable_uids', buyable_uids) # query orders and return result sort = self.sort() try: res = soup.lazy(query, sort_index=sort['index'], reverse=sort['reverse'], with_size=True) length = res.next() except: length = 0 pass return length, res
def query(self, soup): # fetch user vendor uids vendor_uids = get_vendor_uids_for() # filter by given vendor uid or user vendor uids vendor_uid = self.request.form.get('vendor') if vendor_uid: vendor_uid = uuid.UUID(vendor_uid) # raise if given vendor uid not in user vendor uids if vendor_uid not in vendor_uids: raise Unauthorized query = Any('vendor_uids', [vendor_uid]) else: query = Any('vendor_uids', vendor_uids) # filter by customer if given customer = self.request.form.get('customer') if customer: query = query & Eq('creator', customer) # Filter by state if given state = self.request.form.get('state') if state: query = query & Eq('state', state) # Filter by salaried if given salaried = self.request.form.get('salaried') if salaried: query = query & Eq('salaried', salaried) # filter by search term if given term = self.request.form['sSearch'].decode('utf-8') if term: # append * for proper fulltext search term += '*' query = query & Contains(self.search_text_index, term) # get buyable uids for given context, get all buyables on site root # use explicit IPloneSiteRoot to make it play nice with lineage if not IPloneSiteRoot.providedBy(self.context): buyable_uids = self._get_buyables_in_context() query = query & Any('buyable_uids', buyable_uids) # query orders and return result sort = self.sort() res = soup.lazy(query, sort_index=sort['index'], reverse=sort['reverse'], with_size=True) length = res.next() return length, res
def query(self, soup): query = Eq('creator', plone.api.user.get_current().getId()) # filter by search term if given term = self.request.form['sSearch'].decode('utf-8') if term: # append * for proper fulltext search term += '*' query = query & Contains(self.search_text_index, term) # query orders and return result sort = self.sort() res = soup.lazy(query, sort_index=sort['index'], reverse=sort['reverse'], with_size=True) length = res.next() return length, res
def _mk_query(self): self.docids = () query = self.request.GET.get('query', None) if not query: return if self.request.GET.get('glob', False): if '*' not in query: query = "%s*" % query query_obj = Contains('searchable_text', query) & Eq( 'search_visible', True) type_name = self.request.GET.getall('type_name') if type_name: query_obj &= Any('type_name', type_name) try: self.docids = self.root.catalog.query(query_obj)[1] except ParseError: if not self.request.is_xhr: msg = _(u"Invalid search query - try something else!") self.flash_messages.add(msg, type="danger")
def parse_query_params(self, query): if not query: return [] queries = [] for index, value in query.items(): if not value or value in ["*", "**"]: continue if index not in self.indexes: continue if six.PY2: value = value.decode("utf-8") if index == self.text_index: queries.append(Contains("text", value)) elif index in self.keyword_indexes: queries.append(Any(index, value)) else: queries.append(Eq(index, value)) if not queries: return None return And(*queries)
def test_negate(self): from repoze.catalog.query import Contains inst = self._makeOne('index', 'val') self.assertEqual(inst.negate(), Contains('index', 'val'))
def _text_checker(self, text): # used for query if len(text) < 1: return None return Contains('text', text + '*')
def queryText(zg, searchstring): print 'searching for', searchstring terms = searchstring.split() query = And(*[Contains('abstract', term) for term in terms]) res = zg.nodecatalog.query(query, sort_index='abstract') return res
from repoze.catalog.query import Any from repoze.catalog.query import Contains from repoze.catalog.query import Name from betahaus.pyracont.factories import createSchema from zope.index.text.parsetree import ParseError from voteit.core.views.base_view import BaseView from voteit.core.models.interfaces import IMeeting from voteit.core.models.schemas import button_search from voteit.core.security import VIEW from voteit.core.helpers import strip_and_truncate from voteit.core import VoteITMF as _ SEARCH_VIEW_QUERY = Eq('path', Name('path')) \ & Contains('searchable_text', Name('searchable_text')) \ & Any('content_type', ('DiscussionPost', 'Proposal', 'AgendaItem' )) \ & Any('allowed_to_view', Name('allowed_to_view')) class SearchView(BaseView): """ Handle incoming search query and display result. """ @view_config(context=IMeeting, name="search", renderer="templates/search.pt", permission = VIEW) def search(self): schema = createSchema('SearchSchema').bind(context = self.context, request = self.request) form = Form(schema, buttons=(button_search,)) self.api.register_form_resources(form) appstruct = {} self.response['results'] = []
def select(cls, attempt=0, sort_index=None, reverse=False, limit=None, *args, **kwargs): catalog = cls._get_catalog() qo = None for key in kwargs: key_parts = key.split('__', 1) original_key = key key = key_parts[0] if key not in catalog.keys(): print catalog.keys() raise NoIndex( 'The field %s is not in the list of indexed fields for %s' % (key, cls.__name__)) value = kwargs[original_key] if isinstance(value, ZODBModel): value = unicode(value) if len(key_parts) == 2: if key_parts[1] == 'gt': nqo = Gt(key, value) elif key_parts[1] == 'lt': nqo = Lt(key, value) elif key_parts[1] == 'gte': nqo = Ge(key, value) elif key_parts[1] == 'lte': nqo = Le(key, value) elif key_parts[1] == 'contains': nqo = Contains(key, value) elif key_parts[1] == 'ncontains': nqo = DoesNotContain(key, value) elif key_parts[1] == 'ne': nqo = NotEq(key, value) elif key_parts[1] == 'in': nqo = Any(key, value) elif key_parts[1] == 'nin': nqo = NotAny(key, value) else: raise Exception("Unknown comparator %s" % (key_parts[1])) else: nqo = Eq(key, value) if qo: qo = qo & nqo else: qo = nqo root = cls._get_root() if qo: _, results = catalog.query(qo, sort_index=sort_index, reverse=reverse, limit=limit) else: _, results = catalog.sort_result(root.keys(), sort_index=sort_index, reverse=reverse, limit=limit) try: return [root[x] for x in results] except KeyError, e: if attempt < 2: cls.index() return cls.select(attempt=attempt + 1, *args, **kwargs) raise e