def update_haystack_settings(): """Update the haystack settings in site config.""" search_backend_id = (siteconfig.get('search_backend_id') or defaults['search_backend_id']) search_backend = search_backend_registry.get_search_backend( search_backend_id) if not search_backend: raise ImproperlyConfigured(_( 'The search engine "%s" could not be found. If this is ' 'provided by an extension, you will have to make sure that ' 'extension is enabled.' % search_backend_id )) apply_setting( 'HAYSTACK_CONNECTIONS', None, { 'default': search_backend.configuration, }) # Re-initialize Haystack's connection information to use the updated # settings. connections.connections_info = settings.HAYSTACK_CONNECTIONS connections._connections = {}
def clean_search_backend_id(self): """Clean the ``search_backend_id`` field. This will ensure the chosen search backend is valid (i.e., it is available in the registry) and that its dependencies have been installed. Returns: unicode: The search backend ID. Raises: django.core.exceptions.ValidationError: Raised if the search engine ID chosen cannot be used. """ search_backend_id = self.cleaned_data['search_backend_id'] search_backend = search_backend_registry.get_search_backend( search_backend_id) if not search_backend: raise ValidationError( _('The search engine "%s" could not be found. If this is ' 'provided by an extension, you will have to make sure that ' 'extension is enabled..') % search_backend_id) search_backend.validate() return search_backend_id
def clean_search_backend_id(self): """Clean the ``search_backend_id`` field. This will ensure the chosen search backend is valid (i.e., it is available in the registry) and that its dependencies have been installed. Returns: unicode: The search backend ID. Raises: django.core.exceptions.ValidationError: Raised if the search engine ID chosen cannot be used. """ search_backend_id = self.cleaned_data['search_backend_id'] search_backend = search_backend_registry.get_search_backend( search_backend_id) if not search_backend: raise ValidationError( ugettext('The search engine "%s" could not be found. ' 'If this is provided by an extension, you will have ' 'to make sure that extension is enabled.') % search_backend_id ) search_backend.validate() return search_backend_id
def _load_forwarded_engine(self): """Load a forwarded Haystack search engine. This will look up the current site configuration and determine the proper Review Board and Haystack search backends to load, setting any forwarding state. IF there's an issue at all with loading the search backends, an error will be logged and search will be disabled. This is thread-safe. """ cur_load_gen = self._load_gen with self._load_lock: if self._load_gen == cur_load_gen: self.reset_forwarding() siteconfig = SiteConfiguration.objects.get_current() search_backend_id = siteconfig.get('search_backend_id') search_backend = search_backend_registry.get_search_backend( search_backend_id) if search_backend is None: logger.error( 'The search engine "%s" could not be found. ' 'If this is provided by an extension, you ' 'will have to make sure that extension is ' 'enabled. Disabling search.', search_backend_id) engine = None else: try: engine_cls = load_backend( search_backend.haystack_backend_name) engine = engine_cls(using=self.using) except Exception as e: logger.error( 'Error loading the search engine "%s": ' '%s', search_backend_id, e) engine = None if engine is None: # Disable search, since it's useless at this point. siteconfig.set('search_enable', False) siteconfig.save(update_fields=('settings', )) else: self._forwarded_engine = engine self._search_backend = None self._forwarded_options = search_backend.configuration self._load_gen = cur_load_gen + 1
def save(self): """Save the form and sub-form for the selected search backend. This forces a site configuration reload. """ search_backend_id = self.cleaned_data['search_backend_id'] backend_form = self.search_backend_forms[search_backend_id] backend = search_backend_registry.get_search_backend(search_backend_id) backend.configuration = backend.get_configuration_from_form_data( backend_form.cleaned_data) super(SearchSettingsForm, self).save() # Reload any import changes to the Django settings. load_site_config()
def clean(self): """Clean the form and the sub-form for the selected search backend. Returns: dict: The cleaned data. """ cleaned_data = self.cleaned_data if cleaned_data['search_enable']: search_backend_id = cleaned_data.get('search_backend_id') # The search_backend_id field is only available if the backend # passed validation. if search_backend_id: backend_form = self.search_backend_forms[search_backend_id] # Validate the configuration form. if backend_form.is_valid(): # Validate the search backend, ensuring the configuration # is correct. search_backend = \ search_backend_registry.get_search_backend( search_backend_id) configuration = \ search_backend.get_configuration_from_form_data( backend_form.cleaned_data) try: if func_accepts_kwargs(search_backend.validate): search_backend.validate( configuration=configuration) else: RemovedInReviewBoard50Warning.warn( '%s.validate() must accept keyword ' 'arguments. This will be required in ' 'Review Board 5.0.' % search_backend.__class__.__name__) search_backend.validate() except ValidationError as e: self.add_error('search_backend_id', e.error_list) else: # The configuration form had issues. self._errors.update(backend_form.errors) return cleaned_data