class PageIndex(indexes.RealTimeSearchIndex): """ Index for FeinCMS Page objects Body is generated using a complex template which includes rendered content for many content objects """ url = fields.CharField(model_attr="_cached_url") body = fields.CharField(document=True, use_template=True) #: Currently included so we can generate search results pages without a # database query (Solr will include it the results): title = fields.CharField(model_attr="title") creation_date = fields.DateTimeField(model_attr='creation_date', null=True) modification_date = fields.DateTimeField(model_attr='modification_date', null=True) def should_update(self, instance, **kwargs): return instance.is_active() def get_queryset(self): """Return a Django QuerySet used for all search-related queries. Currently we index all active pages""" return Page.objects.active() def get_updated_field(self): return "modification_date"
class EntryIndex(indexes.SearchIndex, indexes.Indexable): url = fields.CharField(model_attr="get_absolute_url") text = fields.CharField(document=True, use_template=True) title = fields.CharField(model_attr="title") slug = fields.CharField(model_attr="slug") author = fields.CharField(model_attr="author") content = fields.CharField(model_attr="content") published_on = fields.DateTimeField(model_attr='published_on', null=True) last_changed = fields.DateTimeField(model_attr='last_changed', null=True) content_auto = indexes.EdgeNgramField(model_attr='title') def should_update(self, instance, **kwargs): return instance.is_active() def get_model(self): return Entry def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.filter(is_active=True) def get_updated_field(self): return "last_changed"
class PageIndex(indexes.SearchIndex, indexes.Indexable): """ Index for FeinCMS Page objects Body is generated using a complex template which includes rendered content for many content objects """ url = fields.CharField(model_attr="_cached_url") text = fields.CharField(document=True, use_template=True) title = fields.CharField(model_attr="title") slug = fields.CharField(model_attr="slug") page_title = fields.CharField(model_attr="_page_title") meta_description = fields.CharField(model_attr="meta_description") meta_keywords = fields.CharField(model_attr="meta_keywords") content_title = fields.CharField(model_attr="_content_title") content = fields.CharField(model_attr="content") creation_date = fields.DateTimeField(model_attr='creation_date', null=True) modification_date = fields.DateTimeField(model_attr='modification_date', null=True) def should_update(self, instance, **kwargs): return instance.is_active() def get_model(self): return Page def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.filter(active=True) def get_updated_field(self): return "modification_date"
class FileIndex(indexes.SearchIndex, indexes.Indexable): """ Index for File objects """ text = fields.CharField(document=True, use_template=True) url = fields.CharField(model_attr="url") title = fields.CharField(model_attr="name", null=True) description = fields.CharField(model_attr="description", null=True) folder = fields.CharField(model_attr="folder", null=True) original_filename = fields.CharField(model_attr="original_filename", null=True) uploaded_at = fields.DateTimeField(model_attr='uploaded_at', null=True) content_auto = indexes.EdgeNgramField(model_attr='name') def get_model(self): return File def index_queryset(self, using=None): return self.get_model().objects.filter(is_public=True) def get_updated_field(self): return "uploaded_at"
class PackageIndex(indexes.SearchIndex, indexes.Indexable): """ Index for Package objects """ text = fields.CharField(document=True, use_template=True) title = fields.CharField(model_attr="title") slug = fields.CharField(model_attr="slug") repo_description = fields.CharField(model_attr="repo_description") repo_url = fields.CharField(model_attr="repo_url") pypi_url = fields.CharField(model_attr="pypi_url") pypi_description = fields.CharField(model_attr="pypi_description") documentation_url = fields.CharField(model_attr="documentation_url") last_fetched = fields.DateTimeField(model_attr='last_fetched', null=True) def get_model(self): return Package def index_queryset(self, using=None): """Used when the entire index for model is updated.""" return self.get_model().objects.all() def get_updated_field(self): return "last_fetched"
class PageIndex(indexes.SearchIndex, indexes.Indexable): """ Index for FeinCMS Page objects Body is generated using a complex template which includes rendered content for many content objects """ url = fields.CharField(model_attr="_cached_url") text = fields.CharField(document=True, use_template=True) title = fields.CharField(model_attr="title") slug = fields.CharField(model_attr="slug") page_title = fields.CharField(model_attr="_page_title") meta_description = fields.CharField(model_attr="meta_description") meta_keywords = fields.CharField(model_attr="meta_keywords") content_title = fields.CharField(model_attr="_content_title") creation_date = fields.DateTimeField(model_attr='creation_date', null=True) modification_date = fields.DateTimeField(model_attr='modification_date', null=True) content_auto = indexes.EdgeNgramField(model_attr='title') def should_update(self, instance, **kwargs): return instance.is_active() def get_model(self): return Page def index_queryset(self, using=None): """Used when the entire index for model is updated.""" kwargs = {"active": True} # if permissions are enabled then we want only public pages # https://github.com/leonardo-modules/leonardo-module-pagepermissions if hasattr(self.get_model(), 'permissions'): kwargs['permissions__isnull'] = True # https://github.com/leonardo-modules/leonardo-page-search if hasattr(self.get_model(), 'search_exclude'): kwargs['search_exclude'] = False return self.get_model().objects.filter(**kwargs)
class NewsIndex(SearchIndex, Indexable): # common facets; period = fields.FacetMultiValueField() state_name = fields.FacetMultiValueField() financial_mechanism_ss = fields.FacetMultiValueField() programme_area_ss = fields.FacetMultiValueField() priority_sector_ss = fields.FacetMultiValueField() programme_name = fields.FacetMultiValueField() programme_status = fields.FacetMultiValueField() outcome_ss = fields.FacetMultiValueField() kind = fields.FacetCharField() # specific facets project_name = fields.FacetMultiValueField() project_name_auto = fields.EdgeNgramField() project_status = fields.FacetMultiValueField() geotarget = fields.FacetMultiValueField() geotarget_auto = fields.EdgeNgramField() theme_ss = fields.FacetMultiValueField() # specific fields text = fields.CharField(document=True, use_template=True) summary = fields.CharField(model_attr="summary", indexed=False, null=True) name = fields.CharField(model_attr="title", indexed=False) url = fields.CharField(model_attr="link", indexed=False) image = fields.CharField(model_attr="image", indexed=False) created_dt = fields.DateTimeField(model_attr="created", indexed=False, null=True) def get_model(self): return News def prepare_kind(self, obj): return "News" def index_queryset(self, using=None): return (self.get_model().objects.select_related( "project", "project__state", ).prefetch_related( "programmes", "programmes__states", "programmes__indicators", "programmes__programme_areas", "programmes__programme_areas__priority_sector", "project__programme_areas", "project__priority_sectors", "project__themes", )) def prepare_period(self, obj): if obj.project: return [obj.project.get_funding_period_display()] return list( set(programme.get_funding_period_display() for programme in obj.programmes.all())) def prepare_state_name(self, obj): if obj.project: return [obj.project.state.name] else: # Get this from ProgrammeOutcome, because of IN22 return list( set(state.name for programme in obj.programmes.all() for state in programme.states.all())) def prepare_financial_mechanism_ss(self, obj): if obj.project: return obj.project.financial_mechanisms_display return list( set(fm for programme in obj.programmes.all() for fm in programme.financial_mechanisms_display)) def prepare_programme_area_ss(self, obj): if obj.project: return [ programme_area.name for programme_area in obj.project.programme_areas.all() ] return list( set(area.name for programme in obj.programmes.all() for area in programme.programme_areas.all())) def prepare_priority_sector_ss(self, obj): if obj.project: return [ sector.name for sector in obj.project.priority_sectors.all() ] return list( set(area.priority_sector.name for programme in obj.programmes.all() for area in programme.programme_areas.all())) def prepare_programme_name(self, obj): if obj.project: return [obj.project.programme.display_name] return list( set([programme.display_name for programme in obj.programmes.all()])) def prepare_project_name(self, obj): return obj.project and obj.project.display_name def prepare_programme_status(self, obj): if obj.project: return [obj.project.programme.status] return list( set([programme.status for programme in obj.programmes.all()])) def prepare_outcome_ss(self, obj): if obj.project: return list( set(" ".join(indicator.header.split()) for indicator in obj.project.programme.indicators.all())) return list( set(" ".join(indicator.header.split()) for programme in obj.programmes.all() for indicator in programme.indicators.all())) def prepare_project_status(self, obj): if obj.project: return [obj.project.status] return None def prepare_geotarget(self, obj): return obj.project and obj.project.geotarget def prepare_theme_ss(self, obj): if obj.project: return list(set([theme.name for theme in obj.project.themes.all()])) return None def prepare(self, obj): self.prepared_data = super().prepare(obj) self.prepared_data["geotarget_auto"] = (" ".join( self.prepared_data["geotarget"]) if self.prepared_data["geotarget"] else None) self.prepared_data["project_name_auto"] = ( " ".join(self.prepared_data["project_name"]) if self.prepared_data["project_name"] else None) return self.prepared_data