Exemplo n.º 1
0
    def options(self):
        search_types = [{
            'id': 'images',
            'label': 'Image',
            'query': {
                'portal_type': 'Image'
            }
        }, {
            'id': 'page',
            'label': 'Page',
            'query': {
                'portal_type': ['Document', 'Folder']
            }
        }]

        ptypes = api.portal.get_tool('portal_types')
        allow_anyway = ['Audio']
        for type_id in ptypes.objectIds():
            if type_id in ('Link', 'Document', 'Folder'):
                continue
            _type = ptypes[type_id]
            if not _type.global_allow and type_id not in allow_anyway:
                continue
            search_types.append({
                'id': type_id.lower(),
                'label': _type.title,
                'query': {
                    'portal_type': type_id
                }
            })
        # search_types.append({
        #     'id': 'audio',
        #     'label': 'Audio',
        #     'query': {
        #         'portal_type': 'Audio'
        #     }
        # })
        # search_types.sort(key=lambda type: type['label'])

        additional_sites = []
        es = ElasticSearchCatalog(api.portal.get_tool('portal_catalog'))
        if es.enabled:
            query = {
                "size": 0,
                "aggregations": {
                    "totals": {
                        "terms": {
                            "field": "domain"
                        }
                    }
                }
            }
            try:
                result = es.connection.search(
                    index=es.index_name,
                    doc_type=CRAWLED_SITE_ES_DOC_TYPE,
                    body=query)
                for res in result['aggregations']['totals']['buckets']:
                    site_name = res.get('key')
                    if '.' not in site_name or 'amazon' in site_name:
                        continue
                    additional_sites.append(site_name)
            except TransportError:
                pass

        parsed = urlparse(get_public_url())
        return json.dumps({
            'searchTypes':
            sorted(search_types, key=lambda st: st['label']),
            'additionalSites':
            sorted(additional_sites),
            'currentSiteLabel':
            parsed.netloc,
            'searchHelpText':
            api.portal.get_registry_record('castle.search_page_help_text',
                                           None),
        })
Exemplo n.º 2
0
 def get_public_url(self):
     return utils.get_public_url()
Exemplo n.º 3
0
    def options(self):
        search_types = [{
            'id': 'images',
            'label': 'Images',
            'query': {
                'portal_type': 'Image'
            }
        }, {
            'id': 'page',
            'label': 'Page',
            'query': {
                'portal_type': ['Document', 'Folder']
            }
        }]

        ptypes = api.portal.get_tool('portal_types')
        for type_id in ptypes.objectIds():
            if type_id in ('Link', 'Document', 'Folder'):
                continue
            _type = ptypes[type_id]
            if not _type.global_allow:
                continue
            search_types.append({
                'id': type_id.lower(),
                'label': _type.title,
                'query': {
                    'portal_type': type_id
                }
            })
        search_types.extend([{
            'id': 'video',
            'label': 'Video',
            'query': {
                'portal_type': 'Video'
            }
        }, {
            'id': 'audio',
            'label': 'Audio',
            'query': {
                'portal_type': 'Audio'
            }
        }])

        additional_sites = []
        es = ElasticSearchCatalog(api.portal.get_tool('portal_catalog'))
        if es.enabled:
            query = {
                "size": 0,
                "aggregations": {
                    "totals": {
                        "terms": {
                            "field": "domain"
                        }
                    }
                }
            }
            try:
                result = es.connection.search(
                    index=es.index_name,
                    doc_type=CRAWLED_SITE_ES_DOC_TYPE,
                    body=query)
                for res in result['aggregations']['totals']['buckets']:
                    site_name = res.get('key')
                    if '.' not in site_name or 'amazon' in site_name:
                        continue
                    additional_sites.append(site_name)
            except TransportError:
                return []

        parsed = urlparse(get_public_url())

        return json.dumps({
            'searchTypes': search_types,
            'additionalSites': [s for s in sorted(additional_sites)],
            'currentSiteLabel': parsed.netloc
        })