def __call__(self):
        if self.request.method != 'POST':
            self.request.response.setStatus(405)
            return ''
        self.request.stdin.seek(0, 0)
        payload = json.load(self.request.stdin)
        if not isinstance(payload, dict):
            self.request.response.setStatus(400)
            return ''
        if 'fields' in payload:
            if (not isinstance(payload['fields'], list) or
                'contents' in payload['fields']):
                # Prevent people to retrieve the fulltext.
                self.request.response.setStatus(400)
                return ''
        authorizedFilter = {
            'terms': {
                'authorizedUsers': self._listAllowedRolesAndUsers(
                    _getAuthenticatedUser(self.context)),
                'execution': 'or'}}
        if 'query' in payload:
            if 'filtered' in payload['query']:
                filtered = payload['query']['filtered']
                if ('filter' not in filtered or
                    not isinstance(filtered['filter'], dict)):
                    self.request.response.setStatus(400)
                    return ''
                if 'and' not in filtered['filter']:
                    filters = [filtered.pop('filter')]
                    filtered['filter'] = {'and': filters}
                else:
                    filters = filtered['filter']['and']
                    if not isinstance(filters, list):
                        self.request.response.setStatus(400)
                        return ''
                filters.append(authorizedFilter)
            else:
                query = payload.pop('query')
                payload['query'] = {
                    'filtered': {
                        'query': query,
                        'filter': authorizedFilter}}
        else:
            payload['query'] = {
                'filtered': {'filter': authorizedFilter}}

        settings = IElasticSettings(getUtility(IPloneSiteRoot))
        try:
            response = urllib2.urlopen(
                random.choice(settings.get_search_urls()),
                json.dumps(payload))
        except:
            self.request.response.setStatus(500)
            return ''

        self.request.response.setHeader(
            'Content-Type',
            'application/json;charset=UTF-8')
        return response.read()
 def update(self):
     settings = IElasticSettings(getUtility(IPloneSiteRoot))
     if settings.public_through_plone:
         urls = [absoluteURL(self.context, self.request) + '/search.json']
     else:
         urls = settings.get_search_urls()
     self.server_urls = json.dumps(urls)
     self.expanded = ''
     if 'advanced' in self.request.form:
         self.expanded = 'expanded'
     self.request.set('disable_border', 1)
     self.request.set('disable_plone.leftcolumn', 1)
     self.request.set('disable_plone.rightcolumn', 1)
 def update(self):
     settings = IElasticSettings(getUtility(IPloneSiteRoot))
     if settings.public_through_plone:
         urls = [absoluteURL(self.context, self.request) + '/search.json']
     else:
         urls = settings.get_search_urls()
     self.server_urls = json.dumps(urls)
     self.expanded = ''
     if 'advanced' in self.request.form:
         self.expanded = 'expanded'
     self.request.set('disable_border', 1)
     self.request.set('disable_plone.leftcolumn', 1)
     self.request.set('disable_plone.rightcolumn', 1)
 def delete_index(self, action, data):
     send = IStatusMessage(self.request).add
     try:
         delete_index(IElasticSettings(self.context))
     except:
         send("Error while deleting the index.", type='error')
     else:
         send("Index deleted.")
Beispiel #5
0
 def _get_settings(self):
     if self._settings is None:
         portal = queryUtility(IPloneSiteRoot)
         if portal is None:
             return None
         self._settings = IElasticSettings(portal)
         if self._settings.only_published:
             self._get_status = getToolByName(portal,
                                              'portal_workflow').getInfoFor
     return self._settings
    def __call__(self):
        if self.request.method != 'POST':
            self.request.response.setStatus(405)
            return ''
        self.request.stdin.seek(0, 0)
        payload = json.load(self.request.stdin)
        if not isinstance(payload, dict):
            self.request.response.setStatus(400)
            return ''
        if 'fields' in payload:
            if (not isinstance(payload['fields'], list)
                    or 'contents' in payload['fields']):
                # Prevent people to retrieve the fulltext.
                self.request.response.setStatus(400)
                return ''
        authorizedFilter = {
            'terms': {
                'authorizedUsers':
                self._listAllowedRolesAndUsers(
                    _getAuthenticatedUser(self.context)),
                'execution':
                'or'
            }
        }
        if 'query' in payload:
            if 'filtered' in payload['query']:
                filtered = payload['query']['filtered']
                if ('filter' not in filtered
                        or not isinstance(filtered['filter'], dict)):
                    self.request.response.setStatus(400)
                    return ''
                if 'and' not in filtered['filter']:
                    filters = [filtered.pop('filter')]
                    filtered['filter'] = {'and': filters}
                else:
                    filters = filtered['filter']['and']
                    if not isinstance(filters, list):
                        self.request.response.setStatus(400)
                        return ''
                filters.append(authorizedFilter)
            else:
                query = payload.pop('query')
                payload['query'] = {
                    'filtered': {
                        'query': query,
                        'filter': authorizedFilter
                    }
                }
        else:
            payload['query'] = {'filtered': {'filter': authorizedFilter}}

        settings = IElasticSettings(getUtility(IPloneSiteRoot))
        try:
            response = urllib2.urlopen(
                random.choice(settings.get_search_urls()), json.dumps(payload))
        except:
            self.request.response.setStatus(500)
            return ''

        self.request.response.setHeader('Content-Type',
                                        'application/json;charset=UTF-8')
        return response.read()